Skip to content

Commit d7cfb2c

Browse files
authored
Add os tests (#2307)
* initial schema * Add expandWindowsSecurityContext Func * add flattener * fix os attribute and tests * add host_process * fix testconfig * remove duplicates * WIP: marked as required despite being optional * add check for os value * update tests and expandOS function
1 parent 0cdc945 commit d7cfb2c

File tree

2 files changed

+100
-16
lines changed

2 files changed

+100
-16
lines changed

kubernetes/schema_pod_spec.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,37 @@ func podSpecFields(isUpdatable, isComputed bool) map[string]*schema.Schema {
346346
Type: schema.TypeInt,
347347
},
348348
},
349+
"windows_options": {
350+
Type: schema.TypeList,
351+
MaxItems: 1,
352+
Description: "The Windows specific settings applied to all containers. If unspecified, the options within a container's SecurityContext will be used. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. Note that this field cannot be set when spec.os.name is linux.",
353+
Optional: true,
354+
Elem: &schema.Resource{
355+
Schema: map[string]*schema.Schema{
356+
"gmsa_credential_spec": {
357+
Type: schema.TypeString,
358+
Description: "GMSACredentialSpec is where the GMSA admission webhook inlines the contents of the GMSA credential spec named by the GMSACredentialSpecName field",
359+
Optional: true,
360+
},
361+
"gmsa_credential_spec_name": {
362+
Type: schema.TypeString,
363+
Description: "GMSACredentialSpecName is the name of the GMSA credential spec to use.",
364+
Optional: true,
365+
},
366+
"host_process": {
367+
Type: schema.TypeBool,
368+
Description: "HostProcess determines if a container should be run as a 'Host Process' container. Default value is false.",
369+
Default: false,
370+
Optional: true,
371+
},
372+
"run_as_username": {
373+
Type: schema.TypeString,
374+
Description: "The UserName in Windows to run the entrypoint of the container process. Defaults to the 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.",
375+
Optional: true,
376+
},
377+
},
378+
},
379+
},
349380
"sysctl": {
350381
Type: schema.TypeList,
351382
Optional: true,

kubernetes/structures_pod.go

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ func flattenPodSecurityContext(in *v1.PodSecurityContext) []interface{} {
250250
att["sysctl"] = flattenSysctls(in.Sysctls)
251251
}
252252

253+
if in.WindowsOptions != nil {
254+
att["windows_options"] = flattenWindowsOptions(*in.WindowsOptions)
255+
}
256+
253257
if len(att) > 0 {
254258
return []interface{}{att}
255259
}
@@ -706,21 +710,6 @@ func flattenPodEphemeralVolumeSource(in *v1.EphemeralVolumeSource) []interface{}
706710

707711
// Expanders
708712

709-
func expandOS(l []interface{}) *v1.PodOS {
710-
if len(l) == 0 || l[0] == nil {
711-
return nil
712-
}
713-
714-
in := l[0].(map[string]interface{})
715-
obj := &v1.PodOS{}
716-
717-
if v, ok := in["name"].(string); ok {
718-
obj.Name = v1.OSName(v)
719-
}
720-
721-
return obj
722-
}
723-
724713
func expandPodTargetState(p []interface{}) []string {
725714
if len(p) > 0 {
726715
t := make([]string, len(p))
@@ -839,7 +828,7 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) {
839828
obj.NodeSelector = nodeSelectors
840829
}
841830

842-
if v, ok := in["os"].([]interface{}); ok {
831+
if v, ok := in["os"].([]interface{}); ok && len(v) != 0 {
843832
obj.OS = expandOS(v)
844833
}
845834

@@ -914,6 +903,67 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) {
914903
return obj, nil
915904
}
916905

906+
func expandOS(l []interface{}) *v1.PodOS {
907+
if len(l) == 0 || l[0] == nil {
908+
return nil
909+
}
910+
911+
in := l[0].(map[string]interface{})
912+
913+
return &v1.PodOS{
914+
Name: v1.OSName(in["name"].(string)),
915+
}
916+
}
917+
918+
func expandWindowsOptions(l []interface{}) *v1.WindowsSecurityContextOptions {
919+
if len(l) == 0 || l[0] == nil {
920+
return &v1.WindowsSecurityContextOptions{}
921+
}
922+
923+
in := l[0].(map[string]interface{})
924+
obj := &v1.WindowsSecurityContextOptions{}
925+
926+
if v, ok := in["gmsa_credential_spec"].(string); ok {
927+
obj.GMSACredentialSpec = ptrToString(v)
928+
}
929+
930+
if v, ok := in["host_process"].(bool); ok {
931+
obj.HostProcess = ptrToBool(v)
932+
}
933+
934+
if v, ok := in["gmsa_credential_spec_name"].(string); ok {
935+
obj.GMSACredentialSpecName = ptrToString(v)
936+
}
937+
938+
if v, ok := in["run_as_username"].(string); ok {
939+
obj.RunAsUserName = ptrToString(v)
940+
}
941+
942+
return obj
943+
}
944+
945+
func flattenWindowsOptions(in v1.WindowsSecurityContextOptions) []interface{} {
946+
att := make(map[string]interface{})
947+
948+
if in.GMSACredentialSpec != nil {
949+
att["gmsa_credential_spec"] = *in.GMSACredentialSpec
950+
}
951+
952+
if in.GMSACredentialSpecName != nil {
953+
att["gmsa_credential_spec_name"] = *in.GMSACredentialSpecName
954+
}
955+
956+
if in.HostProcess != nil {
957+
att["host_process"] = *in.HostProcess
958+
}
959+
960+
if in.RunAsUserName != nil {
961+
att["run_as_username"] = *in.RunAsUserName
962+
}
963+
964+
return []interface{}{att}
965+
}
966+
917967
func expandPodDNSConfig(l []interface{}) (*v1.PodDNSConfig, error) {
918968
if len(l) == 0 || l[0] == nil {
919969
return &v1.PodDNSConfig{}, nil
@@ -1002,6 +1052,9 @@ func expandPodSecurityContext(l []interface{}) (*v1.PodSecurityContext, error) {
10021052
policy := v1.PodFSGroupChangePolicy(v)
10031053
obj.FSGroupChangePolicy = &policy
10041054
}
1055+
if v, ok := in["windows_options"].([]interface{}); ok && len(v) > 0 {
1056+
obj.WindowsOptions = expandWindowsOptions(v)
1057+
}
10051058
return obj, nil
10061059
}
10071060

0 commit comments

Comments
 (0)