Skip to content

Pod YAML: Add support for lifecycle.stopSignal #26491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/kubernetes_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ✅ |
Expand Down
7 changes: 7 additions & 0 deletions pkg/k8s.io/api/core/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
57 changes: 57 additions & 0 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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"))
})
})