Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 5edec1d

Browse files
committed
Include default envs from containerd.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 03cd5a3 commit 5edec1d

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

pkg/server/helpers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,18 @@ type generator struct {
446446
}
447447

448448
func newCustomGenerator(g generate.Generator) generator {
449-
return generator{
449+
cg := generator{
450450
Generator: g,
451451
envCache: make(map[string]int),
452452
}
453+
spec := g.Spec()
454+
if spec != nil && spec.Process != nil {
455+
for i, env := range spec.Process.Env {
456+
kv := strings.SplitN(env, "=", 2)
457+
cg.envCache[kv[0]] = i
458+
}
459+
}
460+
return cg
453461
}
454462

455463
// AddProcessEnv overrides the original AddProcessEnv. It uses

pkg/server/helpers_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/containerd/containerd/containers"
2525
"github.com/containerd/containerd/linux/runctypes"
2626
imagedigest "github.com/opencontainers/go-digest"
27+
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
2728
"github.com/stretchr/testify/assert"
2829
"golang.org/x/net/context"
2930
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
@@ -216,6 +217,7 @@ func TestOrderedMounts(t *testing.T) {
216217

217218
func TestCustomGenerator(t *testing.T) {
218219
for desc, test := range map[string]struct {
220+
existing []string
219221
kv [][2]string
220222
expected []string
221223
expectNil bool
@@ -248,16 +250,44 @@ func TestCustomGenerator(t *testing.T) {
248250
{"k3", "v3"},
249251
{"k3", "v4"},
250252
{"k1", "v5"},
253+
{"k4", "v6"},
251254
},
252255
expected: []string{
253256
"k1=v5",
254257
"k2=v2",
255258
"k3=v4",
259+
"k4=v6",
260+
},
261+
},
262+
"existing env": {
263+
existing: []string{
264+
"k1=v1",
265+
"k2=v2",
266+
"k3=v3",
267+
},
268+
kv: [][2]string{
269+
{"k3", "v4"},
270+
{"k2", "v5"},
271+
{"k4", "v6"},
272+
},
273+
expected: []string{
274+
"k1=v1",
275+
"k2=v5",
276+
"k3=v4",
277+
"k4=v6",
256278
},
257279
},
258280
} {
259281
t.Logf("TestCase %q", desc)
260-
g := newSpecGenerator(nil)
282+
var spec *runtimespec.Spec
283+
if len(test.existing) > 0 {
284+
spec = &runtimespec.Spec{
285+
Process: &runtimespec.Process{
286+
Env: test.existing,
287+
},
288+
}
289+
}
290+
g := newSpecGenerator(spec)
261291
for _, kv := range test.kv {
262292
g.AddProcessEnv(kv[0], kv[1])
263293
}

0 commit comments

Comments
 (0)