Skip to content

Commit

Permalink
Merge pull request #823 from jlandowner/controller-event-char2
Browse files Browse the repository at this point in the history
Update Kubernetes Events
  • Loading branch information
oruharo authored May 22, 2024
2 parents bc9db02 + bba912b commit 81b7f32
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions internal/controllers/cluster_instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
12 changes: 6 additions & 6 deletions internal/controllers/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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]
Expand All @@ -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)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down

0 comments on commit 81b7f32

Please sign in to comment.