Skip to content

Correct generate kube on containers userns annotation #25948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 11 additions & 1 deletion libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,17 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
if !podmanOnly && define.IsReservedAnnotation(k) {
continue
}
kubeAnnotations[fmt.Sprintf("%s/%s", k, removeUnderscores(ctr.Name()))] = v
// Certain annotations should be applied to the whole pod.
// For others, add container name as a suffix.
// For annotations such as this, error if already set.
if k == define.UserNsAnnotation {
if oldV, ok := kubeAnnotations[k]; ok && oldV != v {
return nil, fmt.Errorf("two or more containers have differing user namespace configuration, cannot place in same Kubernetes pod: %w", define.ErrInvalidArg)
}
kubeAnnotations[k] = v
} else {
kubeAnnotations[fmt.Sprintf("%s/%s", k, removeUnderscores(ctr.Name()))] = v
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to play the grumpy old man yelling "keep off my grass" for a moment. The code is fine, but I detest one letter variables, especially so if they are used multiple times across more than one or two lines. I realize k and v were predefined in the code, but in instances like this, please consider expanding them to perhaps key and value, just to make them much easier to pick out of the code, and IMO, makes it an easier read/review.

I now return back to my recliner to get back to napping.

}

// Convert auto-update labels into kube annotations
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/generate_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,27 @@ var _ = Describe("Podman kube generate", func() {
Expect(kube).Should(ExitCleanly())
})

It("multiple containers with same user namespace configuration", func() {
name1 := "c1"
name2 := "c2"
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=30", "-dt", "--name", name1, ALPINE, "top")
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=30", "-dt", "--name", name2, ALPINE, "top")

gen := podmanTest.PodmanExitCleanly("kube", "generate", name1, name2)
Expect(gen.OutputToString()).To(ContainSubstring("io.podman.annotations.userns: auto:size=10"))
})

It("multiple containers with differing user namespace configuration", func() {
name1 := "c1"
name2 := "c2"
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=30", "-dt", "--name", name1, ALPINE, "top")
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=40", "-dt", "--name", name2, ALPINE, "top")

gen := podmanTest.Podman([]string{"kube", "generate", name1, name2})
gen.WaitWithDefaultTimeout()
Expect(gen).Should(ExitWithError(125, "two or more containers have differing user namespace configuration, cannot place in same Kubernetes pod: invalid argument"))
})

It("with containers in pods should fail", func() {
pod1 := podmanTest.Podman([]string{"run", "-dt", "--pod", "new:pod1", "--name", "top1", CITEST_IMAGE, "top"})
pod1.WaitWithDefaultTimeout()
Expand Down