Skip to content

Commit

Permalink
clear last applied configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrancisc committed Apr 5, 2024
1 parent 6b578e1 commit c952c18
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

View workflow job for this annotation

GitHub Actions / GolangCI Lint

S1033: unnecessary guard around call to delete (gosimple)
delete(obj.GetAnnotations(), LastAppliedConfigurationAnnotationKey)
}
newConfiguration = GetNewConfiguration(obj)
if annotations == nil {
annotations = map[string]string{}
}
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c952c18

Please sign in to comment.