From 5840a02c6a967723bc8f71b70e00f8b90a08d164 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Thu, 15 Jun 2023 23:43:25 +0200 Subject: [PATCH] fix: Multi source apps resolve revision in .status field (#14081) Signed-off-by: Jorge Turrado --- controller/state.go | 7 +++++++ controller/state_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/controller/state.go b/controller/state.go index 147e0393ef3093..94b3ea166f229d 100644 --- a/controller/state.go +++ b/controller/state.go @@ -173,6 +173,13 @@ func (m *appStateManager) getRepoObjs(app *v1alpha1.Application, sources []v1alp } for i, source := range sources { + // The iteration values are assigned to the respective iteration variables as in an assignment statement. + // The iteration variables may be declared by the “range” clause using a form of short variable declaration (:=). + // In this case their types are set to the types of the respective iteration values and their scope is the block of the "for" statement; + // they are re-used in each iteration. If the iteration variables are declared outside the "for" statement, + // after execution their values will be those of the last iteration. + // https://golang.org/ref/spec#For_statements + source := source if len(revisions) < len(sources) || revisions[i] == "" { revisions[i] = source.TargetRevision } diff --git a/controller/state_test.go b/controller/state_test.go index 623e04213e300f..db57a7b2c76ea6 100644 --- a/controller/state_test.go +++ b/controller/state_test.go @@ -318,6 +318,45 @@ func TestCompareAppStateExtraHook(t *testing.T) { assert.Equal(t, 0, len(app.Status.Conditions)) } +// TestAppRevisions tests that revisions are properly propagated +func TestAppRevisions(t *testing.T) { + obj1 := NewPod() + obj1.SetNamespace(test.FakeDestNamespace) + data := fakeData{ + manifestResponse: &apiclient.ManifestResponse{ + Manifests: []string{toJSON(t, obj1)}, + Namespace: test.FakeDestNamespace, + Server: test.FakeClusterURL, + Revision: "abc123", + }, + managedLiveObjs: make(map[kube.ResourceKey]*unstructured.Unstructured), + } + ctrl := newFakeController(&data) + + // single source + { + app := newFakeApp() + revisions := make([]string, 0) + revisions = append(revisions, "") + compRes := ctrl.appStateManager.CompareAppState(app, &defaultProj, revisions, app.Spec.GetSources(), false, false, nil, app.Spec.HasMultipleSources()) + assert.NotNil(t, compRes) + assert.NotNil(t, compRes.syncStatus) + assert.NotEmpty(t, compRes.syncStatus.Revision) + assert.Len(t, compRes.syncStatus.Revisions, 0) + } + // multisource + { + app := newFakeMultiSourceApp() + revisions := make([]string, 0) + revisions = append(revisions, "") + compRes := ctrl.appStateManager.CompareAppState(app, &defaultProj, revisions, app.Spec.GetSources(), false, false, nil, app.Spec.HasMultipleSources()) + assert.NotNil(t, compRes) + assert.NotNil(t, compRes.syncStatus) + assert.Empty(t, compRes.syncStatus.Revision) + assert.Len(t, compRes.syncStatus.Revisions, 2) + } +} + func toJSON(t *testing.T, obj *unstructured.Unstructured) string { data, err := json.Marshal(obj) assert.NoError(t, err)