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/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 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