Skip to content
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

Add Capability support to play kube #3692

Merged
merged 2 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions libpod/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,26 @@ func determineCapAddDropFromCapabilities(defaultCaps, containerCaps []string) *v
drop []v1.Capability
add []v1.Capability
)
dedupDrop := make(map[string]bool)
dedupAdd := make(map[string]bool)
// Find caps in the defaultCaps but not in the container's
// those indicate a dropped cap
for _, capability := range defaultCaps {
if !util.StringInSlice(capability, containerCaps) {
drop = append(drop, v1.Capability(capability))
if _, ok := dedupDrop[capability]; !ok {
drop = append(drop, v1.Capability(capability))
dedupDrop[capability] = true
}
}
}
// Find caps in the container but not in the defaults; those indicate
// an added cap
for _, capability := range containerCaps {
if !util.StringInSlice(capability, defaultCaps) {
add = append(add, v1.Capability(capability))
if _, ok := dedupAdd[capability]; !ok {
add = append(add, v1.Capability(capability))
dedupAdd[capability] = true
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/adapter/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,15 @@ func kubeContainerToCreateConfig(ctx context.Context, containerYAML v1.Container
if containerYAML.SecurityContext.AllowPrivilegeEscalation != nil {
containerConfig.NoNewPrivs = !*containerYAML.SecurityContext.AllowPrivilegeEscalation
}

}
if caps := containerYAML.SecurityContext.Capabilities; caps != nil {
for _, capability := range caps.Add {
Copy link
Member

Choose a reason for hiding this comment

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

I think lint is going to want this changed to containerConfig.CapAdd = append(containerConfig.CapAdd, caps.Add...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

they shouldn't be compatible types though

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh, yeah the conversion should break that one.

containerConfig.CapAdd = append(containerConfig.CapAdd, string(capability))
}
for _, capability := range caps.Drop {
containerConfig.CapDrop = append(containerConfig.CapDrop, string(capability))
}
}

containerConfig.Command = []string{}
Expand Down
75 changes: 67 additions & 8 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ spec:
{{ with .Containers }}
{{ range . }}
- command:
- {{ .Cmd }}
{{ range .Cmd }}
- {{.}}
{{ end }}
env:
- name: PATH
value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Expand All @@ -39,7 +41,21 @@ spec:
resources: {}
securityContext:
allowPrivilegeEscalation: true
capabilities: {}
{{ if .Caps }}
capabilities:
{{ with .CapAdd }}
add:
{{ range . }}
- {{.}}
{{ end }}
{{ end }}
{{ with .CapDrop }}
drop:
{{ range . }}
- {{.}}
{{ end }}
{{ end }}
{{ end }}
privileged: false
readOnlyRootFilesystem: false
workingDir: /
Expand All @@ -54,9 +70,12 @@ type Pod struct {
}

type Container struct {
Cmd string
Image string
Name string
Cmd []string
Image string
Name string
Caps bool
CapAdd []string
CapDrop []string
}

func generateKubeYaml(ctrs []Container, fileName string) error {
Expand Down Expand Up @@ -104,8 +123,8 @@ var _ = Describe("Podman generate kube", func() {

It("podman play kube test correct command", func() {
ctrName := "testCtr"
ctrCmd := "top"
testContainer := Container{ctrCmd, ALPINE, ctrName}
ctrCmd := []string{"top"}
testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")

err := generateKubeYaml([]Container{testContainer}, tempFile)
Expand All @@ -118,6 +137,46 @@ var _ = Describe("Podman generate kube", func() {
inspect := podmanTest.Podman([]string{"inspect", ctrName})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(ContainSubstring(ctrCmd))
Expect(inspect.OutputToString()).To(ContainSubstring(ctrCmd[0]))
})

It("podman play kube cap add", func() {
ctrName := "testCtr"
ctrCmd := []string{"cat", "/proc/self/status"}
capAdd := "CAP_SYS_ADMIN"
testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capAdd}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")

err := generateKubeYaml([]Container{testContainer}, tempFile)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", tempFile})
kube.WaitWithDefaultTimeout()
Expect(kube.ExitCode()).To(Equal(0))

inspect := podmanTest.Podman([]string{"inspect", ctrName})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(ContainSubstring(capAdd))
})

It("podman play kube cap add", func() {
ctrName := "testCtr"
ctrCmd := []string{"cat", "/proc/self/status"}
capDrop := "CAP_SYS_ADMIN"
testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capDrop}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")

err := generateKubeYaml([]Container{testContainer}, tempFile)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", tempFile})
kube.WaitWithDefaultTimeout()
Expect(kube.ExitCode()).To(Equal(0))

inspect := podmanTest.Podman([]string{"inspect", ctrName})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(ContainSubstring(capDrop))
})
})