From bc84b17787f094acda65b2a837c7f36b9c09d088 Mon Sep 17 00:00:00 2001 From: Jonathan Innis Date: Mon, 17 Feb 2025 17:37:08 -0800 Subject: [PATCH] chore: Ensure that NodeClaim is always logged with Node (#7750) --- pkg/controllers/interruption/controller.go | 4 ++-- pkg/controllers/nodeclaim/garbagecollection/controller.go | 2 +- pkg/controllers/nodeclaim/tagging/controller.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/controllers/interruption/controller.go b/pkg/controllers/interruption/controller.go index c96d42409c54..81a91054a0ec 100644 --- a/pkg/controllers/interruption/controller.go +++ b/pkg/controllers/interruption/controller.go @@ -195,9 +195,9 @@ func (c *Controller) deleteMessage(ctx context.Context, msg *sqstypes.Message) e // handleNodeClaim retrieves the action for the message and then performs the appropriate action against the node func (c *Controller) handleNodeClaim(ctx context.Context, msg messages.Message, nodeClaim *karpv1.NodeClaim, node *corev1.Node) error { action := actionForMessage(msg) - ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("NodeClaim", klog.KRef("", nodeClaim.Name), "action", string(action))) + ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("NodeClaim", klog.KObj(nodeClaim), "action", string(action))) if node != nil { - ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("Node", klog.KRef("", node.Name))) + ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("Node", klog.KObj(node))) } // Record metric and event for this action diff --git a/pkg/controllers/nodeclaim/garbagecollection/controller.go b/pkg/controllers/nodeclaim/garbagecollection/controller.go index e1f92e2d6ad9..8dd4b8fa17c6 100644 --- a/pkg/controllers/nodeclaim/garbagecollection/controller.go +++ b/pkg/controllers/nodeclaim/garbagecollection/controller.go @@ -104,7 +104,7 @@ func (c *Controller) garbageCollect(ctx context.Context, nodeClaim *karpv1.NodeC if err := c.kubeClient.Delete(ctx, &node); err != nil { return client.IgnoreNotFound(err) } - log.FromContext(ctx).WithValues("Node", klog.KRef("", node.Name)).V(1).Info("garbage collected node") + log.FromContext(ctx).WithValues("Node", klog.KObj(&node)).V(1).Info("garbage collected node") } return nil } diff --git a/pkg/controllers/nodeclaim/tagging/controller.go b/pkg/controllers/nodeclaim/tagging/controller.go index 383cfa0bf985..6b7398c583b3 100644 --- a/pkg/controllers/nodeclaim/tagging/controller.go +++ b/pkg/controllers/nodeclaim/tagging/controller.go @@ -20,6 +20,7 @@ import ( "time" "k8s.io/apimachinery/pkg/api/equality" + "k8s.io/klog/v2" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -65,7 +66,7 @@ func (c *Controller) Reconcile(ctx context.Context, nodeClaim *karpv1.NodeClaim) if !isTaggable(nodeClaim) { return reconcile.Result{}, nil } - ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("provider-id", nodeClaim.Status.ProviderID)) + ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("Node", klog.KRef("", nodeClaim.Status.NodeName), "provider-id", nodeClaim.Status.ProviderID)) id, err := utils.ParseInstanceID(nodeClaim.Status.ProviderID) if err != nil { // We don't throw an error here since we don't want to retry until the ProviderID has been updated.