From 6821b3382e1ec9828164b8f77f9ee7f5c16cf058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haram=20Nyg=C3=A5rd?= Date: Tue, 20 Aug 2024 13:06:17 +0200 Subject: [PATCH] Add status diff for routing --- api/v1alpha1/routing_types.go | 25 +++++++++++++++++++ api/v1alpha1/status_types.go | 2 +- ...skiperator.kartverket.no_applications.yaml | 2 +- .../skiperator.kartverket.no_routings.yaml | 2 +- .../skiperator.kartverket.no_skipjobs.yaml | 2 +- internal/controllers/application.go | 2 +- internal/controllers/routing.go | 14 +++++++++++ 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/api/v1alpha1/routing_types.go b/api/v1alpha1/routing_types.go index 19e7e32e..cdb03de8 100644 --- a/api/v1alpha1/routing_types.go +++ b/api/v1alpha1/routing_types.go @@ -5,6 +5,7 @@ import ( "github.com/nais/liberator/pkg/namegen" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/validation" + "time" ) //+kubebuilder:object:root=true @@ -117,3 +118,27 @@ func (in *Routing) GetDefaultLabels() map[string]string { func (in *Routing) GetCommonSpec() *CommonSpec { panic("common spec not available for routing resource type") } + +func (in *Routing) SetDefaultStatus() { + var msg string + + if in.Status.Summary.Status == "" { + msg = "Default Routing status, it has not initialized yet" + } else { + msg = "Routing is trying to reconcile" + } + + in.Status.Summary = Status{ + Status: PENDING, + Message: msg, + TimeStamp: time.Now().String(), + } + + if in.Status.SubResources == nil { + in.Status.SubResources = make(map[string]Status) + } + + if len(in.Status.Conditions) == 0 { + in.Status.Conditions = make([]metav1.Condition, 0) + } +} diff --git a/api/v1alpha1/status_types.go b/api/v1alpha1/status_types.go index b3353bfe..b5cee9fa 100644 --- a/api/v1alpha1/status_types.go +++ b/api/v1alpha1/status_types.go @@ -5,7 +5,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -// ApplicationStatus +// SkiperatorStatus // // A status field shown on a Skiperator resource which contains information regarding deployment of the resource. // +kubebuilder:object:generate=true diff --git a/config/crd/skiperator.kartverket.no_applications.yaml b/config/crd/skiperator.kartverket.no_applications.yaml index 54b5f688..26e0a27d 100644 --- a/config/crd/skiperator.kartverket.no_applications.yaml +++ b/config/crd/skiperator.kartverket.no_applications.yaml @@ -1098,7 +1098,7 @@ spec: type: object status: description: |- - ApplicationStatus + SkiperatorStatus A status field shown on a Skiperator resource which contains information regarding deployment of the resource. properties: diff --git a/config/crd/skiperator.kartverket.no_routings.yaml b/config/crd/skiperator.kartverket.no_routings.yaml index ed673d78..d516aec0 100644 --- a/config/crd/skiperator.kartverket.no_routings.yaml +++ b/config/crd/skiperator.kartverket.no_routings.yaml @@ -72,7 +72,7 @@ spec: type: object status: description: |- - ApplicationStatus + SkiperatorStatus A status field shown on a Skiperator resource which contains information regarding deployment of the resource. properties: diff --git a/config/crd/skiperator.kartverket.no_skipjobs.yaml b/config/crd/skiperator.kartverket.no_skipjobs.yaml index dd0e22fd..c9d51c77 100644 --- a/config/crd/skiperator.kartverket.no_skipjobs.yaml +++ b/config/crd/skiperator.kartverket.no_skipjobs.yaml @@ -840,7 +840,7 @@ spec: type: object status: description: |- - ApplicationStatus + SkiperatorStatus A status field shown on a Skiperator resource which contains information regarding deployment of the resource. properties: diff --git a/internal/controllers/application.go b/internal/controllers/application.go index e2602e9e..192ed401 100644 --- a/internal/controllers/application.go +++ b/internal/controllers/application.go @@ -123,7 +123,7 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req reconcile.Req return common.RequeueWithError(err) } - //TODO do we need this actually? + // TODO do we need this actually? isApplicationMarkedToBeDeleted := application.GetDeletionTimestamp() != nil if isApplicationMarkedToBeDeleted { if err = r.finalizeApplication(application, ctx); err != nil { diff --git a/internal/controllers/routing.go b/internal/controllers/routing.go index d99afdc1..f76195b8 100644 --- a/internal/controllers/routing.go +++ b/internal/controllers/routing.go @@ -68,6 +68,20 @@ func (r *RoutingReconciler) Reconcile(ctx context.Context, req reconcile.Request return common.DoNotRequeue() } + tmpStatus := routing.GetStatus().DeepCopy() + + routing.SetDefaultStatus() + statusDiff, err := common.GetObjectDiff(tmpStatus, routing.GetStatus()) + if err != nil { + return common.RequeueWithError(err) + } + + if len(statusDiff) > 0 { + rLog.Info("Status has changed", "diff", statusDiff) + err = r.GetClient().Status().Update(ctx, routing) + return reconcile.Result{Requeue: true}, err + } + if err := r.setDefaultSpec(ctx, routing); err != nil { rLog.Error(err, "error when trying to set default spec") r.SetErrorState(ctx, routing, err, "error when trying to set default spec", "DefaultSpecFailure")