Skip to content

Commit cb24660

Browse files
Merge pull request #25645 from jankaluza/24418
Add support for --pids-limit in podman kube play.
2 parents f5ab9d1 + f15b088 commit cb24660

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

docs/source/markdown/podman-kube-play.1.md.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ by `podman kube play` to create them.
5555

5656
Note: To customize the name of the infra container created during `podman kube play`, use the **io.podman.annotations.infra.name** annotation in the pod definition. This annotation is automatically set when generating a kube yaml from a pod that was created with the `--infra-name` flag set.
5757

58+
Note: Use the **io.podman.annotations.pids-limit/$ctrname** annotation to configure the pod's pids limit.
59+
5860
`Kubernetes PersistentVolumeClaims`
5961

6062
A Kubernetes PersistentVolumeClaim represents a Podman named volume. Only the PersistentVolumeClaim name is required by Podman to create a volume. Kubernetes annotations can be used to make use of the available options for Podman volumes.

libpod/define/annotations.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ const (
169169
// KubeImageAutomountAnnotation
170170
KubeImageAutomountAnnotation = "io.podman.annotations.kube.image.volumes.mount"
171171

172+
// PIDsLimitAnnotation is used to limit the number of PIDs
173+
PIDsLimitAnnotation = "io.podman.annotations.pids-limit"
174+
172175
// TotalAnnotationSizeLimitB is the max length of annotations allowed by Kubernetes.
173176
TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB
174177
)

pkg/specgen/generate/kube/kube.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
375375
s.Annotations[define.InspectAnnotationApparmor] = apparmor
376376
}
377377

378+
if pidslimit, ok := annotations[define.PIDsLimitAnnotation+"/"+opts.Container.Name]; ok {
379+
s.Annotations[define.PIDsLimitAnnotation] = pidslimit
380+
pidslimitAsInt, err := strconv.ParseInt(pidslimit, 10, 0)
381+
if err != nil {
382+
return nil, err
383+
}
384+
if s.ResourceLimits == nil {
385+
s.ResourceLimits = &spec.LinuxResources{}
386+
}
387+
s.ResourceLimits.Pids = &spec.LinuxPids{
388+
Limit: pidslimitAsInt,
389+
}
390+
}
391+
378392
if label, ok := opts.Annotations[define.InspectAnnotationLabel+"/"+opts.Container.Name]; ok {
379393
if label == "nested" {
380394
s.ContainerSecurityConfig.LabelNested = &localTrue

pkg/specgenutil/specgen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
527527
s.Annotations[define.UserNsAnnotation] = c.UserNS
528528
}
529529

530+
if c.PIDsLimit != nil {
531+
s.Annotations[define.PIDsLimitAnnotation] = strconv.FormatInt(*c.PIDsLimit, 10)
532+
}
533+
530534
if len(c.StorageOpts) > 0 {
531535
opts := make(map[string]string, len(c.StorageOpts))
532536
for _, opt := range c.StorageOpts {

test/e2e/play_kube_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6180,4 +6180,18 @@ spec:
61806180
Expect(execArr[len(execArr)-1]).To(Not(ContainSubstring(arr[len(arr)-1])))
61816181
})
61826182

6183+
It("test pids-limit annotation", func() {
6184+
ctrAnnotation := "io.podman.annotations.pids-limit/" + defaultCtrName
6185+
pod := getPod(withAnnotation(ctrAnnotation, "10"), withPodInitCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"printenv", "container"}), withInitCtr(), withName("init-test"))), withCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"top"}))))
6186+
err := generateKubeYaml("pod", pod, kubeYaml)
6187+
Expect(err).ToNot(HaveOccurred())
6188+
6189+
kube := podmanTest.Podman([]string{"kube", "play", kubeYaml})
6190+
kube.WaitWithDefaultTimeout()
6191+
Expect(kube).Should(ExitCleanly())
6192+
6193+
exec := podmanTest.PodmanExitCleanly("exec", "testPod-"+defaultCtrName, "cat", "/sys/fs/cgroup/pids.max")
6194+
Expect(exec.OutputToString()).To(Equal("10"))
6195+
})
6196+
61836197
})

0 commit comments

Comments
 (0)