Skip to content

Commit 9883465

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

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ spec:
283283
- containerPort: 80
284284
`
285285

286+
var podWithoutAnImage = `
287+
apiVersion: v1
288+
kind: Pod
289+
spec:
290+
containers:
291+
- name: podDoesntHaveAnImage
292+
ports:
293+
- containerPort: 80
294+
`
295+
286296
var subpathTestNamedVolume = `
287297
apiVersion: v1
288298
kind: Pod
@@ -2637,6 +2647,14 @@ var _ = Describe("Podman kube play", func() {
26372647
Expect(kube).Should(ExitWithError(125, "pod does not have a name"))
26382648
})
26392649

2650+
It("should error if pod doesn't have an image", func() {
2651+
err := writeYaml(podWithoutAnImage, kubeYaml)
2652+
Expect(err).ToNot(HaveOccurred())
2653+
2654+
kube := podmanTest.PodmanExitCleanly("kube", "play", kubeYaml)
2655+
Expect(kube).Should(ExitWithError(125, `"container "podDoesntHaveAnImage" is missing the required 'image' field`))
2656+
})
2657+
26402658
It("support container liveness probe", func() {
26412659
err := writeYaml(livenessProbePodYaml, kubeYaml)
26422660
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)