From 9688801ecc5d132e79aa14c7c96d99a1db549e19 Mon Sep 17 00:00:00 2001 From: oleksandr-codefresh Date: Wed, 22 Nov 2023 14:12:10 +0200 Subject: [PATCH 1/2] [ diff_test.go ]: added test case to check if switching from label tracking to annotation+label works well --- util/argo/diff/diff_test.go | 19 ++++++++++++++ util/argo/testdata/data.go | 6 +++++ .../testdata/desired_app_mixed_tracking.yaml | 26 +++++++++++++++++++ .../live_app_with_old_label_tracking.yaml | 25 ++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 util/argo/testdata/desired_app_mixed_tracking.yaml create mode 100644 util/argo/testdata/live_app_with_old_label_tracking.yaml diff --git a/util/argo/diff/diff_test.go b/util/argo/diff/diff_test.go index 2c95d7404d299..b1bfe277416a9 100644 --- a/util/argo/diff/diff_test.go +++ b/util/argo/diff/diff_test.go @@ -1,6 +1,8 @@ package diff_test import ( + "github.com/argoproj/argo-cd/v2/common" + argo2 "github.com/argoproj/argo-cd/v2/util/argo" "testing" "github.com/stretchr/testify/assert" @@ -154,6 +156,23 @@ func TestStateDiff(t *testing.T) { assert.Equal(t, float64(tc.expectedPredictedReplicas), predictedReplicas) }) } + + t.Run("will find diff while moving from label tracking to annotation+label", func(t *testing.T) { + // given + params := defaultDiffConfigParams() + params.trackingMethod = string(argo2.TrackingMethodAnnotationAndLabel) + params.label = common.LabelKeyAppInstance + dc := diffConfig(t, params) + + // when + result, err := argo.StateDiff(testutil.YamlToUnstructured(testdata.LiveAppWithOldLabelTrackingYaml), testutil.YamlToUnstructured(testdata.DesiredAppMixedTrackingYaml), dc) + + // then + assert.NoError(t, err) + assert.NotNil(t, result) + assert.True(t, result.Modified) + }) + } func TestDiffConfigBuilder(t *testing.T) { type fixture struct { diff --git a/util/argo/testdata/data.go b/util/argo/testdata/data.go index f3aa78f6e6e34..0ca852a1897fa 100644 --- a/util/argo/testdata/data.go +++ b/util/argo/testdata/data.go @@ -14,4 +14,10 @@ var ( //go:embed desired_validating_webhook.yaml DesiredValidatingWebhookYaml string + + //go:embed live_app_with_old_label_tracking.yaml + LiveAppWithOldLabelTrackingYaml string + + //go:embed desired_app_mixed_tracking.yaml + DesiredAppMixedTrackingYaml string ) diff --git a/util/argo/testdata/desired_app_mixed_tracking.yaml b/util/argo/testdata/desired_app_mixed_tracking.yaml new file mode 100644 index 0000000000000..e2fa4cca5544d --- /dev/null +++ b/util/argo/testdata/desired_app_mixed_tracking.yaml @@ -0,0 +1,26 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + annotations: + argocd.argoproj.io/tracking-id: 'parent:argoproj.io/Application:argo/child' + some-annotation: 'some' + labels: + app.kubernetes.io/instance: parent + name: child + namespace: argo +spec: + destination: + name: in-cluster + namespace: argo + project: default + source: + path: empty + repoURL: 'https://github.com/argoproj/argocd-example-apps' + targetRevision: main + syncPolicy: + automated: + allowEmpty: true + prune: true + selfHeal: false + syncOptions: + - CreateNamespace=true diff --git a/util/argo/testdata/live_app_with_old_label_tracking.yaml b/util/argo/testdata/live_app_with_old_label_tracking.yaml new file mode 100644 index 0000000000000..40be90003e11b --- /dev/null +++ b/util/argo/testdata/live_app_with_old_label_tracking.yaml @@ -0,0 +1,25 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + annotations: + some-annotation: 'some' + labels: + app.kubernetes.io/instance: parent + name: child + namespace: argo +spec: + destination: + name: in-cluster + namespace: argo + project: default + source: + path: empty + repoURL: 'https://github.com/argoproj/argocd-example-apps' + targetRevision: main + syncPolicy: + automated: + allowEmpty: true + prune: true + selfHeal: false + syncOptions: + - CreateNamespace=true From 8d85dd45bbd6190db61cbd8e1d42186c8572936d Mon Sep 17 00:00:00 2001 From: oleksandr-codefresh Date: Wed, 22 Nov 2023 15:15:22 +0200 Subject: [PATCH 2/2] bugfix [ resource_tracking.go ]: fixed issue when changing tracking method from label to annotation+label didn't see required changes to resources --- util/argo/resource_tracking.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/util/argo/resource_tracking.go b/util/argo/resource_tracking.go index 7e7e0be50dd69..6817971a4860e 100644 --- a/util/argo/resource_tracking.go +++ b/util/argo/resource_tracking.go @@ -204,6 +204,12 @@ func (rt *resourceTracking) Normalize(config, live *unstructured.Unstructured, l return nil } + liveInstanceAnnotation, err := argokube.GetAppInstanceAnnotation(live, common.AnnotationKeyAppInstance) + + if err == nil && liveInstanceAnnotation == "" && trackingMethod != string(TrackingMethodLabel) { + return nil + } + annotation, err := argokube.GetAppInstanceAnnotation(config, common.AnnotationKeyAppInstance) if err != nil { return err