From f469f6f47dd94c7ff5520a359fcdedf64375e5aa Mon Sep 17 00:00:00 2001 From: dmicheneau <47741512+dmicheneau@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:37:02 +0200 Subject: [PATCH] chore: rename row Images Age to Last-sync --- api/v1alpha1/image_types.go | 12 ++++++------ cmd/kimup/scheduler.go | 19 +++++++++++-------- .../bases/kimup.cloudavenue.io_images.yaml | 8 ++++---- internal/controller/image_controller.go | 2 +- internal/kubeclient/image.go | 2 +- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/api/v1alpha1/image_types.go b/api/v1alpha1/image_types.go index d9a3766..32f6bd4 100644 --- a/api/v1alpha1/image_types.go +++ b/api/v1alpha1/image_types.go @@ -101,9 +101,9 @@ type ( ImageStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - Tag string `json:"tag"` - Result string `json:"result"` - Age string `json:"age"` + Tag string `json:"tag"` + Result ImageStatusLastSync `json:"result"` + Time string `json:"time"` } ) @@ -114,7 +114,7 @@ type ( // +kubebuilder:printcolumn:name="Image",type=string,JSONPath=`.spec.image` // +kubebuilder:printcolumn:name="Tag",type=string,JSONPath=`.status.tag` // +kubebuilder:printcolumn:name="Last-Result",type=string,JSONPath=`.status.result` -// +kubebuilder:printcolumn:name="Last-Sync",type=date,JSONPath=`.status.age` +// +kubebuilder:printcolumn:name="Last-Sync",type=date,JSONPath=`.status.time` type Image struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` @@ -142,13 +142,13 @@ func (i *Image) SetStatusTag(tag string) { } // SetStatusExecution sets the status execution of the image -func (i *Image) SetStatusResult(result string) { +func (i *Image) SetStatusResult(result ImageStatusLastSync) { i.Status.Result = result } // SetStatusTime sets the status time of the image func (i *Image) SetStatusTime(time string) { - i.Status.Age = time + i.Status.Time = time } // GetImageWithTag returns the image name with the tag diff --git a/cmd/kimup/scheduler.go b/cmd/kimup/scheduler.go index 4a34b42..dec1238 100644 --- a/cmd/kimup/scheduler.go +++ b/cmd/kimup/scheduler.go @@ -54,17 +54,20 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { defer cancel() image, err := k.Image().Get(ctx, e.Data()["namespace"].(string), e.Data()["image"].(string)) - // TODO - Add a defer function to update the status of kind Image defer func() { // update the status of the image image.SetStatusTime(time.Now().Format(time.RFC3339)) err := k.Image().UpdateStatus(ctx, image) if err != nil { - log.Errorf("Error updating status of image %s in namespace %s: %v", e.Data()["image"], e.Data()["namespace"], err) + log.WithError(err). + WithFields(log.Fields{ + "Namespace": e.Data()["namespace"], + "Image": e.Data()["image"], + }).Error("Error updating status of image") } }() if err != nil { - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncErrorGetImage)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorGetImage) if err := crontab.RemoveJob(crontab.BuildKey(e.Data()["namespace"].(string), e.Data()["image"].(string))); err != nil { return err } @@ -75,7 +78,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { if image.Spec.ImagePullSecrets != nil { auths, err = k.GetPullSecretsForImage(ctx, image) - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncErrorPullSecrets)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorPullSecrets) if err != nil { return err } @@ -106,7 +109,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { if err != nil { // Prometheus metrics - Increment the counter for the registry with error metrics.Registry().TotalErr().Inc() - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncErrorRegistry)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorRegistry) return err } @@ -128,7 +131,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { for _, rule := range image.Spec.Rules { r, err := rules.GetRule(rule.Type) if err != nil { - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncErrorGetRule)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorGetRule) log.Errorf("Error getting rule: %v", err) continue } @@ -160,7 +163,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { if match { for _, action := range rule.Actions { a, err := actions.GetActionWithUntypedName(action.Type) - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncErrorAction)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncErrorAction) if err != nil { log.Errorf("Error getting action: %v", err) continue @@ -193,7 +196,7 @@ func initScheduler(ctx context.Context, k kubeclient.Interface) { } } - image.SetStatusResult(string(v1alpha1.ImageStatusLastSyncSuccess)) + image.SetStatusResult(v1alpha1.ImageStatusLastSyncSuccess) return k.Image().Update(ctx, image) }) diff --git a/config/crd/bases/kimup.cloudavenue.io_images.yaml b/config/crd/bases/kimup.cloudavenue.io_images.yaml index 33c1a6b..12355ea 100644 --- a/config/crd/bases/kimup.cloudavenue.io_images.yaml +++ b/config/crd/bases/kimup.cloudavenue.io_images.yaml @@ -24,7 +24,7 @@ spec: - jsonPath: .status.result name: Last-Result type: string - - jsonPath: .status.age + - jsonPath: .status.time name: Last-Sync type: date name: v1alpha1 @@ -219,8 +219,6 @@ spec: status: description: ImageStatus defines the observed state of Image properties: - age: - type: string result: type: string tag: @@ -228,10 +226,12 @@ spec: INSERT ADDITIONAL STATUS FIELD - define observed state of cluster Important: Run "make" to regenerate code after modifying this file type: string + time: + type: string required: - - age - result - tag + - time type: object type: object served: true diff --git a/internal/controller/image_controller.go b/internal/controller/image_controller.go index 75ba6e6..e78d991 100644 --- a/internal/controller/image_controller.go +++ b/internal/controller/image_controller.go @@ -80,7 +80,7 @@ func (r *ImageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl equal, err := an.CheckSum().IsEqual(image.Spec) if err != nil || !equal { an.Action().Set(annotations.ActionReload) - image.SetStatusResult(string(kimupv1alpha1.ImageStatusLastSyncScheduled)) + image.SetStatusResult(kimupv1alpha1.ImageStatusLastSyncScheduled) r.Recorder.Event(&image, "Normal", string(ImageUpdate), "Image configuration has changed. Reloading image.") } diff --git a/internal/kubeclient/image.go b/internal/kubeclient/image.go index 56cff3f..621b873 100644 --- a/internal/kubeclient/image.go +++ b/internal/kubeclient/image.go @@ -165,7 +165,7 @@ func (i *ImageObj) Watch(ctx context.Context) (chan WatchInterface[v1alpha1.Imag } // UpdateStatus updates the status of the image object. -// It takes a context and a pointer to the image object as parameters. +// It takes a context and a image object as parameters. // Returns an error if the operation fails; otherwise, it returns nil. func (i *ImageObj) UpdateStatus(ctx context.Context, image v1alpha1.Image) error { u, err := encodeUnstructured(image)