Skip to content
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

Add restart-sec for container service files #16475

Merged
merged 1 commit into from
Nov 10, 2022
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
8 changes: 8 additions & 0 deletions pkg/systemd/generate/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type containerInfo struct {
NotifyAccess string
StopTimeout uint
RestartPolicy string
RestartSec uint
StartLimitBurst string
PIDFile string
ContainerIDFile string
Expand Down Expand Up @@ -87,6 +88,9 @@ Environment={{{{- range $index, $value := .ExtraEnvs -}}}}{{{{if $index}}}} {{{{
Environment={{{{ $value }}}}{{{{end}}}}
{{{{- end}}}}
Restart={{{{.RestartPolicy}}}}
{{{{- if .RestartSec}}}}
RestartSec={{{{.RestartSec}}}}
{{{{- end}}}}
{{{{- if .StartLimitBurst}}}}
StartLimitBurst={{{{.StartLimitBurst}}}}
{{{{- end}}}}
Expand Down Expand Up @@ -282,6 +286,10 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
info.RestartPolicy = *options.RestartPolicy
}

if options.RestartSec != nil {
info.RestartSec = *options.RestartSec
}

// Make sure the executable is set.
if info.Executable == "" {
executable, err := os.Executable()
Expand Down
45 changes: 45 additions & 0 deletions pkg/systemd/generate/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,32 @@ ExecStopPost=/usr/bin/podman rm \
Type=notify
NotifyAccess=all

[Install]
WantedBy=default.target
`
goodRestartSec := `# container-foobar.service
# autogenerated by Podman CI

[Unit]
Description=Podman container-foobar.service
Documentation=man:podman-generate-systemd(1)
Wants=network-online.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
RestartSec=15
TimeoutStopSec=70
ExecStart=/usr/bin/podman start foobar
ExecStop=/usr/bin/podman stop \
-t 10 foobar
ExecStopPost=/usr/bin/podman stop \
-t 10 foobar
PIDFile=/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid
Type=forking

[Install]
WantedBy=default.target
`
Expand Down Expand Up @@ -1658,6 +1684,25 @@ WantedBy=default.target
false,
false,
},
{"good with RestartSec",
containerInfo{
Executable: "/usr/bin/podman",
ServiceName: "container-foobar",
ContainerNameOrID: "foobar",
PIDFile: "/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid",
StopTimeout: 10,
PodmanVersion: "CI",
EnvVariable: define.EnvVariable,
GraphRoot: "/var/lib/containers/storage",
RunRoot: "/var/run/containers/storage",
RestartSec: 15,
},
goodRestartSec,
false,
false,
false,
false,
},
}
for _, tt := range tests {
test := tt
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ var _ = Describe("Podman generate systemd", func() {

// Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("RestartSec=15"))

n = podmanTest.Podman([]string{"create", "--name", "foocontainer", "alpine", "top"})
n.WaitWithDefaultTimeout()
Expect(n).Should(Exit(0))

session2 := podmanTest.Podman([]string{"generate", "systemd", "--restart-sec", "15", "--name", "foocontainer"})
session2.WaitWithDefaultTimeout()
Expect(session2).Should(Exit(0))
Expect(session2.OutputToString()).To(ContainSubstring("RestartSec=15"))
})

It("podman generate systemd --new=false pod", func() {
Expand Down