Skip to content

Commit

Permalink
Merge pull request kubernetes#60627 from dims/create-fake-etc-hosts-f…
Browse files Browse the repository at this point in the history
…or-conformance-test

Automatic merge from submit-queue (batch tested with PRs 60642, 60627). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Create fake /etc/hosts for conformance test

**What this PR does / why we need it**:

    "KubeletManagedEtcHosts should test kubelet managed /etc/hosts file"
    conformance test fails in the CI's Docker-In-Docker environment.

    This test mounts a /etc/hosts file and checks if "# Kubernetes-managed
    hosts file." string is present or not under various conditions. The
    specific failure with DIND happens when the /etc/hosts picked up
    from the box where e2e test are running already has this string. This
    happens because our CI runs on kubernetes and the e2e tests are running
    in a container that was started on kubernetes (and hence already has
    that string)

    To avoid this situation, we create a new /etc/hosts file with known
    contents (and does not have the "# Kubernetes-managed hosts file."
    string)

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

**Special notes for your reviewer**:
Please see:
https://k8s-testgrid.appspot.com/sig-testing-misc#ci-kubernetes-local-e2e
https://k8s-gubernator.appspot.com/build/kubernetes-jenkins/logs/ci-kubernetes-local-e2e/285

**Release note**:

```release-note
NONE
```
  • Loading branch information
Kubernetes Submit Queue committed Mar 7, 2018
2 parents e9f996b + ba20e63 commit c74c826
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion test/e2e/common/kubelet_etc_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package common

import (
"io/ioutil"
"os"
"strings"
"time"

Expand All @@ -40,6 +42,7 @@ type KubeletManagedHostConfig struct {
hostNetworkPod *v1.Pod
pod *v1.Pod
f *framework.Framework
tmpEtcHostFile *os.File
}

var _ = framework.KubeDescribe("KubeletManagedEtcHosts", func() {
Expand All @@ -59,6 +62,8 @@ var _ = framework.KubeDescribe("KubeletManagedEtcHosts", func() {

By("Running the test")
config.verifyEtcHosts()

config.cleanup()
})
})

Expand All @@ -76,13 +81,39 @@ func (config *KubeletManagedHostConfig) verifyEtcHosts() {
}

func (config *KubeletManagedHostConfig) setup() {
etcHostContents := `127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters`

// Write the data to a temp file.
var err error
config.tmpEtcHostFile, err = ioutil.TempFile("", "etc-hosts")
if err != nil {
framework.Failf("failed to create temp file for /etc/hosts: %v", err)
}
if _, err := config.tmpEtcHostFile.Write([]byte(etcHostContents)); err != nil {
framework.Failf("Failed to write temp file for /etc/hosts data: %v", err)
}
if err := config.tmpEtcHostFile.Close(); err != nil {
framework.Failf("Failed to close temp file: %v", err)
}

By("Creating hostNetwork=false pod")
config.createPodWithoutHostNetwork()

By("Creating hostNetwork=true pod")
config.createPodWithHostNetwork()
}

func (config *KubeletManagedHostConfig) cleanup() {
if config.tmpEtcHostFile != nil {
os.Remove(config.tmpEtcHostFile.Name())
}
}

func (config *KubeletManagedHostConfig) createPodWithoutHostNetwork() {
podSpec := config.createPodSpec(etcHostsPodName)
config.pod = config.f.PodClient().CreateSync(podSpec)
Expand Down Expand Up @@ -184,7 +215,7 @@ func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod {
Name: "host-etc-hosts",
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: "/etc/hosts",
Path: config.tmpEtcHostFile.Name(),
Type: hostPathType,
},
},
Expand Down

0 comments on commit c74c826

Please sign in to comment.