From 96a14514b85fe6dc6a26ad8a645c3e72aec61859 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Wed, 21 Aug 2024 23:10:22 +0000 Subject: [PATCH] reduce cyclomatic complexity --- cli/docker.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/cli/docker.go b/cli/docker.go index eae77da..a080fdc 100644 --- a/cli/docker.go +++ b/cli/docker.go @@ -82,8 +82,9 @@ const ( // with UID/GID 1000 will be mapped to `UserNamespaceOffset` + 1000 // on the host. Changing this value will result in improper mappings // on existing containers. - UserNamespaceOffset = 100000 - devDir = "/dev" + UserNamespaceOffset = 100000 + devDir = "/dev" + defaultShutdownTimeout = time.Minute ) var ( @@ -355,7 +356,7 @@ func dockerCmd(ch chan func() error) *cobra.Command { cliflag.IntVarP(cmd.Flags(), &flags.cpus, "cpus", "", EnvCPUs, 0, "Number of CPUs to allocate inner container. e.g. 2") cliflag.IntVarP(cmd.Flags(), &flags.memory, "memory", "", EnvMemory, 0, "Max memory to allocate to the inner container in bytes.") cliflag.BoolVarP(cmd.Flags(), &flags.disableIDMappedMount, "disable-idmapped-mount", "", EnvDisableIDMappedMount, false, "Disable idmapped mounts in sysbox. Note that you may need an alternative (e.g. shiftfs).") - cliflag.DurationVarP(cmd.Flags(), &flags.shutdownTimeout, "shutdown-timeout", "", EnvShutdownTimeout, time.Minute, "Duration after which envbox will be forcefully terminated.") + cliflag.DurationVarP(cmd.Flags(), &flags.shutdownTimeout, "shutdown-timeout", "", EnvShutdownTimeout, defaultShutdownTimeout, "Duration after which envbox will be forcefully terminated.") // Test flags. cliflag.BoolVarP(cmd.Flags(), &flags.noStartupLogs, "no-startup-log", "", "", false, "Do not log startup logs. Useful for testing.") @@ -732,26 +733,31 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker return xerrors.Errorf("exec inspect: %w", err) } - shutdownCh <- func() error { - log.Debug(ctx, "killing container", slog.F("bootstrap_pid", bootstrapPID)) + shutdownCh <- killBootstrapCmd(ctx, log, bootstrapPID, bootstrapExec.ID, client, flags.shutdownTimeout) - timeout := time.Minute - if flags.shutdownTimeout != time.Minute { - timeout = flags.shutdownTimeout - log.Debug(ctx, "using custom shutdown timeout", slog.F("timeout", timeout.String())) - } + return nil +} + +// KillBootstrapCmd is the command we run when we receive a signal +// to kill the envbox container. +func killBootstrapCmd(ctx context.Context, log slog.Logger, pid int, execID string, client dockerutil.DockerClient, timeout time.Duration) func() error { + return func() error { + log.Debug(ctx, "killing container", + slog.F("bootstrap_pid", pid), + slog.F("timeout", timeout.String()), + ) ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() // The PID returned is the PID _outside_ the container... //nolint:gosec - out, err := exec.CommandContext(ctx, "kill", "-TERM", strconv.Itoa(bootstrapPID)).CombinedOutput() + out, err := exec.CommandContext(ctx, "kill", "-TERM", strconv.Itoa(pid)).CombinedOutput() if err != nil { return xerrors.Errorf("kill bootstrap process (%s): %w", out, err) } log.Debug(ctx, "sent kill signal waiting for process to exit") - err = dockerutil.WaitForExit(ctx, client, bootstrapExec.ID) + err = dockerutil.WaitForExit(ctx, client, execID) if err != nil { return xerrors.Errorf("wait for exit: %w", err) } @@ -759,8 +765,6 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Docker log.Debug(ctx, "bootstrap process successfully exited") return nil } - - return nil } //nolint:revive