k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/common/storage/downwardapi.go (about) 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 "context" 21 "fmt" 22 23 v1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/api/resource" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/util/uuid" 27 "k8s.io/kubernetes/test/e2e/feature" 28 "k8s.io/kubernetes/test/e2e/framework" 29 e2epodoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" 30 imageutils "k8s.io/kubernetes/test/utils/image" 31 admissionapi "k8s.io/pod-security-admission/api" 32 33 "github.com/onsi/ginkgo/v2" 34 ) 35 36 var _ = SIGDescribe("Downward API", framework.WithSerial(), framework.WithDisruptive(), feature.EphemeralStorage, func() { 37 f := framework.NewDefaultFramework("downward-api") 38 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 39 40 ginkgo.Context("Downward API tests for local ephemeral storage", func() { 41 ginkgo.It("should provide container's limits.ephemeral-storage and requests.ephemeral-storage as env vars", func(ctx context.Context) { 42 podName := "downward-api-" + string(uuid.NewUUID()) 43 env := []v1.EnvVar{ 44 { 45 Name: "EPHEMERAL_STORAGE_LIMIT", 46 ValueFrom: &v1.EnvVarSource{ 47 ResourceFieldRef: &v1.ResourceFieldSelector{ 48 Resource: "limits.ephemeral-storage", 49 }, 50 }, 51 }, 52 { 53 Name: "EPHEMERAL_STORAGE_REQUEST", 54 ValueFrom: &v1.EnvVarSource{ 55 ResourceFieldRef: &v1.ResourceFieldSelector{ 56 Resource: "requests.ephemeral-storage", 57 }, 58 }, 59 }, 60 } 61 expectations := []string{ 62 fmt.Sprintf("EPHEMERAL_STORAGE_LIMIT=%d", 64*1024*1024), 63 fmt.Sprintf("EPHEMERAL_STORAGE_REQUEST=%d", 32*1024*1024), 64 } 65 66 testDownwardAPIForEphemeralStorage(ctx, f, podName, env, expectations) 67 }) 68 69 ginkgo.It("should provide default limits.ephemeral-storage from node allocatable", func(ctx context.Context) { 70 podName := "downward-api-" + string(uuid.NewUUID()) 71 env := []v1.EnvVar{ 72 { 73 Name: "EPHEMERAL_STORAGE_LIMIT", 74 ValueFrom: &v1.EnvVarSource{ 75 ResourceFieldRef: &v1.ResourceFieldSelector{ 76 Resource: "limits.ephemeral-storage", 77 }, 78 }, 79 }, 80 } 81 expectations := []string{ 82 "EPHEMERAL_STORAGE_LIMIT=[1-9]", 83 } 84 pod := &v1.Pod{ 85 ObjectMeta: metav1.ObjectMeta{ 86 Name: podName, 87 Labels: map[string]string{"name": podName}, 88 }, 89 Spec: v1.PodSpec{ 90 Containers: []v1.Container{ 91 { 92 Name: "dapi-container", 93 Image: imageutils.GetE2EImage(imageutils.BusyBox), 94 Command: []string{"sh", "-c", "env"}, 95 Env: env, 96 }, 97 }, 98 RestartPolicy: v1.RestartPolicyNever, 99 }, 100 } 101 102 testDownwardAPIUsingPod(ctx, f, pod, env, expectations) 103 }) 104 }) 105 106 }) 107 108 func testDownwardAPIForEphemeralStorage(ctx context.Context, f *framework.Framework, podName string, env []v1.EnvVar, expectations []string) { 109 pod := &v1.Pod{ 110 ObjectMeta: metav1.ObjectMeta{ 111 Name: podName, 112 Labels: map[string]string{"name": podName}, 113 }, 114 Spec: v1.PodSpec{ 115 Containers: []v1.Container{ 116 { 117 Name: "dapi-container", 118 Image: imageutils.GetE2EImage(imageutils.BusyBox), 119 Command: []string{"sh", "-c", "env"}, 120 Resources: v1.ResourceRequirements{ 121 Requests: v1.ResourceList{ 122 v1.ResourceEphemeralStorage: resource.MustParse("32Mi"), 123 }, 124 Limits: v1.ResourceList{ 125 v1.ResourceEphemeralStorage: resource.MustParse("64Mi"), 126 }, 127 }, 128 Env: env, 129 }, 130 }, 131 RestartPolicy: v1.RestartPolicyNever, 132 }, 133 } 134 135 testDownwardAPIUsingPod(ctx, f, pod, env, expectations) 136 } 137 138 func testDownwardAPIUsingPod(ctx context.Context, f *framework.Framework, pod *v1.Pod, env []v1.EnvVar, expectations []string) { 139 e2epodoutput.TestContainerOutputRegexp(ctx, f, "downward api env vars", pod, 0, expectations) 140 }