-
Notifications
You must be signed in to change notification settings - Fork 39.2k
/
expansion.go
407 lines (364 loc) · 11.9 KB
/
expansion.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Copyright 2016 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 (
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"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
imageutils "k8s.io/kubernetes/test/utils/image"
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
)
// These tests exercise the Kubernetes expansion syntax $(VAR).
// For more information, see:
// https://github.com/kubernetes/community/blob/master/contributors/design-proposals/node/expansion.md
var _ = SIGDescribe("Variable Expansion", func() {
f := framework.NewDefaultFramework("var-expansion")
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelBaseline
/*
Release: v1.9
Testname: Environment variables, expansion
Description: Create a Pod with environment variables. Environment variables defined using previously defined environment variables MUST expand to proper values.
*/
framework.ConformanceIt("should allow composing env vars into new env vars [NodeConformance]", func() {
envVars := []v1.EnvVar{
{
Name: "FOO",
Value: "foo-value",
},
{
Name: "BAR",
Value: "bar-value",
},
{
Name: "FOOBAR",
Value: "$(FOO);;$(BAR)",
},
}
pod := newPod([]string{"sh", "-c", "env"}, envVars, nil, nil)
f.TestContainerOutput("env composition", pod, 0, []string{
"FOO=foo-value",
"BAR=bar-value",
"FOOBAR=foo-value;;bar-value",
})
})
/*
Release: v1.9
Testname: Environment variables, command expansion
Description: Create a Pod with environment variables and container command using them. Container command using the defined environment variables MUST expand to proper values.
*/
framework.ConformanceIt("should allow substituting values in a container's command [NodeConformance]", func() {
envVars := []v1.EnvVar{
{
Name: "TEST_VAR",
Value: "test-value",
},
}
pod := newPod([]string{"sh", "-c", "TEST_VAR=wrong echo \"$(TEST_VAR)\""}, envVars, nil, nil)
f.TestContainerOutput("substitution in container's command", pod, 0, []string{
"test-value",
})
})
/*
Release: v1.9
Testname: Environment variables, command argument expansion
Description: Create a Pod with environment variables and container command arguments using them. Container command arguments using the defined environment variables MUST expand to proper values.
*/
framework.ConformanceIt("should allow substituting values in a container's args [NodeConformance]", func() {
envVars := []v1.EnvVar{
{
Name: "TEST_VAR",
Value: "test-value",
},
}
pod := newPod([]string{"sh", "-c"}, envVars, nil, nil)
pod.Spec.Containers[0].Args = []string{"TEST_VAR=wrong echo \"$(TEST_VAR)\""}
f.TestContainerOutput("substitution in container's args", pod, 0, []string{
"test-value",
})
})
/*
Release: v1.19
Testname: VolumeSubpathEnvExpansion, subpath expansion
Description: Make sure a container's subpath can be set using an expansion of environment variables.
*/
framework.ConformanceIt("should allow substituting values in a volume subpath", func() {
envVars := []v1.EnvVar{
{
Name: "POD_NAME",
Value: "foo",
},
}
mounts := []v1.VolumeMount{
{
Name: "workdir1",
MountPath: "/logscontainer",
SubPathExpr: "$(POD_NAME)",
},
{
Name: "workdir1",
MountPath: "/testcontainer",
},
}
volumes := []v1.Volume{
{
Name: "workdir1",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
pod := newPod([]string{}, envVars, mounts, volumes)
envVars[0].Value = pod.ObjectMeta.Name
pod.Spec.Containers[0].Command = []string{"sh", "-c", "test -d /testcontainer/" + pod.ObjectMeta.Name + ";echo $?"}
f.TestContainerOutput("substitution in volume subpath", pod, 0, []string{
"0",
})
})
/*
Release: v1.19
Testname: VolumeSubpathEnvExpansion, subpath with backticks
Description: Make sure a container's subpath can not be set using an expansion of environment variables when backticks are supplied.
*/
framework.ConformanceIt("should fail substituting values in a volume subpath with backticks [Slow]", func() {
envVars := []v1.EnvVar{
{
Name: "POD_NAME",
Value: "..",
},
}
mounts := []v1.VolumeMount{
{
Name: "workdir1",
MountPath: "/logscontainer",
SubPathExpr: "$(POD_NAME)",
},
}
volumes := []v1.Volume{
{
Name: "workdir1",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
pod := newPod(nil, envVars, mounts, volumes)
// Pod should fail
testPodFailSubpath(f, pod)
})
/*
Release: v1.19
Testname: VolumeSubpathEnvExpansion, subpath with absolute path
Description: Make sure a container's subpath can not be set using an expansion of environment variables when absolute path is supplied.
*/
framework.ConformanceIt("should fail substituting values in a volume subpath with absolute path [Slow]", func() {
absolutePath := "/tmp"
if framework.NodeOSDistroIs("windows") {
// Windows does not typically have a C:\tmp folder.
absolutePath = "C:\\Users"
}
envVars := []v1.EnvVar{
{
Name: "POD_NAME",
Value: absolutePath,
},
}
mounts := []v1.VolumeMount{
{
Name: "workdir1",
MountPath: "/logscontainer",
SubPathExpr: "$(POD_NAME)",
},
}
volumes := []v1.Volume{
{
Name: "workdir1",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
pod := newPod(nil, envVars, mounts, volumes)
// Pod should fail
testPodFailSubpath(f, pod)
})
/*
Release: v1.19
Testname: VolumeSubpathEnvExpansion, subpath ready from failed state
Description: Verify that a failing subpath expansion can be modified during the lifecycle of a container.
*/
framework.ConformanceIt("should verify that a failing subpath expansion can be modified during the lifecycle of a container [Slow]", func() {
envVars := []v1.EnvVar{
{
Name: "POD_NAME",
Value: "foo",
},
{
Name: "ANNOTATION",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.annotations['mysubpath']",
},
},
},
}
mounts := []v1.VolumeMount{
{
Name: "workdir1",
MountPath: "/subpath_mount",
SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
},
{
Name: "workdir1",
MountPath: "/volume_mount",
},
}
volumes := []v1.Volume{
{
Name: "workdir1",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
pod := newPod([]string{"sh", "-c", "tail -f /dev/null"}, envVars, mounts, volumes)
pod.ObjectMeta.Annotations = map[string]string{"notmysubpath": "mypath"}
ginkgo.By("creating the pod with failed condition")
var podClient *framework.PodClient = f.PodClient()
pod = podClient.Create(pod)
err := e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
framework.ExpectError(err, "while waiting for pod to be running")
ginkgo.By("updating the pod")
podClient.Update(pod.ObjectMeta.Name, func(pod *v1.Pod) {
if pod.ObjectMeta.Annotations == nil {
pod.ObjectMeta.Annotations = make(map[string]string)
}
pod.ObjectMeta.Annotations["mysubpath"] = "mypath"
})
ginkgo.By("waiting for pod running")
err = e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
framework.ExpectNoError(err, "while waiting for pod to be running")
ginkgo.By("deleting the pod gracefully")
err = e2epod.DeletePodWithWait(f.ClientSet, pod)
framework.ExpectNoError(err, "failed to delete pod")
})
/*
Release: v1.19
Testname: VolumeSubpathEnvExpansion, subpath test writes
Description: Verify that a subpath expansion can be used to write files into subpaths.
1. valid subpathexpr starts a container running
2. test for valid subpath writes
3. successful expansion of the subpathexpr isn't required for volume cleanup
*/
framework.ConformanceIt("should succeed in writing subpaths in container [Slow]", func() {
envVars := []v1.EnvVar{
{
Name: "POD_NAME",
Value: "foo",
},
{
Name: "ANNOTATION",
ValueFrom: &v1.EnvVarSource{
FieldRef: &v1.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.annotations['mysubpath']",
},
},
},
}
mounts := []v1.VolumeMount{
{
Name: "workdir1",
MountPath: "/subpath_mount",
SubPathExpr: "$(ANNOTATION)/$(POD_NAME)",
},
{
Name: "workdir1",
MountPath: "/volume_mount",
},
}
volumes := []v1.Volume{
{
Name: "workdir1",
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}
pod := newPod([]string{"sh", "-c", "tail -f /dev/null"}, envVars, mounts, volumes)
pod.ObjectMeta.Annotations = map[string]string{"mysubpath": "mypath"}
ginkgo.By("creating the pod")
var podClient *framework.PodClient = f.PodClient()
pod = podClient.Create(pod)
ginkgo.By("waiting for pod running")
err := e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
framework.ExpectNoError(err, "while waiting for pod to be running")
ginkgo.By("creating a file in subpath")
cmd := "touch /volume_mount/mypath/foo/test.log"
_, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
if err != nil {
framework.Failf("expected to be able to write to subpath")
}
ginkgo.By("test for file in mounted path")
cmd = "test -f /subpath_mount/test.log"
_, _, err = f.ExecShellInPodWithFullOutput(pod.Name, cmd)
if err != nil {
framework.Failf("expected to be able to verify file")
}
ginkgo.By("updating the annotation value")
podClient.Update(pod.ObjectMeta.Name, func(pod *v1.Pod) {
pod.ObjectMeta.Annotations["mysubpath"] = "mynewpath"
})
ginkgo.By("waiting for annotated pod running")
err = e2epod.WaitTimeoutForPodRunningInNamespace(f.ClientSet, pod.Name, pod.Namespace, framework.PodStartShortTimeout)
framework.ExpectNoError(err, "while waiting for annotated pod to be running")
ginkgo.By("deleting the pod gracefully")
err = e2epod.DeletePodWithWait(f.ClientSet, pod)
framework.ExpectNoError(err, "failed to delete pod")
})
})
func testPodFailSubpath(f *framework.Framework, pod *v1.Pod) {
var podClient *framework.PodClient = f.PodClient()
pod = podClient.Create(pod)
defer func() {
e2epod.DeletePodWithWait(f.ClientSet, pod)
}()
err := e2epod.WaitForPodContainerToFail(f.ClientSet, pod.Namespace, pod.Name, 0, "CreateContainerConfigError", framework.PodStartShortTimeout)
framework.ExpectNoError(err, "while waiting for the pod container to fail")
}
func newPod(command []string, envVars []v1.EnvVar, mounts []v1.VolumeMount, volumes []v1.Volume) *v1.Pod {
podName := "var-expansion-" + string(uuid.NewUUID())
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Labels: map[string]string{"name": podName},
},
Spec: v1.PodSpec{
Containers: []v1.Container{newContainer("dapi-container", command, envVars, mounts)},
RestartPolicy: v1.RestartPolicyNever,
Volumes: volumes,
},
}
}
func newContainer(containerName string, command []string, envVars []v1.EnvVar, mounts []v1.VolumeMount) v1.Container {
return v1.Container{
Name: containerName,
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Command: command,
Env: envVars,
VolumeMounts: mounts,
}
}