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

Kubelet manage hosts file for HostNetwork Pods instead of Docker #49140

Merged

Conversation

evie404
Copy link
Contributor

@evie404 evie404 commented Jul 18, 2017

What this PR does / why we need it: Currently, Docker manages the hosts file for containers inside Pods using hostNetwork. It creates discrepancy between how we treat hostNetwork and non-hostNetwork Pods. Kubelet should manage the file regardless of the network setup.

Which issue this PR fixes: fixes #48397 more context in #43632 (comment)

Special notes for your reviewer: Because the new logic relies on reading the node filesystem, I'm not sure how to write a proper unit test. I was thinking about using a node e2e test to cover the case, but suggestions are greatly welcomed.

Release note:

Kubelet now manages `/etc/hosts` file for both hostNetwork Pods and non-hostNetwork Pods.

/kind feature
/sig node

@yujuhong @hongchaodeng @thockin
@kubernetes/sig-network-feature-requests @kubernetes/sig-node-feature-requests

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/network Categorizes an issue or PR as relevant to SIG Network. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 18, 2017
@k8s-ci-robot
Copy link
Contributor

Hi @rickypai. 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.

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

@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 Jul 18, 2017
@k8s-ci-robot
Copy link
Contributor

@rickypai: Reiterating the mentions to trigger a notification:
@kubernetes/sig-network-feature-requests, @kubernetes/sig-node-feature-requests.

In response to this:

What this PR does / why we need it: Currently, Docker manages the hosts file for containers inside Pods using hostNetwork. It creates discrepancy between how we treat hostNetwork and non-hostNetwork Pods. Kubelet should manage the file regardless of the network setup.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #48397 more context in #43632 (comment)

Special notes for your reviewer: Because the new logic relies on reading the node filesystem, I'm not sure how to write a proper unit test. I was thinking about using a node e2e test to cover the case, but suggestions are greatly welcomed.

Release note:

Kubelet now manages `/etc/hosts` file for both hostNetwork Pods and non-hostNetwork Pods.

/kind feature
/sig node

@yujuhong @hongchaodeng @thockin
@kubernetes/sig-network-feature-requests @kubernetes/sig-node-feature-requests

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-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jul 18, 2017
@hongchaodeng
Copy link
Contributor

@yujuhong @thockin

This is very useful and important feature. Can we get this in 1.8 ?

@yujuhong yujuhong assigned thockin and yujuhong and unassigned dims and dchen1107 Jul 18, 2017
@yujuhong
Copy link
Contributor

I'd like to see this happen since it improves consistency, eliminating the burden for integration. Marking this 1.8 tentatively.
/cc @mrunalp @Random-Liu as this could affect the CRI shim implementation.


if useHostNetwork {
// if Pod is using host network, read hosts file from the node's filesystem.
hostsFileContent, err = nodeHostsFileContent()
Copy link
Member

Choose a reason for hiding this comment

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

Also add hostAliases for host-networked containers?

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 plan to do this as a follow-up. this is to keep the change and relevant discussions focused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the work is covered in: #48398

@feiskyer
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 19, 2017
@evie404
Copy link
Contributor Author

evie404 commented Jul 19, 2017

/test pull-kubernetes-e2e-gce-etcd3

@evie404
Copy link
Contributor Author

evie404 commented Jul 25, 2017

can i get more reviews on this? thanks a lot

@evie404
Copy link
Contributor Author

evie404 commented Jul 27, 2017

@yujuhong @feiskyer can i get reviews on this? i have some follow-up PRs to enable HostAlias for HostNetwork pods also

@thockin
Copy link
Member

thockin commented Jul 28, 2017

For testing we should probably have a param to define which file to read. You can pass "/etc/hosts" in real code and you can write a bogus file in test code.

/approve

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 28, 2017
@@ -264,8 +264,8 @@ fe00::2 ip6-allrouters
}

for _, testCase := range testCases {
actualContent := string(hostsFileContent(testCase.hostIP, testCase.hostName, testCase.hostDomainName, testCase.hostAliases))
assert.Equal(t, testCase.expectedContent, actualContent, "hosts file content not expected")
actualContent, _ := managedHostsFileContent(testCase.hostIP, testCase.hostName, testCase.hostDomainName, testCase.hostAliases)
Copy link
Member

Choose a reason for hiding this comment

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

Also adds a test case for nodeHostsFileContent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added tests

Copy link
Contributor

@yujuhong yujuhong left a comment

Choose a reason for hiding this comment

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

Test is needed as @thockin and @feiskyer suggested.

I think this should not cause any kubelet in-place upgrade issue, but would be nice to confirm that.

mountEtcHostsFile := !pod.Spec.HostNetwork && len(podIP) > 0 && runtime.GOOS != "windows"
// - OS is not Windows
// Kubernetes will not mount /etc/hosts if:
// - when the pause container is being created, its IP is still unknown. Hence, PodIP will not have been set.
Copy link
Contributor

Choose a reason for hiding this comment

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

Change the comment to reflect the current status since you are touching it:

s/when the pause container/when the pod sandbox

Copy link
Contributor Author

Choose a reason for hiding this comment

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

didn't know the pause container was changed. will update the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated comment

func hostsFileContent(hostIP, hostName, hostDomainName string, hostAliases []v1.HostAlias) []byte {
// managedHostsFileContent generates the content of the managed etc hosts based on Pod IP and other
// information.
func managedHostsFileContent(hostIP, hostName, hostDomainName string, hostAliases []v1.HostAlias) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think returning the error is necessary since it's always nil. I think it's ok to not add it.

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 added it so it'd have the same return as nodeHostsFileContent. i can remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed extra error

@evie404
Copy link
Contributor Author

evie404 commented Jul 29, 2017

/test pull-kubernetes-bazel

1 similar comment
@evie404
Copy link
Contributor Author

evie404 commented Jul 29, 2017

/test pull-kubernetes-bazel

@evie404 evie404 force-pushed the rpai/hostnetwork_etc_hosts branch from 2e863c9 to 7bf79ea Compare July 31, 2017 18:10
Copy link

@cmluciano cmluciano left a comment

Choose a reason for hiding this comment

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

can you squash some of these commits?

@evie404 evie404 force-pushed the rpai/hostnetwork_etc_hosts branch from 7bf79ea to a5fb7e0 Compare July 31, 2017 18:44
@evie404
Copy link
Contributor Author

evie404 commented Jul 31, 2017

squashed all commits

@evie404
Copy link
Contributor Author

evie404 commented Jul 31, 2017 via email

@evie404
Copy link
Contributor Author

evie404 commented Jul 31, 2017

/retest

@thockin thockin removed their assignment Aug 1, 2017
@evie404
Copy link
Contributor Author

evie404 commented Aug 4, 2017

can i get another review? thanks a lot!


// nodeHostsFileContent reads the content of node's hosts file.
func nodeHostsFileContent(hostsFilePath string) ([]byte, error) {
// `etcHostsPath` references the location of the hosts file on the node.
Copy link
Contributor

Choose a reason for hiding this comment

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

Move the comment to where the variable is used (line 234)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,8 @@
# hosts file for testing.
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 not add test files in the pkg/kubelet? The test itself should write a temporary file and clean up after itself.

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 think it's brittle to try to write files during a test. what if the test run fails unexpectedly? then the file stays there and you end up potentially polluting the testing infrastructure :/

Copy link
Member

@feiskyer feiskyer Aug 4, 2017

Choose a reason for hiding this comment

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

Better to create the test file dynamically when running tests, e.g. under /tmp dir. The test could be useful when hostAliases is introduced.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it should be written to a temp directory, or you can mock the filesystem call. I don't have a strong opinion since many of existing unit tests write to disk already. I don't want test data checked in the non-testing directory though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@evie404 evie404 force-pushed the rpai/hostnetwork_etc_hosts branch from a5fb7e0 to f0e2ad1 Compare August 4, 2017 18:20
@evie404
Copy link
Contributor Author

evie404 commented Aug 9, 2017

@yujuhong can you take a look again? thanks a lot

}

for _, testCase := range testCases {
hostsFilePath := path.Join("/", "tmp", testCase.hostsFileName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: use ioutil.TempDir and ioutil.WriteFile to simplify the code and write to an OS specific temp directory.
Example: https://github.com/kubernetes/kubernetes/blob/v1.8.0-alpha.2/pkg/kubelet/dockershim/helpers_test.go#L161

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@evie404 evie404 force-pushed the rpai/hostnetwork_etc_hosts branch from f0e2ad1 to fb4bff0 Compare August 10, 2017 18:41
@evie404
Copy link
Contributor Author

evie404 commented Aug 10, 2017

/retest

@thockin
Copy link
Member

thockin commented Aug 11, 2017

Is this going in for 1.8?

@evie404
Copy link
Contributor Author

evie404 commented Aug 11, 2017

@thockin it should be. i make an effort to respond to code review comments within 24hrs. is there a deadline i'm working against? i presume sept 1st?

@evie404
Copy link
Contributor Author

evie404 commented Aug 11, 2017

@yujuhong can you take a look again? thanks a lot

@yujuhong
Copy link
Contributor

Is this going in for 1.8?

This PR alone doesn't (or shouldn't) have user-facing impact and is not really a feature, so I am treating it as a consistency cleanup for the code.
I didn't have any specific target milestone in mind, but it should get in once it's ready.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 14, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: rickypai, thockin, yujuhong

Associated issue: 48397

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

Automatic merge from submit-queue (batch tested with PRs 50094, 48966, 49478, 50593, 49140)

@k8s-github-robot k8s-github-robot merged commit b161831 into kubernetes:master Aug 14, 2017
k8s-github-robot pushed a commit that referenced this pull request Aug 24, 2017
Automatic merge from submit-queue

Support HostAlias for HostNetwork Pods

**What this PR does / why we need it**: Currently, HostAlias does not support HostNetwork pods because historically, kubelet only manages hosts file for non-HostNetwork pods. With the recent change in #49140, kubelet now manages hosts file for all Pods, which enables HostAlias support also.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #48398

**Special notes for your reviewer**: might be easier to review commit-by-commit

**Release note**:

```release-note
HostAlias is now supported for both non-HostNetwork Pods and HostNetwork Pods.
```

@yujuhong @hongchaodeng @thockin
@evie404 evie404 deleted the rpai/hostnetwork_etc_hosts branch September 28, 2017 20:20
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/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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/node Categorizes an issue or PR as relevant to SIG Node. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kubelet should manage hosts file for HostNetwork Pods instead of Docker
10 participants