-
Notifications
You must be signed in to change notification settings - Fork 39.6k
/
secrets.go
152 lines (131 loc) · 4.32 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package node
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/test/e2e/framework"
e2eoutput "k8s.io/kubernetes/test/e2e/framework/pod/output"
"k8s.io/kubernetes/test/e2e/upgrades"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo/v2"
)
// SecretUpgradeTest test that a secret is available before and after
// a cluster upgrade.
type SecretUpgradeTest struct {
secret *v1.Secret
}
// Name returns the tracking name of the test.
func (SecretUpgradeTest) Name() string { return "[sig-storage] [sig-api-machinery] secret-upgrade" }
// Setup creates a secret and then verifies that a pod can consume it.
func (t *SecretUpgradeTest) Setup(ctx context.Context, f *framework.Framework) {
secretName := "upgrade-secret"
ns := f.Namespace
t.secret = &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns.Name,
Name: secretName,
},
Data: map[string][]byte{
"data": []byte("keep it secret"),
},
}
ginkgo.By("Creating a secret")
var err error
if t.secret, err = f.ClientSet.CoreV1().Secrets(ns.Name).Create(ctx, t.secret, metav1.CreateOptions{}); err != nil {
framework.Failf("unable to create test secret %s: %v", t.secret.Name, err)
}
ginkgo.By("Making sure the secret is consumable")
t.testPod(ctx, f)
}
// Test waits for the upgrade to complete, and then verifies that a
// pod can still consume the secret.
func (t *SecretUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) {
<-done
ginkgo.By("Consuming the secret after upgrade")
t.testPod(ctx, f)
}
// Teardown cleans up any remaining resources.
func (t *SecretUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) {
// rely on the namespace deletion to clean up everything
}
// testPod creates a pod that consumes a secret and prints it out. The
// output is then verified.
func (t *SecretUpgradeTest) testPod(ctx context.Context, f *framework.Framework) {
volumeName := "secret-volume"
volumeMountPath := "/etc/secret-volume"
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-secrets-" + string(uuid.NewUUID()),
Namespace: t.secret.ObjectMeta.Namespace,
},
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: t.secret.ObjectMeta.Name,
},
},
},
},
Containers: []v1.Container{
{
Name: "secret-volume-test",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{
"mounttest",
fmt.Sprintf("--file_content=%s/data", volumeMountPath),
fmt.Sprintf("--file_mode=%s/data", volumeMountPath),
},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumeMountPath,
},
},
},
{
Name: "secret-env-test",
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: []string{"sh", "-c", "env"},
Env: []v1.EnvVar{
{
Name: "SECRET_DATA",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: t.secret.ObjectMeta.Name,
},
Key: "data",
},
},
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
expectedOutput := []string{
"content of file \"/etc/secret-volume/data\": keep it secret",
"mode of file \"/etc/secret-volume/data\": -rw-r--r--",
}
e2eoutput.TestContainerOutput(ctx, f, "volume consume secrets", pod, 0, expectedOutput)
expectedOutput = []string{"SECRET_DATA=keep it secret"}
e2eoutput.TestContainerOutput(ctx, f, "env consume secrets", pod, 1, expectedOutput)
}