From 76ab6503c09da24e4527dafe21901dd46f9f20f3 Mon Sep 17 00:00:00 2001 From: Jesse Suen Date: Fri, 2 Sep 2022 11:30:42 -0700 Subject: [PATCH] fix: appset controller should preserve argocd refresh annotation Signed-off-by: Jesse Suen --- .../controllers/applicationset_controller.go | 36 +++++++++++-------- .../applicationset_controller_test.go | 16 ++++----- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/applicationset/controllers/applicationset_controller.go b/applicationset/controllers/applicationset_controller.go index 209bf426da579..f26823e89f5b7 100644 --- a/applicationset/controllers/applicationset_controller.go +++ b/applicationset/controllers/applicationset_controller.go @@ -20,11 +20,16 @@ import ( "time" "github.com/go-logr/logr" + log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" + apierr "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/record" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/source" @@ -33,18 +38,10 @@ import ( "github.com/argoproj/argo-cd/v2/applicationset/utils" "github.com/argoproj/argo-cd/v2/common" argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - "github.com/argoproj/argo-cd/v2/util/db" - - log "github.com/sirupsen/logrus" - "k8s.io/apimachinery/pkg/runtime" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" argoutil "github.com/argoproj/argo-cd/v2/util/argo" - - apierr "k8s.io/apimachinery/pkg/api/errors" + "github.com/argoproj/argo-cd/v2/util/db" ) const ( @@ -55,6 +52,13 @@ const ( ReconcileRequeueOnValidationError = time.Minute * 3 ) +var ( + preservedAnnotations = []string{ + NotifiedAnnotationKey, + argov1alpha1.AnnotationKeyRefresh, + } +) + // ApplicationSetReconciler reconciles a ApplicationSet object type ApplicationSetReconciler struct { client.Client @@ -527,12 +531,16 @@ func (r *ApplicationSetReconciler) createOrUpdateInCluster(ctx context.Context, // Copy only the Application/ObjectMeta fields that are significant, from the generatedApp found.Spec = generatedApp.Spec - // Preserve argo cd notifications state (https://github.com/argoproj/applicationset/issues/180) - if state, exists := found.ObjectMeta.Annotations[NotifiedAnnotationKey]; exists { - if generatedApp.Annotations == nil { - generatedApp.Annotations = map[string]string{} + // Preserve specially treated argo cd annotations: + // * https://github.com/argoproj/applicationset/issues/180 + // * https://github.com/argoproj/argo-cd/issues/10500 + for _, key := range preservedAnnotations { + if state, exists := found.ObjectMeta.Annotations[key]; exists { + if generatedApp.Annotations == nil { + generatedApp.Annotations = map[string]string{} + } + generatedApp.Annotations[key] = state } - generatedApp.Annotations[NotifiedAnnotationKey] = state } found.ObjectMeta.Annotations = generatedApp.Annotations diff --git a/applicationset/controllers/applicationset_controller_test.go b/applicationset/controllers/applicationset_controller_test.go index a945365470012..0419aca1e6dab 100644 --- a/applicationset/controllers/applicationset_controller_test.go +++ b/applicationset/controllers/applicationset_controller_test.go @@ -13,9 +13,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" corev1 "k8s.io/api/core/v1" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" + kubefake "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" crtclient "sigs.k8s.io/controller-runtime/pkg/client" @@ -25,10 +27,6 @@ import ( "github.com/argoproj/argo-cd/v2/applicationset/generators" "github.com/argoproj/argo-cd/v2/applicationset/utils" argov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" - - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - kubefake "k8s.io/client-go/kubernetes/fake" - "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned/fake" @@ -755,7 +753,7 @@ func TestCreateOrUpdateInCluster(t *testing.T) { }, }, { - name: "Ensure that argocd notifications state annotation is preserved from an existing app", + name: "Ensure that argocd notifications state and refresh annotation is preserved from an existing app", appSet: argoprojiov1alpha1.ApplicationSet{ ObjectMeta: metav1.ObjectMeta{ Name: "name", @@ -781,8 +779,9 @@ func TestCreateOrUpdateInCluster(t *testing.T) { ResourceVersion: "2", Labels: map[string]string{"label-key": "label-value"}, Annotations: map[string]string{ - "annot-key": "annot-value", - NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`, + "annot-key": "annot-value", + NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`, + argov1alpha1.AnnotationKeyRefresh: string(argov1alpha1.RefreshTypeNormal), }, }, Spec: argov1alpha1.ApplicationSpec{ @@ -811,7 +810,8 @@ func TestCreateOrUpdateInCluster(t *testing.T) { Namespace: "namespace", ResourceVersion: "3", Annotations: map[string]string{ - NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`, + NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`, + argov1alpha1.AnnotationKeyRefresh: string(argov1alpha1.RefreshTypeNormal), }, }, Spec: argov1alpha1.ApplicationSpec{