configmap_manager_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 configmap
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "time"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/util/clock"
  23. clientset "k8s.io/client-go/kubernetes"
  24. "k8s.io/client-go/kubernetes/fake"
  25. "k8s.io/kubernetes/pkg/kubelet/util/manager"
  26. )
  27. func checkObject(t *testing.T, store manager.Store, ns, name string, shouldExist bool) {
  28. _, err := store.Get(ns, name)
  29. if shouldExist && err != nil {
  30. t.Errorf("unexpected actions: %#v", err)
  31. }
  32. if !shouldExist && (err == nil || !strings.Contains(err.Error(), fmt.Sprintf("object %q/%q not registered", ns, name))) {
  33. t.Errorf("unexpected actions: %#v", err)
  34. }
  35. }
  36. func noObjectTTL() (time.Duration, bool) {
  37. return time.Duration(0), false
  38. }
  39. func getConfigMap(fakeClient clientset.Interface) manager.GetObjectFunc {
  40. return func(namespace, name string, opts metav1.GetOptions) (runtime.Object, error) {
  41. return fakeClient.CoreV1().ConfigMaps(namespace).Get(name, opts)
  42. }
  43. }
  44. type envConfigMaps struct {
  45. envVarNames []string
  46. envFromNames []string
  47. }
  48. type configMapsToAttach struct {
  49. containerEnvConfigMaps []envConfigMaps
  50. volumes []string
  51. }
  52. func podWithConfigMaps(ns, podName string, toAttach configMapsToAttach) *v1.Pod {
  53. pod := &v1.Pod{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Namespace: ns,
  56. Name: podName,
  57. },
  58. Spec: v1.PodSpec{},
  59. }
  60. for i, configMaps := range toAttach.containerEnvConfigMaps {
  61. container := v1.Container{
  62. Name: fmt.Sprintf("container-%d", i),
  63. }
  64. for _, name := range configMaps.envFromNames {
  65. envFrom := v1.EnvFromSource{
  66. ConfigMapRef: &v1.ConfigMapEnvSource{
  67. LocalObjectReference: v1.LocalObjectReference{
  68. Name: name,
  69. },
  70. },
  71. }
  72. container.EnvFrom = append(container.EnvFrom, envFrom)
  73. }
  74. for _, name := range configMaps.envVarNames {
  75. envSource := &v1.EnvVarSource{
  76. ConfigMapKeyRef: &v1.ConfigMapKeySelector{
  77. LocalObjectReference: v1.LocalObjectReference{
  78. Name: name,
  79. },
  80. },
  81. }
  82. container.Env = append(container.Env, v1.EnvVar{ValueFrom: envSource})
  83. }
  84. pod.Spec.Containers = append(pod.Spec.Containers, container)
  85. }
  86. for _, configMap := range toAttach.volumes {
  87. volume := &v1.ConfigMapVolumeSource{
  88. LocalObjectReference: v1.LocalObjectReference{Name: configMap},
  89. }
  90. pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
  91. Name: configMap,
  92. VolumeSource: v1.VolumeSource{
  93. ConfigMap: volume,
  94. },
  95. })
  96. }
  97. return pod
  98. }
  99. func TestCacheBasedConfigMapManager(t *testing.T) {
  100. fakeClient := &fake.Clientset{}
  101. store := manager.NewObjectStore(getConfigMap(fakeClient), clock.RealClock{}, noObjectTTL, 0)
  102. manager := &configMapManager{
  103. manager: manager.NewCacheBasedManager(store, getConfigMapNames),
  104. }
  105. // Create a pod with some configMaps.
  106. s1 := configMapsToAttach{
  107. containerEnvConfigMaps: []envConfigMaps{
  108. {envVarNames: []string{"s1"}},
  109. {envFromNames: []string{"s20"}},
  110. },
  111. volumes: []string{"s2"},
  112. }
  113. manager.RegisterPod(podWithConfigMaps("ns1", "name1", s1))
  114. manager.RegisterPod(podWithConfigMaps("ns2", "name2", s1))
  115. // Update the pod with a different configMaps.
  116. s2 := configMapsToAttach{
  117. containerEnvConfigMaps: []envConfigMaps{
  118. {envVarNames: []string{"s3"}},
  119. {envVarNames: []string{"s4"}},
  120. {envFromNames: []string{"s40"}},
  121. },
  122. }
  123. // Create another pod, but with same configMaps in different namespace.
  124. manager.RegisterPod(podWithConfigMaps("ns2", "name2", s2))
  125. // Create and delete a pod with some other configMaps.
  126. s3 := configMapsToAttach{
  127. containerEnvConfigMaps: []envConfigMaps{
  128. {envVarNames: []string{"s6"}},
  129. {envFromNames: []string{"s60"}},
  130. },
  131. }
  132. manager.RegisterPod(podWithConfigMaps("ns3", "name", s3))
  133. manager.UnregisterPod(podWithConfigMaps("ns3", "name", s3))
  134. existingMaps := map[string][]string{
  135. "ns1": {"s1", "s2", "s20"},
  136. "ns2": {"s3", "s4", "s40"},
  137. }
  138. shouldExist := func(ns, configMap string) bool {
  139. if cmaps, ok := existingMaps[ns]; ok {
  140. for _, cm := range cmaps {
  141. if cm == configMap {
  142. return true
  143. }
  144. }
  145. }
  146. return false
  147. }
  148. for _, ns := range []string{"ns1", "ns2", "ns3"} {
  149. for _, configMap := range []string{"s1", "s2", "s3", "s4", "s5", "s6", "s20", "s40", "s50"} {
  150. checkObject(t, store, ns, configMap, shouldExist(ns, configMap))
  151. }
  152. }
  153. }