Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log when k8s resources are created/updated/deleted successfully #8094

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pkg/controller/common/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,25 @@ func ReconcileResource(params Params) error {
if err != nil {
return err
}
log := ulog.FromContext(params.Context)
namespace := params.Expected.GetNamespace()
name := params.Expected.GetName()

gvk, err := apiutil.GVKForObject(params.Expected, scheme.Scheme)
if err != nil {
return err
}
kind := gvk.Kind

if params.Owner != nil {
if err := controllerutil.SetControllerReference(params.Owner, params.Expected, scheme.Scheme); err != nil {
return err
}
}

kind := gvk.Kind
namespace := params.Expected.GetNamespace()
name := params.Expected.GetName()
log := ulog.FromContext(params.Context).WithValues("kind", kind, "namespace", namespace, "name", name)

create := func() error {
log.Info("Creating resource", "kind", kind, "namespace", namespace, "name", name)
log.Info("Creating resource")
if params.PreCreate != nil {
if err := params.PreCreate(); err != nil {
return err
Expand All @@ -106,6 +108,7 @@ func ReconcileResource(params Params) error {
if err != nil {
return err
}
log.Info("Created resource successfully")
return nil
}

Expand All @@ -119,8 +122,7 @@ func ReconcileResource(params Params) error {
}

if params.NeedsRecreate != nil && params.NeedsRecreate() {
log.Info("Resource cannot be updated, hence will be deleted and then recreated", "kind", kind, "namespace", namespace, "name", name)
log.Info("Deleting resource", "kind", kind, "namespace", namespace, "name", name)
log.Info("Deleting resource as it cannot be updated, it will be recreated")
reconciledMeta, err := meta.Accessor(params.Reconciled)
if err != nil {
return err
Expand All @@ -138,13 +140,14 @@ func ReconcileResource(params Params) error {
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to delete %s %s/%s: %w", kind, namespace, name, err)
}
log.Info("Deleted resource successfully")
return create()
}

//nolint:nestif
// Update if needed
if params.NeedsUpdate() {
log.Info("Updating resource", "kind", kind, "namespace", namespace, "name", name)
log.Info("Updating resource")
if params.PreUpdate != nil {
if err := params.PreUpdate(); err != nil {
return err
Expand Down Expand Up @@ -179,6 +182,7 @@ func ReconcileResource(params Params) error {
if params.PostUpdate != nil {
params.PostUpdate()
}
log.Info("Updated resource successfully")
}
return nil
}