Skip to content

Conversation

@anndono
Copy link
Contributor

@anndono anndono commented Dec 18, 2025

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR fixes logger initialization to correctly derive loggers from the request context when available.

Changes:

  • Replaces usages of log.Background() with log.FromContextOrBackground(ctx) when ctx is available
  • Moves logger initialization to the appropriate scope
  • Renamed some of the unused parameters from _ to ctx because now that the context is actively used for logging
  • Moved selected context.Background() initializations earlier in the function to allow reuse of the same context

Which issue(s) this PR fixes:

Fixes #
ref: #1575

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. labels Dec 18, 2025
@github-actions github-actions bot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Dec 18, 2025
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 18, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @anndono. Thanks for your PR.

I'm waiting for a github.com member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 18, 2025
@nilo19 nilo19 requested a review from Copilot December 18, 2025 01:01
Copy link
Contributor

@nilo19 nilo19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran go test ./... locally; all tests passed.

This looks like a solid step toward Kubernetes contextual logging guidance (ref: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md): prefer deriving the logger from the provided ctx (via klog.FromContext / log.FromContextOrBackground) instead of using a global/background logger, and initialize the logger in the correct scope (e.g., inside leader-election callbacks).

One follow-up for consistency: there are still a few functions that accept ctx but use klog.Background() instead of the context logger (e.g. pkg/provider/azure_vmss.go EnsureHostInPool / ensureBackendPoolDeletedFromNode, and pkg/provider/azure_loadbalancer.go getEligibleLoadBalancersForService). Consider switching those to log.FromContextOrBackground(ctx) (or klog.FromContext(ctx)) so request/controller-scoped values make it into logs.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves logger initialization throughout the codebase by replacing log.Background() with log.FromContextOrBackground(ctx) when a context is available. This allows loggers to inherit contextual information from request contexts, providing better traceability and debugging capabilities.

Key Changes:

  • Replace log.Background() with log.FromContextOrBackground(ctx) in functions that have context available
  • Rename unused context parameters from _ to ctx to enable logger context usage
  • Move logger initialization to appropriate scopes, particularly in cache getter functions where context becomes available
  • Move some context.Background() initializations earlier in functions to enable context reuse

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/provider/subnet/subnet.go Updated logger initialization in CreateOrUpdate to use context
pkg/provider/storage/storage_account.go Updated logger in GetStorageAccesskey to use context
pkg/provider/storage/fileservice/fileservice_repo.go Updated logger in Get to use context
pkg/provider/storage/azure_storageaccount.go Updated loggers in multiple storage account methods to use context
pkg/provider/securitygroup/azure_securitygroup_repo.go Moved logger initialization into getter closure and updated CreateOrUpdateSecurityGroup
pkg/provider/azure_zones.go Updated loggers in zone-related methods to use context
pkg/provider/azure_vmssflex_cache.go Updated loggers in VMSS Flex cache methods to use context
pkg/provider/azure_vmssflex.go Updated loggers in VMSS Flex methods to use context
pkg/provider/azure_vmss_repo.go Moved logger initialization after context creation in CreateOrUpdateVMSS
pkg/provider/azure_vmss_cache.go Moved logger initialization into cache getter closures
pkg/provider/azure_vmss.go Updated loggers and moved initialization after context creation where applicable
pkg/provider/azure_vmsets_repo.go Updated loggers and moved initialization into cache getter closure
pkg/provider/azure_standard.go Updated loggers in availability set methods to use context
pkg/provider/azure_routes.go Updated loggers in route methods and renamed parameter from _ to ctx in DeleteRoute
pkg/provider/azure_privatelinkservice.go Updated loggers in private link service methods to use context
pkg/provider/azure_local_services.go Updated loggers in load balancer backend pool methods to use context
pkg/provider/azure_loadbalancer_repo.go Updated loggers and moved initialization into cache getter closure
pkg/provider/azure_loadbalancer.go Updated loggers in load balancer methods to use context
pkg/provider/azure_interface_repo.go Updated logger in CreateOrUpdateInterface to use context
pkg/provider/azure_instances_v2.go Updated loggers in InstancesV2 methods to use context
pkg/provider/azure_instances_v1.go Updated loggers in InstancesV1 methods to use context
pkg/provider/azure_instance_metadata.go Renamed parameter from _ to ctx and updated loggers in metadata methods
pkg/provider/azure_controller_vmssflex.go Updated loggers in VMSS Flex controller methods to use context
pkg/provider/azure_controller_vmss.go Updated loggers in VMSS controller methods to use context
pkg/provider/azure_controller_standard.go Renamed parameter from _ to ctx and updated logger in DeleteCacheForNode
pkg/provider/azure.go Updated logger in InitializeCloudFromConfig to use context
pkg/nodemanager/nodemanager.go Updated loggers in node manager methods to use context
pkg/nodeipam/node_ipam_controller.go Updated logger in Run to use context
pkg/nodeipam/ipam/range_allocator.go Updated logger in Run to use context
pkg/nodeipam/ipam/cloud_cidr_allocator.go Created context earlier and updated loggers to use it
pkg/credentialprovider/identity_bindings_credentials.go Moved logger into DialContext closure and updated GetToken logger
pkg/credentialprovider/azure_credentials.go Created context earlier and updated loggers to use it
cmd/cloud-node-manager/app/nodemanager.go Updated loggers in Run and startControllers to use context
cmd/cloud-controller-manager/app/core.go Updated logger in startRouteController to use context
cmd/cloud-controller-manager/app/controllermanager.go Moved logger initialization into RunWrapper and runAsync closures

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nilo19
Copy link
Contributor

nilo19 commented Dec 18, 2025

/ok-to-test
/lgtm

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 18, 2025
@nilo19
Copy link
Contributor

nilo19 commented Dec 18, 2025

/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 18, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: anndono, nilo19

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 18, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 38 out of 38 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@k8s-ci-robot k8s-ci-robot merged commit 432deb3 into kubernetes-sigs:master Dec 18, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants