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

[Dual-stack] Fix generateAPIPodStatus() of kubelet handling Secondary IP #116879

Merged

Conversation

lzhecheng
Copy link
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

[Dual-stack] Fix generateAPIPodStatus() of kubelet handling Secondary IP. hostIPs order may not be be consistent. If secondary IP is before primary one, current logic adds primary IP twice into PodIPs, which leads to error: "may specify no more than one IP for each IP family".

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

[Dual-stack] Fix generateAPIPodStatus() of kubelet handling Secondary IP. hostIPs order may not be be consistent. If secondary IP is before primary one, current logic adds primary IP twice into PodIPs, which leads to error: "may specify no more than one IP for each IP family".

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


@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Mar 23, 2023
@k8s-ci-robot k8s-ci-robot added area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. 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. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 23, 2023
@lzhecheng lzhecheng force-pushed the fix-generateAPIPodStatus-dualstack branch from 568868e to f35d0e7 Compare March 23, 2023 04:56
@jackfrancis
Copy link
Contributor

/assign

pkg/kubelet/kubelet_pods.go Outdated Show resolved Hide resolved
@k8s-ci-robot
Copy link
Contributor

@aojea: GitHub didn't allow me to request PR reviews from the following users: he, has, a, to, try, organize, this, keep.

Note that only kubernetes members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

I see, if the first hostIP returns IPA, and the subsequent request returms IPB,IPA in different order, it will add { IPA, IPA}

However, there are a lot of places in the code that depend on the order of the returned address, that is why we have

func (kl *Kubelet) sortPodIPs(podIPs []string) []string {

that is called from

convertStatusToAPIStatus

/cc @danwinship he has a keep to try to organize this

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.

@aojea
Copy link
Member

aojea commented Mar 27, 2023

/assign

@lzhecheng lzhecheng force-pushed the fix-generateAPIPodStatus-dualstack branch 3 times, most recently from 8b36f12 to bc7ee64 Compare March 28, 2023 05:22
@lzhecheng
Copy link
Contributor Author

/test pull-kubernetes-e2e-kind

pkg/kubelet/kubelet_pods.go Outdated Show resolved Hide resolved
pkg/kubelet/kubelet_pods.go Outdated Show resolved Hide resolved
@bart0sh
Copy link
Contributor

bart0sh commented Mar 30, 2023

/triage accepted
/priority important-soon

@k8s-ci-robot k8s-ci-robot added the triage/accepted Indicates an issue or PR is ready to be actively worked on. label Mar 30, 2023
s.PodIPs = append(s.PodIPs, v1.PodIP{IP: hostIPs[1].String()})
ok, err := utilnet.IsDualStackIPStrings([]string{s.PodIPs[0].IP, hostIPs[1].String()})
if err != nil {
kl.recorder.Eventf(pod, v1.EventTypeWarning, "InvalidHostIPs", "host IP is invalid %q", hostIPs[1].String())
Copy link
Contributor

Choose a reason for hiding this comment

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

You can't get an error here. s.PodIPs is validated and hostIPs comes from a function that only returns valid IPs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Undetstood, but I think we'd better still handle the error?

dualStack, err := netutils.IsDualStackIPStrings(service.Spec.ClusterIPs)
if err != nil { // though we check for that earlier. safe > sorry

Copy link
Member

Choose a reason for hiding this comment

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

yeh, I think there is also a linter now or is going to be added to enforce that

s.PodIPs = append(s.PodIPs, v1.PodIP{IP: hostIPs[1].String()})
} else {
kl.recorder.Eventf(pod, v1.EventTypeWarning, "InvalidHostIPs",
"Inconsistent addresses order received from the cloud-provider, IP family of the second host IP %q is the same as the first one configured in the kubelet %q",
Copy link
Contributor

Choose a reason for hiding this comment

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

The wording here is not great in terms of helping the admin understand what to do.

Also, if we are going to notice the "you requested IPv4 but the cloud provider wants dual-stack IPv6-primary" case, then we should really also notice the "you requested IPv4 but the cloud provider wants single-stack IPv6" case too. (It is likely that the original reporter was also running into that case but just didn't notice yet because it doesn't cause any explicit errors. But it may cause problems later with Services.)

So, starting at line 1627:

  • if utilnet.IPFamilyOfString(s.HostIP) != utilnet.IPFamilyOf(hostIPs[0]) then we need to log a warning:
    • if s.HostIP is IPv4 then the problem is that kubelet auto-detected an IPv4 node IP, but the cloud provider wants this to be a single-stack IPv6 or dual-stack IPv6-primary node, and so the fix is for the admin to either (a) pass --node-ip :: to kubelet to tell it to prefer IPv6 like the cloud provider wants, (b) pass --node-ip <EXPLICIT-IPV4-ADDRESS> to request a single-stack IPv4 node with a specific IP. (Once CloudDualStackNodeIPs is beta we can also suggest (c) pass --node-ip <EXPLICIT-IPV4-ADDRESS>,<EXPLICIT-IPV6-ADDRESS>.)
    • if s.HostIP is IPv6 then the problem is that the admin passed --node-ip :: to request an IPv6 node IP, but the cloud provider wants this to be a single-stack IPv4 or dual-stack IPv4-primary node, and so the fix is for the admin to either (a) not pass --node-ip :: to kubelet and let it default to IPv4-primary like the cloud provider wants, (b)/(c) pass an explicit IPv6 IP as above
  • regardless of whether it logged a warning, it should do s.HostIP = hostIPs[0].String(), as it does now
  • if the pod is host-network then:
    • it should set s.PodIPs[0] to hostIPs[0] if s.PodIPs is unset, as it does now
    • it should set s.PodIPs[1] to hostIPs[1] if len(hostIPs) == 2 && len(s.PodIPs) == 1 and utilnet.IPFamilyOfString(s.PodIPs[0].IP) != utilnet.IPFamilyOf(hostIPs[1])
      • (the family mismatch can only happen if we have already logged an error above, so we don't need to warn again here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if s.HostIP is IPv4 then the problem is that kubelet auto-detected an IPv4 node IP, but the cloud provider

I think you are talking about hostIPs[0]?

Thanks for the detailed explanation. Updated.

@lzhecheng lzhecheng force-pushed the fix-generateAPIPodStatus-dualstack branch from 7f43437 to c297ee8 Compare April 11, 2023 06:33
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 11, 2023
@lzhecheng lzhecheng force-pushed the fix-generateAPIPodStatus-dualstack branch from c297ee8 to c8c6ff8 Compare April 11, 2023 07:40
@lzhecheng
Copy link
Contributor Author

/retest

@aojea
Copy link
Member

aojea commented Apr 11, 2023

I think is ok, but defer to Dan, he is driving all this node-ip effort and the person more familiar with it
/assign @danwinship @thockin

@lzhecheng
Copy link
Contributor Author

@danwinship hello, do you have any other comments? Thank you.

Comment on lines 1628 to 1632
ok, err := utilnet.IsDualStackIPStrings([]string{s.HostIP, hostIPs[0].String()})
if err != nil {
kl.recorder.Eventf(pod, v1.EventTypeWarning, "InvalidHostIPs", "host IPs are invalid %q or %q", s.HostIP, hostIPs[0].String())
}
if ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think

if utilnet.IPFamilyOfString(s.HostIP) != utilnet.IPFamilyOf(hostIPs[0]) {

is clearer: the error case can't actually happen anyway, and we're comparing exactly two IPs, not scanning a possibly-long list of IPs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also prefer clearer code. Updated.

}
if ok {
// Once CloudDualStackNodeIPs is beta, <EXPLICIT-IPV4-ADDRESS>,<EXPLICIT-IPV6-ADDRESS> can be set to --node-ip.
if utilnet.IPFamilyOf(hostIPs[0]) == utilnet.IPv4 {
Copy link
Contributor

Choose a reason for hiding this comment

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

if utilnet.IsIPv4(hostIPs[0]) {

actually, wait, this is backwards: hostIPs is what the cloud provider chose, s.HostIP is what kubelet detected.

Comment on lines 1635 to 1638
// kubelet auto-detected an IPv4 node IP (hostIPs[0]), but the cloud provider wants this to be
// a single-stack IPv6 or dual-stack IPv6-primary node
kl.recorder.Eventf(pod, v1.EventTypeWarning, "HostIPsIPFamilyMismatch",
"set [::] or an explicit IPv4 address to --node-ip")
Copy link
Contributor

Choose a reason for hiding this comment

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

The information that is in the comment needs to be in the Event text instead to make it understandable (after which you no longer need the comment).

"Kubelet auto-detected an IPv4 node IP, but the cloud provider selected an IPv6 node IP; pass an explicit `--node-ip` to kubelet to fix this."

Comment on lines 1640 to 1643
// kubelet auto-detected an IPv6 node IP (hostIPs[0]), but the cloud provider wants this to be
// a single-stack IPv4 or dual-stack IPv4-primary node
kl.recorder.Eventf(pod, v1.EventTypeWarning, "HostIPsIPFamilyMismatch",
"do not set --node-ip or set an explicit IPv4 address to --node-ip")
Copy link
Contributor

Choose a reason for hiding this comment

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

likewise. Also, the fix here is to pass an explicit IPv6 address, not IPv4.

actually, we don't really even need separate errors for the v4 and v6 case. Just replace this all (1627-1646) with:

if s.HostIP != "" && utilnet.IPFamilyOfString(s.HostIP) != utilnet.IPFamilyOf(hostIPs[0]) {
        kl.recorder.Eventf(pod, v1.EventTypeWarning, "HostIPsIPFamilyMismatch",
                "Kubelet detected an IPv%s node IP (%s), but the cloud provider selected an IPv%s node IP (%s); pass an explicit `--node-ip` to kubelet to fix this.",
                utilnet.IPFamilyOfString(s.HostIP), s.HostIP, utilnet.IPFamilyOf(hostIPs[0])), hostIPs[0].String())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -1635,7 +1655,12 @@ func (kl *Kubelet) generateAPIPodStatus(pod *v1.Pod, podStatus *kubecontainer.Po
}
// Secondary IP is not set #105320
if len(hostIPs) == 2 && len(s.PodIPs) == 1 {
s.PodIPs = append(s.PodIPs, v1.PodIP{IP: hostIPs[1].String()})
ok, err := utilnet.IsDualStackIPStrings([]string{s.PodIPs[0].IP, hostIPs[1].String()})
Copy link
Contributor

Choose a reason for hiding this comment

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

again, I think using IPFamilyOf != IPFamilyOf would be clearer here, and get rid of the unnecessary error check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

hostIPs order may not be be consistent. If secondary IP is before
primary one, current logic adds primary IP twice into PodIPs, which
leads to error: "may specify no more than one IP for each IP family".
In this case, the second IP shouldn't be added.

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>
@lzhecheng lzhecheng force-pushed the fix-generateAPIPodStatus-dualstack branch from c8c6ff8 to 985cf71 Compare July 3, 2023 06:47
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 3, 2023
@danwinship
Copy link
Contributor

/lgtm
but needs a node approver

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

LGTM label has been added.

Git tree hash: fe173fc6f04c8dc9bb1440ee78c5caa1aca2d19d

@lzhecheng
Copy link
Contributor Author

/assign @Random-Liu
@Random-Liu @thockin could you please help review the PR? It has lgtm now.

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

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

Thanks!

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: lzhecheng, thockin

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 Jul 7, 2023
@k8s-triage-robot
Copy link

The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass.

This bot retests PRs for certain kubernetes repos according to the following rules:

  • The PR does have any do-not-merge/* labels
  • The PR does not have the needs-ok-to-test label
  • The PR is mergeable (does not have a needs-rebase label)
  • The PR is approved (has cncf-cla: yes, lgtm, approved labels)
  • The PR is failing tests required for merge

You can:

/retest

1 similar comment
@k8s-triage-robot
Copy link

The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass.

This bot retests PRs for certain kubernetes repos according to the following rules:

  • The PR does have any do-not-merge/* labels
  • The PR does not have the needs-ok-to-test label
  • The PR is mergeable (does not have a needs-rebase label)
  • The PR is approved (has cncf-cla: yes, lgtm, approved labels)
  • The PR is failing tests required for merge

You can:

/retest

@k8s-ci-robot k8s-ci-robot merged commit 1e0b4c8 into kubernetes:master Jul 8, 2023
12 checks passed
SIG Node PR Triage automation moved this from Needs Reviewer to Done Jul 8, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.28 milestone Jul 8, 2023
@lzhecheng lzhecheng deleted the fix-generateAPIPodStatus-dualstack branch July 8, 2023 08:46
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/kubelet 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. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/node Categorizes an issue or PR as relevant to SIG Node. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

9 participants