k8s.io/kubernetes@v1.29.3/test/e2e/storage/subpath.go (about)

     1  /*
     2  Copyright 2018 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  	"context"
    21  
    22  	"github.com/onsi/ginkgo/v2"
    23  	v1 "k8s.io/api/core/v1"
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	"k8s.io/kubernetes/test/e2e/storage/testsuites"
    28  	"k8s.io/kubernetes/test/e2e/storage/utils"
    29  	admissionapi "k8s.io/pod-security-admission/api"
    30  )
    31  
    32  var _ = utils.SIGDescribe("Subpath", func() {
    33  	f := framework.NewDefaultFramework("subpath")
    34  	f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
    35  
    36  	ginkgo.Context("Atomic writer volumes", func() {
    37  		var err error
    38  
    39  		ginkgo.BeforeEach(func(ctx context.Context) {
    40  			ginkgo.By("Setting up data")
    41  			secret := &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "my-secret"}, Data: map[string][]byte{"secret-key": []byte("secret-value")}}
    42  			_, err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(ctx, secret, metav1.CreateOptions{})
    43  			if err != nil && !apierrors.IsAlreadyExists(err) {
    44  				framework.ExpectNoError(err, "while creating secret")
    45  			}
    46  
    47  			configmap := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "my-configmap"}, Data: map[string]string{"configmap-key": "configmap-value"}}
    48  			_, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(ctx, configmap, metav1.CreateOptions{})
    49  			if err != nil && !apierrors.IsAlreadyExists(err) {
    50  				framework.ExpectNoError(err, "while creating configmap")
    51  			}
    52  		})
    53  
    54  		/*
    55  		  Release: v1.12
    56  		  Testname: SubPath: Reading content from a secret volume.
    57  		  Description: Containers in a pod can read content from a secret mounted volume which was configured with a subpath.
    58  		*/
    59  		framework.ConformanceIt("should support subpaths with secret pod", func(ctx context.Context) {
    60  			pod := testsuites.SubpathTestPod(f, "secret-key", "secret", &v1.VolumeSource{Secret: &v1.SecretVolumeSource{SecretName: "my-secret"}}, f.NamespacePodSecurityLevel)
    61  			testsuites.TestBasicSubpath(ctx, f, "secret-value", pod)
    62  		})
    63  
    64  		/*
    65  		  Release: v1.12
    66  		  Testname: SubPath: Reading content from a configmap volume.
    67  		  Description: Containers in a pod can read content from a configmap mounted volume which was configured with a subpath.
    68  		*/
    69  		framework.ConformanceIt("should support subpaths with configmap pod", func(ctx context.Context) {
    70  			pod := testsuites.SubpathTestPod(f, "configmap-key", "configmap", &v1.VolumeSource{ConfigMap: &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: "my-configmap"}}}, f.NamespacePodSecurityLevel)
    71  			testsuites.TestBasicSubpath(ctx, f, "configmap-value", pod)
    72  		})
    73  
    74  		/*
    75  		  Release: v1.12
    76  		  Testname: SubPath: Reading content from a configmap volume.
    77  		  Description: Containers in a pod can read content from a configmap mounted volume which was configured with a subpath and also using a mountpath that is a specific file.
    78  		*/
    79  		framework.ConformanceIt("should support subpaths with configmap pod with mountPath of existing file", func(ctx context.Context) {
    80  			pod := testsuites.SubpathTestPod(f, "configmap-key", "configmap", &v1.VolumeSource{ConfigMap: &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{Name: "my-configmap"}}}, f.NamespacePodSecurityLevel)
    81  			file := "/etc/resolv.conf"
    82  			pod.Spec.Containers[0].VolumeMounts[0].MountPath = file
    83  			testsuites.TestBasicSubpathFile(ctx, f, "configmap-value", pod, file)
    84  		})
    85  
    86  		/*
    87  		  Release: v1.12
    88  		  Testname: SubPath: Reading content from a downwardAPI volume.
    89  		  Description: Containers in a pod can read content from a downwardAPI mounted volume which was configured with a subpath.
    90  		*/
    91  		framework.ConformanceIt("should support subpaths with downward pod", func(ctx context.Context) {
    92  			pod := testsuites.SubpathTestPod(f, "downward/podname", "downwardAPI", &v1.VolumeSource{
    93  				DownwardAPI: &v1.DownwardAPIVolumeSource{
    94  					Items: []v1.DownwardAPIVolumeFile{{Path: "downward/podname", FieldRef: &v1.ObjectFieldSelector{APIVersion: "v1", FieldPath: "metadata.name"}}},
    95  				},
    96  			}, f.NamespacePodSecurityLevel)
    97  			testsuites.TestBasicSubpath(ctx, f, pod.Name, pod)
    98  		})
    99  
   100  		/*
   101  		  Release: v1.12
   102  		  Testname: SubPath: Reading content from a projected volume.
   103  		  Description: Containers in a pod can read content from a projected mounted volume which was configured with a subpath.
   104  		*/
   105  		framework.ConformanceIt("should support subpaths with projected pod", func(ctx context.Context) {
   106  			pod := testsuites.SubpathTestPod(f, "projected/configmap-key", "projected", &v1.VolumeSource{
   107  				Projected: &v1.ProjectedVolumeSource{
   108  					Sources: []v1.VolumeProjection{
   109  						{ConfigMap: &v1.ConfigMapProjection{
   110  							LocalObjectReference: v1.LocalObjectReference{Name: "my-configmap"},
   111  							Items:                []v1.KeyToPath{{Path: "projected/configmap-key", Key: "configmap-key"}},
   112  						}},
   113  					},
   114  				},
   115  			}, f.NamespacePodSecurityLevel)
   116  			testsuites.TestBasicSubpath(ctx, f, "configmap-value", pod)
   117  		})
   118  
   119  	})
   120  
   121  	ginkgo.Context("Container restart", func() {
   122  		ginkgo.It("should verify that container can restart successfully after configmaps modified", func(ctx context.Context) {
   123  			configmapToModify := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "my-configmap-to-modify"}, Data: map[string]string{"configmap-key": "configmap-value"}}
   124  			configmapModified := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "my-configmap-to-modify"}, Data: map[string]string{"configmap-key": "configmap-modified-value"}}
   125  			testsuites.TestPodContainerRestartWithConfigmapModified(ctx, f, configmapToModify, configmapModified)
   126  		})
   127  	})
   128  })