configmap.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package common
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/apimachinery/pkg/util/uuid"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. imageutils "k8s.io/kubernetes/test/utils/image"
  24. "github.com/onsi/ginkgo"
  25. )
  26. var _ = ginkgo.Describe("[sig-node] ConfigMap", func() {
  27. f := framework.NewDefaultFramework("configmap")
  28. /*
  29. Release : v1.9
  30. Testname: ConfigMap, from environment field
  31. Description: Create a Pod with an environment variable value set using a value from ConfigMap. A ConfigMap value MUST be accessible in the container environment.
  32. */
  33. framework.ConformanceIt("should be consumable via environment variable [NodeConformance]", func() {
  34. name := "configmap-test-" + string(uuid.NewUUID())
  35. configMap := newConfigMap(f, name)
  36. ginkgo.By(fmt.Sprintf("Creating configMap %v/%v", f.Namespace.Name, configMap.Name))
  37. var err error
  38. if configMap, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(context.TODO(), configMap, metav1.CreateOptions{}); err != nil {
  39. framework.Failf("unable to create test configMap %s: %v", configMap.Name, err)
  40. }
  41. pod := &v1.Pod{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: "pod-configmaps-" + string(uuid.NewUUID()),
  44. },
  45. Spec: v1.PodSpec{
  46. Containers: []v1.Container{
  47. {
  48. Name: "env-test",
  49. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  50. Command: []string{"sh", "-c", "env"},
  51. Env: []v1.EnvVar{
  52. {
  53. Name: "CONFIG_DATA_1",
  54. ValueFrom: &v1.EnvVarSource{
  55. ConfigMapKeyRef: &v1.ConfigMapKeySelector{
  56. LocalObjectReference: v1.LocalObjectReference{
  57. Name: name,
  58. },
  59. Key: "data-1",
  60. },
  61. },
  62. },
  63. },
  64. },
  65. },
  66. RestartPolicy: v1.RestartPolicyNever,
  67. },
  68. }
  69. f.TestContainerOutput("consume configMaps", pod, 0, []string{
  70. "CONFIG_DATA_1=value-1",
  71. })
  72. })
  73. /*
  74. Release: v1.9
  75. Testname: ConfigMap, from environment variables
  76. Description: Create a Pod with a environment source from ConfigMap. All ConfigMap values MUST be available as environment variables in the container.
  77. */
  78. framework.ConformanceIt("should be consumable via the environment [NodeConformance]", func() {
  79. name := "configmap-test-" + string(uuid.NewUUID())
  80. configMap := newEnvFromConfigMap(f, name)
  81. ginkgo.By(fmt.Sprintf("Creating configMap %v/%v", f.Namespace.Name, configMap.Name))
  82. var err error
  83. if configMap, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(context.TODO(), configMap, metav1.CreateOptions{}); err != nil {
  84. framework.Failf("unable to create test configMap %s: %v", configMap.Name, err)
  85. }
  86. pod := &v1.Pod{
  87. ObjectMeta: metav1.ObjectMeta{
  88. Name: "pod-configmaps-" + string(uuid.NewUUID()),
  89. },
  90. Spec: v1.PodSpec{
  91. Containers: []v1.Container{
  92. {
  93. Name: "env-test",
  94. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  95. Command: []string{"sh", "-c", "env"},
  96. EnvFrom: []v1.EnvFromSource{
  97. {
  98. ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: name}},
  99. },
  100. {
  101. Prefix: "p_",
  102. ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: name}},
  103. },
  104. },
  105. },
  106. },
  107. RestartPolicy: v1.RestartPolicyNever,
  108. },
  109. }
  110. f.TestContainerOutput("consume configMaps", pod, 0, []string{
  111. "data_1=value-1", "data_2=value-2", "data_3=value-3",
  112. "p_data_1=value-1", "p_data_2=value-2", "p_data_3=value-3",
  113. })
  114. })
  115. /*
  116. Release : v1.14
  117. Testname: ConfigMap, with empty-key
  118. Description: Attempt to create a ConfigMap with an empty key. The creation MUST fail.
  119. */
  120. framework.ConformanceIt("should fail to create ConfigMap with empty key", func() {
  121. configMap, err := newConfigMapWithEmptyKey(f)
  122. framework.ExpectError(err, "created configMap %q with empty key in namespace %q", configMap.Name, f.Namespace.Name)
  123. })
  124. ginkgo.It("should update ConfigMap successfully", func() {
  125. name := "configmap-test-" + string(uuid.NewUUID())
  126. configMap := newConfigMap(f, name)
  127. ginkgo.By(fmt.Sprintf("Creating ConfigMap %v/%v", f.Namespace.Name, configMap.Name))
  128. _, err := f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(context.TODO(), configMap, metav1.CreateOptions{})
  129. framework.ExpectNoError(err, "failed to create ConfigMap")
  130. configMap.Data = map[string]string{
  131. "data": "value",
  132. }
  133. ginkgo.By(fmt.Sprintf("Updating configMap %v/%v", f.Namespace.Name, configMap.Name))
  134. _, err = f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Update(context.TODO(), configMap, metav1.UpdateOptions{})
  135. framework.ExpectNoError(err, "failed to update ConfigMap")
  136. configMapFromUpdate, err := f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Get(context.TODO(), name, metav1.GetOptions{})
  137. framework.ExpectNoError(err, "failed to get ConfigMap")
  138. ginkgo.By(fmt.Sprintf("Verifying update of ConfigMap %v/%v", f.Namespace.Name, configMap.Name))
  139. framework.ExpectEqual(configMapFromUpdate.Data, configMap.Data)
  140. })
  141. ginkgo.It("should run through a ConfigMap lifecycle", func() {
  142. testNamespaceName := f.Namespace.Name
  143. testConfigMapName := "test-configmap" + string(uuid.NewUUID())
  144. _, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Create(context.TODO(), &v1.ConfigMap{
  145. ObjectMeta: metav1.ObjectMeta{
  146. Name: testConfigMapName,
  147. Labels: map[string]string{
  148. "test-configmap-static": "true",
  149. },
  150. },
  151. Data: map[string]string{
  152. "valueName": "value",
  153. },
  154. }, metav1.CreateOptions{})
  155. framework.ExpectNoError(err, "failed to create ConfigMap")
  156. configMapPatchPayload, err := json.Marshal(v1.ConfigMap{
  157. ObjectMeta: metav1.ObjectMeta{
  158. Labels: map[string]string{
  159. "test-configmap": "patched",
  160. },
  161. },
  162. Data: map[string]string{
  163. "valueName": "value1",
  164. },
  165. })
  166. framework.ExpectNoError(err, "failed to marshal patch data")
  167. _, err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Patch(context.TODO(), testConfigMapName, types.StrategicMergePatchType, []byte(configMapPatchPayload), metav1.PatchOptions{})
  168. framework.ExpectNoError(err, "failed to patch ConfigMap")
  169. configMap, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Get(context.TODO(), testConfigMapName, metav1.GetOptions{})
  170. framework.ExpectNoError(err, "failed to get ConfigMap")
  171. framework.ExpectEqual(configMap.Data["valueName"], "value1", "failed to patch ConfigMap")
  172. framework.ExpectEqual(configMap.Labels["test-configmap"], "patched", "failed to patch ConfigMap")
  173. // listing in all namespaces to hit the endpoint
  174. configMapList, err := f.ClientSet.CoreV1().ConfigMaps("").List(context.TODO(), metav1.ListOptions{
  175. LabelSelector: "test-configmap-static=true",
  176. })
  177. framework.ExpectNoError(err, "failed to list ConfigMaps with LabelSelector")
  178. framework.ExpectNotEqual(len(configMapList.Items), 0, "no ConfigMaps found in ConfigMap list")
  179. testConfigMapFound := false
  180. for _, cm := range configMapList.Items {
  181. if cm.ObjectMeta.Name == testConfigMapName &&
  182. cm.ObjectMeta.Namespace == testNamespaceName &&
  183. cm.ObjectMeta.Labels["test-configmap-static"] == "true" &&
  184. cm.Data["valueName"] == "value1" {
  185. testConfigMapFound = true
  186. break
  187. }
  188. }
  189. framework.ExpectEqual(testConfigMapFound, true, "failed to find ConfigMap in list")
  190. err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).DeleteCollection(context.TODO(), &metav1.DeleteOptions{}, metav1.ListOptions{
  191. LabelSelector: "test-configmap-static=true",
  192. })
  193. framework.ExpectNoError(err, "failed to delete ConfigMap collection with LabelSelector")
  194. })
  195. })
  196. func newEnvFromConfigMap(f *framework.Framework, name string) *v1.ConfigMap {
  197. return &v1.ConfigMap{
  198. ObjectMeta: metav1.ObjectMeta{
  199. Namespace: f.Namespace.Name,
  200. Name: name,
  201. },
  202. Data: map[string]string{
  203. "data_1": "value-1",
  204. "data_2": "value-2",
  205. "data_3": "value-3",
  206. },
  207. }
  208. }
  209. func newConfigMapWithEmptyKey(f *framework.Framework) (*v1.ConfigMap, error) {
  210. name := "configmap-test-emptyKey-" + string(uuid.NewUUID())
  211. configMap := &v1.ConfigMap{
  212. ObjectMeta: metav1.ObjectMeta{
  213. Namespace: f.Namespace.Name,
  214. Name: name,
  215. },
  216. Data: map[string]string{
  217. "": "value-1",
  218. },
  219. }
  220. ginkgo.By(fmt.Sprintf("Creating configMap that has name %s", configMap.Name))
  221. return f.ClientSet.CoreV1().ConfigMaps(f.Namespace.Name).Create(context.TODO(), configMap, metav1.CreateOptions{})
  222. }