Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

chore(kubelet): migrate utils to contextual logging #130480

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from

Conversation

omerap12
Copy link
Member

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Which issue(s) this PR fixes:

Part of #130069

Special notes for your reviewer:

Since some functions are part of interfaces, I had to modify other areas as well (such as pkg/kubelet/config/config.go, etc.).

Does this PR introduce a user-facing change?

NONE

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

NONE

@k8s-ci-robot k8s-ci-robot added 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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubelet area/test sig/node Categorizes an issue or PR as relevant to SIG Node. sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Feb 27, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Feb 27, 2025
@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from 6e6debd to ccaed7e Compare February 27, 2025 21:08
@omerap12
Copy link
Member Author

/retest

@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from ccaed7e to 5f7d0ed Compare February 28, 2025 07:50
@omerap12
Copy link
Member Author

/retest

@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from 5f7d0ed to c0e0f8a Compare February 28, 2025 11:46
@omerap12
Copy link
Member Author

/assign bart0sh

@@ -73,7 +74,7 @@ type objectStore struct {
}

// NewObjectStore returns a new ttl-based instance of Store interface.
func NewObjectStore(getObject GetObjectFunc, clock clock.Clock, getTTL GetObjectTTLFunc, ttl time.Duration) Store {
func NewObjectStore(getObject GetObjectFunc, clock clock.Clock, getTTL GetObjectTTLFunc, ttl time.Duration, ctx context.Context) Store {
Copy link
Contributor

Choose a reason for hiding this comment

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

ctx is not used in this function.

@@ -168,11 +169,11 @@ type objectCache struct {
lock sync.RWMutex
items map[objectKey]*objectCacheItem
stopped bool
ctx context.Context
Copy link
Contributor

Choose a reason for hiding this comment

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

Storing context is not a good practice, please try to avoid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I see. So, some functions will include context.TODO() because they are part of an interface used by many kubelet components. Are we okay with that?

Copy link
Contributor

Choose a reason for hiding this comment

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

It depends on how many. In this particular case it would probably make sense to store a logger instead of context.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll switch to context.TODO instead of storing the context so you can see the difference. Then, we can decide on the best approach.

@bart0sh
Copy link
Contributor

bart0sh commented Feb 28, 2025

/triage accepted
/priority important-longterm
/assign

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Feb 28, 2025
@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from c0e0f8a to 01ffa69 Compare February 28, 2025 12:46
@k8s-ci-robot k8s-ci-robot added area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/storage Categorizes an issue or PR as relevant to SIG Storage. labels Feb 28, 2025
@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from 01ffa69 to 56a66f2 Compare February 28, 2025 12:50
@omerap12 omerap12 requested a review from bart0sh February 28, 2025 19:54
pkg/kubelet/config/config.go Outdated Show resolved Hide resolved
pkg/kubelet/config/config_test.go Outdated Show resolved Hide resolved
pkg/kubelet/status/status_manager.go Outdated Show resolved Hide resolved
pkg/kubelet/status/status_manager.go Show resolved Hide resolved
@@ -343,7 +344,10 @@ func (c *objectCache) Get(namespace, name string) (runtime.Object, error) {
if c.isImmutable(object) {
item.setImmutable()
if item.stop() {
klog.V(4).InfoS("Stopped watching for changes - object is immutable", "obj", klog.KRef(namespace, name))
// TODO: it needs to be replaced by a proper context in the future
ctx := context.TODO()
Copy link
Contributor

@HirazawaUi HirazawaUi Mar 1, 2025

Choose a reason for hiding this comment

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

We should place ctx := context.TODO() at the beginning of the function.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you sure? This context is only being used for the log message here, so I thought it would be better to create the context only when needed

Copy link
Contributor

Choose a reason for hiding this comment

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

If it's only used here, do you think the following approach would be better?

// TODO: replace context.TODO() with proper context
klog.FromContext(context.TODO()).V(4).Info("Stopped watching for changes - object is immutable", "obj", klog.KRef(namespace, name))

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, thanks!

@omerap12 omerap12 force-pushed the migrate-kubelete-util branch 2 times, most recently from bba929b to 2bff882 Compare March 1, 2025 14:42
@omerap12 omerap12 requested a review from HirazawaUi March 1, 2025 14:44
@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from 2bff882 to 1bb2164 Compare March 2, 2025 07:26
@@ -34,9 +35,11 @@ import (
// It uses /proc/stat first, which is more accurate, and falls back to the less accurate
// unix.Sysinfo if /proc/stat failed.
func GetBootTime() (time.Time, error) {
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, pass context from the upper layers, thanks!

@@ -343,7 +344,8 @@ func (c *objectCache) Get(namespace, name string) (runtime.Object, error) {
if c.isImmutable(object) {
item.setImmutable()
if item.stop() {
klog.V(4).InfoS("Stopped watching for changes - object is immutable", "obj", klog.KRef(namespace, name))
// TODO: it needs to be replaced by a proper context in the future
klog.FromContext(context.TODO()).V(4).Info("Stopped watching for changes - object is immutable", "obj", klog.KRef(namespace, name))
Copy link
Contributor

Choose a reason for hiding this comment

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

The same here. Please, avoid usage of context.TODO() in migrated code. Pass it from upper layers instead, thanks!

These should preferably be fixed, unless there are strong reasons not to do it:

$ git grep '\(Background()\)\|\(TODO()\)' ./pkg/kubelet/util
pkg/kubelet/util/manager/cache_based_manager_test.go:           return fakeClient.CoreV1().Secrets(namespace).Get(context.TODO(), name, opts)
pkg/kubelet/util/manager/watch_based_manager.go:                                klog.FromContext(context.TODO()).V(4).Info("Stopped watching for changes - object is immutable", "obj", klog.KRef(namespace, name))
pkg/kubelet/util/manager/watch_based_manager.go:        ctx := context.TODO()
pkg/kubelet/util/manager/watch_based_manager_test.go:           return fakeClient.CoreV1().Secrets(namespace).List(context.TODO(), opts)
pkg/kubelet/util/manager/watch_based_manager_test.go:           return fakeClient.CoreV1().Secrets(namespace).Watch(context.TODO(), opts)
pkg/kubelet/util/nodelease.go:                  if node, err := c.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{}); err == nil {

@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from 1bb2164 to f05ed4d Compare March 2, 2025 16:58
@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. sig/scalability Categorizes an issue or PR as relevant to SIG Scalability. labels Mar 2, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: omerap12
Once this PR has been reviewed and has the lgtm label, please assign dchen1107 for approval. For more information see the Code Review Process.

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

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

Signed-off-by: Omer Aplatony <omerap12@gmail.com>
@omerap12 omerap12 force-pushed the migrate-kubelete-util branch from f05ed4d to 73afd04 Compare March 2, 2025 17:18
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 2, 2025
ctx := context.TODO()
logger := klog.FromContext(ctx)

pod, err := m.kubeClient.CoreV1().Pods(status.podNamespace).Get(ctx, status.podName, metav1.GetOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not make massive changes to other components. This PR should concentrate on migrating kubelet/util + as little as possible changes in the upper layer.

func IsTmpfsNoswapOptionSupported(mounter mount.Interface, mountPath string) bool {
isTmpfsNoswapOptionSupportedHelper := func() bool {
func IsTmpfsNoswapOptionSupported(logger klog.Logger, mounter mount.Interface, mountPath string) bool {
// TODO: it needs to be replaced by a proper context in the future
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment doesn't seem to be needed anymore.

@@ -121,7 +122,8 @@ func isSwapOnAccordingToProcSwaps(procSwapsContent []byte) bool {
// IsSwapOn detects whether swap in enabled on the system by inspecting
// /proc/swaps. If the file does not exist, an os.NotFound error will be returned.
// If running on windows, swap is assumed to always be false.
func IsSwapOn() (bool, error) {
func IsSwapOn(logger klog.Logger) (bool, error) {
// TODO: it needs to be replaced by a proper context in the future
Copy link
Contributor

Choose a reason for hiding this comment

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

The same here.

@bart0sh
Copy link
Contributor

bart0sh commented Mar 3, 2025

@omerap12 This PR differs from the others in the series as it passes logger around instead of context. Other PRs mostly pass ctx to at least public interfaces and use logger at the lowest level, usually in a local functions that most probably would never use context. I'm not insisting, but it would make the series more consistent if we consider passing ctx to at least public interfaces.
Does this make sense to you?

@omerap12
Copy link
Member Author

omerap12 commented Mar 3, 2025

@omerap12 This PR differs from the others in the series as it passes logger around instead of context. Other PRs mostly pass ctx to at least public interfaces and use logger at the lowest level, usually in a local functions that most probably would never use context. I'm not insisting, but it would make the series more consistent if we consider passing ctx to at least public interfaces. Does this make sense to you?

Hmm, that seems reasonable to me. I initially thought it might be better to pass only the logger since the function only uses the context for logging, but I see your point. I'll make the adjustment.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 15, 2025
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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.

@bart0sh bart0sh moved this from PRs - Needs Reviewer to PRs Waiting on Author in SIG Node CI/Test Board Apr 19, 2025
@dims dims added the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label May 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver area/kubelet area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/scalability Categorizes an issue or PR as relevant to SIG Scalability. sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: PRs Waiting on Author
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.