Merge pull request #26491 from ArthurWuTW/25389

Pod YAML: Add support for `lifecycle.stopSignal`
This commit is contained in:
openshift-merge-bot[bot]
2025-06-24 19:44:30 +00:00
committed by GitHub
4 changed files with 73 additions and 0 deletions

View File

@ -112,6 +112,7 @@ Note: **N/A** means that the option cannot be supported in a single-node Podman
| resources\.requests | ✅ |
| lifecycle\.postStart | no |
| lifecycle\.preStop | no |
| lifecycle\.stopSignal | ✅ |
| terminationMessagePath | no |
| terminationMessagePolicy | no |
| livenessProbe | ✅ |

View File

@ -1331,6 +1331,13 @@ type Lifecycle struct {
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
// +optional
PreStop *Handler `json:"preStop,omitempty"`
// StopSignal defines the signal to be sent to the container when stopping.
// This value is configured via the container's Lifecycle and overrides any
// stop signal defined in the container image. If no StopSignal is specified,
// the default signal (SIGTERM) will be used.
// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#defining-custom-stop-signals
// +optional
StopSignal *string `json:"stopSignal,omitempty"`
}
type ConditionStatus string

View File

@ -656,6 +656,14 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
s.StopTimeout = &timeout
}
if lifecycle := opts.Container.Lifecycle; lifecycle != nil && lifecycle.StopSignal != nil {
stopSignal, err := util.ParseSignal(*lifecycle.StopSignal)
if err != nil {
return nil, err
}
s.StopSignal = &stopSignal
}
return s, nil
}

View File

@ -1444,6 +1444,32 @@ items:
restartPolicy: Never
`
var stopSignalSIGUSR1 = `
apiVersion: v1
kind: Pod
metadata:
name: testpod
spec:
containers:
- name: testctr
image: ` + CITEST_IMAGE + `
lifecycle:
stopSignal: SIGUSR1
`
var stopSignalNosuchSignal = `
apiVersion: v1
kind: Pod
metadata:
name: noSuchSigTest
spec:
containers:
- name: test1
image: ` + CITEST_IMAGE + `
lifecycle:
stopSignal: noSuchSignal
`
var (
defaultCtrName = "testCtr"
defaultCtrCmd = []string{"top"}
@ -6223,4 +6249,35 @@ spec:
exec := podmanTest.PodmanExitCleanly("exec", "testPod-"+defaultCtrName, "cat", "/sys/fs/cgroup/cpuset.mems.effective")
Expect(exec.OutputToString()).To(Equal("0"))
})
It("test Lifecycle stopSignal", func() {
// Default StopSignal SIGTERM
err = writeYaml(simplePodYaml, kubeYaml)
Expect(err).ToNot(HaveOccurred())
podmanTest.PodmanExitCleanly("kube", "play", kubeYaml)
inspect := podmanTest.PodmanExitCleanly("inspect", "libpod-test-")
ctr := inspect.InspectContainerToJSON()
Expect(ctr[0].Config).To(HaveField("StopSignal", "SIGTERM"))
// Custom StopSignal SIGUSR1
err = writeYaml(stopSignalSIGUSR1, kubeYaml)
Expect(err).ToNot(HaveOccurred())
podmanTest.PodmanExitCleanly("kube", "play", kubeYaml)
inspect = podmanTest.PodmanExitCleanly("inspect", "testpod-testctr")
ctr = inspect.InspectContainerToJSON()
Expect(ctr[0].Config).To(HaveField("StopSignal", "SIGUSR1"))
// No such StopSignal
err = writeYaml(stopSignalNosuchSignal, kubeYaml)
Expect(err).ToNot(HaveOccurred())
session := podmanTest.Podman([]string{"kube", "play", kubeYaml})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitWithError(125, "invalid signal: noSuchSignal"))
})
})