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

r/pod+rc: Avoid crash in reading container.security_context capability #53

Merged
merged 1 commit into from
Aug 22, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func TestAccKubernetesPod_with_pod_security_context(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_non_root", "true"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_user", "101"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.supplemental_groups.#", "1"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.supplemental_groups.988695518", "101"),
),
},
},
Expand Down Expand Up @@ -286,6 +288,14 @@ func TestAccKubernetesPod_with_container_security_context(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.#", "1"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.privileged", "true"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.run_as_user", "1"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.se_linux_options.#", "1"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.se_linux_options.0.level", "s0:c123,c456"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.capabilities.#", "1"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.capabilities.0.add.#", "2"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.capabilities.0.add.0", "NET_ADMIN"),
resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.container.0.security_context.0.capabilities.0.add.1", "SYS_TIME"),
),
},
},
Expand Down Expand Up @@ -752,13 +762,14 @@ resource "kubernetes_pod" "test" {
se_linux_options {
level = "s0:c123,c456"
}
capabilities {
add = ["NET_ADMIN", "SYS_TIME"]
}
}
}
}
}


`, podName, imageName)
`, podName, imageName)
}

func testAccKubernetesPodConfigWithVolumeMounts(secretName, podName, imageName string) string {
Expand Down
4 changes: 2 additions & 2 deletions kubernetes/structures_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func flattenCapability(in []v1.Capability) []string {
att := make([]string, 0, len(in))
att := make([]string, len(in), len(in))
Copy link
Contributor

Choose a reason for hiding this comment

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

I may be wrong, but I don't think this is needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is needed, because direct access via [i] requires elements to be already pre-allocated.

It wouldn't be necessary if we used append(), but that's unnecessary since we know precise length of the array. I would only use append when I don't know how many elements I'm going to have as it allocates and re-allocates the memory for you (i.e. possibly more than you really need).

https://play.golang.org/p/s9YliIhbQy
https://blog.golang.org/go-slices-usage-and-internals

for i, v := range in {
att[i] = string(v)
}
Expand Down Expand Up @@ -510,7 +510,7 @@ func expandContainerSecurityContext(l []interface{}) *v1.SecurityContext {
func expandCapabilitySlice(s []interface{}) []v1.Capability {
result := make([]v1.Capability, len(s), len(s))
for k, v := range s {
result[k] = v.(v1.Capability)
result[k] = v1.Capability(v.(string))
}
return result
}
Expand Down