From bba912b16033a36ccffd70f14cee920eba486994 Mon Sep 17 00:00:00 2001 From: jlandowner Date: Wed, 22 May 2024 12:28:02 +0900 Subject: [PATCH] Update Kubernetes Events - Reason and Message: capitalize only the first letter. - Reason: trim spaces. --- .../controllers/cluster_instance_controller.go | 6 +++--- internal/controllers/instance_controller.go | 12 ++++++------ internal/controllers/user_controller.go | 18 +++++++++--------- internal/controllers/workspace_controller.go | 10 ++++++---- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/internal/controllers/cluster_instance_controller.go b/internal/controllers/cluster_instance_controller.go index c3630280..5343b9d7 100644 --- a/internal/controllers/cluster_instance_controller.go +++ b/internal/controllers/cluster_instance_controller.go @@ -54,21 +54,21 @@ func (r *ClusterInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ // 1. Build Unstructured objects objects, err := template.BuildObjects(tmpl.Spec, &inst) if err != nil { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "BuildFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build manifests from Template: %v", err) return ctrl.Result{}, err } // 2. Transform the objects objects, err = transformer.ApplyTransformers(ctx, transformer.AllTransformers(&inst, r.Scheme, tmpl), objects) if err != nil { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "BuildFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build resources: %v", err) return ctrl.Result{}, err } // 3. Reconcile objects if errs := r.impl.reconcileObjects(ctx, &inst, objects); len(errs) != 0 { for _, err := range errs { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "SyncFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "SyncFailed", "Failed to sync objects: %v", err) } // requeue return ctrl.Result{}, fmt.Errorf("apply child objects failed: %w", errs[0]) diff --git a/internal/controllers/instance_controller.go b/internal/controllers/instance_controller.go index c5cbce58..75a5d7a5 100644 --- a/internal/controllers/instance_controller.go +++ b/internal/controllers/instance_controller.go @@ -64,21 +64,21 @@ func (r *InstanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c // 1. Build Unstructured objects objects, err := template.BuildObjects(tmpl.Spec, &inst) if err != nil { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "BuildFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build manifests from Template: %v", err) return ctrl.Result{}, err } // 2. Transform the objects objects, err = transformer.ApplyTransformers(ctx, transformer.AllTransformers(&inst, r.Scheme, tmpl), objects) if err != nil { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "BuildFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build resources: %v", err) return ctrl.Result{}, err } // 3. Reconcile objects if errs := r.impl.reconcileObjects(ctx, &inst, objects); len(errs) != 0 { for _, err := range errs { - r.Recorder.Event(&inst, corev1.EventTypeWarning, "SyncFailed", err.Error()) + r.Recorder.Eventf(&inst, corev1.EventTypeWarning, "SyncFailed", "Failed to sync objects: %v", err) } // requeue return ctrl.Result{}, fmt.Errorf("apply child objects failed: %w", errs[0]) @@ -162,7 +162,7 @@ func (r *instanceReconciler) reconcileObjects(ctx context.Context, inst cosmov1a if err != nil { errs = append(errs, fmt.Errorf("failed to create resource: kind = %s name = %s: %w", built.GetKind(), built.GetName(), err)) } else { - r.Recorder.Eventf(inst, corev1.EventTypeNormal, "Synced", "%s %s created", built.GetKind(), built.GetName()) + r.Recorder.Eventf(inst, corev1.EventTypeNormal, "Synced", "%s %s is created", built.GetKind(), built.GetName()) } currAppliedMap[created.GetUID()] = unstToObjectRef(created) @@ -203,10 +203,10 @@ func (r *instanceReconciler) reconcileObjects(ctx context.Context, inst cosmov1a for _, d := range shouldDeletes { if skip, err := prune(ctx, r.Client, d); err != nil { log.Error(err, "failed to delete unused obj", "pruneAPIVersion", d.APIVersion, "pruneKind", d.Kind, "pruneName", d.Name, "pruneNamespace", d.Namespace) - r.Recorder.Eventf(inst, corev1.EventTypeWarning, "GCFailed", "failed to delete unused obj: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) + r.Recorder.Eventf(inst, corev1.EventTypeWarning, "GCFailed", "Failed to delete unused obj: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) } else if !skip { log.Info("deleted unmanaged object", "apiVersion", d.APIVersion, "kind", d.Kind, "name", d.Name, "namespace", d.Namespace) - r.Recorder.Eventf(inst, corev1.EventTypeNormal, "GC", "deleted unmanaged object: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) + r.Recorder.Eventf(inst, corev1.EventTypeNormal, "GC", "Deleted unmanaged object: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) } } } diff --git a/internal/controllers/user_controller.go b/internal/controllers/user_controller.go index a5f4b575..57d7f4de 100644 --- a/internal/controllers/user_controller.go +++ b/internal/controllers/user_controller.go @@ -51,12 +51,12 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return r.patchNamespaceToUserDesired(&ns, user) }) if err != nil { - r.Recorder.Eventf(&user, corev1.EventTypeWarning, "Sync Failed", "failed to sync namespace: %v", err) + r.Recorder.Eventf(&user, corev1.EventTypeWarning, "SyncFailed", "Failed to sync namespace %s: %v", ns.Name, err) return ctrl.Result{}, fmt.Errorf("failed to sync namespace: %w", err) } if op != controllerutil.OperationResultNone { log.Info("namespace synced", "namespace", ns.Name) - r.Recorder.Eventf(&user, corev1.EventTypeNormal, string(op), "successfully reconciled. namespace synced") + r.Recorder.Eventf(&user, corev1.EventTypeNormal, "Synced", "Successfully reconciled. Namespace %s is %s", ns.Name, op) } user.Status.Phase = ns.Status.Phase @@ -80,12 +80,12 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. // generate default password if password secret is not found if _, err := password.GetDefaultPassword(ctx, r.Client, user.Name); err != nil && apierrs.IsNotFound(err) { if err := password.ResetPassword(ctx, r.Client, user.Name); err != nil { - r.Recorder.Eventf(&user, corev1.EventTypeWarning, "InitFailed", "failed to reset password: %v", err) + r.Recorder.Eventf(&user, corev1.EventTypeWarning, "PasswordInitFailed", "Failed to reset password: %v", err) log.Error(err, "failed to reset password") return ctrl.Result{}, err } log.Info("password secret initialized") - r.Recorder.Eventf(&user, corev1.EventTypeNormal, "PasswordSecret Initialized", "successfully reset password secret") + r.Recorder.Eventf(&user, corev1.EventTypeNormal, "PasswordInitialized", "Successfully reset password secret") } } @@ -124,7 +124,7 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. if op != controllerutil.OperationResultNone { log.Info("addon synced", "addon", addon) - r.Recorder.Eventf(&user, corev1.EventTypeNormal, "Addon Synced", fmt.Sprintf("addon %s is %s", addon.Template.Name, op)) + r.Recorder.Eventf(&user, corev1.EventTypeNormal, "AddonSynced", "Addon %s is %s", addon.Template.Name, op) } else { log.Debug().Info("the result of update addon instance operation is None", "addon", addon) } @@ -150,8 +150,8 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. if len(addonErrs) > 0 { for _, e := range addonErrs { - r.Recorder.Eventf(&user, corev1.EventTypeWarning, "AddonFailed", "failed to create or update user addon: %v", e) - log.Error(e, "failed to create or update user addon") + r.Recorder.Eventf(&user, corev1.EventTypeWarning, "AddonFailed", "Failed to sync addon: %v", e) + log.Error(e, "failed to sync user addon") } user.Status.Phase = "AddonFailed" err = addonErrs[0] @@ -172,10 +172,10 @@ func (r *UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. for _, d := range shouldDeletes { if skip, err := prune(ctx, r.Client, d); err != nil { log.Error(err, "failed to delete unused addon", "pruneAPIVersion", d.APIVersion, "pruneKind", d.Kind, "pruneName", d.Name, "pruneNamespace", d.Namespace) - r.Recorder.Eventf(&user, corev1.EventTypeWarning, "GCFailed", "failed to delete unused addon: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) + r.Recorder.Eventf(&user, corev1.EventTypeWarning, "GCFailed", "Failed to delete unused addon: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) } else if !skip { log.Info("deleted unmanaged addon", "apiVersion", d.APIVersion, "kind", d.Kind, "name", d.Name, "namespace", d.Namespace) - r.Recorder.Eventf(&user, corev1.EventTypeNormal, "GC", "deleted unmanaged addon: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) + r.Recorder.Eventf(&user, corev1.EventTypeNormal, "GC", "Deleted unmanaged addon: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace) } } } diff --git a/internal/controllers/workspace_controller.go b/internal/controllers/workspace_controller.go index eb00c26a..bfa182c2 100644 --- a/internal/controllers/workspace_controller.go +++ b/internal/controllers/workspace_controller.go @@ -78,11 +78,12 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return nil }) if err != nil { - return ctrl.Result{}, err + r.Recorder.Eventf(&ws, corev1.EventTypeWarning, "SyncFailed", "Failed to sync instance %s: %v", inst.Name, err) + return ctrl.Result{}, fmt.Errorf("failed to sync instance: %w", err) } if op != controllerutil.OperationResultNone { log.Info("instance synced", "instance", inst.Name) - r.Recorder.Eventf(&ws, corev1.EventTypeNormal, string(op), "successfully reconciled. instance synced") + r.Recorder.Eventf(&ws, corev1.EventTypeNormal, "Synced", "Successfully reconciled. Instance %s is %s", inst.Name, op) } else { log.Debug().Info("the result of update workspace instance operation is None", "instance", inst.Name) } @@ -107,11 +108,12 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return r.TraefikIngressRouteCfg.PatchTraefikIngressRouteAsDesired(&ir, ws, r.Scheme) }) if err != nil { - return ctrl.Result{}, err + r.Recorder.Eventf(&ws, corev1.EventTypeWarning, "SyncFailed", "Failed to sync traefik ingress route %s: %v", ir.Name, err) + return ctrl.Result{}, fmt.Errorf("failed to sync traefik ingress route: %w", err) } if op != controllerutil.OperationResultNone { log.Info("traefik ingress route synced", "ingressroute", ir.Name) - r.Recorder.Eventf(&ws, corev1.EventTypeNormal, string(op), "successfully reconciled. traefik ingress route synced") + r.Recorder.Eventf(&ws, corev1.EventTypeNormal, "Synced", "Successfully reconciled. Traefik ingress route %s is %s", ir.Name, op) } // generate URL and set to status