From 566b78dd02e91f376e9f219cb96ddba153671eb5 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 22 Nov 2021 11:05:59 +0100 Subject: generate systemd: add --start-timeout flag Add a new flag to set the start timeout for a generated systemd unit. To make naming consistent, add a new --stop-timeout flag as well and let the previous --time map to it. Fixes: #11618 Signed-off-by: Valentin Rothberg --- pkg/api/handlers/libpod/generate.go | 3 +++ pkg/api/server/register_generate.go | 9 +++++++-- pkg/bindings/generate/types.go | 2 ++ pkg/bindings/generate/types_systemd_options.go | 15 +++++++++++++++ pkg/domain/entities/generate.go | 2 ++ pkg/domain/infra/tunnel/generate.go | 14 +++++++++----- pkg/systemd/generate/containers.go | 17 ++++++++++++++--- pkg/systemd/generate/pods.go | 6 +++--- 8 files changed, 55 insertions(+), 13 deletions(-) (limited to 'pkg') diff --git a/pkg/api/handlers/libpod/generate.go b/pkg/api/handlers/libpod/generate.go index 5205d875d..1411c680e 100644 --- a/pkg/api/handlers/libpod/generate.go +++ b/pkg/api/handlers/libpod/generate.go @@ -23,10 +23,12 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) { TemplateUnitFile bool `schema:"templateUnitFile"` RestartPolicy *string `schema:"restartPolicy"` StopTimeout uint `schema:"stopTimeout"` + StartTimeout uint `schema:"startTimeout"` ContainerPrefix string `schema:"containerPrefix"` PodPrefix string `schema:"podPrefix"` Separator string `schema:"separator"` }{ + StartTimeout: 0, StopTimeout: util.DefaultContainerConfig().Engine.StopTimeout, ContainerPrefix: "container", PodPrefix: "pod", @@ -46,6 +48,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) { NoHeader: query.NoHeader, TemplateUnitFile: query.TemplateUnitFile, RestartPolicy: query.RestartPolicy, + StartTimeout: &query.StartTimeout, StopTimeout: &query.StopTimeout, ContainerPrefix: query.ContainerPrefix, PodPrefix: query.PodPrefix, diff --git a/pkg/api/server/register_generate.go b/pkg/api/server/register_generate.go index e10c7029c..0e36394cf 100644 --- a/pkg/api/server/register_generate.go +++ b/pkg/api/server/register_generate.go @@ -37,10 +37,15 @@ func (s *APIServer) registerGenerateHandlers(r *mux.Router) error { // default: false // description: Do not generate the header including the Podman version and the timestamp. // - in: query - // name: time + // name: startTimeout + // type: integer + // default: 0 + // description: Start timeout in seconds. + // - in: query + // name: stopTimeout // type: integer // default: 10 - // description: Stop timeout override. + // description: Stop timeout in seconds. // - in: query // name: restartPolicy // default: on-failure diff --git a/pkg/bindings/generate/types.go b/pkg/bindings/generate/types.go index 6f2594604..092474e4a 100644 --- a/pkg/bindings/generate/types.go +++ b/pkg/bindings/generate/types.go @@ -20,6 +20,8 @@ type SystemdOptions struct { TemplateUnitFile *bool // RestartPolicy - systemd restart policy. RestartPolicy *string + // StartTimeout - time when starting the container. + StartTimeout *uint // StopTimeout - time when stopping the container. StopTimeout *uint // ContainerPrefix - systemd unit name prefix for containers diff --git a/pkg/bindings/generate/types_systemd_options.go b/pkg/bindings/generate/types_systemd_options.go index b26aa7fc2..d60f1d70e 100644 --- a/pkg/bindings/generate/types_systemd_options.go +++ b/pkg/bindings/generate/types_systemd_options.go @@ -92,6 +92,21 @@ func (o *SystemdOptions) GetRestartPolicy() string { return *o.RestartPolicy } +// WithStartTimeout set field StartTimeout to given value +func (o *SystemdOptions) WithStartTimeout(value uint) *SystemdOptions { + o.StartTimeout = &value + return o +} + +// GetStartTimeout returns value of field StartTimeout +func (o *SystemdOptions) GetStartTimeout() uint { + if o.StartTimeout == nil { + var z uint + return z + } + return *o.StartTimeout +} + // WithStopTimeout set field StopTimeout to given value func (o *SystemdOptions) WithStopTimeout(value uint) *SystemdOptions { o.StopTimeout = &value diff --git a/pkg/domain/entities/generate.go b/pkg/domain/entities/generate.go index dfb5bfc6c..7e80e5d2d 100644 --- a/pkg/domain/entities/generate.go +++ b/pkg/domain/entities/generate.go @@ -10,6 +10,8 @@ type GenerateSystemdOptions struct { New bool // RestartPolicy - systemd restart policy. RestartPolicy *string + // StartTimeout - time when starting the container. + StartTimeout *uint // StopTimeout - time when stopping the container. StopTimeout *uint // ContainerPrefix - systemd unit name prefix for containers diff --git a/pkg/domain/infra/tunnel/generate.go b/pkg/domain/infra/tunnel/generate.go index 3a35dd59c..d62a318d6 100644 --- a/pkg/domain/infra/tunnel/generate.go +++ b/pkg/domain/infra/tunnel/generate.go @@ -8,14 +8,18 @@ import ( ) func (ic *ContainerEngine) GenerateSystemd(ctx context.Context, nameOrID string, opts entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) { - options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New).WithNoHeader(opts.NoHeader).WithTemplateUnitFile(opts.TemplateUnitFile) - options.WithPodPrefix(opts.PodPrefix).WithSeparator(opts.Separator) - if opts.RestartPolicy != nil { - options.WithRestartPolicy(*opts.RestartPolicy) + options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New).WithNoHeader(opts.NoHeader).WithTemplateUnitFile(opts.TemplateUnitFile).WithPodPrefix(opts.PodPrefix).WithSeparator(opts.Separator) + + if opts.StartTimeout != nil { + options.WithStartTimeout(*opts.StartTimeout) } - if to := opts.StopTimeout; to != nil { + if opts.StopTimeout != nil { options.WithStopTimeout(*opts.StopTimeout) } + if opts.RestartPolicy != nil { + options.WithRestartPolicy(*opts.RestartPolicy) + } + return generate.Systemd(ic.ClientCtx, nameOrID, options) } diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go index 95ff13371..2fdec5fb1 100644 --- a/pkg/systemd/generate/containers.go +++ b/pkg/systemd/generate/containers.go @@ -73,6 +73,8 @@ type containerInfo struct { ExecStartPre string // ExecStart of the unit. ExecStart string + // TimeoutStartSec of the unit. + TimeoutStartSec uint // TimeoutStopSec of the unit. TimeoutStopSec uint // ExecStop of the unit. @@ -109,6 +111,9 @@ Restart={{{{.RestartPolicy}}}} {{{{- if .StartLimitBurst}}}} StartLimitBurst={{{{.StartLimitBurst}}}} {{{{- end}}}} +{{{{- if ne .TimeoutStartSec 0}}}} +TimeoutStartSec={{{{.TimeoutStartSec}}}} +{{{{- end}}}} TimeoutStopSec={{{{.TimeoutStopSec}}}} {{{{- if .ExecStartPre}}}} ExecStartPre={{{{.ExecStartPre}}}} @@ -148,9 +153,14 @@ func ContainerUnit(ctr *libpod.Container, options entities.GenerateSystemdOption } func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSystemdOptions) (*containerInfo, error) { - timeout := ctr.StopTimeout() + stopTimeout := ctr.StopTimeout() if options.StopTimeout != nil { - timeout = *options.StopTimeout + stopTimeout = *options.StopTimeout + } + + startTimeout := uint(0) + if options.StartTimeout != nil { + startTimeout = *options.StartTimeout } config := ctr.Config() @@ -185,7 +195,8 @@ func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSyste ContainerNameOrID: nameOrID, RestartPolicy: define.DefaultRestartPolicy, PIDFile: conmonPidFile, - StopTimeout: timeout, + TimeoutStartSec: startTimeout, + StopTimeout: stopTimeout, GenerateTimestamp: true, CreateCommand: createCommand, RunRoot: runRoot, diff --git a/pkg/systemd/generate/pods.go b/pkg/systemd/generate/pods.go index 38f7e8e3e..f4cc31c8e 100644 --- a/pkg/systemd/generate/pods.go +++ b/pkg/systemd/generate/pods.go @@ -195,9 +195,9 @@ func generatePodInfo(pod *libpod.Pod, options entities.GenerateSystemdOptions) ( return nil, errors.Wrap(err, "could not find infra container") } - timeout := infraCtr.StopTimeout() + stopTimeout := infraCtr.StopTimeout() if options.StopTimeout != nil { - timeout = *options.StopTimeout + stopTimeout = *options.StopTimeout } config := infraCtr.Config() @@ -223,7 +223,7 @@ func generatePodInfo(pod *libpod.Pod, options entities.GenerateSystemdOptions) ( ServiceName: serviceName, InfraNameOrID: ctrNameOrID, PIDFile: conmonPidFile, - StopTimeout: timeout, + StopTimeout: stopTimeout, GenerateTimestamp: true, CreateCommand: createCommand, } -- cgit v1.2.3-54-g00ecf