secret_manager_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2018 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 secret
  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 getSecret(fakeClient clientset.Interface) manager.GetObjectFunc {
  40. return func(namespace, name string, opts metav1.GetOptions) (runtime.Object, error) {
  41. return fakeClient.CoreV1().Secrets(namespace).Get(name, opts)
  42. }
  43. }
  44. type envSecrets struct {
  45. envVarNames []string
  46. envFromNames []string
  47. }
  48. type secretsToAttach struct {
  49. imagePullSecretNames []string
  50. containerEnvSecrets []envSecrets
  51. }
  52. func podWithSecrets(ns, podName string, toAttach secretsToAttach) *v1.Pod {
  53. pod := &v1.Pod{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Namespace: ns,
  56. Name: podName,
  57. },
  58. Spec: v1.PodSpec{},
  59. }
  60. for _, name := range toAttach.imagePullSecretNames {
  61. pod.Spec.ImagePullSecrets = append(
  62. pod.Spec.ImagePullSecrets, v1.LocalObjectReference{Name: name})
  63. }
  64. for i, secrets := range toAttach.containerEnvSecrets {
  65. container := v1.Container{
  66. Name: fmt.Sprintf("container-%d", i),
  67. }
  68. for _, name := range secrets.envFromNames {
  69. envFrom := v1.EnvFromSource{
  70. SecretRef: &v1.SecretEnvSource{
  71. LocalObjectReference: v1.LocalObjectReference{
  72. Name: name,
  73. },
  74. },
  75. }
  76. container.EnvFrom = append(container.EnvFrom, envFrom)
  77. }
  78. for _, name := range secrets.envVarNames {
  79. envSource := &v1.EnvVarSource{
  80. SecretKeyRef: &v1.SecretKeySelector{
  81. LocalObjectReference: v1.LocalObjectReference{
  82. Name: name,
  83. },
  84. },
  85. }
  86. container.Env = append(container.Env, v1.EnvVar{ValueFrom: envSource})
  87. }
  88. pod.Spec.Containers = append(pod.Spec.Containers, container)
  89. }
  90. return pod
  91. }
  92. func TestCacheBasedSecretManager(t *testing.T) {
  93. fakeClient := &fake.Clientset{}
  94. store := manager.NewObjectStore(getSecret(fakeClient), clock.RealClock{}, noObjectTTL, 0)
  95. manager := &secretManager{
  96. manager: manager.NewCacheBasedManager(store, getSecretNames),
  97. }
  98. // Create a pod with some secrets.
  99. s1 := secretsToAttach{
  100. imagePullSecretNames: []string{"s1"},
  101. containerEnvSecrets: []envSecrets{
  102. {envVarNames: []string{"s1"}},
  103. {envVarNames: []string{"s2"}},
  104. {envFromNames: []string{"s20"}},
  105. },
  106. }
  107. manager.RegisterPod(podWithSecrets("ns1", "name1", s1))
  108. // Update the pod with a different secrets.
  109. s2 := secretsToAttach{
  110. imagePullSecretNames: []string{"s1"},
  111. containerEnvSecrets: []envSecrets{
  112. {envVarNames: []string{"s3"}},
  113. {envVarNames: []string{"s4"}},
  114. {envFromNames: []string{"s40"}},
  115. },
  116. }
  117. manager.RegisterPod(podWithSecrets("ns1", "name1", s2))
  118. // Create another pod, but with same secrets in different namespace.
  119. manager.RegisterPod(podWithSecrets("ns2", "name2", s2))
  120. // Create and delete a pod with some other secrets.
  121. s3 := secretsToAttach{
  122. imagePullSecretNames: []string{"s5"},
  123. containerEnvSecrets: []envSecrets{
  124. {envVarNames: []string{"s6"}},
  125. {envFromNames: []string{"s60"}},
  126. },
  127. }
  128. manager.RegisterPod(podWithSecrets("ns3", "name", s3))
  129. manager.UnregisterPod(podWithSecrets("ns3", "name", s3))
  130. // We should have only: s1, s3 and s4 secrets in namespaces: ns1 and ns2.
  131. for _, ns := range []string{"ns1", "ns2", "ns3"} {
  132. for _, secret := range []string{"s1", "s2", "s3", "s4", "s5", "s6", "s20", "s40", "s50"} {
  133. shouldExist :=
  134. (secret == "s1" || secret == "s3" || secret == "s4" || secret == "s40") && (ns == "ns1" || ns == "ns2")
  135. checkObject(t, store, ns, secret, shouldExist)
  136. }
  137. }
  138. }