configmaps.go 4.3 KB

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