secret_manager_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "context"
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "time"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/util/clock"
  24. clientset "k8s.io/client-go/kubernetes"
  25. "k8s.io/client-go/kubernetes/fake"
  26. "k8s.io/kubernetes/pkg/kubelet/util/manager"
  27. )
  28. func checkObject(t *testing.T, store manager.Store, ns, name string, shouldExist bool) {
  29. _, err := store.Get(ns, name)
  30. if shouldExist && err != nil {
  31. t.Errorf("unexpected actions: %#v", err)
  32. }
  33. if !shouldExist && (err == nil || !strings.Contains(err.Error(), fmt.Sprintf("object %q/%q not registered", ns, name))) {
  34. t.Errorf("unexpected actions: %#v", err)
  35. }
  36. }
  37. func noObjectTTL() (time.Duration, bool) {
  38. return time.Duration(0), false
  39. }
  40. func getSecret(fakeClient clientset.Interface) manager.GetObjectFunc {
  41. return func(namespace, name string, opts metav1.GetOptions) (runtime.Object, error) {
  42. return fakeClient.CoreV1().Secrets(namespace).Get(context.TODO(), name, opts)
  43. }
  44. }
  45. type envSecrets struct {
  46. envVarNames []string
  47. envFromNames []string
  48. }
  49. type secretsToAttach struct {
  50. imagePullSecretNames []string
  51. containerEnvSecrets []envSecrets
  52. }
  53. func podWithSecrets(ns, podName string, toAttach secretsToAttach) *v1.Pod {
  54. pod := &v1.Pod{
  55. ObjectMeta: metav1.ObjectMeta{
  56. Namespace: ns,
  57. Name: podName,
  58. },
  59. Spec: v1.PodSpec{},
  60. }
  61. for _, name := range toAttach.imagePullSecretNames {
  62. pod.Spec.ImagePullSecrets = append(
  63. pod.Spec.ImagePullSecrets, v1.LocalObjectReference{Name: name})
  64. }
  65. for i, secrets := range toAttach.containerEnvSecrets {
  66. container := v1.Container{
  67. Name: fmt.Sprintf("container-%d", i),
  68. }
  69. for _, name := range secrets.envFromNames {
  70. envFrom := v1.EnvFromSource{
  71. SecretRef: &v1.SecretEnvSource{
  72. LocalObjectReference: v1.LocalObjectReference{
  73. Name: name,
  74. },
  75. },
  76. }
  77. container.EnvFrom = append(container.EnvFrom, envFrom)
  78. }
  79. for _, name := range secrets.envVarNames {
  80. envSource := &v1.EnvVarSource{
  81. SecretKeyRef: &v1.SecretKeySelector{
  82. LocalObjectReference: v1.LocalObjectReference{
  83. Name: name,
  84. },
  85. },
  86. }
  87. container.Env = append(container.Env, v1.EnvVar{ValueFrom: envSource})
  88. }
  89. pod.Spec.Containers = append(pod.Spec.Containers, container)
  90. }
  91. return pod
  92. }
  93. func TestCacheBasedSecretManager(t *testing.T) {
  94. fakeClient := &fake.Clientset{}
  95. store := manager.NewObjectStore(getSecret(fakeClient), clock.RealClock{}, noObjectTTL, 0)
  96. manager := &secretManager{
  97. manager: manager.NewCacheBasedManager(store, getSecretNames),
  98. }
  99. // Create a pod with some secrets.
  100. s1 := secretsToAttach{
  101. imagePullSecretNames: []string{"s1"},
  102. containerEnvSecrets: []envSecrets{
  103. {envVarNames: []string{"s1"}},
  104. {envVarNames: []string{"s2"}},
  105. {envFromNames: []string{"s20"}},
  106. },
  107. }
  108. manager.RegisterPod(podWithSecrets("ns1", "name1", s1))
  109. // Update the pod with a different secrets.
  110. s2 := secretsToAttach{
  111. imagePullSecretNames: []string{"s1"},
  112. containerEnvSecrets: []envSecrets{
  113. {envVarNames: []string{"s3"}},
  114. {envVarNames: []string{"s4"}},
  115. {envFromNames: []string{"s40"}},
  116. },
  117. }
  118. manager.RegisterPod(podWithSecrets("ns1", "name1", s2))
  119. // Create another pod, but with same secrets in different namespace.
  120. manager.RegisterPod(podWithSecrets("ns2", "name2", s2))
  121. // Create and delete a pod with some other secrets.
  122. s3 := secretsToAttach{
  123. imagePullSecretNames: []string{"s5"},
  124. containerEnvSecrets: []envSecrets{
  125. {envVarNames: []string{"s6"}},
  126. {envFromNames: []string{"s60"}},
  127. },
  128. }
  129. manager.RegisterPod(podWithSecrets("ns3", "name", s3))
  130. manager.UnregisterPod(podWithSecrets("ns3", "name", s3))
  131. // We should have only: s1, s3 and s4 secrets in namespaces: ns1 and ns2.
  132. for _, ns := range []string{"ns1", "ns2", "ns3"} {
  133. for _, secret := range []string{"s1", "s2", "s3", "s4", "s5", "s6", "s20", "s40", "s50"} {
  134. shouldExist :=
  135. (secret == "s1" || secret == "s3" || secret == "s4" || secret == "s40") && (ns == "ns1" || ns == "ns2")
  136. checkObject(t, store, ns, secret, shouldExist)
  137. }
  138. }
  139. }