From 48b6819b85a28a6d90bd4fe99b3b65a5490b8229 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:09:46 +0100 Subject: [PATCH] extracted status update logic and removed hardcoded statuses --- api/v1alpha1/condition_types.go | 18 ++++++++++ controllers/componentversion_controller.go | 21 +++++------ controllers/configuration_controller.go | 31 ++++++++-------- controllers/localization_controller.go | 35 ++++++++++--------- controllers/resource_controller.go | 21 +++++------ controllers/snapshot_controller.go | 5 +-- .../status}/identifiable_contract.go | 8 ++--- .../status}/mutate_condition_status.go | 2 +- .../status}/register_status_defer.go | 6 ++-- 9 files changed, 85 insertions(+), 62 deletions(-) rename {controllers => pkg/status}/identifiable_contract.go (74%) rename {controllers => pkg/status}/mutate_condition_status.go (97%) rename {controllers => pkg/status}/register_status_defer.go (93%) diff --git a/api/v1alpha1/condition_types.go b/api/v1alpha1/condition_types.go index 1af7bcb2..da44d651 100644 --- a/api/v1alpha1/condition_types.go +++ b/api/v1alpha1/condition_types.go @@ -61,4 +61,22 @@ const ( // CreateRepositoryNameReason is used when the generating a new repository name fails. CreateRepositoryNameReason = "CreateRepositoryNameFailed" + + // ConfigRefNotReadyWithErrorReason is used when configuration reference is not ready yet with an error. + ConfigRefNotReadyWithErrorReason = "ConfigRefNotReadyWithError" + + // ConfigRefNotReadyReason is used when configuration ref is not ready yet and there was no error. + ConfigRefNotReadyReason = "ConfigRefNotReady" + + // SourceRefNotReadyWithErrorReason is used when the source ref is not ready and there was an error. + SourceRefNotReadyWithErrorReason = "SourceRefNotReadyWithError" + + // SourceRefNotReadyReason is used when the source ref is not ready and there was no error. + SourceRefNotReadyReason = "SourceRefNotReady" + + // PatchStrategicMergeSourceRefNotReadyWithErrorReason is used when source ref for patch strategic merge is not ready and there was an error. + PatchStrategicMergeSourceRefNotReadyWithErrorReason = "PatchStrategicMergeSourceRefNotReadyWithError" + + // PatchStrategicMergeSourceRefNotReadyReason is used when source ref for patch strategic merge is not ready and there was no error. + PatchStrategicMergeSourceRefNotReadyReason = "PatchStrategicMergeSourceRefNotReady" ) diff --git a/controllers/componentversion_controller.go b/controllers/componentversion_controller.go index 9c8c7bd8..f3d99ed6 100644 --- a/controllers/componentversion_controller.go +++ b/controllers/componentversion_controller.go @@ -15,6 +15,7 @@ import ( "github.com/fluxcd/pkg/runtime/conditions" "github.com/fluxcd/pkg/runtime/patch" rreconcile "github.com/fluxcd/pkg/runtime/reconcile" + "github.com/open-component-model/ocm-controller/pkg/status" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -139,7 +140,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req // Always attempt to patch the object and status after each reconciliation. defer func() { - if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { + if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { retErr = errors.Join(retErr, derr) } }() @@ -153,7 +154,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req if err != nil { // we don't fail here, because all manifests might have been applied at once or the secret // for authentication is being reconciled. - MarkAsStalled( + status.MarkAsStalled( r.EventRecorder, obj, v1alpha1.AuthenticatedContextCreationFailedReason, @@ -167,7 +168,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req update, version, err := r.checkVersion(ctx, octx, obj) if err != nil { // The component might not be there yet. We don't fail but keep polling instead. - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.CheckVersionFailedReason, @@ -196,7 +197,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req ok, err := r.OCMClient.VerifyComponent(ctx, octx, obj, version) if err != nil { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.VerificationFailedReason, @@ -209,7 +210,7 @@ func (r *ComponentVersionReconciler) Reconcile(ctx context.Context, req ctrl.Req } if !ok { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.VerificationFailedReason, @@ -233,7 +234,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con cv, err := r.OCMClient.GetComponentVersion(ctx, octx, obj, obj.Spec.Component, version) if err != nil { err = fmt.Errorf("failed to get component version: %w", err) - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.ComponentVersionInvalidReason, @@ -249,7 +250,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con cd, err := dv.ConvertFrom(cv.GetDescriptor()) if err != nil { err = fmt.Errorf("failed to convert component descriptor: %w", err) - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.ConvertComponentDescriptorFailedReason, @@ -264,7 +265,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con componentName, err := component.ConstructUniqueName(cd.GetName(), cd.GetVersion(), nil) if err != nil { err = fmt.Errorf("failed to generate name: %w", err) - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, @@ -301,7 +302,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con if err != nil { err = fmt.Errorf("failed to create or update component descriptor: %w", err) - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.CreateOrUpdateComponentDescriptorFailedReason, @@ -325,7 +326,7 @@ func (r *ComponentVersionReconciler) reconcile(ctx context.Context, octx ocm.Con componentDescriptor.References, err = r.parseReferences(ctx, octx, obj, cv.GetDescriptor().References) if err != nil { err = fmt.Errorf("failed to parse references: %w", err) - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, v1alpha1.ParseReferencesFailedReason, diff --git a/controllers/configuration_controller.go b/controllers/configuration_controller.go index 1a79f740..83eacc0b 100644 --- a/controllers/configuration_controller.go +++ b/controllers/configuration_controller.go @@ -15,6 +15,7 @@ import ( "github.com/fluxcd/pkg/runtime/patch" rreconcile "github.com/fluxcd/pkg/runtime/reconcile" sourcev1 "github.com/fluxcd/source-controller/api/v1" + "github.com/open-component-model/ocm-controller/pkg/status" "golang.org/x/exp/slices" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -168,7 +169,7 @@ func (r *ConfigurationReconciler) Reconcile( // Always attempt to patch the object and status after each reconciliation. defer func() { - if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { + if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { err = errors.Join(err, derr) } }() @@ -181,16 +182,16 @@ func (r *ConfigurationReconciler) Reconcile( // check dependencies are ready ready, err := r.checkReadiness(ctx, obj.GetNamespace(), &obj.Spec.SourceRef) if err != nil { - MarkNotReady(r.EventRecorder, obj, "SourceRefNotReadyWithError", err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceRefNotReadyWithErrorReason, err.Error()) return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "SourceRefNotReady", + v1alpha1.SourceRefNotReadyReason, fmt.Sprintf("source ref not yet ready: %s", obj.Spec.SourceRef.Name), ) @@ -200,20 +201,20 @@ func (r *ConfigurationReconciler) Reconcile( if obj.Spec.ConfigRef != nil { ready, err := r.checkReadiness(ctx, obj.GetNamespace(), obj.Spec.ConfigRef) if err != nil { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "ConfigRefNotReadyWithError", + v1alpha1.ConfigRefNotReadyWithErrorReason, fmt.Sprintf("config ref not yet ready with error: %s: %s", obj.Spec.ConfigRef.Name, err), ) return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "ConfigRefNotReady", + v1alpha1.ConfigRefNotReadyReason, fmt.Sprintf("config ref not yet ready: %s", obj.Spec.ConfigRef.Name), ) @@ -224,10 +225,10 @@ func (r *ConfigurationReconciler) Reconcile( if obj.Spec.PatchStrategicMerge != nil { ready, err := r.checkFluxSourceReadiness(ctx, obj.Spec.PatchStrategicMerge.Source.SourceRef) if err != nil { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "PatchStrategicMergeSourceRefNotReadyWithError", + v1alpha1.PatchStrategicMergeSourceRefNotReadyWithErrorReason, fmt.Sprintf("patch strategic merge source ref not yet ready with error: %s: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name, err), ) @@ -235,10 +236,10 @@ func (r *ConfigurationReconciler) Reconcile( } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "PatchStrategicMergeSourceRefNotReady", + v1alpha1.PatchStrategicMergeSourceRefNotReadyReason, fmt.Sprintf("patch strategic merge source ref not yet ready: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name), ) @@ -252,7 +253,7 @@ func (r *ConfigurationReconciler) Reconcile( name, err := snapshot.GenerateSnapshotName(obj.GetName()) if err != nil { err := fmt.Errorf("failed to generate snapshot name for: %s: %s", obj.GetName(), err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) return ctrl.Result{}, err } @@ -288,13 +289,13 @@ func (r *ConfigurationReconciler) reconcile( if errors.Is(err, errTar) { err = fmt.Errorf("source resource is not a tar archive: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error()) return ctrl.Result{}, err } err = fmt.Errorf("failed to reconcile mutation object: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error()) return ctrl.Result{}, err } diff --git a/controllers/localization_controller.go b/controllers/localization_controller.go index 7fbc742a..55568ad8 100644 --- a/controllers/localization_controller.go +++ b/controllers/localization_controller.go @@ -16,6 +16,7 @@ import ( rreconcile "github.com/fluxcd/pkg/runtime/reconcile" sourcev1 "github.com/fluxcd/source-controller/api/v1" "github.com/fluxcd/source-controller/api/v1beta2" + "github.com/open-component-model/ocm-controller/pkg/status" "golang.org/x/exp/slices" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -151,7 +152,7 @@ func (r *LocalizationReconciler) Reconcile( // Always attempt to patch the object and status after each reconciliation. defer func() { - if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { + if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { err = errors.Join(err, derr) } }() @@ -164,16 +165,16 @@ func (r *LocalizationReconciler) Reconcile( // check dependencies are ready ready, err := r.checkReadiness(ctx, obj.GetNamespace(), &obj.Spec.SourceRef) if err != nil { - MarkNotReady(r.EventRecorder, obj, "SourceRefNotReadyWithError", err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceRefNotReadyWithErrorReason, err.Error()) return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "SourceRefNotReady", + v1alpha1.SourceRefNotReadyReason, fmt.Sprintf("source ref not yet ready: %s", obj.Spec.SourceRef.Name), ) @@ -183,20 +184,20 @@ func (r *LocalizationReconciler) Reconcile( if obj.Spec.ConfigRef != nil { ready, err := r.checkReadiness(ctx, obj.GetNamespace(), obj.Spec.ConfigRef) if err != nil { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "ConfigRefNotReadyWithError", + v1alpha1.ConfigRefNotReadyWithErrorReason, fmt.Sprintf("config ref not yet ready with error: %s: %s", obj.Spec.ConfigRef.Name, err), ) return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "ConfigRefNotReady", + v1alpha1.ConfigRefNotReadyReason, fmt.Sprintf("config ref not yet ready: %s", obj.Spec.ConfigRef.Name), ) @@ -207,10 +208,10 @@ func (r *LocalizationReconciler) Reconcile( if obj.Spec.PatchStrategicMerge != nil { ready, err := r.checkFluxSourceReadiness(ctx, obj.Spec.PatchStrategicMerge.Source.SourceRef) if err != nil { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "PatchStrategicMergeSourceRefNotReadyWithError", + v1alpha1.PatchStrategicMergeSourceRefNotReadyWithErrorReason, fmt.Sprintf("patch strategic merge source ref not yet ready with error: %s: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name, err), ) @@ -218,10 +219,10 @@ func (r *LocalizationReconciler) Reconcile( } if !ready { - MarkNotReady( + status.MarkNotReady( r.EventRecorder, obj, - "PatchStrategicMergeSourceRefNotReady", + v1alpha1.PatchStrategicMergeSourceRefNotReadyReason, fmt.Sprintf("patch strategic merge source ref not yet ready: %s", obj.Spec.PatchStrategicMerge.Source.SourceRef.Name), ) @@ -235,7 +236,7 @@ func (r *LocalizationReconciler) Reconcile( name, err := snapshot.GenerateSnapshotName(obj.GetName()) if err != nil { err = fmt.Errorf("failed to generate snapshot name for: %s: %s", obj.GetName(), err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) return ctrl.Result{}, err } @@ -269,13 +270,13 @@ func (r *LocalizationReconciler) reconcile( if errors.Is(err, errTar) { err = fmt.Errorf("source resource is not a tar archive: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.SourceReasonNotATarArchiveReason, err.Error()) return ctrl.Result{}, err } err = fmt.Errorf("failed to reconcile mutation object: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ReconcileMutationObjectFailedReason, err.Error()) return ctrl.Result{}, err } @@ -431,9 +432,9 @@ func makeRequestsForLocalizations(ll ...v1alpha1.Localization) []reconcile.Reque requests := make([]reconcile.Request, len(refs)) for i, item := range refs { - // if the observedgeneration is -1 + // if the ObservedGeneration is -1 // then the object has not been initialised yet - // so we should not trigger a reconcilation for sourceRef/configRefs + // so we should not trigger a reconciliation for sourceRef/configRefs if item.Status.ObservedGeneration != -1 { requests[i] = reconcile.Request{ NamespacedName: types.NamespacedName{ diff --git a/controllers/resource_controller.go b/controllers/resource_controller.go index 31757a3e..18aae35c 100644 --- a/controllers/resource_controller.go +++ b/controllers/resource_controller.go @@ -18,6 +18,7 @@ import ( "github.com/open-component-model/ocm-controller/pkg/component" "github.com/open-component-model/ocm-controller/pkg/ocm" "github.com/open-component-model/ocm-controller/pkg/snapshot" + "github.com/open-component-model/ocm-controller/pkg/status" ocmmetav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -100,7 +101,7 @@ func (r *ResourceReconciler) Reconcile( // Always attempt to patch the object and status after each reconciliation. defer func() { - if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { + if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, obj.GetRequeueAfter()); derr != nil { err = errors.Join(err, derr) } }() @@ -116,7 +117,7 @@ func (r *ResourceReconciler) Reconcile( name, err := snapshot.GenerateSnapshotName(obj.GetName()) if err != nil { err = fmt.Errorf("failed to generate snapshot name for: %s: %s", obj.GetName(), err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.NameGenerationFailedReason, err.Error()) return ctrl.Result{}, err } @@ -154,13 +155,13 @@ func (r *ResourceReconciler) reconcile( } err = fmt.Errorf("failed to get component version: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotFoundReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotFoundReason, err.Error()) return ctrl.Result{}, err } if !conditions.IsReady(&componentVersion) { - MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotReadyReason, "component version not ready yet") + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentVersionNotReadyReason, "component version not ready yet") return ctrl.Result{RequeueAfter: obj.GetRequeueAfter()}, nil } @@ -170,7 +171,7 @@ func (r *ResourceReconciler) reconcile( octx, err := r.OCMClient.CreateAuthenticatedOCMContext(ctx, &componentVersion) if err != nil { err = fmt.Errorf("failed to create authenticated client: %w", err) - MarkAsStalled(r.EventRecorder, obj, v1alpha1.AuthenticatedContextCreationFailedReason, err.Error()) + status.MarkAsStalled(r.EventRecorder, obj, v1alpha1.AuthenticatedContextCreationFailedReason, err.Error()) return ctrl.Result{}, nil } @@ -178,7 +179,7 @@ func (r *ResourceReconciler) reconcile( reader, digest, err := r.OCMClient.GetResource(ctx, octx, &componentVersion, obj.Spec.SourceRef.ResourceRef) if err != nil { err = fmt.Errorf("failed to get resource: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.GetResourceFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.GetResourceFailedReason, err.Error()) return ctrl.Result{}, err } @@ -194,7 +195,7 @@ func (r *ResourceReconciler) reconcile( componentDescriptor, err := component.GetComponentDescriptor(ctx, r.Client, obj.GetReferencePath(), componentVersion.Status.ComponentDescriptor) if err != nil { err = fmt.Errorf("failed to get component descriptor for resource: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.GetComponentDescriptorFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.GetComponentDescriptorFailedReason, err.Error()) return ctrl.Result{}, err } @@ -204,14 +205,14 @@ func (r *ResourceReconciler) reconcile( "couldn't find component descriptor for reference '%s' or any root components", obj.GetReferencePath(), ) - MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentDescriptorNotFoundReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.ComponentDescriptorNotFoundReason, err.Error()) return ctrl.Result{}, err } if obj.GetSnapshotName() == "" { err := errors.New("snapshot name should not be empty") - MarkNotReady(r.EventRecorder, obj, "SnapshotNameEmpty", err.Error()) + status.MarkNotReady(r.EventRecorder, obj, "SnapshotNameEmpty", err.Error()) return ctrl.Result{}, err } @@ -250,7 +251,7 @@ func (r *ResourceReconciler) reconcile( }) if err != nil { err = fmt.Errorf("failed to create or update snapshot: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.CreateOrUpdateSnapshotFailedReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.CreateOrUpdateSnapshotFailedReason, err.Error()) return ctrl.Result{}, err } diff --git a/controllers/snapshot_controller.go b/controllers/snapshot_controller.go index a7273d75..774b11c9 100644 --- a/controllers/snapshot_controller.go +++ b/controllers/snapshot_controller.go @@ -16,6 +16,7 @@ import ( rreconcile "github.com/fluxcd/pkg/runtime/reconcile" "github.com/google/go-containerregistry/pkg/v1/remote/transport" "github.com/open-component-model/ocm-controller/pkg/cache" + "github.com/open-component-model/ocm-controller/pkg/status" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" kuberecorder "k8s.io/client-go/tools/record" @@ -93,7 +94,7 @@ func (r *SnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r // Always attempt to patch the object and status after each reconciliation. defer func() { - if derr := updateStatus(ctx, patchHelper, obj, r.EventRecorder, 0); derr != nil { + if derr := status.UpdateStatus(ctx, patchHelper, obj, r.EventRecorder, 0); derr != nil { err = errors.Join(err, derr) } }() @@ -106,7 +107,7 @@ func (r *SnapshotReconciler) Reconcile(ctx context.Context, req ctrl.Request) (r name, err := ocm.ConstructRepositoryName(obj.Spec.Identity) if err != nil { err = fmt.Errorf("failed to construct name: %w", err) - MarkNotReady(r.EventRecorder, obj, v1alpha1.CreateRepositoryNameReason, err.Error()) + status.MarkNotReady(r.EventRecorder, obj, v1alpha1.CreateRepositoryNameReason, err.Error()) return ctrl.Result{}, err } diff --git a/controllers/identifiable_contract.go b/pkg/status/identifiable_contract.go similarity index 74% rename from controllers/identifiable_contract.go rename to pkg/status/identifiable_contract.go index 4ff832ff..a4fed602 100644 --- a/controllers/identifiable_contract.go +++ b/pkg/status/identifiable_contract.go @@ -1,4 +1,4 @@ -package controllers +package status import ( "github.com/fluxcd/pkg/runtime/conditions" @@ -6,15 +6,15 @@ import ( // IdentifiableClientObject defines an object which can create an identity for itself. type IdentifiableClientObject interface { - StatusMutator + Mutator conditions.Setter // GetVID constructs an identifier for an object. GetVID() map[string]string } -// StatusMutator allows mutating specific status fields of an object. -type StatusMutator interface { +// Mutator allows mutating specific status fields of an object. +type Mutator interface { // SetObservedGeneration mutates the observed generation field of an object. SetObservedGeneration(v int64) } diff --git a/controllers/mutate_condition_status.go b/pkg/status/mutate_condition_status.go similarity index 97% rename from controllers/mutate_condition_status.go rename to pkg/status/mutate_condition_status.go index 63f72274..4990691a 100644 --- a/controllers/mutate_condition_status.go +++ b/pkg/status/mutate_condition_status.go @@ -1,4 +1,4 @@ -package controllers +package status import ( eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" diff --git a/controllers/register_status_defer.go b/pkg/status/register_status_defer.go similarity index 93% rename from controllers/register_status_defer.go rename to pkg/status/register_status_defer.go index 6899ec2e..3b48406c 100644 --- a/controllers/register_status_defer.go +++ b/pkg/status/register_status_defer.go @@ -1,4 +1,4 @@ -package controllers +package status import ( "context" @@ -13,8 +13,8 @@ import ( kuberecorder "k8s.io/client-go/tools/record" ) -// updateStatus takes an object which can identify itself and updates its status including ObservedGeneration. -func updateStatus( +// UpdateStatus takes an object which can identify itself and updates its status including ObservedGeneration. +func UpdateStatus( ctx context.Context, patchHelper *patch.SerialPatcher, obj IdentifiableClientObject,