Skip to content

Commit

Permalink
Add tags to cluster node pools (#5931)
Browse files Browse the repository at this point in the history
I'm excited to present my first attempt to contribute back to Terraform. Thanks in advance for guiding me to get this PR in good shape to get merged.

Goal of PR
Add tags to AKS nodepools

Changes

Implemented tags for nodepools resource and datasource
Extended some of the tests
Updated documentation
  • Loading branch information
wyarde authored Mar 10, 2020
1 parent 13ca314 commit 55eb723
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
Computed: true,
},

"tags": tags.SchemaDataSource(),

"os_disk_size_gb": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -763,6 +765,10 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
agentPoolProfile["enable_node_public_ip"] = *profile.EnableNodePublicIP
}

if profile.Tags != nil {
agentPoolProfile["tags"] = tags.Flatten(profile.Tags)
}

agentPoolProfiles = append(agentPoolProfiles, agentPoolProfile)
}

Expand Down
6 changes: 6 additions & 0 deletions azurerm/internal/services/containers/kubernetes_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -105,6 +106,8 @@ func SchemaDefaultNodePool() *schema.Schema {
Elem: &schema.Schema{Type: schema.TypeString},
},

"tags": tags.Schema(),

"os_disk_size_gb": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -146,6 +149,7 @@ func ConvertDefaultNodePoolToAgentPool(input *[]containerservice.ManagedClusterA
ScaleSetEvictionPolicy: defaultCluster.ScaleSetEvictionPolicy,
NodeLabels: defaultCluster.NodeLabels,
NodeTaints: defaultCluster.NodeTaints,
Tags: defaultCluster.Tags,
},
}
}
Expand All @@ -159,13 +163,15 @@ func ExpandDefaultNodePool(d *schema.ResourceData) (*[]containerservice.ManagedC
nodeLabels := utils.ExpandMapStringPtrString(nodeLabelsRaw)
nodeTaintsRaw := raw["node_taints"].([]interface{})
nodeTaints := utils.ExpandStringSlice(nodeTaintsRaw)
t := d.Get("tags").(map[string]interface{})

profile := containerservice.ManagedClusterAgentPoolProfile{
EnableAutoScaling: utils.Bool(enableAutoScaling),
EnableNodePublicIP: utils.Bool(raw["enable_node_public_ip"].(bool)),
Name: utils.String(raw["name"].(string)),
NodeLabels: nodeLabels,
NodeTaints: nodeTaints,
Tags: tags.Expand(t),
Type: containerservice.AgentPoolType(raw["type"].(string)),
VMSize: containerservice.VMSizeTypes(raw["vm_size"].(string)),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -54,6 +55,8 @@ func resourceArmKubernetesClusterNodePool() *schema.Resource {
ValidateFunc: validation.IntBetween(1, 100),
},

"tags": tags.Schema(),

"vm_size": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -200,12 +203,14 @@ func resourceArmKubernetesClusterNodePoolCreate(d *schema.ResourceData, meta int
count := d.Get("node_count").(int)
enableAutoScaling := d.Get("enable_auto_scaling").(bool)
osType := d.Get("os_type").(string)
t := d.Get("tags").(map[string]interface{})
vmSize := d.Get("vm_size").(string)

profile := containerservice.ManagedClusterAgentPoolProfileProperties{
OsType: containerservice.OSType(osType),
EnableAutoScaling: utils.Bool(enableAutoScaling),
EnableNodePublicIP: utils.Bool(d.Get("enable_node_public_ip").(bool)),
Tags: tags.Expand(t),
Type: containerservice.VirtualMachineScaleSets,
VMSize: containerservice.VMSizeTypes(vmSize),

Expand Down Expand Up @@ -365,6 +370,11 @@ func resourceArmKubernetesClusterNodePoolUpdate(d *schema.ResourceData, meta int
props.NodeTaints = nodeTaints
}

if d.HasChange("tags") {
t := d.Get("tags").(map[string]interface{})
props.Tags = tags.Expand(t)
}

// validate the auto-scale fields are both set/unset to prevent a continual diff
maxCount := 0
if props.MaxCount != nil {
Expand Down Expand Up @@ -492,7 +502,7 @@ func resourceArmKubernetesClusterNodePoolRead(d *schema.ResourceData, meta inter
d.Set("vm_size", string(props.VMSize))
}

return nil
return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmKubernetesClusterNodePoolDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func testAccAzureRMKubernetesClusterNodePool_autoScale(t *testing.T) {
Config: testAccAzureRMKubernetesClusterNodePool_autoScaleConfig(data, clientId, clientSecret),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesNodePoolExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
),
},
data.ImportStep(),
Expand All @@ -45,6 +46,7 @@ func testAccAzureRMKubernetesClusterNodePool_autoScale(t *testing.T) {
Config: testAccAzureRMKubernetesClusterNodePool_manualScaleConfig(data, clientId, clientSecret),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesNodePoolExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.Environment", "Staging"),
),
},
data.ImportStep(),
Expand All @@ -53,6 +55,7 @@ func testAccAzureRMKubernetesClusterNodePool_autoScale(t *testing.T) {
Config: testAccAzureRMKubernetesClusterNodePool_autoScaleConfig(data, clientId, clientSecret),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesNodePoolExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "0"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -597,6 +600,7 @@ func testAccAzureRMKubernetesClusterNodePool_windows(t *testing.T) {
Config: testAccAzureRMKubernetesClusterNodePool_windowsConfig(data, clientId, clientSecret),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesNodePoolExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.Os", "Windows"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -881,6 +885,10 @@ resource "azurerm_kubernetes_cluster_node_pool" "test" {
kubernetes_cluster_id = azurerm_kubernetes_cluster.test.id
vm_size = "Standard_DS2_v2"
node_count = 1
tags = {
Environment = "Staging"
}
}
`, template)
}
Expand Down Expand Up @@ -1104,6 +1112,10 @@ resource "azurerm_kubernetes_cluster_node_pool" "test" {
vm_size = "Standard_DS2_v2"
node_count = 1
os_type = "Windows"
tags = {
Os = "Windows"
}
}
`, template)
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/terrafmt-acctests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if ${error}; then
echo "$ find azurerm | egrep \"_test.go\" | sort | while read f; do terrafmt fmt -f \$f; done"
echo ""
echo "on windows:"
echo "$ Get-ChildItem -Path . -Recurse -Filter \"_test.go\" | foreach {terrafmt fmt -f $.name}"
echo "$ Get-ChildItem -Path . -Recurse -Filter \"*_test.go\" | foreach {terrafmt fmt -f $_.fullName}"
echo ""
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion scripts/terrafmt-website.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if ${error}; then
echo "$ find . | egrep html.markdown | sort | while read f; do terrafmt fmt \$f; done"
echo ""
echo "on windows:"
echo "$ Get-ChildItem -Path . -Recurse -Filter \"*html.markdown\" | foreach {terrafmt fmt $.name}"
echo "$ Get-ChildItem -Path . -Recurse -Filter \"*html.markdown\" | foreach {terrafmt fmt $_.fullName}"
echo ""
exit 1
fi
Expand Down
5 changes: 4 additions & 1 deletion website/docs/d/kubernetes_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,18 @@ A `agent_pool_profile` block exports the following:

* `name` - The name assigned to this pool of agents.

* `node_taints` - The list of Kubernetes taints which are applied to nodes in the agent pool

* `os_disk_size_gb` - The size of the Agent VM's Operating System Disk in GB.

* `os_type` - The Operating System used for the Agents.

* `tags` - A mapping of tags to assign to the resource.

* `vm_size` - The size of each VM in the Agent Pool (e.g. `Standard_F1`).

* `vnet_subnet_id` - The ID of the Subnet where the Agents in the Pool are provisioned.

* `node_taints` - The list of Kubernetes taints which are applied to nodes in the agent pool

---

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/kubernetes_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ A `default_node_pool` block supports the following:

* `type` - (Optional) The type of Node Pool which should be created. Possible values are `AvailabilitySet` and `VirtualMachineScaleSets`. Defaults to `VirtualMachineScaleSets`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

* `vnet_subnet_id` - (Required) The ID of a Subnet where the Kubernetes Node Pool should exist. Changing this forces a new resource to be created.

~> **NOTE:** A Route Table must be configured on this Subnet.
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/kubernetes_cluster_node_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ resource "azurerm_kubernetes_cluster_node_pool" "example" {
kubernetes_cluster_id = azurerm_kubernetes_cluster.example.id
vm_size = "Standard_DS2_v2"
node_count = 1
tags = {
Environment = "Production"
}
}
```

Expand Down Expand Up @@ -78,6 +82,8 @@ The following arguments are supported:

* `os_type` - (Optional) The Operating System which should be used for this Node Pool. Changing this forces a new resource to be created. Possible values are `Linux` and `Windows`. Defaults to `Linux`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

* `vnet_subnet_id` - (Optional) The ID of the Subnet where this Node Pool should exist.

-> **NOTE:** At this time the `vnet_subnet_id` must be the same for all node pools in the cluster
Expand Down

0 comments on commit 55eb723

Please sign in to comment.