secrets.go 4.0 KB

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