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

Conversation

mheon
Copy link
Member

@mheon mheon commented Apr 22, 2025

The podman generate kube command on containers follows a different codepath from pods. Pods store a lot of pod-level configuration - including user namespace information - in annotations, so it can be restored by play kube. Generating for a container does not do the same thing, because we don't have a pod.

However, per-container generation was still generating a nearly identical user namespace annotation to a pod. Example:

In Pod:
io.podman.annotations.userns: auto:size=40
Not in Pod:
io.podman.annotations.userns/awesomegreider: auto:size=2048

The second annotation seems like it should apply a user namespace config to the generated Kubernetes pod. Instead, it's just adding an annotation to the awesomegreider container, that says said container has a user namespace, when it does not in fact have a user namespace configured because it is now in a pod.

After this PR, both containers in and out of pods generate identical annotations (the In Pod version, missing container name) and as such should generate pods with appropriately configured user namespaces. I also added some conflict detection to refuse to generate if you try to generate YAML containing two containers with conflicting user namespace configuration.

Fixes #25896

Does this PR introduce a user-facing change?

Fixed a bug where the `podman generate kube` command created incorrect user namespace annotations when run on containers that are not part of a pod ([#25896](https://github.com/containers/podman/issues/25896)).

Copy link
Contributor

openshift-ci bot commented Apr 22, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mheon

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 22, 2025
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

I think this also needs a positive test where you make sure the generated annotation looks as expected.

Comment on lines 1031 to 1032
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=10", "-dt", "--name", name1, ALPINE, "top")
_ = podmanTest.PodmanExitCleanly("run", "--userns", "auto:size=10", "-dt", "--name", name2, ALPINE, "top")
Copy link
Member

Choose a reason for hiding this comment

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

They have the exact same userns option so I would expect that to work and just use that one?! I agree that it should error if they are actually different.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough, I'll check if the annotations are identical and allow if so

@baude
Copy link
Member

baude commented Apr 22, 2025

I really like when the issue is cited in the code or test. I think @Luap99 is quite good at this. It helps us remember why we did things, etc.

In addition to Pauls comment for the positive test, LGTM

thanks @mheon

The `podman generate kube` command on containers follows a
different codepath from pods. Pods store a lot of pod-level
configuration - including user namespace information - in
annotations, so it can be restored by `play kube`. Generating for
a container does not do the same thing, because we don't have a
pod.

However, per-container generation was still generating a nearly
identical user namespace annotation to a pod. Example:

In Pod:
  io.podman.annotations.userns: auto:size=40
Not in Pod:
  io.podman.annotations.userns/awesomegreider: auto:size=2048

The second annotation seems like it should apply a user namespace
config to the generated Kubernetes pod. Instead, it's just adding
an annotation to the awesomegreider container, that says said
container has a user namespace, when it does not in fact have a
user namespace configured because it is now in a pod.

After this PR, both containers in and out of pods generate
identical annotations (the In Pod version, missing container
name) and as such should generate pods with appropriately
configured user namespaces. I also added some conflict detection
to refuse to generate if you try to generate YAML containing two
containers with conflicting user namespace configuration.

Fixes containers#25896

Signed-off-by: Matt Heon <[email protected]>
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.

@TomSweeneyRedHat
Copy link
Member

LGTM
but a bunch of test unhappiness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

podman kube play does not respect size= attribute to io.podman.annotations.userns annotation
4 participants