Skip to content

Commit

Permalink
Fix OS Attribute (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheneska authored Oct 13, 2023
1 parent bd30afb commit 4dff7d1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 10 deletions.
51 changes: 51 additions & 0 deletions kubernetes/resource_kubernetes_pod_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,39 @@ func TestAccKubernetesPodV1_phase(t *testing.T) {
})
}

func TestAccKubernetesPodV1_os(t *testing.T) {
name := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "kubernetes_pod_v1.test"
imageName := busyboxImage

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesPodV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesPodV1ConfigOS(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
resource.TestCheckResourceAttr(resourceName, "spec.0.os.0.name", "linux"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
{
Config: testAccKubernetesPodV1ConfigOS(name, imageName),
PlanOnly: true,
},
},
})
}

func testAccCheckCSIDriverExists(csiDriverName string) error {
conn, err := testAccProvider.Meta().(KubeClientsets).MainClientset()
if err != nil {
Expand Down Expand Up @@ -3439,3 +3472,21 @@ func testAccKubernetesPodConfigPhase(name, imageName string) string {
}
`, name, imageName)
}

func testAccKubernetesPodV1ConfigOS(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_pod_v1" "test" {
metadata {
name = "%s"
}
spec {
os {
name = "linux"
}
container {
image = "%s"
name = "containername"
}
}
}
`, name, imageName)
}
6 changes: 4 additions & 2 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ func podSpecFields(isUpdatable, isComputed bool) map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{string(api.Linux), string(api.Windows)}, false),
Description: "Name is the name of the operating system. The currently supported values are linux and windows.",
},
},
},
Expand Down
35 changes: 27 additions & 8 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var builtInTolerations = map[string]string{

// Flatteners

func flattenOS(in v1.PodOS) []interface{} {
att := make(map[string]interface{})
if in.Name != "" {
att["name"] = in.Name
}
return []interface{}{att}
}

func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
att := make(map[string]interface{})
if in.ActiveDeadlineSeconds != nil {
Expand Down Expand Up @@ -92,10 +100,8 @@ func flattenPodSpec(in v1.PodSpec) ([]interface{}, error) {
}
att["image_pull_secrets"] = flattenLocalObjectReferenceArray(in.ImagePullSecrets)

if in.OS.Name != "" {
att["os"] = map[string]interface{}{
"name": in.OS.Name,
}
if in.OS != nil {
att["os"] = flattenOS(*in.OS)
}

if in.NodeName != "" {
Expand Down Expand Up @@ -700,6 +706,21 @@ func flattenPodEphemeralVolumeSource(in *v1.EphemeralVolumeSource) []interface{}

// Expanders

func expandOS(l []interface{}) *v1.PodOS {
if len(l) == 0 || l[0] == nil {
return nil
}

in := l[0].(map[string]interface{})
obj := &v1.PodOS{}

if v, ok := in["name"].(string); ok {
obj.Name = v1.OSName(v)
}

return obj
}

func expandPodTargetState(p []interface{}) []string {
if len(p) > 0 {
t := make([]string, len(p))
Expand Down Expand Up @@ -818,10 +839,8 @@ func expandPodSpec(p []interface{}) (*v1.PodSpec, error) {
obj.NodeSelector = nodeSelectors
}

if v, ok := in["os"].(map[string]interface{}); ok {
if n, ok := v["name"].(string); ok && n != "" {
obj.OS.Name = v1.OSName(n)
}
if v, ok := in["os"].([]interface{}); ok {
obj.OS = expandOS(v)
}

if v, ok := in["runtime_class_name"].(string); ok && v != "" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/pod.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ The following arguments are supported:
* `pod_affinity_term` - (Required) A pod affinity term, associated with the corresponding weight.
* `weight` - (Required) Weight associated with matching the corresponding `pod_affinity_term`, in the range 1-100.

### `os`

#### Arguments

* `name` - (Required) Name is the name of the operating system. The currently supported values are `linux` and `windows`.

### `container`

#### Arguments
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/pod_v1.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ The following arguments are supported:
* `image_pull_secrets` - (Optional) ImagePullSecrets is an optional list of references to secrets in the same namespace to use for pulling any of the images used by this PodSpec. If specified, these secrets will be passed to individual puller implementations for them to use. For example, in the case of docker, only DockerConfig type secrets are honored. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod)
* `node_name` - (Optional) NodeName is a request to schedule this pod onto a specific node. If it is non-empty, the scheduler simply schedules this pod onto that node, assuming that it fits resource requirements.
* `node_selector` - (Optional) NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/configuration/assign-pod-node/).
* `os` - (Optional) Specifies the OS of the containers in the pod.
* `priority_class_name` - (Optional) If specified, indicates the pod's priority. 'system-node-critical' and 'system-cluster-critical' are two special keywords which indicate the highest priorities with the former being the highest priority. Any other name must be defined by creating a PriorityClass object with that name. If not specified, the pod priority will be default or zero if there is no default.
* `restart_policy` - (Optional) Restart policy for all containers within the pod. One of Always, OnFailure, Never. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy).
* `runtime_class_name` - (Optional) RuntimeClassName is a feature for selecting the container runtime configuration. The container runtime configuration is used to run a Pod's containers. For more info see [Kubernetes reference](https://kubernetes.io/docs/concepts/containers/runtime-class)
Expand Down Expand Up @@ -326,6 +327,12 @@ The following arguments are supported:
* `pod_affinity_term` - (Required) A pod affinity term, associated with the corresponding weight.
* `weight` - (Required) Weight associated with matching the corresponding `pod_affinity_term`, in the range 1-100.

### `os`

#### Arguments

* `name` - (Required) Name is the name of the operating system. The currently supported values are `linux` and `windows`.

### `container`

#### Arguments
Expand Down

0 comments on commit 4dff7d1

Please sign in to comment.