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

Fix 1324: Add fs_group_change_policy for the pod security_context #1451

Closed
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
61 changes: 61 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,39 @@ func TestAccKubernetesPod_with_pod_security_context_run_as_group(t *testing.T) {
})
}

func TestAccKubernetesPod_with_pod_security_context_fs_group_change_policy(t *testing.T) {
var conf api.Pod

podName := acctest.RandomWithPrefix("tf-acc-test")
imageName := nginxImageVersion
resourceName := "kubernetes_pod.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfUnsupportedSecurityContextRunAsGroup(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesPodDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesPodConfigWithSecurityContextFSChangePolicy(podName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.fs_group", "100"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_group", "100"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_non_root", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_user", "101"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.fs_group_change_policy", "OnRootMismatch"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
},
})
}

func TestAccKubernetesPod_with_container_liveness_probe_using_exec(t *testing.T) {
var conf api.Pod

Expand Down Expand Up @@ -1508,6 +1541,34 @@ func testAccKubernetesPodConfigWithSecurityContextRunAsGroup(podName, imageName
`, podName, imageName)
}

func testAccKubernetesPodConfigWithSecurityContextFSChangePolicy(podName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_pod" "test" {
metadata {
labels = {
app = "pod_label"
}

name = "%s"
}

spec {
security_context {
fs_group = 100
run_as_group = 100
run_as_non_root = true
run_as_user = 101
fs_group_change_policy = "OnRootMismatch"
}

container {
image = "%s"
name = "containername"
}
}
}
`, podName, imageName)
}

func testAccKubernetesPodConfigWithLivenessProbeUsingExec(podName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_pod" "test" {
metadata {
Expand Down
10 changes: 10 additions & 0 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ func podSpecFields(isUpdatable, isComputed bool) map[string]*schema.Schema {
Schema: seLinuxOptionsField(isUpdatable),
},
},
"fs_group_change_policy": {
Type: schema.TypeString,
Description: "fsGroupChangePolicy defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(api.FSGroupChangeAlways),
string(api.FSGroupChangeOnRootMismatch),
}, false),
ForceNew: !isUpdatable,
},
"supplemental_groups": {
Type: schema.TypeSet,
Description: "A list of groups applied to the first process run in each container, in addition to the container's primary GID. If unspecified, no groups will be added to any container.",
Expand Down
8 changes: 7 additions & 1 deletion kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func flattenPodSecurityContext(in *v1.PodSecurityContext) []interface{} {
if in.RunAsUser != nil {
att["run_as_user"] = strconv.Itoa(int(*in.RunAsUser))
}
if in.FSGroupChangePolicy != nil {
att["fs_group_change_policy"] = *in.FSGroupChangePolicy
}
if len(in.SupplementalGroups) > 0 {
att["supplemental_groups"] = newInt64Set(schema.HashSchema(&schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -877,7 +880,10 @@ func expandPodSecurityContext(l []interface{}) (*v1.PodSecurityContext, error) {
if v, ok := in["sysctl"].([]interface{}); ok && len(v) > 0 {
obj.Sysctls = expandSysctls(v)
}

if v, ok := in["fs_group_change_policy"].(string); ok && v != "" {
policy := v1.PodFSGroupChangePolicy(v)
obj.FSGroupChangePolicy = &policy
}
return obj, nil
}

Expand Down