diff --git a/test/e2e/common/docker_containers.go b/test/e2e/common/docker_containers.go index 090039136f2..7388c75857a 100644 --- a/test/e2e/common/docker_containers.go +++ b/test/e2e/common/docker_containers.go @@ -20,11 +20,9 @@ import ( "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" "k8s.io/kubernetes/test/e2e/framework" e2epod "k8s.io/kubernetes/test/e2e/framework/pod" - imageutils "k8s.io/kubernetes/test/utils/image" ) var _ = framework.KubeDescribe("Docker Containers", func() { @@ -36,12 +34,13 @@ var _ = framework.KubeDescribe("Docker Containers", func() { Description: Default command and arguments from the docker image entrypoint MUST be used when Pod does not specify the container command */ framework.ConformanceIt("should use the image defaults if command and args are blank [NodeConformance]", func() { - pod := f.PodClient().Create(entrypointTestPod()) + pod := entrypointTestPod(f.Namespace.Name) + pod.Spec.Containers[0].Args = nil + pod = f.PodClient().Create(pod) err := e2epod.WaitForPodNameRunningInNamespace(f.ClientSet, pod.Name, f.Namespace.Name) framework.ExpectNoError(err, "Expected pod %q to be running, got error: %v", pod.Name, err) - pollLogs := func() (string, error) { - return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName) + return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name) } // The agnhost's image default entrypoint / args are: "/agnhost pause" @@ -55,9 +54,7 @@ var _ = framework.KubeDescribe("Docker Containers", func() { Description: Default command and from the docker image entrypoint MUST be used when Pod does not specify the container command but the arguments from Pod spec MUST override when specified. */ framework.ConformanceIt("should be able to override the image's default arguments (docker cmd) [NodeConformance]", func() { - pod := entrypointTestPod() - pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"} - + pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments") f.TestContainerOutput("override arguments", pod, 0, []string{ "[/agnhost entrypoint-tester override arguments]", }) @@ -71,8 +68,8 @@ var _ = framework.KubeDescribe("Docker Containers", func() { Description: Default command from the docker image entrypoint MUST NOT be used when Pod specifies the container command. Command from Pod spec MUST override the command in the image. */ framework.ConformanceIt("should be able to override the image's default command (docker entrypoint) [NodeConformance]", func() { - pod := entrypointTestPod() - pod.Spec.Containers[0].Command = []string{"/agnhost-2", "entrypoint-tester"} + pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester") + pod.Spec.Containers[0].Command = []string{"/agnhost-2"} f.TestContainerOutput("override command", pod, 0, []string{ "[/agnhost-2 entrypoint-tester]", @@ -85,9 +82,8 @@ var _ = framework.KubeDescribe("Docker Containers", func() { Description: Default command and arguments from the docker image entrypoint MUST NOT be used when Pod specifies the container command and arguments. Command and arguments from Pod spec MUST override the command and arguments in the image. */ framework.ConformanceIt("should be able to override the image's default command and arguments [NodeConformance]", func() { - pod := entrypointTestPod() + pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments") pod.Spec.Containers[0].Command = []string{"/agnhost-2"} - pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"} f.TestContainerOutput("override all", pod, 0, []string{ "[/agnhost-2 entrypoint-tester override arguments]", @@ -95,26 +91,13 @@ var _ = framework.KubeDescribe("Docker Containers", func() { }) }) -const testContainerName = "test-container" - // Return a prototypical entrypoint test pod -func entrypointTestPod() *v1.Pod { +func entrypointTestPod(namespace string, entrypointArgs ...string) *v1.Pod { podName := "client-containers-" + string(uuid.NewUUID()) + pod := e2epod.NewAgnhostPod(namespace, podName, nil, nil, nil, entrypointArgs...) one := int64(1) - return &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: podName, - }, - Spec: v1.PodSpec{ - Containers: []v1.Container{ - { - Name: testContainerName, - Image: imageutils.GetE2EImage(imageutils.Agnhost), - }, - }, - RestartPolicy: v1.RestartPolicyNever, - TerminationGracePeriodSeconds: &one, - }, - } + pod.Spec.TerminationGracePeriodSeconds = &one + pod.Spec.RestartPolicy = v1.RestartPolicyNever + return pod } diff --git a/test/e2e/common/kubelet_etc_hosts.go b/test/e2e/common/kubelet_etc_hosts.go index b4ff6ecde90..701fbd78f3c 100644 --- a/test/e2e/common/kubelet_etc_hosts.go +++ b/test/e2e/common/kubelet_etc_hosts.go @@ -25,7 +25,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/klog/v2" "k8s.io/kubernetes/test/e2e/framework" - imageutils "k8s.io/kubernetes/test/utils/image" + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" ) const ( @@ -36,8 +36,6 @@ const ( etcHostsOriginalPath = "/etc/hosts-original" ) -var etcHostsImageName = imageutils.GetE2EImage(imageutils.Agnhost) - type KubeletManagedHostConfig struct { hostNetworkPod *v1.Pod pod *v1.Pod @@ -153,61 +151,28 @@ func (config *KubeletManagedHostConfig) getFileContents(podName, containerName, func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod { hostPathType := new(v1.HostPathType) *hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate)) + mounts := []v1.VolumeMount{ + { + Name: "host-etc-hosts", + MountPath: etcHostsOriginalPath, + }, + } + multipleMounts := []v1.VolumeMount{ + mounts[0], + { + Name: "host-etc-hosts", + MountPath: etcHostsPath, + }, + } pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: podName, }, Spec: v1.PodSpec{ Containers: []v1.Container{ - { - Name: "busybox-1", - Image: etcHostsImageName, - ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sleep", - "900", - }, - VolumeMounts: []v1.VolumeMount{ - { - Name: "host-etc-hosts", - MountPath: etcHostsOriginalPath, - }, - }, - }, - { - Name: "busybox-2", - Image: etcHostsImageName, - ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sleep", - "900", - }, - VolumeMounts: []v1.VolumeMount{ - { - Name: "host-etc-hosts", - MountPath: etcHostsOriginalPath, - }, - }, - }, - { - Name: "busybox-3", - Image: etcHostsImageName, - ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sleep", - "900", - }, - VolumeMounts: []v1.VolumeMount{ - { - Name: "host-etc-hosts", - MountPath: etcHostsPath, - }, - { - Name: "host-etc-hosts", - MountPath: etcHostsOriginalPath, - }, - }, - }, + e2epod.NewAgnhostContainer("busybox-1", mounts, nil), + e2epod.NewAgnhostContainer("busybox-2", mounts, nil), + e2epod.NewAgnhostContainer("busybox-3", multipleMounts, nil), }, Volumes: []v1.Volume{ { @@ -222,12 +187,19 @@ func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod { }, }, } + return pod } func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName string) *v1.Pod { hostPathType := new(v1.HostPathType) *hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate)) + mounts := []v1.VolumeMount{ + { + Name: "host-etc-hosts", + MountPath: etcHostsOriginalPath, + }, + } pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: podName, @@ -236,36 +208,8 @@ func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName str HostNetwork: true, SecurityContext: &v1.PodSecurityContext{}, Containers: []v1.Container{ - { - Name: "busybox-1", - Image: etcHostsImageName, - ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sleep", - "900", - }, - VolumeMounts: []v1.VolumeMount{ - { - Name: "host-etc-hosts", - MountPath: etcHostsOriginalPath, - }, - }, - }, - { - Name: "busybox-2", - Image: etcHostsImageName, - ImagePullPolicy: v1.PullIfNotPresent, - Command: []string{ - "sleep", - "900", - }, - VolumeMounts: []v1.VolumeMount{ - { - Name: "host-etc-hosts", - MountPath: etcHostsOriginalPath, - }, - }, - }, + e2epod.NewAgnhostContainer("busybox-1", mounts, nil), + e2epod.NewAgnhostContainer("busybox-2", mounts, nil), }, Volumes: []v1.Volume{ { diff --git a/test/e2e/common/lifecycle_hook.go b/test/e2e/common/lifecycle_hook.go index 0150cb4913d..2eabcb65e7b 100644 --- a/test/e2e/common/lifecycle_hook.go +++ b/test/e2e/common/lifecycle_hook.go @@ -25,6 +25,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/kubernetes/test/e2e/framework" + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" imageutils "k8s.io/kubernetes/test/utils/image" "github.com/onsi/ginkgo" @@ -41,26 +42,13 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() { ) ginkgo.Context("when create a pod with lifecycle hook", func() { var targetIP, targetURL string - podHandleHookRequest := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod-handle-http-request", - }, - Spec: v1.PodSpec{ - Containers: []v1.Container{ - { - Name: "pod-handle-http-request", - Image: imageutils.GetE2EImage(imageutils.Agnhost), - Args: []string{"netexec"}, - Ports: []v1.ContainerPort{ - { - ContainerPort: 8080, - Protocol: v1.ProtocolTCP, - }, - }, - }, - }, + ports := []v1.ContainerPort{ + { + ContainerPort: 8080, + Protocol: v1.ProtocolTCP, }, } + podHandleHookRequest := e2epod.NewAgnhostPod("", "pod-handle-http-request", nil, nil, ports, "netexec") ginkgo.BeforeEach(func() { podClient = f.PodClient() ginkgo.By("create the container to handle the HTTPGet hook request.") diff --git a/test/e2e/common/projected_configmap.go b/test/e2e/common/projected_configmap.go index e218e9b21d4..a7414377bc5 100644 --- a/test/e2e/common/projected_configmap.go +++ b/test/e2e/common/projected_configmap.go @@ -125,7 +125,6 @@ var _ = ginkgo.Describe("[sig-storage] Projected configMap", func() { name := "projected-configmap-test-upd-" + string(uuid.NewUUID()) volumeName := "projected-configmap-volume" volumeMountPath := "/etc/projected-configmap-volume" - containerName := "projected-configmap-volume-test" configMap := &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Namespace: f.Namespace.Name, @@ -142,51 +141,14 @@ var _ = ginkgo.Describe("[sig-storage] Projected configMap", func() { framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) } - pod := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod-projected-configmaps-" + string(uuid.NewUUID()), - }, - Spec: v1.PodSpec{ - Volumes: []v1.Volume{ - { - Name: volumeName, - VolumeSource: v1.VolumeSource{ - Projected: &v1.ProjectedVolumeSource{ - Sources: []v1.VolumeProjection{ - { - ConfigMap: &v1.ConfigMapProjection{ - LocalObjectReference: v1.LocalObjectReference{ - Name: name, - }, - }, - }, - }, - }, - }, - }, - }, - Containers: []v1.Container{ - { - Name: containerName, - Image: imageutils.GetE2EImage(imageutils.Agnhost), - Args: []string{"mounttest", "--break_on_expected_content=false", containerTimeoutArg, "--file_content_in_loop=/etc/projected-configmap-volume/data-1"}, - VolumeMounts: []v1.VolumeMount{ - { - Name: volumeName, - MountPath: volumeMountPath, - ReadOnly: true, - }, - }, - }, - }, - RestartPolicy: v1.RestartPolicyNever, - }, - } + pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath, + "--break_on_expected_content=false", containerTimeoutArg, "--file_content_in_loop=/etc/projected-configmap-volume/data-1") + ginkgo.By("Creating the pod") f.PodClient().CreateSync(pod) pollLogs := func() (string, error) { - return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName) + return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name) } gomega.Eventually(pollLogs, podLogTimeout, framework.Poll).Should(gomega.ContainSubstring("value-1")) @@ -529,49 +491,8 @@ func doProjectedConfigMapE2EWithoutMappings(f *framework.Framework, asUser bool, framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) } - pod := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod-projected-configmaps-" + string(uuid.NewUUID()), - }, - Spec: v1.PodSpec{ - SecurityContext: &v1.PodSecurityContext{}, - Volumes: []v1.Volume{ - { - Name: volumeName, - VolumeSource: v1.VolumeSource{ - Projected: &v1.ProjectedVolumeSource{ - Sources: []v1.VolumeProjection{ - { - ConfigMap: &v1.ConfigMapProjection{ - LocalObjectReference: v1.LocalObjectReference{ - Name: name, - }, - }, - }, - }, - }, - }, - }, - }, - Containers: []v1.Container{ - { - Name: "projected-configmap-volume-test", - Image: imageutils.GetE2EImage(imageutils.Agnhost), - Args: []string{ - "mounttest", - "--file_content=/etc/projected-configmap-volume/data-1", - "--file_mode=/etc/projected-configmap-volume/data-1"}, - VolumeMounts: []v1.VolumeMount{ - { - Name: volumeName, - MountPath: volumeMountPath, - }, - }, - }, - }, - RestartPolicy: v1.RestartPolicyNever, - }, - } + pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath, + "--file_content=/etc/projected-configmap-volume/data-1", "--file_mode=/etc/projected-configmap-volume/data-1") if asUser { setPodNonRootUser(pod) @@ -611,54 +532,12 @@ func doProjectedConfigMapE2EWithMappings(f *framework.Framework, asUser bool, fs framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) } - pod := &v1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "pod-projected-configmaps-" + string(uuid.NewUUID()), - }, - Spec: v1.PodSpec{ - SecurityContext: &v1.PodSecurityContext{}, - Volumes: []v1.Volume{ - { - Name: volumeName, - VolumeSource: v1.VolumeSource{ - Projected: &v1.ProjectedVolumeSource{ - Sources: []v1.VolumeProjection{ - { - ConfigMap: &v1.ConfigMapProjection{ - LocalObjectReference: v1.LocalObjectReference{ - Name: name, - }, - Items: []v1.KeyToPath{ - { - Key: "data-2", - Path: "path/to/data-2", - }, - }, - }, - }, - }, - }, - }, - }, - }, - Containers: []v1.Container{ - { - Name: "projected-configmap-volume-test", - Image: imageutils.GetE2EImage(imageutils.Agnhost), - Args: []string{ - "mounttest", - "--file_content=/etc/projected-configmap-volume/path/to/data-2", - "--file_mode=/etc/projected-configmap-volume/path/to/data-2"}, - VolumeMounts: []v1.VolumeMount{ - { - Name: volumeName, - MountPath: volumeMountPath, - ReadOnly: true, - }, - }, - }, - }, - RestartPolicy: v1.RestartPolicyNever, + pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath, + "--file_content=/etc/projected-configmap-volume/path/to/data-2", "--file_mode=/etc/projected-configmap-volume/path/to/data-2") + pod.Spec.Volumes[0].VolumeSource.Projected.Sources[0].ConfigMap.Items = []v1.KeyToPath{ + { + Key: "data-2", + Path: "path/to/data-2", }, } @@ -686,3 +565,29 @@ func doProjectedConfigMapE2EWithMappings(f *framework.Framework, asUser bool, fs } f.TestContainerOutputRegexp("consume configMaps", pod, 0, output) } + +func createProjectedConfigMapMounttestPod(namespace, volumeName, referenceName, mountPath string, mounttestArgs ...string) *v1.Pod { + volumes := []v1.Volume{ + { + Name: volumeName, + VolumeSource: v1.VolumeSource{ + Projected: &v1.ProjectedVolumeSource{ + Sources: []v1.VolumeProjection{ + { + ConfigMap: &v1.ConfigMapProjection{ + LocalObjectReference: v1.LocalObjectReference{ + Name: referenceName, + }, + }, + }, + }, + }, + }, + }, + } + podName := "pod-projected-configmaps-" + string(uuid.NewUUID()) + mounttestArgs = append([]string{"mounttest"}, mounttestArgs...) + pod := e2epod.NewAgnhostPod(namespace, podName, volumes, createMounts(volumeName, mountPath, true), nil, mounttestArgs...) + pod.Spec.RestartPolicy = v1.RestartPolicyNever + return pod +} diff --git a/test/e2e/common/util.go b/test/e2e/common/util.go index 23d02a465f5..d8f628374da 100644 --- a/test/e2e/common/util.go +++ b/test/e2e/common/util.go @@ -241,3 +241,14 @@ func getFileModeRegex(filePath string, mask *int32) string { return fmt.Sprintf("(%s|%s)", linuxOutput, windowsOutput) } + +// createMounts creates a v1.VolumeMount list with a single element. +func createMounts(volumeName, volumeMountPath string, readOnly bool) []v1.VolumeMount { + return []v1.VolumeMount{ + { + Name: volumeName, + MountPath: volumeMountPath, + ReadOnly: readOnly, + }, + } +}