Skip to content

Commit fc131a2

Browse files
Merge pull request containers#7671 from zhangguanzhang/play-kube-handle-restartPolicy
handle the restartPolicy for play kube and generate kube
2 parents 4b037d2 + f0ccac1 commit fc131a2

File tree

4 files changed

+119
-14
lines changed

4 files changed

+119
-14
lines changed

libpod/kube.go

+18
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ func (p *Pod) GenerateForKube() (*v1.Pod, []v1.ServicePort, error) {
7777
}
7878
pod.Spec.HostAliases = extraHost
7979

80+
// vendor/k8s.io/api/core/v1/types.go: v1.Container cannot save restartPolicy
81+
// so set it at here
82+
for _, ctr := range allContainers {
83+
if !ctr.IsInfra() {
84+
switch ctr.Config().RestartPolicy {
85+
case RestartPolicyAlways:
86+
pod.Spec.RestartPolicy = v1.RestartPolicyAlways
87+
case RestartPolicyOnFailure:
88+
pod.Spec.RestartPolicy = v1.RestartPolicyOnFailure
89+
case RestartPolicyNo:
90+
pod.Spec.RestartPolicy = v1.RestartPolicyNever
91+
default: // some pod create from cmdline, such as "", so set it to Never
92+
pod.Spec.RestartPolicy = v1.RestartPolicyNever
93+
}
94+
break
95+
}
96+
}
97+
8098
if p.SharesPID() {
8199
// unfortunately, go doesn't have a nice way to specify a pointer to a bool
82100
b := true

pkg/domain/infra/abi/play.go

+13
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
299299
return nil, err
300300
}
301301

302+
var ctrRestartPolicy string
303+
switch podYAML.Spec.RestartPolicy {
304+
case v1.RestartPolicyAlways:
305+
ctrRestartPolicy = libpod.RestartPolicyAlways
306+
case v1.RestartPolicyOnFailure:
307+
ctrRestartPolicy = libpod.RestartPolicyOnFailure
308+
case v1.RestartPolicyNever:
309+
ctrRestartPolicy = libpod.RestartPolicyNo
310+
default: // Default to Always
311+
ctrRestartPolicy = libpod.RestartPolicyAlways
312+
}
313+
302314
containers := make([]*libpod.Container, 0, len(podYAML.Spec.Containers))
303315
for _, container := range podYAML.Spec.Containers {
304316
pullPolicy := util.PullImageMissing
@@ -326,6 +338,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
326338
if err != nil {
327339
return nil, err
328340
}
341+
conf.RestartPolicy = ctrRestartPolicy
329342
ctr, err := createconfig.CreateContainerFromCreateConfig(ctx, ic.Libpod, conf, pod)
330343
if err != nil {
331344
return nil, err

test/e2e/generate_kube_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package integration
33
import (
44
"os"
55
"path/filepath"
6+
"strconv"
67

78
. "github.com/containers/podman/v2/test/utils"
89
"github.com/ghodss/yaml"
@@ -201,6 +202,39 @@ var _ = Describe("Podman generate kube", func() {
201202
// Expect(err).To(BeNil())
202203
})
203204

205+
It("podman generate kube on pod with restartPolicy", func() {
206+
// podName, set, expect
207+
testSli := [][]string{
208+
{"testPod1", "", "Never"}, // some pod create from cmdline, so set it to Never
209+
{"testPod2", "always", "Always"},
210+
{"testPod3", "on-failure", "OnFailure"},
211+
{"testPod4", "no", "Never"},
212+
}
213+
214+
for k, v := range testSli {
215+
podName := v[0]
216+
podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName})
217+
podSession.WaitWithDefaultTimeout()
218+
Expect(podSession.ExitCode()).To(Equal(0))
219+
220+
ctrName := "ctr" + strconv.Itoa(k)
221+
ctr1Session := podmanTest.Podman([]string{"create", "--name", ctrName, "--pod", podName,
222+
"--restart", v[1], ALPINE, "top"})
223+
ctr1Session.WaitWithDefaultTimeout()
224+
Expect(ctr1Session.ExitCode()).To(Equal(0))
225+
226+
kube := podmanTest.Podman([]string{"generate", "kube", podName})
227+
kube.WaitWithDefaultTimeout()
228+
Expect(kube.ExitCode()).To(Equal(0))
229+
230+
pod := new(v1.Pod)
231+
err := yaml.Unmarshal(kube.Out.Contents(), pod)
232+
Expect(err).To(BeNil())
233+
234+
Expect(string(pod.Spec.RestartPolicy)).To(Equal(v[2]))
235+
}
236+
})
237+
204238
It("podman generate kube on pod with ports", func() {
205239
podName := "test"
206240
podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName, "-p", "4000:4000", "-p", "5000:5000"})

test/e2e/play_kube_test.go

+54-14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ metadata:
4646
{{ end }}
4747
4848
spec:
49+
restartPolicy: {{ .RestartPolicy }}
4950
hostname: {{ .Hostname }}
5051
hostAliases:
5152
{{ range .HostAliases }}
@@ -165,6 +166,7 @@ spec:
165166
{{- end }}
166167
{{- end }}
167168
spec:
169+
restartPolicy: {{ .RestartPolicy }}
168170
hostname: {{ .Hostname }}
169171
containers:
170172
{{ with .Ctrs }}
@@ -274,13 +276,14 @@ func generateDeploymentKubeYaml(deployment *Deployment, fileName string) error {
274276

275277
// Pod describes the options a kube yaml can be configured at pod level
276278
type Pod struct {
277-
Name string
278-
Hostname string
279-
HostAliases []HostAlias
280-
Ctrs []*Ctr
281-
Volumes []*Volume
282-
Labels map[string]string
283-
Annotations map[string]string
279+
Name string
280+
RestartPolicy string
281+
Hostname string
282+
HostAliases []HostAlias
283+
Ctrs []*Ctr
284+
Volumes []*Volume
285+
Labels map[string]string
286+
Annotations map[string]string
284287
}
285288

286289
type HostAlias struct {
@@ -293,13 +296,14 @@ type HostAlias struct {
293296
// if no containers are added, it will add the default container
294297
func getPod(options ...podOption) *Pod {
295298
p := Pod{
296-
Name: defaultPodName,
297-
Hostname: "",
298-
HostAliases: nil,
299-
Ctrs: make([]*Ctr, 0),
300-
Volumes: make([]*Volume, 0),
301-
Labels: make(map[string]string),
302-
Annotations: make(map[string]string),
299+
Name: defaultPodName,
300+
RestartPolicy: "Never",
301+
Hostname: "",
302+
HostAliases: nil,
303+
Ctrs: make([]*Ctr, 0),
304+
Volumes: make([]*Volume, 0),
305+
Labels: make(map[string]string),
306+
Annotations: make(map[string]string),
303307
}
304308
for _, option := range options {
305309
option(&p)
@@ -312,6 +316,12 @@ func getPod(options ...podOption) *Pod {
312316

313317
type podOption func(*Pod)
314318

319+
func withPodName(name string) podOption {
320+
return func(pod *Pod) {
321+
pod.Name = name
322+
}
323+
}
324+
315325
func withHostname(h string) podOption {
316326
return func(pod *Pod) {
317327
pod.Hostname = h
@@ -333,6 +343,12 @@ func withCtr(c *Ctr) podOption {
333343
}
334344
}
335345

346+
func withRestartPolicy(policy string) podOption {
347+
return func(pod *Pod) {
348+
pod.RestartPolicy = policy
349+
}
350+
}
351+
336352
func withLabel(k, v string) podOption {
337353
return func(pod *Pod) {
338354
pod.Labels[k] = v
@@ -649,6 +665,30 @@ var _ = Describe("Podman generate kube", func() {
649665
Expect(inspect.OutputToString()).To(ContainSubstring(`[echo hello world]`))
650666
})
651667

668+
It("podman play kube test restartPolicy", func() {
669+
// podName, set, expect
670+
testSli := [][]string{
671+
{"testPod1", "", "always"}, // Default eqaul to always
672+
{"testPod2", "Always", "always"},
673+
{"testPod3", "OnFailure", "on-failure"},
674+
{"testPod4", "Never", "no"},
675+
}
676+
for _, v := range testSli {
677+
pod := getPod(withPodName(v[0]), withRestartPolicy(v[1]))
678+
err := generatePodKubeYaml(pod, kubeYaml)
679+
Expect(err).To(BeNil())
680+
681+
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
682+
kube.WaitWithDefaultTimeout()
683+
Expect(kube.ExitCode()).To(Equal(0))
684+
685+
inspect := podmanTest.Podman([]string{"inspect", getCtrNameInPod(pod), "--format", "{{.HostConfig.RestartPolicy.Name}}"})
686+
inspect.WaitWithDefaultTimeout()
687+
Expect(inspect.ExitCode()).To(Equal(0))
688+
Expect(inspect.OutputToString()).To(Equal(v[2]))
689+
}
690+
})
691+
652692
It("podman play kube test hostname", func() {
653693
pod := getPod()
654694
err := generatePodKubeYaml(pod, kubeYaml)

0 commit comments

Comments
 (0)