...

Source file src/k8s.io/kubernetes/test/e2e/common/storage/util.go

Documentation: k8s.io/kubernetes/test/e2e/common/storage

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package storage
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	"k8s.io/kubernetes/test/e2e/framework"
    25  )
    26  
    27  var (
    28  	// non-Administrator Windows user used in tests. This is the Windows equivalent of the Linux non-root UID usage.
    29  	nonAdminTestUserName = "ContainerUser"
    30  	// non-root UID used in tests.
    31  	nonRootTestUserID = int64(1000)
    32  )
    33  
    34  // setPodNonRootUser configures the Pod to run as a non-root user.
    35  // For Windows, it sets the RunAsUserName field to ContainerUser, and for Linux, it sets the RunAsUser field to 1000.
    36  func setPodNonRootUser(pod *v1.Pod) {
    37  	if framework.NodeOSDistroIs("windows") {
    38  		pod.Spec.SecurityContext.WindowsOptions = &v1.WindowsSecurityContextOptions{RunAsUserName: &nonAdminTestUserName}
    39  	} else {
    40  		pod.Spec.SecurityContext.RunAsUser = &nonRootTestUserID
    41  	}
    42  }
    43  
    44  // getFileModeRegex returns a file mode related regex which should be matched by the mounttest pods' output.
    45  // If the given mask is nil, then the regex will contain the default OS file modes, which are 0644 for Linux and 0775 for Windows.
    46  func getFileModeRegex(filePath string, mask *int32) string {
    47  	var (
    48  		linuxMask   int32
    49  		windowsMask int32
    50  	)
    51  	if mask == nil {
    52  		linuxMask = int32(0644)
    53  		windowsMask = int32(0775)
    54  	} else {
    55  		linuxMask = *mask
    56  		windowsMask = *mask
    57  	}
    58  
    59  	linuxOutput := fmt.Sprintf("mode of file \"%s\": %v", filePath, os.FileMode(linuxMask))
    60  	windowsOutput := fmt.Sprintf("mode of Windows file \"%v\": %s", filePath, os.FileMode(windowsMask))
    61  
    62  	return fmt.Sprintf("(%s|%s)", linuxOutput, windowsOutput)
    63  }
    64  
    65  // createMounts creates a v1.VolumeMount list with a single element.
    66  func createMounts(volumeName, volumeMountPath string, readOnly bool) []v1.VolumeMount {
    67  	return []v1.VolumeMount{
    68  		{
    69  			Name:      volumeName,
    70  			MountPath: volumeMountPath,
    71  			ReadOnly:  readOnly,
    72  		},
    73  	}
    74  }
    75  

View as plain text