Skip to content

Commit

Permalink
move cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrancisc committed Apr 8, 2024
1 parent c952c18 commit aacc2c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
13 changes: 7 additions & 6 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (c ApplyClient) applyObject(ctx context.Context, obj client.Object, options
if config.saveConfiguration {
// set current object as annotation
annotations := obj.GetAnnotations()
// reset the previous config to avoid recursive embedding of the object
if _, found := obj.GetAnnotations()[LastAppliedConfigurationAnnotationKey]; found {
delete(obj.GetAnnotations(), LastAppliedConfigurationAnnotationKey)
}
newConfiguration = GetNewConfiguration(obj)
if annotations == nil {
annotations = map[string]string{}
Expand Down Expand Up @@ -209,7 +205,12 @@ func clusterIP(obj runtime.Object) (string, bool, error) {
}
}

func GetNewConfiguration(newResource runtime.Object) string {
func GetNewConfiguration(newResource client.Object) string {
// reset the previous config to avoid recursive embedding of the object
copyResource := newResource.DeepCopyObject().(client.Object)
if _, found := copyResource.GetAnnotations()[LastAppliedConfigurationAnnotationKey]; found {

Check failure on line 211 in pkg/client/client.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

S1033: unnecessary guard around call to delete (gosimple)
delete(copyResource.GetAnnotations(), LastAppliedConfigurationAnnotationKey)
}
newJSON, err := marshalObjectContent(newResource)
if err != nil {
log.Error(err, "unable to marshal the object", "object", newResource)
Expand Down Expand Up @@ -282,7 +283,7 @@ func ApplyUnstructuredObjectsWithNewLabels(ctx context.Context, cl client.Client
for _, unstructuredObj := range unstructuredObjects {
log.Info("applying object", "object_namespace", unstructuredObj.GetNamespace(), "object_name", unstructuredObj.GetObjectKind().GroupVersionKind().Kind+"/"+unstructuredObj.GetName())
MergeLabels(unstructuredObj, newLabels)
_, err := applyClient.ApplyObject(ctx, unstructuredObj)
_, err := applyClient.ApplyObject(ctx, unstructuredObj, SaveConfiguration(false))
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,17 @@ func TestApplySingle(t *testing.T) {
},
}
existingSA.Secrets = secretRefs
existingLastAppliedAnnotation := map[string]string{
client.LastAppliedConfigurationAnnotationKey: client.GetNewConfiguration(existingSA),
}
existingSA.SetAnnotations(existingLastAppliedAnnotation) // let's set the last applied annotation
cl, cli := newClient(t)
_, err := cl.ApplyRuntimeObject(context.TODO(), existingSA)
require.NoError(t, err)

// when
// we update with existing annotations
newSA := existingSA.DeepCopy()
existingLastAppliedAnnotation := map[string]string{
client.LastAppliedConfigurationAnnotationKey: client.GetNewConfiguration(newSA),
}
newSA.SetAnnotations(existingLastAppliedAnnotation) // let's set the last applied annotation
_, err = cl.ApplyRuntimeObject(context.TODO(), newSA) // then

Expand Down

0 comments on commit aacc2c6

Please sign in to comment.