mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	This is the result of automatically editing source files like this:
    go install golang.org/x/tools/cmd/goimports@latest
    find ./test/e2e* -name "*.go" | xargs env PATH=$GOPATH/bin:$PATH ./e2e-framework-sed.sh
with e2e-framework-sed.sh containing this:
sed -i \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecCommandInContainer(/e2epod.ExecCommandInContainer(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecCommandInContainerWithFullOutput(/e2epod.ExecCommandInContainerWithFullOutput(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInContainer(/e2epod.ExecShellInContainer(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInPod(/e2epod.ExecShellInPod(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecShellInPodWithFullOutput(/e2epod.ExecShellInPodWithFullOutput(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.ExecWithOptions(/e2epod.ExecWithOptions(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.MatchContainerOutput(/e2eoutput.MatchContainerOutput(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.PodClient(/e2epod.NewPodClient(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.PodClientNS(/e2epod.PodClientNS(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.TestContainerOutput(/e2eoutput.TestContainerOutput(\1, /" \
    -e "s/\(f\|fr\|\w\w*\.[fF]\w*\)\.TestContainerOutputRegexp(/e2eoutput.TestContainerOutputRegexp(\1, /" \
    -e "s/framework.AddOrUpdateLabelOnNode\b/e2enode.AddOrUpdateLabelOnNode/" \
    -e "s/framework.AllNodes\b/e2edebug.AllNodes/" \
    -e "s/framework.AllNodesReady\b/e2enode.AllNodesReady/" \
    -e "s/framework.ContainerResourceGatherer\b/e2edebug.ContainerResourceGatherer/" \
    -e "s/framework.ContainerResourceUsage\b/e2edebug.ContainerResourceUsage/" \
    -e "s/framework.CreateEmptyFileOnPod\b/e2eoutput.CreateEmptyFileOnPod/" \
    -e "s/framework.DefaultPodDeletionTimeout\b/e2epod.DefaultPodDeletionTimeout/" \
    -e "s/framework.DumpAllNamespaceInfo\b/e2edebug.DumpAllNamespaceInfo/" \
    -e "s/framework.DumpDebugInfo\b/e2eoutput.DumpDebugInfo/" \
    -e "s/framework.DumpNodeDebugInfo\b/e2edebug.DumpNodeDebugInfo/" \
    -e "s/framework.EtcdUpgrade\b/e2eproviders.EtcdUpgrade/" \
    -e "s/framework.EventsLister\b/e2edebug.EventsLister/" \
    -e "s/framework.ExecOptions\b/e2epod.ExecOptions/" \
    -e "s/framework.ExpectNodeHasLabel\b/e2enode.ExpectNodeHasLabel/" \
    -e "s/framework.ExpectNodeHasTaint\b/e2enode.ExpectNodeHasTaint/" \
    -e "s/framework.GCEUpgradeScript\b/e2eproviders.GCEUpgradeScript/" \
    -e "s/framework.ImagePrePullList\b/e2epod.ImagePrePullList/" \
    -e "s/framework.KubectlBuilder\b/e2ekubectl.KubectlBuilder/" \
    -e "s/framework.LocationParamGKE\b/e2eproviders.LocationParamGKE/" \
    -e "s/framework.LogSizeDataTimeseries\b/e2edebug.LogSizeDataTimeseries/" \
    -e "s/framework.LogSizeGatherer\b/e2edebug.LogSizeGatherer/" \
    -e "s/framework.LogsSizeData\b/e2edebug.LogsSizeData/" \
    -e "s/framework.LogsSizeDataSummary\b/e2edebug.LogsSizeDataSummary/" \
    -e "s/framework.LogsSizeVerifier\b/e2edebug.LogsSizeVerifier/" \
    -e "s/framework.LookForStringInLog\b/e2eoutput.LookForStringInLog/" \
    -e "s/framework.LookForStringInPodExec\b/e2eoutput.LookForStringInPodExec/" \
    -e "s/framework.LookForStringInPodExecToContainer\b/e2eoutput.LookForStringInPodExecToContainer/" \
    -e "s/framework.MasterAndDNSNodes\b/e2edebug.MasterAndDNSNodes/" \
    -e "s/framework.MasterNodes\b/e2edebug.MasterNodes/" \
    -e "s/framework.MasterUpgradeGKE\b/e2eproviders.MasterUpgradeGKE/" \
    -e "s/framework.NewKubectlCommand\b/e2ekubectl.NewKubectlCommand/" \
    -e "s/framework.NewLogsVerifier\b/e2edebug.NewLogsVerifier/" \
    -e "s/framework.NewNodeKiller\b/e2enode.NewNodeKiller/" \
    -e "s/framework.NewResourceUsageGatherer\b/e2edebug.NewResourceUsageGatherer/" \
    -e "s/framework.NodeHasTaint\b/e2enode.NodeHasTaint/" \
    -e "s/framework.NodeKiller\b/e2enode.NodeKiller/" \
    -e "s/framework.NodesSet\b/e2edebug.NodesSet/" \
    -e "s/framework.PodClient\b/e2epod.PodClient/" \
    -e "s/framework.RemoveLabelOffNode\b/e2enode.RemoveLabelOffNode/" \
    -e "s/framework.ResourceConstraint\b/e2edebug.ResourceConstraint/" \
    -e "s/framework.ResourceGathererOptions\b/e2edebug.ResourceGathererOptions/" \
    -e "s/framework.ResourceUsagePerContainer\b/e2edebug.ResourceUsagePerContainer/" \
    -e "s/framework.ResourceUsageSummary\b/e2edebug.ResourceUsageSummary/" \
    -e "s/framework.RunHostCmd\b/e2eoutput.RunHostCmd/" \
    -e "s/framework.RunHostCmdOrDie\b/e2eoutput.RunHostCmdOrDie/" \
    -e "s/framework.RunHostCmdWithFullOutput\b/e2eoutput.RunHostCmdWithFullOutput/" \
    -e "s/framework.RunHostCmdWithRetries\b/e2eoutput.RunHostCmdWithRetries/" \
    -e "s/framework.RunKubectl\b/e2ekubectl.RunKubectl/" \
    -e "s/framework.RunKubectlInput\b/e2ekubectl.RunKubectlInput/" \
    -e "s/framework.RunKubectlOrDie\b/e2ekubectl.RunKubectlOrDie/" \
    -e "s/framework.RunKubectlOrDieInput\b/e2ekubectl.RunKubectlOrDieInput/" \
    -e "s/framework.RunKubectlWithFullOutput\b/e2ekubectl.RunKubectlWithFullOutput/" \
    -e "s/framework.RunKubemciCmd\b/e2ekubectl.RunKubemciCmd/" \
    -e "s/framework.RunKubemciWithKubeconfig\b/e2ekubectl.RunKubemciWithKubeconfig/" \
    -e "s/framework.SingleContainerSummary\b/e2edebug.SingleContainerSummary/" \
    -e "s/framework.SingleLogSummary\b/e2edebug.SingleLogSummary/" \
    -e "s/framework.TimestampedSize\b/e2edebug.TimestampedSize/" \
    -e "s/framework.WaitForAllNodesSchedulable\b/e2enode.WaitForAllNodesSchedulable/" \
    -e "s/framework.WaitForSSHTunnels\b/e2enode.WaitForSSHTunnels/" \
    -e "s/framework.WorkItem\b/e2edebug.WorkItem/" \
    "$@"
for i in "$@"; do
    # Import all sub packages and let goimports figure out which of those
    # are redundant (= already imported) or not needed.
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2edebug "k8s.io/kubernetes/test/e2e/framework/debug"' "$i"
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"' "$i"
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2enode "k8s.io/kubernetes/test/e2e/framework/node"' "$i"
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"' "$i"
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2epod "k8s.io/kubernetes/test/e2e/framework/pod"' "$i"
    sed -i -e '/"k8s.io.kubernetes.test.e2e.framework"/a e2eproviders "k8s.io/kubernetes/test/e2e/framework/providers"' "$i"
    goimports -w "$i"
done
		
	
		
			
				
	
	
		
			213 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			213 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
Copyright 2017 The Kubernetes Authors.
 | 
						|
 | 
						|
Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
you may not use this file except in compliance with the License.
 | 
						|
You may obtain a copy of the License at
 | 
						|
 | 
						|
    http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
 | 
						|
Unless required by applicable law or agreed to in writing, software
 | 
						|
distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
See the License for the specific language governing permissions and
 | 
						|
limitations under the License.
 | 
						|
*/
 | 
						|
 | 
						|
package node
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	v1 "k8s.io/api/core/v1"
 | 
						|
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						|
	"k8s.io/apimachinery/pkg/util/sets"
 | 
						|
	"k8s.io/kubernetes/test/e2e/framework"
 | 
						|
	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
 | 
						|
	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
 | 
						|
	"k8s.io/kubernetes/test/e2e/storage/utils"
 | 
						|
	imageutils "k8s.io/kubernetes/test/utils/image"
 | 
						|
	admissionapi "k8s.io/pod-security-admission/api"
 | 
						|
 | 
						|
	"github.com/onsi/ginkgo/v2"
 | 
						|
)
 | 
						|
 | 
						|
func preparePod(name string, node *v1.Node, propagation *v1.MountPropagationMode, hostDir string) *v1.Pod {
 | 
						|
	const containerName = "cntr"
 | 
						|
	bTrue := true
 | 
						|
	var oneSecond int64 = 1
 | 
						|
	// The pod prepares /mnt/test/<podname> and sleeps.
 | 
						|
	cmd := fmt.Sprintf("mkdir /mnt/test/%[1]s; sleep 3600", name)
 | 
						|
	pod := &v1.Pod{
 | 
						|
		ObjectMeta: metav1.ObjectMeta{
 | 
						|
			Name: name,
 | 
						|
		},
 | 
						|
		Spec: v1.PodSpec{
 | 
						|
			NodeName: node.Name,
 | 
						|
			Containers: []v1.Container{
 | 
						|
				{
 | 
						|
					Name:    containerName,
 | 
						|
					Image:   imageutils.GetE2EImage(imageutils.BusyBox),
 | 
						|
					Command: []string{"sh", "-c", cmd},
 | 
						|
					VolumeMounts: []v1.VolumeMount{
 | 
						|
						{
 | 
						|
							Name:             "host",
 | 
						|
							MountPath:        "/mnt/test",
 | 
						|
							MountPropagation: propagation,
 | 
						|
						},
 | 
						|
					},
 | 
						|
					SecurityContext: &v1.SecurityContext{
 | 
						|
						Privileged: &bTrue,
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			Volumes: []v1.Volume{
 | 
						|
				{
 | 
						|
					Name: "host",
 | 
						|
					VolumeSource: v1.VolumeSource{
 | 
						|
						HostPath: &v1.HostPathVolumeSource{
 | 
						|
							Path: hostDir,
 | 
						|
						},
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
			// speed up termination of the pod
 | 
						|
			TerminationGracePeriodSeconds: &oneSecond,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	return pod
 | 
						|
}
 | 
						|
 | 
						|
var _ = SIGDescribe("Mount propagation", func() {
 | 
						|
	f := framework.NewDefaultFramework("mount-propagation")
 | 
						|
	f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
 | 
						|
 | 
						|
	ginkgo.It("should propagate mounts within defined scopes", func() {
 | 
						|
		// This test runs two pods: master and slave with respective mount
 | 
						|
		// propagation on common /var/lib/kubelet/XXXX directory. Both mount a
 | 
						|
		// tmpfs to a subdirectory there. We check that these mounts are
 | 
						|
		// propagated to the right places.
 | 
						|
 | 
						|
		hostExec := utils.NewHostExec(f)
 | 
						|
		defer hostExec.Cleanup()
 | 
						|
 | 
						|
		// Pick a node where all pods will run.
 | 
						|
		node, err := e2enode.GetRandomReadySchedulableNode(f.ClientSet)
 | 
						|
		framework.ExpectNoError(err)
 | 
						|
 | 
						|
		// Fail the test if the namespace is not set. We expect that the
 | 
						|
		// namespace is unique and we might delete user data if it's not.
 | 
						|
		if len(f.Namespace.Name) == 0 {
 | 
						|
			framework.ExpectNotEqual(f.Namespace.Name, "")
 | 
						|
			return
 | 
						|
		}
 | 
						|
 | 
						|
		// hostDir is the directory that's shared via HostPath among all pods.
 | 
						|
		// Make sure it's random enough so we don't clash with another test
 | 
						|
		// running in parallel.
 | 
						|
		hostDir := "/var/lib/kubelet/" + f.Namespace.Name
 | 
						|
		defer func() {
 | 
						|
			cleanCmd := fmt.Sprintf("rm -rf %q", hostDir)
 | 
						|
			hostExec.IssueCommand(cleanCmd, node)
 | 
						|
		}()
 | 
						|
 | 
						|
		podClient := e2epod.NewPodClient(f)
 | 
						|
		bidirectional := v1.MountPropagationBidirectional
 | 
						|
		master := podClient.CreateSync(preparePod("master", node, &bidirectional, hostDir))
 | 
						|
 | 
						|
		hostToContainer := v1.MountPropagationHostToContainer
 | 
						|
		slave := podClient.CreateSync(preparePod("slave", node, &hostToContainer, hostDir))
 | 
						|
 | 
						|
		none := v1.MountPropagationNone
 | 
						|
		private := podClient.CreateSync(preparePod("private", node, &none, hostDir))
 | 
						|
		defaultPropagation := podClient.CreateSync(preparePod("default", node, nil, hostDir))
 | 
						|
 | 
						|
		// Check that the pods sees directories of each other. This just checks
 | 
						|
		// that they have the same HostPath, not the mount propagation.
 | 
						|
		podNames := []string{master.Name, slave.Name, private.Name, defaultPropagation.Name}
 | 
						|
		for _, podName := range podNames {
 | 
						|
			for _, dirName := range podNames {
 | 
						|
				cmd := fmt.Sprintf("test -d /mnt/test/%s", dirName)
 | 
						|
				e2epod.ExecShellInPod(f, podName, cmd)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		// Each pod mounts one tmpfs to /mnt/test/<podname> and puts a file there.
 | 
						|
		for _, podName := range podNames {
 | 
						|
			cmd := fmt.Sprintf("mount -t tmpfs e2e-mount-propagation-%[1]s /mnt/test/%[1]s; echo %[1]s > /mnt/test/%[1]s/file", podName)
 | 
						|
			e2epod.ExecShellInPod(f, podName, cmd)
 | 
						|
 | 
						|
			// unmount tmpfs when the test finishes
 | 
						|
			cmd = fmt.Sprintf("umount /mnt/test/%s", podName)
 | 
						|
			defer e2epod.ExecShellInPod(f, podName, cmd)
 | 
						|
		}
 | 
						|
 | 
						|
		// The host mounts one tmpfs to testdir/host and puts a file there so we
 | 
						|
		// can check mount propagation from the host to pods.
 | 
						|
		cmd := fmt.Sprintf("mkdir %[1]q/host; mount -t tmpfs e2e-mount-propagation-host %[1]q/host; echo host > %[1]q/host/file", hostDir)
 | 
						|
		err = hostExec.IssueCommand(cmd, node)
 | 
						|
		framework.ExpectNoError(err)
 | 
						|
 | 
						|
		defer func() {
 | 
						|
			cmd := fmt.Sprintf("umount %q/host", hostDir)
 | 
						|
			hostExec.IssueCommand(cmd, node)
 | 
						|
		}()
 | 
						|
 | 
						|
		// Now check that mounts are propagated to the right containers.
 | 
						|
		// expectedMounts is map of pod name -> expected mounts visible in the
 | 
						|
		// pod.
 | 
						|
		expectedMounts := map[string]sets.String{
 | 
						|
			// Master sees only its own mount and not the slave's one.
 | 
						|
			"master": sets.NewString("master", "host"),
 | 
						|
			// Slave sees master's mount + itself.
 | 
						|
			"slave": sets.NewString("master", "slave", "host"),
 | 
						|
			// Private sees only its own mount
 | 
						|
			"private": sets.NewString("private"),
 | 
						|
			// Default (=private) sees only its own mount
 | 
						|
			"default": sets.NewString("default"),
 | 
						|
		}
 | 
						|
		dirNames := append(podNames, "host")
 | 
						|
		for podName, mounts := range expectedMounts {
 | 
						|
			for _, mountName := range dirNames {
 | 
						|
				cmd := fmt.Sprintf("cat /mnt/test/%s/file", mountName)
 | 
						|
				stdout, stderr, err := e2epod.ExecShellInPodWithFullOutput(f, podName, cmd)
 | 
						|
				framework.Logf("pod %s mount %s: stdout: %q, stderr: %q error: %v", podName, mountName, stdout, stderr, err)
 | 
						|
				msg := fmt.Sprintf("When checking pod %s and directory %s", podName, mountName)
 | 
						|
				shouldBeVisible := mounts.Has(mountName)
 | 
						|
				if shouldBeVisible {
 | 
						|
					framework.ExpectNoError(err, "%s: failed to run %q", msg, cmd)
 | 
						|
					framework.ExpectEqual(stdout, mountName, msg)
 | 
						|
				} else {
 | 
						|
					// We *expect* cat to return error here
 | 
						|
					framework.ExpectError(err, msg)
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		// Find the kubelet PID to ensure we're working with the kubelet's mount namespace
 | 
						|
		cmd = "pidof kubelet"
 | 
						|
		kubeletPid, err := hostExec.IssueCommandWithResult(cmd, node)
 | 
						|
		framework.ExpectNoError(err, "Checking kubelet pid")
 | 
						|
		kubeletPid = strings.TrimSuffix(kubeletPid, "\n")
 | 
						|
		framework.ExpectEqual(strings.Count(kubeletPid, " "), 0, "kubelet should only have a single PID in the system (pidof returned %q)", kubeletPid)
 | 
						|
		enterKubeletMountNS := fmt.Sprintf("nsenter -t %s -m", kubeletPid)
 | 
						|
 | 
						|
		// Check that the master and host mounts are propagated to the container runtime's mount namespace
 | 
						|
		for _, mountName := range []string{"host", master.Name} {
 | 
						|
			cmd := fmt.Sprintf("%s cat \"%s/%s/file\"", enterKubeletMountNS, hostDir, mountName)
 | 
						|
			output, err := hostExec.IssueCommandWithResult(cmd, node)
 | 
						|
			framework.ExpectNoError(err, "host container namespace should see mount from %s: %s", mountName, output)
 | 
						|
			output = strings.TrimSuffix(output, "\n")
 | 
						|
			framework.ExpectEqual(output, mountName, "host container namespace should see mount contents from %s", mountName)
 | 
						|
		}
 | 
						|
 | 
						|
		// Check that the slave, private, and default mounts are not propagated to the container runtime's mount namespace
 | 
						|
		for _, podName := range []string{slave.Name, private.Name, defaultPropagation.Name} {
 | 
						|
			cmd := fmt.Sprintf("%s test ! -e \"%s/%s/file\"", enterKubeletMountNS, hostDir, podName)
 | 
						|
			output, err := hostExec.IssueCommandWithResult(cmd, node)
 | 
						|
			framework.ExpectNoError(err, "host container namespace shouldn't see mount from %s: %s", podName, output)
 | 
						|
		}
 | 
						|
	})
 | 
						|
})
 |