Retry setting annotation on conflict

pull/285/head
stefanprodan 4 years ago
parent 0e52065893
commit 29c1cf1237

@ -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
})
}

@ -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
})
}

Loading…
Cancel
Save