Skip to content

Commit c2553d6

Browse files
authored
Merge pull request #2250 from knightXun/envs
feature: add envs for pouch exec
2 parents 2208220 + 8cb32d2 commit c2553d6

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

apis/swagger.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,6 +3193,11 @@ definitions:
31933193
minItems: 1
31943194
items:
31953195
type: "string"
3196+
Env:
3197+
type: "array"
3198+
description: "envs for exec command in container"
3199+
items:
3200+
type: "string"
31963201
ContainerProcessList:
31973202
description: OK Response to ContainerTop operation
31983203
type: "object"

apis/types/exec_create_config.go

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/exec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ExecCommand struct {
2525
Terminal bool
2626
Detach bool
2727
User string
28+
Envs []string
2829
}
2930

3031
// Init initializes ExecCommand command.
@@ -51,6 +52,7 @@ func (e *ExecCommand) addFlags() {
5152
flagSet.BoolVarP(&e.Terminal, "tty", "t", false, "Allocate a tty device")
5253
flagSet.BoolVarP(&e.Interactive, "interactive", "i", false, "Open container's STDIN")
5354
flagSet.StringVarP(&e.User, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
55+
flagSet.StringArrayVarP(&e.Envs, "env", "e", []string{}, "Set environment variables")
5456
}
5557

5658
// runExec is the entry of ExecCommand command.
@@ -72,6 +74,7 @@ func (e *ExecCommand) runExec(args []string) error {
7274
AttachStdin: !e.Detach && e.Interactive,
7375
Privileged: false,
7476
User: e.User,
77+
Env: e.Envs,
7578
}
7679

7780
createResp, err := apiClient.ContainerCreateExec(ctx, id, createExecConfig)

daemon/mgr/container_exec.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/alibaba/pouch/pkg/user"
1313

1414
"github.com/docker/docker/pkg/stdcopy"
15-
specs "github.com/opencontainers/runtime-spec/specs-go"
15+
"github.com/opencontainers/runtime-spec/specs-go"
1616
"github.com/pkg/errors"
1717
)
1818

@@ -27,11 +27,18 @@ func (mgr *ContainerManager) CreateExec(ctx context.Context, name string, config
2727
return "", fmt.Errorf("container %s is not running", c.ID)
2828
}
2929

30+
envs, err := mergeEnvSlice(c.Config.Env, config.Env)
31+
32+
if err != nil {
33+
return "", err
34+
}
35+
3036
execid := randomid.Generate()
3137
execConfig := &ContainerExecConfig{
3238
ExecID: execid,
3339
ExecCreateConfig: *config,
3440
ContainerID: c.ID,
41+
Env: envs,
3542
}
3643

3744
mgr.ExecProcesses.Put(execid, execConfig)
@@ -109,7 +116,7 @@ func (mgr *ContainerManager) StartExec(ctx context.Context, execid string, attac
109116
Args: execConfig.Cmd,
110117
Terminal: execConfig.Tty,
111118
Cwd: cwd,
112-
Env: c.Config.Env,
119+
Env: execConfig.Env,
113120
User: specs.User{
114121
UID: uid,
115122
GID: gid,

daemon/mgr/container_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ type ContainerExecConfig struct {
116116

117117
// WaitForClean means exec process can be removed.
118118
WaitForClean bool
119+
120+
// Environment variables
121+
Env []string
119122
}
120123

121124
// AttachConfig wraps some infos of attaching.

test/api_container_exec_create_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ func (suite *APIContainerExecSuite) TestContainerCreateExecOk(c *check.C) {
4141
CheckRespStatus(c, resp, 201)
4242
}
4343

44+
// TestContainerCreateExecWithEnvs tests execing containers with Envs.
45+
func (suite *APIContainerExecSuite) TestContainerCreateExecWithEnvs(c *check.C) {
46+
cname := "TestContainerCreateExecWithEnvs"
47+
48+
CreateBusyboxContainerOk(c, cname)
49+
defer DelContainerForceMultyTime(c, cname)
50+
51+
StartContainerOk(c, cname)
52+
53+
obj := map[string]interface{}{
54+
"Cmd": []string{"Env"},
55+
"Detach": true,
56+
"Env": []string{"Test=OK"},
57+
}
58+
59+
body := request.WithJSONBody(obj)
60+
resp, err := request.Post("/containers/"+cname+"/exec", body)
61+
c.Assert(err, check.IsNil)
62+
CheckRespStatus(c, resp, 201)
63+
}
64+
4465
// TestContainerCreateExecNoCmd tests execing containers is OK.
4566
func (suite *APIContainerExecSuite) TestContainerCreateExecNoCmd(c *check.C) {
4667
cname := "TestContainerCreateExecNoCmd"

test/cli_exec_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ func (suite *PouchExecSuite) TestExecMultiCommands(c *check.C) {
7171
}
7272
}
7373

74+
// TestExecWithEnvs is to verify Exec with Envs.
75+
func (suite *PouchExecSuite) TestExecWithEnvs(c *check.C) {
76+
name := "exec-normal3"
77+
res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "sleep", "100000")
78+
defer DelContainerForceMultyTime(c, name)
79+
80+
res.Assert(c, icmd.Success)
81+
82+
res = command.PouchRun("exec", "-e \"Test=OK\"", name, "env")
83+
84+
if out := res.Combined(); !strings.Contains(out, "Test=OK") {
85+
c.Fatalf("unexpected output %s expected %s\n", out, name)
86+
}
87+
}
88+
7489
// TestExecEcho tests exec prints the output.
7590
func (suite *PouchExecSuite) TestExecEcho(c *check.C) {
7691
name := "TestExecEcho"

0 commit comments

Comments
 (0)