Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/orchestrator/pkg/server/env_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build linux

package server

import "maps"

// mergeTemplateEnvVars layers env vars for a sandbox start: the template's
// Docker image ENV vars are the base, and user-provided env vars (from the SDK
// create call) override them on conflict. Per-command env vars from
// process.start override both, applied later in envd. A nil/empty template map
// returns userVars unchanged (preserving nil), so the common no-image-ENV case
// adds nothing.
func mergeTemplateEnvVars(templateVars, userVars map[string]string) map[string]string {
if len(templateVars) == 0 {
return userVars
}

merged := maps.Clone(templateVars)
maps.Copy(merged, userVars)

return merged
}
38 changes: 38 additions & 0 deletions packages/orchestrator/pkg/server/env_merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build linux

package server

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMergeTemplateEnvVars(t *testing.T) {
t.Parallel()

for name, tc := range map[string]struct {
tmpl map[string]string
user map[string]string
want map[string]string
}{
"no template, no user": {nil, nil, nil},
"no template, user only": {nil, map[string]string{"A": "1"}, map[string]string{"A": "1"}},
"template only": {map[string]string{"PYTHONPATH": "/lib"}, nil, map[string]string{"PYTHONPATH": "/lib"}},
"disjoint merged": {map[string]string{"P": "/lib"}, map[string]string{"K": "v"}, map[string]string{"P": "/lib", "K": "v"}},
"user overrides template": {map[string]string{"L": "info", "P": "/lib"}, map[string]string{"L": "debug"}, map[string]string{"L": "debug", "P": "/lib"}},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, mergeTemplateEnvVars(tc.tmpl, tc.user))
})
}
}

// the template map must not be mutated (it is shared template metadata).
func TestMergeTemplateEnvVars_DoesNotMutateTemplate(t *testing.T) {
t.Parallel()
tmpl := map[string]string{"P": "/lib"}
_ = mergeTemplateEnvVars(tmpl, map[string]string{"P": "/override", "K": "v"})
assert.Equal(t, map[string]string{"P": "/lib"}, tmpl, "template metadata map must be left unchanged")
}
8 changes: 8 additions & 0 deletions packages/orchestrator/pkg/server/sandboxes.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
return nil, fmt.Errorf("failed to read template metadata: %w", err)
}

// Propagate the template's Docker image ENV vars to the sandbox runtime.
// They form the base layer; user-provided env vars (already in config from
// the SDK create call) override them, and per-command env vars from
// process.start override both. Mutating config here reaches both the resume
// and the filesystem-only cold-boot branch below, since each sends
// config.Envd.Vars to envd via POST /init (newEnvdInitRequest).
config.Envd.Vars = mergeTemplateEnvVars(meta.Context.EnvVars, config.Envd.Vars)

fsOnly = meta.IsFilesystemOnly()
filesystemBooted = filesystemBoot(meta, req)
if filesystemBooted {
Expand Down