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

Migrate pkg/proxy to contextual logging: Part 1 #122979

Conversation

fatsheep9146
Copy link
Contributor

@fatsheep9146 fatsheep9146 commented Jan 26, 2024

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Use contextual logging for pkg/proxy

After trying to convert pkg/proxy to contextual logging in one pr, I found it's hard to make it and will let the pr grow too large to review.
So I deciced to split the converting job into several parts, here is the part 1.

In this part, I focus on serveral things

  1. Change the functions in cmd/kube-proxy to use context.Context not klog.Logger as input parameters as @danwinship suggested in

Thinking about the eventual migration of pkg/proxy to contextual, in pkg/proxy/nftables at least, we are using context.TODO(), which ought to be fixed at some point to get a proper context.Context from the top level (ie, here). I'm not sure if that has any implications for the right way to migrate these files now...

Originally posted by @danwinship in #122197 (review)

  1. Remove the logger instance in struct ProxyServer, since the "add logger to instance" is often not good option as @pholy said

We had various cases like this in other components. It's always a decision between "add logger" / "add context" / "add logger to instance" - but that last option is often not good because the caller might have a logger with additional key/value pairs that then get ignored when calling a method which uses some other logger that was stored previously.

Originally posted by @pohly in #122197 (comment)

  1. We add a top logger object as subfield for each implementation of proxy.Provider, for example the logger field in struct iptables.Proxier, the reason is that each proxy.Provider will be called as a informer event handler in ProxyServer, there is not a good place to add logger from outside. And there are also some key/values can only be got inside the library of proxy.Provider, for example ipFamily.

Which issue(s) this PR fixes:

Related kubernetes/enhancements#3077

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Migrated the pkg/proxy to use [contextual logging](https://k8s.io/docs/concepts/cluster-administration/system-logs/#contextual-logging).

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

- [KEP]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/3077-contextual-logging

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/M Denotes a PR that changes 30-99 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. labels Jan 26, 2024
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jan 26, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @fatsheep9146. Thanks for your PR.

I'm waiting for a kubernetes 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.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added area/kube-proxy sig/network Categorizes an issue or PR as relevant to SIG Network. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 26, 2024
@fatsheep9146
Copy link
Contributor Author

/label sig/instrumentation

@k8s-ci-robot
Copy link
Contributor

@fatsheep9146: The label(s) /label sig/instrumentation cannot be applied. These labels are supported: api-review, tide/merge-method-merge, tide/merge-method-rebase, tide/merge-method-squash, team/katacoda, refactor, official-cve-feed. Is this label configured under labels -> additional_labels or labels -> restricted_labels in plugin.yaml?

In response to this:

/label sig/instrumentation

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/test-infra repository.

@fatsheep9146
Copy link
Contributor Author

/wg structured-logging

@k8s-ci-robot k8s-ci-robot added wg/structured-logging Categorizes an issue or PR as relevant to WG Structured Logging. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jan 27, 2024
@uablrek
Copy link
Contributor

uablrek commented Jan 29, 2024

The "per-proxier" logger should have WithValue("family", ipFamily). E.g. first in NewProxier() do:

	logger = logger.WithValues("family", ipFamily)

In dual-stack there are two instances of the "proxier", one for IPv4 and one for IPv6. They log individually and often with the same message (and values). It is useful to see which of the proxiers that encounters an error for example.

I had a short discussion with @pohly on slack about performance, and it is fine to create a logger WithValues once.

I made the change above and tested. Here is a sample (json logging used):

{
  "ts": 1706546611912.709,
  "caller": "iptables/proxier.go:630",
  "msg": "Updated proxier node labels",
  "v": 4,
  "family": "IPv4",
  "labels": {
    "beta.kubernetes.io/arch": "amd64",
    "beta.kubernetes.io/os": "linux",
    "kubernetes.io/arch": "amd64",
    "kubernetes.io/hostname": "vm-002",
    "kubernetes.io/os": "linux"
  }
}
{
  "ts": 1706546611912.75,
  "caller": "iptables/proxier.go:630",
  "msg": "Updated proxier node labels",
  "v": 4,
  "family": "IPv6",
  "labels": {
    "beta.kubernetes.io/arch": "amd64",
    "beta.kubernetes.io/os": "linux",
    "kubernetes.io/arch": "amd64",
    "kubernetes.io/hostname": "vm-002",
    "kubernetes.io/os": "linux"
  }
}

@uablrek
Copy link
Contributor

uablrek commented Jan 29, 2024

/cc @danwinship @aroradaman

You should probably monitor this

@fatsheep9146
Copy link
Contributor Author

The "per-proxier" logger should have WithValue("family", ipFamily). E.g. first in NewProxier() do:

	logger = logger.WithValues("family", ipFamily)

In dual-stack there are two instances of the "proxier", one for IPv4 and one for IPv6. They log individually and often with the same message (and values). It is useful to see which of the proxiers that encounters an error for example.

I had a short discussion with @pohly on slack about performance, and it is fine to create a logger WithValues once.

I made the change above and tested. Here is a sample (json logging used):

{
  "ts": 1706546611912.709,
  "caller": "iptables/proxier.go:630",
  "msg": "Updated proxier node labels",
  "v": 4,
  "family": "IPv4",
  "labels": {
    "beta.kubernetes.io/arch": "amd64",
    "beta.kubernetes.io/os": "linux",
    "kubernetes.io/arch": "amd64",
    "kubernetes.io/hostname": "vm-002",
    "kubernetes.io/os": "linux"
  }
}
{
  "ts": 1706546611912.75,
  "caller": "iptables/proxier.go:630",
  "msg": "Updated proxier node labels",
  "v": 4,
  "family": "IPv6",
  "labels": {
    "beta.kubernetes.io/arch": "amd64",
    "beta.kubernetes.io/os": "linux",
    "kubernetes.io/arch": "amd64",
    "kubernetes.io/hostname": "vm-002",
    "kubernetes.io/os": "linux"
  }
}

Thanks! I will take this into account.

pkg/proxy/config/config.go Outdated Show resolved Hide resolved
pkg/proxy/iptables/proxier.go Outdated Show resolved Hide resolved
@fatsheep9146 fatsheep9146 force-pushed the pkg-proxy-support-contextual-logging branch from 4178a58 to 239ea97 Compare February 8, 2024 08:38
Copy link
Member

@aroradaman aroradaman left a comment

Choose a reason for hiding this comment

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

Overall LGTM, few suggestions.

@fatsheep9146 fatsheep9146 force-pushed the pkg-proxy-support-contextual-logging branch from ced8e83 to bfbb11f Compare April 10, 2024 13:41
@uablrek
Copy link
Contributor

uablrek commented Apr 11, 2024

The "root logger" proposed in #122979 (comment) is not included. I think this will be needed in the future, but it's not urgent. Now the global logger is used, which should be ok for now.

/lgtm

(my lgtm will be valid for ipvs only. @danwinship may approve other changes)

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

LGTM label has been added.

Git tree hash: 3544916c905b21be2e77dd83136e5179ac2dc22e

@fatsheep9146
Copy link
Contributor Author

@danwinship could you help review this again?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 20, 2024
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
@fatsheep9146 fatsheep9146 force-pushed the pkg-proxy-support-contextual-logging branch from bfbb11f to be4535b Compare April 22, 2024 05:29
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 22, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 22, 2024
@fatsheep9146
Copy link
Contributor Author

/retest

1 similar comment
@fatsheep9146
Copy link
Contributor Author

/retest

@aroradaman
Copy link
Member

/retest

https://kubernetes.slack.com/archives/C09QYUH5W/p1713628805680269

@k8s-ci-robot
Copy link
Contributor

@fatsheep9146: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-e2e-gci-gce-nftables be4535b link false /test pull-kubernetes-e2e-gci-gce-nftables

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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/test-infra repository. I understand the commands that are listed here.

@danwinship
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: danwinship, fatsheep9146, uablrek

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 22, 2024
@aroradaman
Copy link
Member

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

LGTM label has been added.

Git tree hash: 5a79ecafbd4393b62950efd0e4fae6d9df569a1a

@k8s-ci-robot k8s-ci-robot merged commit 8dd9d1a into kubernetes:master Apr 23, 2024
17 of 18 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.31 milestone Apr 23, 2024
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. area/ipvs area/kube-proxy 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. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/scalability Categorizes an issue or PR as relevant to SIG Scalability. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. wg/structured-logging Categorizes an issue or PR as relevant to WG Structured Logging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants