Merge pull request #16475 from Darth-Mera/restartsec-fix

Add restart-sec for container service files
This commit is contained in:
OpenShift Merge Robot
2022-11-10 20:23:06 +00:00
committed by GitHub
3 changed files with 62 additions and 0 deletions

View File

@ -28,6 +28,7 @@ type containerInfo struct {
NotifyAccess string NotifyAccess string
StopTimeout uint StopTimeout uint
RestartPolicy string RestartPolicy string
RestartSec uint
StartLimitBurst string StartLimitBurst string
PIDFile string PIDFile string
ContainerIDFile string ContainerIDFile string
@ -87,6 +88,9 @@ Environment={{{{- range $index, $value := .ExtraEnvs -}}}}{{{{if $index}}}} {{{{
Environment={{{{ $value }}}}{{{{end}}}} Environment={{{{ $value }}}}{{{{end}}}}
{{{{- end}}}} {{{{- end}}}}
Restart={{{{.RestartPolicy}}}} Restart={{{{.RestartPolicy}}}}
{{{{- if .RestartSec}}}}
RestartSec={{{{.RestartSec}}}}
{{{{- end}}}}
{{{{- if .StartLimitBurst}}}} {{{{- if .StartLimitBurst}}}}
StartLimitBurst={{{{.StartLimitBurst}}}} StartLimitBurst={{{{.StartLimitBurst}}}}
{{{{- end}}}} {{{{- end}}}}
@ -282,6 +286,10 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
info.RestartPolicy = *options.RestartPolicy info.RestartPolicy = *options.RestartPolicy
} }
if options.RestartSec != nil {
info.RestartSec = *options.RestartSec
}
// Make sure the executable is set. // Make sure the executable is set.
if info.Executable == "" { if info.Executable == "" {
executable, err := os.Executable() executable, err := os.Executable()

View File

@ -995,6 +995,32 @@ ExecStopPost=/usr/bin/podman rm \
Type=notify Type=notify
NotifyAccess=all 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] [Install]
WantedBy=default.target WantedBy=default.target
` `
@ -1658,6 +1684,25 @@ WantedBy=default.target
false, false,
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 { for _, tt := range tests {
test := tt test := tt

View File

@ -387,6 +387,15 @@ var _ = Describe("Podman generate systemd", func() {
// Grepping the output (in addition to unit tests) // Grepping the output (in addition to unit tests)
Expect(session.OutputToString()).To(ContainSubstring("RestartSec=15")) 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() { It("podman generate systemd --new=false pod", func() {