Repository navigation
cri: fix duplicated image env vars on checkpoint import - #13623
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes an environment variable duplication bug during CRI checkpoint import by correctly merging image and container env vars without re-appending the image env list, and adds a Linux unit test to prevent regressions.
Changes:
- Replace the env-building logic in
CRImportCheckpointwith a dedicatedmergeContainerEnvshelper to avoid duplicating image env vars. - Add
mergeContainerEnvshelper to centralize and clarify env merge behavior (image envs first, CRI envs appended). - Add a Linux unit test validating ordering and no-duplication regression for checkpoint import env merging.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/cri/server/container_checkpoint_linux.go | Fix env merge logic during checkpoint import and introduce mergeContainerEnvs helper. |
| internal/cri/server/container_checkpoint_linux_test.go | Add unit test covering env ordering and the no-duplication regression. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // mergeContainerEnvs returns the image's environment with the CRI container's | ||
| // environment variables appended. The image variables appear once, followed by | ||
| // the container variables; the image variables are not duplicated. |
| // The input image slice must not be mutated. | ||
| if len(imageEnv) != 2 { | ||
| t.Errorf("imageEnv was mutated, len = %d, want 2", len(imageEnv)) | ||
| } |
bd09ab9 to
1f86afa
Compare
|
Thanks, both good points and applied in the latest push:
|
| func mergeContainerEnvs(imageEnv []string, criEnvs []*runtime.KeyValue) []string { | ||
| env := append([]string{}, imageEnv...) | ||
| for _, e := range criEnvs { | ||
| env = append(env, e.GetKey()+"="+e.GetValue()) | ||
| } | ||
| return env | ||
| } |
There was a problem hiding this comment.
can we just fix this in place? i don't feel it worths a separate function TBH.
There was a problem hiding this comment.
Good call, done. Simplified to the in-place fix (assign the combined slice to imageConfig.Env directly) and dropped the helper and its test.
CRImportCheckpoint built the combined environment (image env followed by the CRI container env) into a local slice, then re-appended that slice to imageConfig.Env, duplicating every image environment variable on the restored container. Assign the combined slice directly instead. Fixes containerd#13611 Signed-off-by: Aman Raj <[email protected]>
1f86afa to
6a677e0
Compare
| imageConfig := image.ImageSpec.Config | ||
| env := append([]string{}, imageConfig.Env...) | ||
| for _, e := range meta.Config.GetEnvs() { | ||
| env = append(env, e.GetKey()+"="+e.GetValue()) | ||
| } | ||
| imageConfig.Env = append(imageConfig.Env, env...) | ||
| imageConfig.Env = env |
CRImportCheckpointbuilt the combined environment (image env followed by the CRI container env) into a local slice, then re-appended that slice toimageConfig.Env, duplicating every image environment variable on the restored container.This assigns the combined slice directly instead.
Fixes #13611