From 29c1cf1237b4bd8cec2a377fc712f6319ac060ff Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Thu, 1 Oct 2020 22:23:59 +0300 Subject: [PATCH] Retry setting annotation on conflict --- cmd/gotk/reconcile_helmrelease.go | 36 ++++++++++++++++++++--------- cmd/gotk/reconcile_kustomization.go | 32 +++++++++++++++++-------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/cmd/gotk/reconcile_helmrelease.go b/cmd/gotk/reconcile_helmrelease.go index d5f9ecda..398bb40d 100644 --- a/cmd/gotk/reconcile_helmrelease.go +++ b/cmd/gotk/reconcile_helmrelease.go @@ -21,14 +21,15 @@ import ( "fmt" "time" - "github.com/fluxcd/pkg/apis/meta" - corev1 "k8s.io/api/core/v1" - "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/fluxcd/pkg/apis/meta" + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" ) @@ -98,14 +99,7 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error { } logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace) - if helmRelease.Annotations == nil { - helmRelease.Annotations = map[string]string{ - meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), - } - } else { - helmRelease.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) - } - if err := kubeClient.Update(ctx, &helmRelease); err != nil { + if err := requestHelmReleaseReconciliation(ctx, kubeClient, namespacedName); err != nil { return err } logger.Successf("HelmRelease annotated") @@ -151,3 +145,23 @@ func helmReleaseReconciliationHandled(ctx context.Context, kubeClient client.Cli return helmRelease.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil } } + +func requestHelmReleaseReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName) error { + var release helmv2.HelmRelease + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { + if err := kubeClient.Get(ctx, namespacedName, &release); err != nil { + return err + } + + if release.Annotations == nil { + release.Annotations = map[string]string{ + meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), + } + } else { + release.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) + } + + err = kubeClient.Update(ctx, &release) + return + }) +} diff --git a/cmd/gotk/reconcile_kustomization.go b/cmd/gotk/reconcile_kustomization.go index dee82809..b31bc706 100644 --- a/cmd/gotk/reconcile_kustomization.go +++ b/cmd/gotk/reconcile_kustomization.go @@ -21,10 +21,11 @@ import ( "fmt" "time" - "github.com/fluxcd/pkg/apis/meta" corev1 "k8s.io/api/core/v1" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/fluxcd/pkg/apis/meta" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" @@ -96,14 +97,7 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error { } logger.Actionf("annotating kustomization %s in %s namespace", name, namespace) - if kustomization.Annotations == nil { - kustomization.Annotations = map[string]string{ - meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), - } - } else { - kustomization.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) - } - if err := kubeClient.Update(ctx, &kustomization); err != nil { + if err := requestKustomizeReconciliation(ctx, kubeClient, namespacedName); err != nil { return err } logger.Successf("kustomization annotated") @@ -150,3 +144,23 @@ func kustomizeReconciliationHandled(ctx context.Context, kubeClient client.Clien return kustomize.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil } } + +func requestKustomizeReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName) error { + var kustomization kustomizev1.Kustomization + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { + if err := kubeClient.Get(ctx, namespacedName, &kustomization); err != nil { + return err + } + + if kustomization.Annotations == nil { + kustomization.Annotations = map[string]string{ + meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano), + } + } else { + kustomization.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano) + } + + err = kubeClient.Update(ctx, &kustomization) + return + }) +}