Skip to content

Commit

Permalink
fix: wrap exec with timeout and _FROM env (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Mar 18, 2024
1 parent 8158904 commit 4f9aeb9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pkg/cri/impl/engine/containerd_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ func (e *ContainerdContainerEngine) Exec(ctx context.Context, c *cri.Container,

pspec := spec.Process
pspec.Terminal = false
pspec.Args = req.Cmd
pspec.Args = wrapTimeout(req.Cmd)
if req.WorkingDir != "" {
pspec.Cwd = req.WorkingDir
}

// Append user specified env
pspec.Env = append(pspec.Env, req.Env...)
pspec.Env = wrapEnv(append(pspec.Env, req.Env...))

task, err := container.Task(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -424,13 +424,13 @@ func (e *ContainerdContainerEngine) ExecAsync(ctx context.Context, c *cri.Contai

pspec := spec.Process
pspec.Terminal = false
pspec.Args = req.Cmd
pspec.Args = wrapTimeout(req.Cmd)
if req.WorkingDir != "" {
pspec.Cwd = req.WorkingDir
}

// Append user specified env
pspec.Env = append(pspec.Env, req.Env...)
pspec.Env = wrapEnv(append(pspec.Env, req.Env...))

task, err := container.Task(ctx, nil)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cri/impl/engine/docker_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ func (e *DockerContainerEngine) Exec(ctx context.Context, c *cri.Container, req
AttachStdout: true,
Detach: false,
DetachKeys: "",
Env: req.Env,
Env: wrapEnv(req.Env),
WorkingDir: req.WorkingDir,
Cmd: req.Cmd,
Cmd: wrapTimeout(req.Cmd),
})
if err != nil {
return invalidResult, err
Expand Down Expand Up @@ -239,9 +239,9 @@ func (e *DockerContainerEngine) ExecAsync(ctx context.Context, c *cri.Container,
AttachStdout: true,
Detach: false,
DetachKeys: "",
Env: req.Env,
Env: wrapEnv(req.Env),
WorkingDir: req.WorkingDir,
Cmd: hackedCmd,
Cmd: wrapTimeout(hackedCmd),
})
if err != nil {
return invalidResult, err
Expand Down
32 changes: 32 additions & 0 deletions pkg/cri/impl/engine/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/

package engine

import (
"github.com/spf13/cast"
"os"
)

var (
timeout = "180"
)

func init() {
s := os.Getenv("CRI_EXEC_TIMEOUT")
if x := cast.ToInt(s); x > 0 {
timeout = s
}
}

// wrapTimeout wraps cmd with timeout -s KILL <seconds> to prevent the process from hanging and not exiting for any reason.
func wrapTimeout(cmd []string) []string {
// timeout -s KILL <seconds> cmd...
return append([]string{"timeout", "-s", "KILL", timeout}, cmd...)
}

// wrapEnv wraps envs with _FROM=holoinsight-agent. This env is used to mark the source of the call.
func wrapEnv(envs []string) []string {
return append(envs, "_FROM=holoinsight-agent")
}

0 comments on commit 4f9aeb9

Please sign in to comment.