k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/upgrades/node/configmaps.go (about) 1 /* 2 Copyright 2017 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 node 18 19 import ( 20 "context" 21 "fmt" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/util/uuid" 26 "k8s.io/kubernetes/test/e2e/framework" 27 e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output" 28 "k8s.io/kubernetes/test/e2e/upgrades" 29 imageutils "k8s.io/kubernetes/test/utils/image" 30 31 "github.com/onsi/ginkgo/v2" 32 ) 33 34 // ConfigMapUpgradeTest tests that a ConfigMap is available before and after 35 // a cluster upgrade. 36 type ConfigMapUpgradeTest struct { 37 configMap *v1.ConfigMap 38 } 39 40 // Name returns the tracking name of the test. 41 func (ConfigMapUpgradeTest) Name() string { 42 return "[sig-storage] [sig-api-machinery] configmap-upgrade" 43 } 44 45 // Setup creates a ConfigMap and then verifies that a pod can consume it. 46 func (t *ConfigMapUpgradeTest) Setup(ctx context.Context, f *framework.Framework) { 47 configMapName := "upgrade-configmap" 48 49 ns := f.Namespace 50 51 t.configMap = &v1.ConfigMap{ 52 ObjectMeta: metav1.ObjectMeta{ 53 Namespace: ns.Name, 54 Name: configMapName, 55 }, 56 Data: map[string]string{ 57 "data": "some configmap data", 58 }, 59 } 60 61 ginkgo.By("Creating a ConfigMap") 62 var err error 63 if t.configMap, err = f.ClientSet.CoreV1().ConfigMaps(ns.Name).Create(ctx, t.configMap, metav1.CreateOptions{}); err != nil { 64 framework.Failf("unable to create test ConfigMap %s: %v", t.configMap.Name, err) 65 } 66 67 ginkgo.By("Making sure the ConfigMap is consumable") 68 t.testPod(ctx, f) 69 } 70 71 // Test waits for the upgrade to complete, and then verifies that a 72 // pod can still consume the ConfigMap. 73 func (t *ConfigMapUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) { 74 <-done 75 ginkgo.By("Consuming the ConfigMap after upgrade") 76 t.testPod(ctx, f) 77 } 78 79 // Teardown cleans up any remaining resources. 80 func (t *ConfigMapUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) { 81 // rely on the namespace deletion to clean up everything 82 } 83 84 // testPod creates a pod that consumes a ConfigMap and prints it out. The 85 // output is then verified. 86 func (t *ConfigMapUpgradeTest) testPod(ctx context.Context, f *framework.Framework) { 87 volumeName := "configmap-volume" 88 volumeMountPath := "/etc/configmap-volume" 89 90 pod := &v1.Pod{ 91 ObjectMeta: metav1.ObjectMeta{ 92 Name: "pod-configmap-" + string(uuid.NewUUID()), 93 Namespace: t.configMap.ObjectMeta.Namespace, 94 }, 95 Spec: v1.PodSpec{ 96 Volumes: []v1.Volume{ 97 { 98 Name: volumeName, 99 VolumeSource: v1.VolumeSource{ 100 ConfigMap: &v1.ConfigMapVolumeSource{ 101 LocalObjectReference: v1.LocalObjectReference{ 102 Name: t.configMap.ObjectMeta.Name, 103 }, 104 }, 105 }, 106 }, 107 }, 108 Containers: []v1.Container{ 109 { 110 Name: "configmap-volume-test", 111 Image: imageutils.GetE2EImage(imageutils.Agnhost), 112 Args: []string{ 113 "mounttest", 114 fmt.Sprintf("--file_content=%s/data", volumeMountPath), 115 fmt.Sprintf("--file_mode=%s/data", volumeMountPath), 116 }, 117 VolumeMounts: []v1.VolumeMount{ 118 { 119 Name: volumeName, 120 MountPath: volumeMountPath, 121 }, 122 }, 123 }, 124 { 125 Name: "configmap-env-test", 126 Image: imageutils.GetE2EImage(imageutils.BusyBox), 127 Command: []string{"sh", "-c", "env"}, 128 Env: []v1.EnvVar{ 129 { 130 Name: "CONFIGMAP_DATA", 131 ValueFrom: &v1.EnvVarSource{ 132 ConfigMapKeyRef: &v1.ConfigMapKeySelector{ 133 LocalObjectReference: v1.LocalObjectReference{ 134 Name: t.configMap.ObjectMeta.Name, 135 }, 136 Key: "data", 137 }, 138 }, 139 }, 140 }, 141 }, 142 }, 143 RestartPolicy: v1.RestartPolicyNever, 144 }, 145 } 146 147 expectedOutput := []string{ 148 "content of file \"/etc/configmap-volume/data\": some configmap data", 149 "mode of file \"/etc/configmap-volume/data\": -rw-r--r--", 150 } 151 e2eoutput.TestContainerOutput(ctx, f, "volume consume configmap", pod, 0, expectedOutput) 152 153 expectedOutput = []string{"CONFIGMAP_DATA=some configmap data"} 154 e2eoutput.TestContainerOutput(ctx, f, "env consume configmap", pod, 1, expectedOutput) 155 }