Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: add unit tests for merge #1174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions pkg/objectstate/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ var (
func ServiceAccountForUpdate(current, updated client.Object) (client.Object, error) {
curSA, ok := current.(*corev1.ServiceAccount)
if !ok {
return updated, ErrWrongObjectType
return nil, ErrWrongObjectType
shajmakh marked this conversation as resolved.
Show resolved Hide resolved
}
updSA, ok := updated.(*corev1.ServiceAccount)
if !ok {
return updated, ErrMismatchingObjects
return nil, ErrMismatchingObjects
}
updSA.Secrets = curSA.Secrets
return MetadataForUpdate(current, updated)
Expand All @@ -47,6 +47,9 @@ func ObjectForUpdate(current, updated client.Object) (client.Object, error) {
}

func MetadataForUpdate(current, updated client.Object) (client.Object, error) {
if !isSameKind(current, updated) {
return nil, ErrMismatchingObjects
}
updated.SetCreationTimestamp(current.GetCreationTimestamp())
updated.SetSelfLink(current.GetSelfLink())
updated.SetGeneration(current.GetGeneration())
Expand All @@ -55,13 +58,17 @@ func MetadataForUpdate(current, updated client.Object) (client.Object, error) {
updated.SetManagedFields(current.GetManagedFields())
updated.SetFinalizers(current.GetFinalizers())

_ = Annotations(current, updated)
_ = Labels(current, updated)
_, _ = Annotations(current, updated)
_, _ = Labels(current, updated)

return updated, nil
}

func Annotations(current, updated client.Object) client.Object {
func Annotations(current, updated client.Object) (client.Object, error) {
if !isSameKind(current, updated) {
return nil, ErrMismatchingObjects
}

updatedAnnotations := updated.GetAnnotations()
curAnnotations := current.GetAnnotations()

Expand All @@ -76,10 +83,14 @@ func Annotations(current, updated client.Object) client.Object {
if len(curAnnotations) != 0 {
updated.SetAnnotations(curAnnotations)
}
return updated
return updated, nil
}

func Labels(current, updated client.Object) client.Object {
func Labels(current, updated client.Object) (client.Object, error) {
if !isSameKind(current, updated) {
return nil, ErrMismatchingObjects
}

updatedLabels := updated.GetLabels()
curLabels := current.GetLabels()

Expand All @@ -94,5 +105,9 @@ func Labels(current, updated client.Object) client.Object {
if len(curLabels) != 0 {
updated.SetLabels(curLabels)
}
return updated
return updated, nil
}

func isSameKind(a, b client.Object) bool {
return a.GetObjectKind().GroupVersionKind().Kind == b.GetObjectKind().GroupVersionKind().Kind
}
Loading
Loading