Skip to content

Commit

Permalink
Add cluster-scoped events in the list of user events
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed May 28, 2024
1 parent d08e1dd commit 386485b
Show file tree
Hide file tree
Showing 12 changed files with 1,842 additions and 25 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func KeepResourceDeletePolicy(obj AnnotationHolder) bool {
}
return v == ResourceAnnEnumDeletePolicyKeep
}

const (
EventAnnKeyUserName = "cosmo-workspace.github.io/user"
EventAnnKeyInstanceName = LabelKeyInstanceName
)
7 changes: 4 additions & 3 deletions internal/controllers/cluster_instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
"github.com/cosmo-workspace/cosmo/pkg/template"
"github.com/cosmo-workspace/cosmo/pkg/transformer"
)
Expand Down Expand Up @@ -54,21 +55,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.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build manifests from Template: %v", err)
kosmo.InstanceEventf(r.Recorder, &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.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build resources: %v", err)
kosmo.InstanceEventf(r.Recorder, &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.Eventf(&inst, corev1.EventTypeWarning, "SyncFailed", "Failed to sync objects: %v", err)
kosmo.InstanceEventf(r.Recorder, &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
15 changes: 8 additions & 7 deletions internal/controllers/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
"github.com/cosmo-workspace/cosmo/pkg/kubeutil"
"github.com/cosmo-workspace/cosmo/pkg/template"
"github.com/cosmo-workspace/cosmo/pkg/transformer"
Expand Down Expand Up @@ -64,21 +65,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.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build manifests from Template: %v", err)
kosmo.InstanceEventf(r.Recorder, &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.Eventf(&inst, corev1.EventTypeWarning, "BuildFailed", "Failed to build resources: %v", err)
kosmo.InstanceEventf(r.Recorder, &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.Eventf(&inst, corev1.EventTypeWarning, "SyncFailed", "Failed to sync objects: %v", err)
kosmo.InstanceEventf(r.Recorder, &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 +163,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 is created", built.GetKind(), built.GetName())
kosmo.InstanceEventf(r.Recorder, inst, corev1.EventTypeNormal, "Synced", "%s %s is created", built.GetKind(), built.GetName())
}
currAppliedMap[created.GetUID()] = unstToObjectRef(created)

Expand Down Expand Up @@ -190,7 +191,7 @@ func (r *instanceReconciler) reconcileObjects(ctx context.Context, inst cosmov1a
if _, err := r.apply(ctx, &built, r.FieldManager); err != nil {
errs = append(errs, fmt.Errorf("failed to apply resource %s %s: %w", built.GetKind(), built.GetName(), err))
} else {
r.Recorder.Eventf(inst, corev1.EventTypeNormal, "Synced", "%s %s is not desired state, synced", built.GetKind(), built.GetName())
kosmo.InstanceEventf(r.Recorder, inst, corev1.EventTypeNormal, "Synced", "%s %s is not desired state, synced", built.GetKind(), built.GetName())
}
}
}
Expand All @@ -203,10 +204,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)
kosmo.InstanceEventf(r.Recorder, 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)
kosmo.InstanceEventf(r.Recorder, inst, corev1.EventTypeNormal, "GC", "Deleted unmanaged object: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/controllers/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
)

// TemplateReconciler reconciles a Template object
Expand Down Expand Up @@ -93,7 +94,8 @@ func notifyUpdateToInstances(ctx context.Context, c client.Client, rec record.Ev
if err := c.Status().Update(ctx, inst); err != nil {
errs = append(errs, fmt.Errorf("failed to update instance status: %s: %w", inst.GetName(), err))
}
rec.Eventf(inst, corev1.EventTypeNormal, "TemplateUpdated", "Detected Template %s is updated", tmpl.GetName())

kosmo.InstanceEventf(rec, inst, corev1.EventTypeNormal, "TemplateUpdated", "Detected Template %s is updated", tmpl.GetName())
}
return errs
}
17 changes: 9 additions & 8 deletions internal/controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cosmo-workspace/cosmo/pkg/auth/password"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/instance"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
"github.com/cosmo-workspace/cosmo/pkg/useraddon"
)

Expand Down Expand Up @@ -51,12 +52,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, "SyncFailed", "Failed to sync namespace %s: %v", ns.Name, err)
kosmo.UserEventf(r.Recorder, &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, "Synced", "Successfully reconciled. Namespace %s is %s", ns.Name, op)
kosmo.UserEventf(r.Recorder, &user, corev1.EventTypeNormal, "Synced", "Successfully reconciled. Namespace %s is %s", ns.Name, op)
}

user.Status.Phase = ns.Status.Phase
Expand All @@ -80,12 +81,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, "PasswordInitFailed", "Failed to reset password: %v", err)
kosmo.UserEventf(r.Recorder, &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, "PasswordInitialized", "Successfully reset password secret")
kosmo.UserEventf(r.Recorder, &user, corev1.EventTypeNormal, "PasswordInitialized", "Successfully reset password secret")
}
}

Expand Down Expand Up @@ -124,7 +125,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, "AddonSynced", "Addon %s is %s", addon.Template.Name, op)
kosmo.UserEventf(r.Recorder, &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,7 +151,7 @@ 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 sync addon: %v", e)
kosmo.UserEventf(r.Recorder, &user, corev1.EventTypeWarning, "AddonFailed", "Failed to sync addon: %v", e)
log.Error(e, "failed to sync user addon")
}
user.Status.Phase = "AddonFailed"
Expand All @@ -172,10 +173,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)
kosmo.UserEventf(r.Recorder, &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)
kosmo.UserEventf(r.Recorder, &user, corev1.EventTypeNormal, "GC", "Deleted unmanaged addon: kind=%s name=%s namespace=%s", d.Kind, d.Name, d.Namespace)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions internal/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/clog"
"github.com/cosmo-workspace/cosmo/pkg/instance"
"github.com/cosmo-workspace/cosmo/pkg/kosmo"
"github.com/cosmo-workspace/cosmo/pkg/workspace"
)

Expand Down Expand Up @@ -78,12 +79,12 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return nil
})
if err != nil {
r.Recorder.Eventf(&ws, corev1.EventTypeWarning, "SyncFailed", "Failed to sync instance %s: %v", inst.Name, err)
kosmo.WorkspaceEventf(r.Recorder, &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, "Synced", "Successfully reconciled. Instance %s is %s", inst.Name, op)
kosmo.WorkspaceEventf(r.Recorder, &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 @@ -108,12 +109,12 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return r.TraefikIngressRouteCfg.PatchTraefikIngressRouteAsDesired(&ir, ws, r.Scheme)
})
if err != nil {
r.Recorder.Eventf(&ws, corev1.EventTypeWarning, "SyncFailed", "Failed to sync traefik ingress route %s: %v", ir.Name, err)
kosmo.WorkspaceEventf(r.Recorder, &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, "Synced", "Successfully reconciled. Traefik ingress route %s is %s", ir.Name, op)
kosmo.WorkspaceEventf(r.Recorder, &ws, corev1.EventTypeNormal, "Synced", "Successfully reconciled. Traefik ingress route %s is %s", ir.Name, op)
}

// generate URL and set to status
Expand Down
2 changes: 1 addition & 1 deletion internal/dashboard/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *Server) GetUser(ctx context.Context, req *connect_go.Request[dashv1alph
}
events, err := s.Klient.ListEvents(ctx, cosmov1alpha1.UserNamespace(user.Name))
if err != nil {
log.Error(err, "failed to list events", "namespace", cosmov1alpha1.UserNamespace(user.Name))
log.Error(err, "failed to list events", "user", user.Name)
}

res := &dashv1alpha1.GetUserResponse{
Expand Down
Loading

0 comments on commit 386485b

Please sign in to comment.