From dd565e782ee229deeb12e44aba23a12b920bdcd9 Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Thu, 15 Jun 2023 17:44:01 -0400 Subject: [PATCH] fix: Multi source apps resolve revision in .status field (#14081) (#14086) Signed-off-by: Jorge Turrado Co-authored-by: Jorge Turrado Ferrero --- 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 6323993e34643..cac9b75af986f 100644 --- a/controller/state.go +++ b/controller/state.go @@ -174,6 +174,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 8883464267538..87a0a58a4277a 100644 --- a/controller/state_test.go +++ b/controller/state_test.go @@ -233,6 +233,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)