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

Ensure that addresses are added in network device index order #909

Merged

Conversation

javanthropus
Copy link
Contributor

@javanthropus javanthropus commented Apr 30, 2024

What type of PR is this?
/kind bug

What this PR does / why we need it:
This ensures that the addresses associated with network devices attached to the host are added to a Node resource's address list in order of device index. For some unknown reason AWS returns the list of network devices for just some of our EC2 instances where the primary device is not first in the list. Without this code change, the addresses of the secondary devices are listed first in the addresses for a Node, and this breaks the ability to interact with pods on the node, such as fetching logs and creating port forwards, because the apiserver always uses the first address of the Node resource to reach the kubelet but is unable to reach kubelet on any of these other addresses.

Which issue(s) this PR fixes:
Fixes #911.

Special notes for your reviewer:
None

Does this PR introduce a user-facing change?:

Addresses associated with the Node resource will be sorted in order of the index of the network device to which they're attached.  In cases where a VPC CNI, such as aws-cni, is used and the order of devices returned for a node is unpredictable, this ensures that Kubernetes uses the right address to interact with the kubelet on the node.

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Apr 30, 2024
Copy link

linux-foundation-easycla bot commented Apr 30, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: javanthropus / name: Jeremy Bopp (e637708)

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Apr 30, 2024
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If cloud-provider-aws contributors determine 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
Copy link
Contributor

Welcome @javanthropus!

It looks like this is your first PR to kubernetes/cloud-provider-aws 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/cloud-provider-aws has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@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 Apr 30, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @javanthropus. 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 the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 30, 2024
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 30, 2024
@@ -1543,6 +1543,11 @@ func extractIPv4NodeAddresses(instance *ec2.Instance) ([]v1.NodeAddress, error)

addresses := []v1.NodeAddress{}

// sort by device index so that the first address added to the addresses list is from the first (primary) device
sort.Slice(instance.NetworkInterfaces, func(i, j int) bool {
return *instance.NetworkInterfaces[i].Attachment.DeviceIndex < *instance.NetworkInterfaces[j].Attachment.DeviceIndex
Copy link
Contributor

@cartermckinnon cartermckinnon Apr 30, 2024

Choose a reason for hiding this comment

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

Change makes sense to me. Can you add checks for a nil attachment and use aws.IntValue to dereference the index? The EC2 API should return sane values but we shouldn't crash if it doesn't

Copy link
Contributor Author

@javanthropus javanthropus Apr 30, 2024

Choose a reason for hiding this comment

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

Should I filter out interfaces where the attachment is nil, or should I just sort them to the end of the list? I feel like filtering them out would be the right solution since they should not have addresses associated if the interface isn't actually attached to an instance.

Copy link
Contributor

@cartermckinnon cartermckinnon Apr 30, 2024

Choose a reason for hiding this comment

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

The check here: https://github.com/kubernetes/cloud-provider-aws/pull/909/files/40202804f2189a7e3958261c8a1732bd8fd7686c#diff-97a31d0f193ea0b4e2c538595daf93405351054377458053a5010f6fdb97f4dcR1554-R1556

Should filter those out I think? so you just need to handle the potential nil pointer here. Start or end of the slice doesn't matter to me 😄

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 believe the updated code should just cause them to be sorted to the end of the list. It avoids allocating another slice, and the code that pulls the addresses out should ignore them anyway.

@cartermckinnon
Copy link
Contributor

/ok-to-test

@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. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 30, 2024
@javanthropus
Copy link
Contributor Author

What's the process for back porting changes? My understanding is that we need to use the 1.27 release branch since our clusters are still running k8s 1.27.

@cartermckinnon cartermckinnon changed the base branch from release-1.30 to master April 30, 2024 22:01
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 30, 2024
@cartermckinnon
Copy link
Contributor

cartermckinnon commented Apr 30, 2024

Ah I didn't notice you had this open against release-1.30. I changed the base to master so you'll need to rebase. After merge you can open cherrypick PR's against older release branches as needed. I use this script to fire them off quickly: https://github.com/kubernetes/kubernetes/blob/master/hack/cherry_pick_pull.sh

example here: https://kops.sigs.k8s.io/contributing/proposing-a-cherry-pick/

@javanthropus
Copy link
Contributor Author

Ah I didn't notice you had this open against release-1.30. I changed the base to master so you'll need to rebase.

Sorry about that. It wasn't clear what the process is for changes like this. Should I go ahead and squash my commits when I rebase?

@cartermckinnon
Copy link
Contributor

Sure you can go ahead and squash! Thx

@@ -928,6 +928,19 @@ func extractIPv4NodeAddresses(instance *ec2.Instance) ([]v1.NodeAddress, error)

addresses := []v1.NodeAddress{}

// sort by device index so that the first address added to the addresses list is from the first (primary) device
sort.Slice(instance.NetworkInterfaces, func(i, j int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we write a unit test for this to confirm that the ordering is what we intend?

Copy link
Contributor Author

@javanthropus javanthropus May 2, 2024

Choose a reason for hiding this comment

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

I could give it a try, but I didn't do so already because I couldn't find much in the way of existing tests here. As a result, I don't have any good examples to base my work on. Did I overlook something?

I can report that I'm running this same patch via a forked version of 1.27.6 on several clusters, and it fixed the issue reported here.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's good information to have. I don't know that it necessarily needs to be a strict blocker, but I don't think it would be too crazy.

I don't see any asynchronous calls here, so it should act entirely on the inputs. You could separate this out into another method to really narrow down the testing, but shouldn't be much more scope to leave as is. Here's a somewhat similar sort of test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the link. I was looking in the wrong place for tests. That's not surprising since I'm very green with golang. I'll try to add some tests tomorrow and push them up.

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 finally managed to understand how the fakes work here, but I don't see any tests for this functionality. I see some tests for Fargate instances, but they don't use the NetworkInterfaces list and rely solely on the PrivateDnsName and PrivateIpAddress fields of the instance. Shouldn't that be sufficient for non-Fargate instances as well? In other words, why does this controller attempt to list all addresses for all network interfaces for a node rather than just the primary address and DNS name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mmerkes, do you know if we can simplify this whole thing and just use the primary address and DNS name of the nodes rather than iterate over all attached addresses?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A test has been added as requested.

@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 May 14, 2024
@javanthropus
Copy link
Contributor Author

/retest-required

@javanthropus
Copy link
Contributor Author

The last couple of failed test runs don't appear to be related to my change. @mmerkes or @cartermckinnon, can one of you PTAL?

@cartermckinnon
Copy link
Contributor

@javanthropus sorry about that, the CI is busted. If we can't get a fix in shortly I'll override.

@hakman
Copy link
Member

hakman commented May 17, 2024

/test pull-cloud-provider-aws-e2e

@hakman
Copy link
Member

hakman commented May 17, 2024

@javanthropus Please rebase instead of merge. Thanks!

@hakman
Copy link
Member

hakman commented May 17, 2024

/retest

@hakman
Copy link
Member

hakman commented May 17, 2024

/test pull-cloud-provider-aws-e2e-kubetest2

@cartermckinnon
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 17, 2024
@k8s-ci-robot k8s-ci-robot merged commit bc848d1 into kubernetes:master May 17, 2024
13 checks passed
@cartermckinnon
Copy link
Contributor

Thanks for fixing this @javanthropus!

cartermckinnon added a commit that referenced this pull request May 23, 2024
…09-upstream-release-1.30

Automated cherry pick of #909: Ensure that addresses are added in network device index order
@javanthropus javanthropus deleted the ensure-network-address-order branch May 23, 2024 22:39
k8s-ci-robot added a commit that referenced this pull request May 24, 2024
…09-upstream-release-1.29

Automated cherry pick of #909: Ensure that addresses are added in network device index order
k8s-ci-robot added a commit that referenced this pull request May 24, 2024
…09-upstream-release-1.28

Automated cherry pick of #909: Ensure that addresses are added in network device index order
k8s-ci-robot added a commit that referenced this pull request May 24, 2024
…09-upstream-release-1.27

Automated cherry pick of #909: Ensure that addresses are added in network device index order
k8s-ci-robot added a commit that referenced this pull request May 24, 2024
…09-upstream-release-1.26

Automated cherry pick of #909: Ensure that addresses are added in network device index order
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. 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. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeviceIndex is not respected when processing multiple network interfaces on a node
5 participants