Skip to content

Commit e1b01ed

Browse files
committed
kube: alias OCI runtime annotations without underscores
Fixes: #26871 Signed-off-by: Jiwoo Ahn <ikwydls1314@gmail.com>
1 parent 1fecb62 commit e1b01ed

4 files changed

Lines changed: 77 additions & 3 deletions

File tree

libpod/define/annotations.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const (
77
// RunOCIKeepOriginalGroups tells the OCI runtime to leak the users
88
// current groups into the container
99
RunOCIKeepOriginalGroups = "run.oci.keep_original_groups"
10+
// KubeMountContextTypeAnnotation is the Kubernetes-safe annotation used to
11+
// round-trip RunOCIMountContextType through kube generate/play.
12+
KubeMountContextTypeAnnotation = "io.podman.annotations.mount-context-type"
13+
// KubeKeepOriginalGroupsAnnotation is the Kubernetes-safe annotation used to
14+
// round-trip RunOCIKeepOriginalGroups through kube generate/play.
15+
KubeKeepOriginalGroupsAnnotation = "io.podman.annotations.keep-original-groups"
1016
// InspectAnnotationCIDFile is used by Inspect to determine if a
1117
// container ID file was created for the container.
1218
// If an annotation with this key is found in the OCI spec, it will be
@@ -186,7 +192,7 @@ const (
186192
// already reserved annotation that Podman sets during container creation.
187193
func IsReservedAnnotation(value string) bool {
188194
switch value {
189-
case InspectAnnotationCIDFile, InspectAnnotationAutoremove, InspectAnnotationPrivileged, InspectAnnotationPublishAll, InspectAnnotationInit, InspectAnnotationLabel, InspectAnnotationSeccomp, InspectAnnotationApparmor, InspectResponseTrue, InspectResponseFalse, VolumesFromAnnotation:
195+
case InspectAnnotationCIDFile, InspectAnnotationAutoremove, InspectAnnotationPrivileged, InspectAnnotationPublishAll, InspectAnnotationInit, InspectAnnotationLabel, InspectAnnotationSeccomp, InspectAnnotationApparmor, InspectResponseTrue, InspectResponseFalse, VolumesFromAnnotation, KubeMountContextTypeAnnotation, KubeKeepOriginalGroupsAnnotation:
190196
return true
191197

192198
default:

libpod/kube.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
618618
if !podmanOnly && (define.IsReservedAnnotation(k)) {
619619
continue
620620
}
621-
podAnnotations[fmt.Sprintf("%s/%s", k, removeUnderscores(ctr.Name()))] = v
621+
podAnnotations[fmt.Sprintf("%s/%s", kubeAnnotationAlias(k), removeUnderscores(ctr.Name()))] = v
622622
}
623623
// Convert auto-update labels into kube annotations
624624
maps.Copy(podAnnotations, getAutoUpdateAnnotations(ctr.Name(), ctr.Labels()))
@@ -764,7 +764,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getServic
764764
if !podmanOnly && define.IsReservedAnnotation(k) {
765765
continue
766766
}
767-
kubeAnnotations[fmt.Sprintf("%s/%s", k, removeUnderscores(ctr.Name()))] = v
767+
kubeAnnotations[fmt.Sprintf("%s/%s", kubeAnnotationAlias(k), removeUnderscores(ctr.Name()))] = v
768768
}
769769

770770
// Convert auto-update labels into kube annotations
@@ -1449,6 +1449,18 @@ func generateKubeVolumeDeviceFromLinuxDevice(devices []specs.LinuxDevice) []v1.V
14491449
return volumeDevices
14501450
}
14511451

1452+
var ociRuntimeToKubeAnnotations = map[string]string{
1453+
define.RunOCIKeepOriginalGroups: define.KubeKeepOriginalGroupsAnnotation,
1454+
define.RunOCIMountContextType: define.KubeMountContextTypeAnnotation,
1455+
}
1456+
1457+
func kubeAnnotationAlias(annotationKey string) string {
1458+
if alias, ok := ociRuntimeToKubeAnnotations[annotationKey]; ok {
1459+
return alias
1460+
}
1461+
return annotationKey
1462+
}
1463+
14521464
func removeUnderscores(s string) string {
14531465
return strings.ReplaceAll(s, "_", "")
14541466
}

pkg/specgen/generate/kube/kube.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ import (
4242
cdiparser "tags.cncf.io/container-device-interface/pkg/parser"
4343
)
4444

45+
var kubeToOCIRuntimeAnnotations = map[string]string{
46+
define.KubeKeepOriginalGroupsAnnotation: define.RunOCIKeepOriginalGroups,
47+
define.KubeMountContextTypeAnnotation: define.RunOCIMountContextType,
48+
}
49+
50+
func restoreKubeAnnotationAliases(annotations map[string]string, containerName string) {
51+
for kubeAnnotation, runtimeAnnotation := range kubeToOCIRuntimeAnnotations {
52+
key := kubeAnnotation + "/" + containerName
53+
if value, ok := annotations[key]; ok {
54+
annotations[runtimeAnnotation] = value
55+
delete(annotations, key)
56+
}
57+
}
58+
}
59+
4560
func ToPodOpt(_ context.Context, podName string, p entities.PodCreateOptions, publishAllPorts bool, podYAML *v1.PodTemplateSpec) (entities.PodCreateOptions, error) {
4661
p.Net = &entities.NetOptions{NoHosts: p.Net.NoHosts, NoHostname: p.Net.NoHostname}
4762

@@ -377,6 +392,7 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
377392
annotations[ann.SandboxID] = opts.PodInfraID
378393
}
379394
s.Annotations = annotations
395+
restoreKubeAnnotationAliases(s.Annotations, opts.Container.Name)
380396

381397
if containerCIDFile, ok := opts.Annotations[define.InspectAnnotationCIDFile+"/"+opts.Container.Name]; ok {
382398
s.Annotations[define.InspectAnnotationCIDFile] = containerCIDFile

test/e2e/play_kube_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,46 @@ spec:
60926092
Expect(kube).Should(ExitWithError(125, "annotation "+define.VolumesFromAnnotation+" without target volume is reserved for internal use"))
60936093
})
60946094

6095+
It("test with keep-groups OCI annotation round trip", func() {
6096+
SkipIfRemote("the --group-add keep-groups option is not supported in remote mode")
6097+
6098+
ctr := "ctr-keep-groups"
6099+
ctrNameInKubePod := ctr + "-pod-" + ctr
6100+
outputFile := filepath.Join(podmanTest.TempDir, "pod.yaml")
6101+
kubeAnnotation := define.KubeKeepOriginalGroupsAnnotation + "/" + ctr
6102+
6103+
podmanTest.PodmanExitCleanly("create", "--name", ctr, "--group-add", "keep-groups", CITEST_IMAGE, "top")
6104+
podmanTest.PodmanExitCleanly("kube", "generate", "-f", outputFile, ctr)
6105+
6106+
kubeYaml, err := os.ReadFile(outputFile)
6107+
Expect(err).ToNot(HaveOccurred())
6108+
Expect(kubeYaml).ToNot(ContainSubstring(define.RunOCIKeepOriginalGroups + "/" + ctr))
6109+
Expect(kubeYaml).To(ContainSubstring(kubeAnnotation + `: "1"`))
6110+
6111+
podmanTest.PodmanExitCleanly("kube", "play", "--start=false", outputFile)
6112+
inspect := podmanTest.PodmanExitCleanly("inspect", "-f", "{{ index .Config.Annotations \""+define.RunOCIKeepOriginalGroups+"\" }}", ctrNameInKubePod)
6113+
Expect(inspect.OutputToString()).To(Equal("1"))
6114+
})
6115+
6116+
It("test with mount-context-type OCI annotation round trip", func() {
6117+
ctr := "ctr-mount-context"
6118+
ctrNameInKubePod := ctr + "-pod-" + ctr
6119+
outputFile := filepath.Join(podmanTest.TempDir, "pod.yaml")
6120+
kubeAnnotation := define.KubeMountContextTypeAnnotation + "/" + ctr
6121+
6122+
podmanTest.PodmanExitCleanly("create", "--name", ctr, "--annotation", define.RunOCIMountContextType+"=rootcontext", CITEST_IMAGE, "top")
6123+
podmanTest.PodmanExitCleanly("kube", "generate", "-f", outputFile, ctr)
6124+
6125+
kubeYaml, err := os.ReadFile(outputFile)
6126+
Expect(err).ToNot(HaveOccurred())
6127+
Expect(kubeYaml).ToNot(ContainSubstring(define.RunOCIMountContextType + "/" + ctr))
6128+
Expect(kubeYaml).To(ContainSubstring(kubeAnnotation + `: rootcontext`))
6129+
6130+
podmanTest.PodmanExitCleanly("kube", "play", "--start=false", outputFile)
6131+
inspect := podmanTest.PodmanExitCleanly("inspect", "-f", "{{ index .Config.Annotations \""+define.RunOCIMountContextType+"\" }}", ctrNameInKubePod)
6132+
Expect(inspect.OutputToString()).To(Equal("rootcontext"))
6133+
})
6134+
60956135
It("test with reserved autoremove annotation in yaml", func() {
60966136
ctr := "ctr"
60976137
ctrNameInKubePod := ctr + "-pod-" + ctr

0 commit comments

Comments
 (0)