mirror of
https://github.com/fluxcd/flux2.git
synced 2026-02-17 13:11:48 +00:00
Retry setting annotation on conflict
This commit is contained in:
parent
0e52065893
commit
29c1cf1237
2 changed files with 48 additions and 20 deletions
|
|
@ -21,14 +21,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fluxcd/pkg/apis/meta"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
"k8s.io/client-go/util/retry"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
|
|
||||||
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
|
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
|
||||||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
|
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)
|
logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace)
|
||||||
if helmRelease.Annotations == nil {
|
if err := requestHelmReleaseReconciliation(ctx, kubeClient, namespacedName); err != 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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Successf("HelmRelease annotated")
|
logger.Successf("HelmRelease annotated")
|
||||||
|
|
@ -151,3 +145,23 @@ func helmReleaseReconciliationHandled(ctx context.Context, kubeClient client.Cli
|
||||||
return helmRelease.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
|
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"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fluxcd/pkg/apis/meta"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/client-go/util/retry"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
|
"github.com/fluxcd/pkg/apis/meta"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"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)
|
logger.Actionf("annotating kustomization %s in %s namespace", name, namespace)
|
||||||
if kustomization.Annotations == nil {
|
if err := requestKustomizeReconciliation(ctx, kubeClient, namespacedName); err != 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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Successf("kustomization annotated")
|
logger.Successf("kustomization annotated")
|
||||||
|
|
@ -150,3 +144,23 @@ func kustomizeReconciliationHandled(ctx context.Context, kubeClient client.Clien
|
||||||
return kustomize.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
|
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…
Reference in a new issue