Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 57ded74

Browse files
authoredSep 10, 2020
Merge pull request #598 from docker/encode_env_values
Properly send env variables containing “=“ in their value in ACI payload. Also properly send quoted values
2 parents bcead6f + 98f0064 commit 57ded74

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed
 

‎aci/convert/container.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
"github.com/compose-spec/compose-go/types"
24-
2524
"github.com/docker/compose-cli/api/containers"
2625
)
2726

@@ -71,7 +70,7 @@ func ContainerToComposeProject(r containers.ContainerConfig) (types.Project, err
7170
func toComposeEnvs(opts []string) types.MappingWithEquals {
7271
result := map[string]*string{}
7372
for _, env := range opts {
74-
tokens := strings.Split(env, "=")
73+
tokens := strings.SplitN(env, "=", 2)
7574
if len(tokens) > 1 {
7675
result[tokens[0]] = &tokens[1]
7776
} else {

‎aci/convert/container_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ func TestConvertRestartPolicy(t *testing.T) {
5353
assert.Equal(t, service1.Name, container.ID)
5454
assert.Equal(t, service1.Deploy.RestartPolicy.Condition, "none")
5555
}
56+
57+
func TestConvertEnvVariables(t *testing.T) {
58+
container := containers.ContainerConfig{
59+
ID: "container1",
60+
Environment: []string{
61+
"key=value",
62+
"key2=value=with=equal",
63+
},
64+
}
65+
project, err := ContainerToComposeProject(container)
66+
assert.NilError(t, err)
67+
service1 := project.Services[0]
68+
assert.Equal(t, service1.Name, container.ID)
69+
assert.DeepEqual(t, service1.Environment, types.MappingWithEquals{
70+
"key": to.StringPtr("value"),
71+
"key2": to.StringPtr("value=with=equal"),
72+
})
73+
}

‎aci/convert/convert.go

+13
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe
424424
status := GetStatus(cc, cg)
425425
platform := string(cg.OsType)
426426

427+
var envVars map[string]string = nil
428+
if cc.EnvironmentVariables != nil && len(*cc.EnvironmentVariables) != 0 {
429+
envVars = map[string]string{}
430+
for _, envVar := range *cc.EnvironmentVariables {
431+
envVars[*envVar.Name] = *envVar.Value
432+
}
433+
}
434+
435+
var config *containers.RuntimeConfig = nil
436+
if envVars != nil {
437+
config = &containers.RuntimeConfig{Env: envVars}
438+
}
427439
c := containers.Container{
428440
ID: containerID,
429441
Status: status,
@@ -439,6 +451,7 @@ func ContainerGroupToContainer(containerID string, cg containerinstance.Containe
439451
Ports: ToPorts(cg.IPAddress, *cc.Ports),
440452
Platform: platform,
441453
RestartPolicyCondition: toContainerRestartPolicy(cg.RestartPolicy),
454+
Config: config,
442455
}
443456

444457
return c

‎api/containers/api.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ type Container struct {
4747
MemoryLimit uint64
4848
PidsCurrent uint64
4949
PidsLimit uint64
50-
Labels []string
51-
Ports []Port
50+
Config *RuntimeConfig `json:",omitempty"`
51+
Labels []string `json:",omitempty"`
52+
Ports []Port `json:",omitempty"`
5253
Platform string
5354
RestartPolicyCondition string
5455
}
5556

57+
// RuntimeConfig config of a created container
58+
type RuntimeConfig struct {
59+
Env map[string]string `json:",omitempty"`
60+
}
61+
5662
// Port represents a published port of a container
5763
type Port struct {
5864
// HostPort is the port number on the host

‎cli/cmd/testdata/inspect-out-id.golden

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"MemoryLimit": 0,
1010
"PidsCurrent": 0,
1111
"PidsLimit": 0,
12-
"Labels": null,
13-
"Ports": null,
1412
"Platform": "Linux",
1513
"RestartPolicyCondition": "none"
1614
}

‎tests/aci-e2e/e2e-aci_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ func TestRunEnvVars(t *testing.T) {
592592
"-e", "MYSQL_DATABASE=mytestdb",
593593
"-e", "MYSQL_USER",
594594
"-e", "MYSQL_PASSWORD=userpwd",
595+
"-e", "DATASOURCE_URL=jdbc:mysql://mydb.mysql.database.azure.com/db1?useSSL=true&requireSSL=false&serverTimezone=America/Recife",
595596
"mysql:5.7",
596597
)
597598
cmd.Env = append(cmd.Env, "MYSQL_USER=user1")
@@ -604,7 +605,16 @@ func TestRunEnvVars(t *testing.T) {
604605

605606
containerInspect, err := ParseContainerInspect(res.Stdout())
606607
assert.NilError(t, err)
608+
assert.Assert(t, containerInspect.Config != nil, "nil container config")
609+
assert.Assert(t, containerInspect.Config.Env != nil, "nil container env variables")
607610
assert.Equal(t, containerInspect.Image, "mysql:5.7")
611+
envVars := containerInspect.Config.Env
612+
assert.Equal(t, len(envVars), 5)
613+
assert.Equal(t, envVars["MYSQL_ROOT_PASSWORD"], "rootpwd")
614+
assert.Equal(t, envVars["MYSQL_DATABASE"], "mytestdb")
615+
assert.Equal(t, envVars["MYSQL_USER"], "user1")
616+
assert.Equal(t, envVars["MYSQL_PASSWORD"], "userpwd")
617+
assert.Equal(t, envVars["DATASOURCE_URL"], "jdbc:mysql://mydb.mysql.database.azure.com/db1?useSSL=true&requireSSL=false&serverTimezone=America/Recife")
608618

609619
check := func(t poll.LogT) poll.Result {
610620
res := c.RunDockerOrExitError("logs", container)

‎tests/e2e/testdata/inspect-id.golden

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"MemoryLimit": 0,
1010
"PidsCurrent": 0,
1111
"PidsLimit": 0,
12-
"Labels": null,
13-
"Ports": null,
1412
"Platform": "Linux",
1513
"RestartPolicyCondition": "none"
1614
}

0 commit comments

Comments
 (0)
This repository has been archived.