Skip to content

Commit 7780e28

Browse files
Merge pull request #26491 from ArthurWuTW/25389
Pod YAML: Add support for `lifecycle.stopSignal`
2 parents 1f1618f + 17601aa commit 7780e28

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

docs/kubernetes_support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Note: **N/A** means that the option cannot be supported in a single-node Podman
112112
| resources\.requests ||
113113
| lifecycle\.postStart | no |
114114
| lifecycle\.preStop | no |
115+
| lifecycle\.stopSignal ||
115116
| terminationMessagePath | no |
116117
| terminationMessagePolicy | no |
117118
| livenessProbe ||

pkg/k8s.io/api/core/v1/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,13 @@ type Lifecycle struct {
13311331
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
13321332
// +optional
13331333
PreStop *Handler `json:"preStop,omitempty"`
1334+
// StopSignal defines the signal to be sent to the container when stopping.
1335+
// This value is configured via the container's Lifecycle and overrides any
1336+
// stop signal defined in the container image. If no StopSignal is specified,
1337+
// the default signal (SIGTERM) will be used.
1338+
// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#defining-custom-stop-signals
1339+
// +optional
1340+
StopSignal *string `json:"stopSignal,omitempty"`
13341341
}
13351342

13361343
type ConditionStatus string

pkg/specgen/generate/kube/kube.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
656656
s.StopTimeout = &timeout
657657
}
658658

659+
if lifecycle := opts.Container.Lifecycle; lifecycle != nil && lifecycle.StopSignal != nil {
660+
stopSignal, err := util.ParseSignal(*lifecycle.StopSignal)
661+
if err != nil {
662+
return nil, err
663+
}
664+
s.StopSignal = &stopSignal
665+
}
666+
659667
return s, nil
660668
}
661669

test/e2e/play_kube_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,32 @@ items:
14441444
restartPolicy: Never
14451445
`
14461446

1447+
var stopSignalSIGUSR1 = `
1448+
apiVersion: v1
1449+
kind: Pod
1450+
metadata:
1451+
name: testpod
1452+
spec:
1453+
containers:
1454+
- name: testctr
1455+
image: ` + CITEST_IMAGE + `
1456+
lifecycle:
1457+
stopSignal: SIGUSR1
1458+
`
1459+
1460+
var stopSignalNosuchSignal = `
1461+
apiVersion: v1
1462+
kind: Pod
1463+
metadata:
1464+
name: noSuchSigTest
1465+
spec:
1466+
containers:
1467+
- name: test1
1468+
image: ` + CITEST_IMAGE + `
1469+
lifecycle:
1470+
stopSignal: noSuchSignal
1471+
`
1472+
14471473
var (
14481474
defaultCtrName = "testCtr"
14491475
defaultCtrCmd = []string{"top"}
@@ -6223,4 +6249,35 @@ spec:
62236249
exec := podmanTest.PodmanExitCleanly("exec", "testPod-"+defaultCtrName, "cat", "/sys/fs/cgroup/cpuset.mems.effective")
62246250
Expect(exec.OutputToString()).To(Equal("0"))
62256251
})
6252+
6253+
It("test Lifecycle stopSignal", func() {
6254+
6255+
// Default StopSignal SIGTERM
6256+
err = writeYaml(simplePodYaml, kubeYaml)
6257+
Expect(err).ToNot(HaveOccurred())
6258+
6259+
podmanTest.PodmanExitCleanly("kube", "play", kubeYaml)
6260+
inspect := podmanTest.PodmanExitCleanly("inspect", "libpod-test-")
6261+
6262+
ctr := inspect.InspectContainerToJSON()
6263+
Expect(ctr[0].Config).To(HaveField("StopSignal", "SIGTERM"))
6264+
6265+
// Custom StopSignal SIGUSR1
6266+
err = writeYaml(stopSignalSIGUSR1, kubeYaml)
6267+
Expect(err).ToNot(HaveOccurred())
6268+
6269+
podmanTest.PodmanExitCleanly("kube", "play", kubeYaml)
6270+
inspect = podmanTest.PodmanExitCleanly("inspect", "testpod-testctr")
6271+
6272+
ctr = inspect.InspectContainerToJSON()
6273+
Expect(ctr[0].Config).To(HaveField("StopSignal", "SIGUSR1"))
6274+
6275+
// No such StopSignal
6276+
err = writeYaml(stopSignalNosuchSignal, kubeYaml)
6277+
Expect(err).ToNot(HaveOccurred())
6278+
6279+
session := podmanTest.Podman([]string{"kube", "play", kubeYaml})
6280+
session.WaitWithDefaultTimeout()
6281+
Expect(session).Should(ExitWithError(125, "invalid signal: noSuchSignal"))
6282+
})
62266283
})

0 commit comments

Comments
 (0)