Skip to content

Commit b3dd8b8

Browse files
authored
change fs_group, run_as_user, run_as_group to strings (#1093)
1 parent e4bb3bc commit b3dd8b8

File tree

4 files changed

+75
-40
lines changed

4 files changed

+75
-40
lines changed

kubernetes/schema_container.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -636,19 +636,21 @@ func securityContextSchema() *schema.Resource {
636636
Description: "Whether this container has a read-only root filesystem. Default is false.",
637637
},
638638
"run_as_group": {
639-
Type: schema.TypeInt,
640-
Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
641-
Optional: true,
639+
Type: schema.TypeString,
640+
Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
641+
Optional: true,
642+
ValidateFunc: validateTypeStringNullableInt,
642643
},
643644
"run_as_non_root": {
644645
Type: schema.TypeBool,
645646
Description: "Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
646647
Optional: true,
647648
},
648649
"run_as_user": {
649-
Type: schema.TypeInt,
650-
Description: "The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
651-
Optional: true,
650+
Type: schema.TypeString,
651+
Description: "The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
652+
Optional: true,
653+
ValidateFunc: validateTypeStringNullableInt,
652654
},
653655
"se_linux_options": {
654656
Type: schema.TypeList,

kubernetes/schema_pod_spec.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,27 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem
247247
Elem: &schema.Resource{
248248
Schema: map[string]*schema.Schema{
249249
"fs_group": {
250-
Type: schema.TypeInt,
251-
Description: "A special supplemental group that applies to all containers in a pod. Some volume types allow the Kubelet to change the ownership of that volume to be owned by the pod: 1. The owning GID will be the FSGroup 2. The setgid bit is set (new files created in the volume will be owned by FSGroup) 3. The permission bits are OR'd with rw-rw---- If unset, the Kubelet will not modify the ownership and permissions of any volume.",
252-
Optional: true,
250+
Type: schema.TypeString,
251+
Description: "A special supplemental group that applies to all containers in a pod. Some volume types allow the Kubelet to change the ownership of that volume to be owned by the pod: 1. The owning GID will be the FSGroup 2. The setgid bit is set (new files created in the volume will be owned by FSGroup) 3. The permission bits are OR'd with rw-rw---- If unset, the Kubelet will not modify the ownership and permissions of any volume.",
252+
Optional: true,
253+
ValidateFunc: validateTypeStringNullableInt,
253254
},
254255
"run_as_group": {
255-
Type: schema.TypeInt,
256-
Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.",
257-
Optional: true,
256+
Type: schema.TypeString,
257+
Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.",
258+
Optional: true,
259+
ValidateFunc: validateTypeStringNullableInt,
258260
},
259261
"run_as_non_root": {
260262
Type: schema.TypeBool,
261263
Description: "Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.",
262264
Optional: true,
263265
},
264266
"run_as_user": {
265-
Type: schema.TypeInt,
266-
Description: "The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.",
267-
Optional: true,
267+
Type: schema.TypeString,
268+
Description: "The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.",
269+
Optional: true,
270+
ValidateFunc: validateTypeStringNullableInt,
268271
},
269272
"se_linux_options": {
270273
Type: schema.TypeList,

kubernetes/structures_container.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package kubernetes
22

33
import (
4-
"k8s.io/api/core/v1"
4+
"strconv"
5+
6+
v1 "k8s.io/api/core/v1"
57
"k8s.io/apimachinery/pkg/api/resource"
68
"k8s.io/apimachinery/pkg/util/intstr"
79
)
@@ -30,13 +32,13 @@ func flattenContainerSecurityContext(in *v1.SecurityContext) []interface{} {
3032
att["read_only_root_filesystem"] = *in.ReadOnlyRootFilesystem
3133
}
3234
if in.RunAsGroup != nil {
33-
att["run_as_group"] = *in.RunAsGroup
35+
att["run_as_group"] = strconv.Itoa(int(*in.RunAsGroup))
3436
}
3537
if in.RunAsNonRoot != nil {
3638
att["run_as_non_root"] = *in.RunAsNonRoot
3739
}
3840
if in.RunAsUser != nil {
39-
att["run_as_user"] = *in.RunAsUser
41+
att["run_as_user"] = strconv.Itoa(int(*in.RunAsUser))
4042
}
4143
if in.SELinuxOptions != nil {
4244
att["se_linux_options"] = flattenSeLinuxOptions(in.SELinuxOptions)
@@ -504,7 +506,11 @@ func expandContainers(ctrs []interface{}) ([]v1.Container, error) {
504506
cs[i].TTY = v.(bool)
505507
}
506508
if v, ok := ctr["security_context"].([]interface{}); ok && len(v) > 0 {
507-
cs[i].SecurityContext = expandContainerSecurityContext(v)
509+
ctx, err := expandContainerSecurityContext(v)
510+
if err != nil {
511+
return cs, err
512+
}
513+
cs[i].SecurityContext = ctx
508514
}
509515

510516
if v, ok := ctr["volume_mount"].([]interface{}); ok && len(v) > 0 {
@@ -550,9 +556,9 @@ func expandHTTPHeaders(l []interface{}) []v1.HTTPHeader {
550556
}
551557
return headers
552558
}
553-
func expandContainerSecurityContext(l []interface{}) *v1.SecurityContext {
559+
func expandContainerSecurityContext(l []interface{}) (*v1.SecurityContext, error) {
554560
if len(l) == 0 || l[0] == nil {
555-
return &v1.SecurityContext{}
561+
return &v1.SecurityContext{}, nil
556562
}
557563
in := l[0].(map[string]interface{})
558564
obj := v1.SecurityContext{}
@@ -568,20 +574,28 @@ func expandContainerSecurityContext(l []interface{}) *v1.SecurityContext {
568574
if v, ok := in["read_only_root_filesystem"]; ok {
569575
obj.ReadOnlyRootFilesystem = ptrToBool(v.(bool))
570576
}
571-
if v, ok := in["run_as_group"]; ok {
572-
obj.RunAsGroup = ptrToInt64(int64(v.(int)))
577+
if v, ok := in["run_as_group"].(string); ok && v != "" {
578+
i, err := strconv.ParseInt(v, 10, 64)
579+
if err != nil {
580+
return &obj, err
581+
}
582+
obj.RunAsGroup = ptrToInt64(int64(i))
573583
}
574584
if v, ok := in["run_as_non_root"]; ok {
575585
obj.RunAsNonRoot = ptrToBool(v.(bool))
576586
}
577-
if v, ok := in["run_as_user"]; ok {
578-
obj.RunAsUser = ptrToInt64(int64(v.(int)))
587+
if v, ok := in["run_as_user"].(string); ok && v != "" {
588+
i, err := strconv.ParseInt(v, 10, 64)
589+
if err != nil {
590+
return &obj, err
591+
}
592+
obj.RunAsUser = ptrToInt64(int64(i))
579593
}
580594
if v, ok := in["se_linux_options"].([]interface{}); ok && len(v) > 0 {
581595
obj.SELinuxOptions = expandSeLinuxOptions(v)
582596
}
583597

584-
return &obj
598+
return &obj, nil
585599
}
586600

587601
func expandCapabilitySlice(s []interface{}) []v1.Capability {

kubernetes/structures_pod.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,16 @@ func flattenPodSecurityContext(in *v1.PodSecurityContext) []interface{} {
159159
att := make(map[string]interface{})
160160

161161
if in.FSGroup != nil {
162-
att["fs_group"] = *in.FSGroup
162+
att["fs_group"] = strconv.Itoa(int(*in.FSGroup))
163163
}
164164
if in.RunAsGroup != nil {
165-
att["run_as_group"] = *in.RunAsGroup
165+
att["run_as_group"] = strconv.Itoa(int(*in.RunAsGroup))
166166
}
167167
if in.RunAsNonRoot != nil {
168168
att["run_as_non_root"] = *in.RunAsNonRoot
169169
}
170170
if in.RunAsUser != nil {
171-
att["run_as_user"] = *in.RunAsUser
171+
att["run_as_user"] = strconv.Itoa(int(*in.RunAsUser))
172172
}
173173
if len(in.SupplementalGroups) > 0 {
174174
att["supplemental_groups"] = newInt64Set(schema.HashSchema(&schema.Schema{
@@ -682,7 +682,11 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) {
682682
}
683683

684684
if v, ok := in["security_context"].([]interface{}); ok && len(v) > 0 {
685-
obj.SecurityContext = expandPodSecurityContext(v)
685+
ctx, err := expandPodSecurityContext(v)
686+
if err != nil {
687+
return obj, err
688+
}
689+
obj.SecurityContext = ctx
686690
}
687691

688692
if v, ok := in["service_account_name"].(string); ok {
@@ -763,23 +767,35 @@ func expandDNSConfigOptions(options []interface{}) ([]v1.PodDNSConfigOption, err
763767
return opts, nil
764768
}
765769

766-
func expandPodSecurityContext(l []interface{}) *v1.PodSecurityContext {
770+
func expandPodSecurityContext(l []interface{}) (*v1.PodSecurityContext, error) {
771+
obj := &v1.PodSecurityContext{}
767772
if len(l) == 0 || l[0] == nil {
768-
return &v1.PodSecurityContext{}
773+
return obj, nil
769774
}
770775
in := l[0].(map[string]interface{})
771-
obj := &v1.PodSecurityContext{}
772-
if v, ok := in["fs_group"].(int); ok {
773-
obj.FSGroup = ptrToInt64(int64(v))
776+
if v, ok := in["fs_group"].(string); ok && v != "" {
777+
i, err := strconv.ParseInt(v, 10, 64)
778+
if err != nil {
779+
return obj, err
780+
}
781+
obj.FSGroup = ptrToInt64(int64(i))
774782
}
775-
if v, ok := in["run_as_group"].(int); ok {
776-
obj.RunAsGroup = ptrToInt64(int64(v))
783+
if v, ok := in["run_as_group"].(string); ok && v != "" {
784+
i, err := strconv.ParseInt(v, 10, 64)
785+
if err != nil {
786+
return obj, err
787+
}
788+
obj.RunAsGroup = ptrToInt64(int64(i))
777789
}
778790
if v, ok := in["run_as_non_root"].(bool); ok {
779791
obj.RunAsNonRoot = ptrToBool(v)
780792
}
781-
if v, ok := in["run_as_user"].(int); ok {
782-
obj.RunAsUser = ptrToInt64(int64(v))
793+
if v, ok := in["run_as_user"].(string); ok && v != "" {
794+
i, err := strconv.ParseInt(v, 10, 64)
795+
if err != nil {
796+
return obj, err
797+
}
798+
obj.RunAsUser = ptrToInt64(int64(i))
783799
}
784800
if v, ok := in["se_linux_options"].([]interface{}); ok && len(v) > 0 {
785801
obj.SELinuxOptions = expandSeLinuxOptions(v)
@@ -791,7 +807,7 @@ func expandPodSecurityContext(l []interface{}) *v1.PodSecurityContext {
791807
obj.Sysctls = expandSysctls(v)
792808
}
793809

794-
return obj
810+
return obj, nil
795811
}
796812

797813
func expandSysctls(l []interface{}) []v1.Sysctl {

0 commit comments

Comments
 (0)