From c952c18811edd93c01e989e7590a57680073bc0b Mon Sep 17 00:00:00 2001 From: Francisc Munteanu Date: Fri, 5 Apr 2024 17:13:12 +0200 Subject: [PATCH] clear last applied configuration --- pkg/client/client.go | 8 ++++++-- pkg/client/client_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 6ed5c3f3..698c34aa 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -113,7 +113,11 @@ func (c ApplyClient) applyObject(ctx context.Context, obj client.Object, options if config.saveConfiguration { // set current object as annotation annotations := obj.GetAnnotations() - newConfiguration = getNewConfiguration(obj) + // 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{} } @@ -205,7 +209,7 @@ func clusterIP(obj runtime.Object) (string, bool, error) { } } -func getNewConfiguration(newResource runtime.Object) string { +func GetNewConfiguration(newResource runtime.Object) string { newJSON, err := marshalObjectContent(newResource) if err != nil { log.Error(err, "unable to marshal the object", "object", newResource) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 78ebcf84..321ad7e8 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -339,6 +339,42 @@ func TestApplySingle(t *testing.T) { assert.Equal(t, "second-value", configMap.Data["first-param"]) }) }) + + t.Run("updates of ServiceAccount", func(t *testing.T) { + + t.Run("should update service account with last applied configuration", func(t *testing.T) { + // given + // there's an existing SA with secret refs + existingSA := newSA() + secretRefs := []corev1.ObjectReference{ + { + Name: "secret", + Namespace: existingSA.Namespace, + }, + } + existingSA.Secrets = secretRefs + 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 + + // then + require.NoError(t, err) + var actualSa corev1.ServiceAccount + err = cli.Get(context.TODO(), types.NamespacedName{Name: "appstudio-user-sa", Namespace: "john-dev"}, &actualSa) // assert sa was created + require.NoError(t, err) + assert.Equal(t, secretRefs, actualSa.Secrets) // secret refs are still there + assert.Equal(t, existingLastAppliedAnnotation[client.LastAppliedConfigurationAnnotationKey], actualSa.Annotations[client.LastAppliedConfigurationAnnotationKey]) // the last apply configuration should match the previous object + }) + }) } func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) {