Skip to content

Commit 85e3bf2

Browse files
committed
[play_kube] Add validation to container image field
Fixes: #27784 Signed-off-by: Lewis Denny <[email protected]>
1 parent d1a3a4a commit 85e3bf2

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/specgen/generate/kube/kube.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
219219
return nil, errors.New("got empty pod name on container creation when playing kube")
220220
}
221221

222+
// We do validate against the Container spec however it has Image set as optional to allow
223+
// higher level config management to default or override container images. Image is
224+
// required for pods so we must manually validate here.
225+
// https://github.com/kubernetes/kubernetes/pull/48406
226+
if opts.Container.Image == "" {
227+
return nil, fmt.Errorf("container %q is missing the required 'image' field", opts.Container.Name)
228+
}
229+
222230
if opts.NoPodPrefix {
223231
s.Name = opts.Container.Name
224232
} else {

test/e2e/play_kube_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ spec:
283283
- containerPort: 80
284284
`
285285

286+
var podWithoutAnImage = `
287+
apiVersion: v1
288+
kind: Pod
289+
metadata:
290+
labels:
291+
app: podDoesntHaveAnImage
292+
name: podDoesntHaveAnImage
293+
spec:
294+
containers:
295+
- name: podDoesntHaveAnImage
296+
ports:
297+
- containerPort: 80
298+
`
299+
286300
var subpathTestNamedVolume = `
287301
apiVersion: v1
288302
kind: Pod
@@ -2637,6 +2651,15 @@ var _ = Describe("Podman kube play", func() {
26372651
Expect(kube).Should(ExitWithError(125, "pod does not have a name"))
26382652
})
26392653

2654+
It("should error if pod doesn't have an image", func() {
2655+
err := writeYaml(podWithoutAnImage, kubeYaml)
2656+
Expect(err).ToNot(HaveOccurred())
2657+
2658+
kube := podmanTest.Podman([]string{"kube", "play", kubeYaml})
2659+
kube.WaitWithDefaultTimeout()
2660+
Expect(kube).Should(ExitWithError(125, `"container "podDoesntHaveAnImage" is missing the required 'image' field`))
2661+
})
2662+
26402663
It("support container liveness probe", func() {
26412664
err := writeYaml(livenessProbePodYaml, kubeYaml)
26422665
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)