Skip to content

Commit

Permalink
Add support for node_labels in azurerm_kubernetes_cluster and azurerm…
Browse files Browse the repository at this point in the history
…_kubernetes_cluster_node_pool resources #4968
  • Loading branch information
dintel committed Jan 27, 2020
1 parent fa23b1d commit 1477e75
Show file tree
Hide file tree
Showing 19 changed files with 330 additions and 122 deletions.
2 changes: 1 addition & 1 deletion azurerm/internal/services/containers/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client
import (
"github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance"
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2018-09-01/containerregistry"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/kubernetes"
Expand Down Expand Up @@ -176,6 +176,14 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
Computed: true,
},

"node_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"node_taints": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -750,6 +758,10 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
agentPoolProfile["max_pods"] = int(*profile.MaxPods)
}

if profile.NodeLabels != nil {
agentPoolProfile["node_labels"] = profile.NodeLabels
}

if profile.NodeTaints != nil {
agentPoolProfile["node_taints"] = *profile.NodeTaints
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/containers/kubernetes_addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package containers
import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
Expand Down
23 changes: 22 additions & 1 deletion azurerm/internal/services/containers/kubernetes_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package containers
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -90,6 +90,14 @@ func SchemaDefaultNodePool() *schema.Schema {
ValidateFunc: validation.IntBetween(1, 100),
},

"node_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"node_taints": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -135,6 +143,7 @@ func ConvertDefaultNodePoolToAgentPool(input *[]containerservice.ManagedClusterA
EnableNodePublicIP: defaultCluster.EnableNodePublicIP,
ScaleSetPriority: defaultCluster.ScaleSetPriority,
ScaleSetEvictionPolicy: defaultCluster.ScaleSetEvictionPolicy,
NodeLabels: defaultCluster.NodeLabels,
NodeTaints: defaultCluster.NodeTaints,
},
}
Expand All @@ -150,13 +159,16 @@ func ExpandDefaultNodePool(d *schema.ResourceData) (*[]containerservice.ManagedC

raw := input[0].(map[string]interface{})
enableAutoScaling := raw["enable_auto_scaling"].(bool)
nodeLabelsRaw := raw["node_labels"].(map[string]interface{})
nodeLabels := utils.ExpandMapStringPtrString(nodeLabelsRaw)
nodeTaintsRaw := raw["node_taints"].([]interface{})
nodeTaints := utils.ExpandStringSlice(nodeTaintsRaw)

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,
Type: containerservice.AgentPoolType(raw["type"].(string)),
VMSize: containerservice.VMSizeTypes(raw["vm_size"].(string)),
Expand Down Expand Up @@ -286,6 +298,14 @@ func FlattenDefaultNodePool(input *[]containerservice.ManagedClusterAgentPoolPro
name = *agentPool.Name
}

var nodeLabels map[string]string
if agentPool.NodeLabels != nil {
nodeLabels = make(map[string]string)
for k, v := range agentPool.NodeLabels {
nodeLabels[k] = *v
}
}

var nodeTaints []string
if agentPool.NodeTaints != nil {
nodeTaints = *agentPool.NodeTaints
Expand All @@ -311,6 +331,7 @@ func FlattenDefaultNodePool(input *[]containerservice.ManagedClusterAgentPoolPro
"min_count": minCount,
"name": name,
"node_count": count,
"node_labels": nodeLabels,
"node_taints": nodeTaints,
"os_disk_size_gb": osDiskSizeGB,
"type": string(agentPool.Type),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -223,6 +223,14 @@ func resourceArmKubernetesCluster() *schema.Resource {
ForceNew: true,
},

"node_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"node_taints": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1230,6 +1238,10 @@ func expandKubernetesClusterAgentPoolProfiles(input []interface{}, isNewResource
return nil, fmt.Errorf("Can't create an AKS cluster with autoscaling enabled but not setting min_count or max_count")
}

if nodeLabels := utils.ExpandMapStringPtrString(config["node_labels"].(map[string]interface{})); len(nodeLabels) > 0 {
profile.NodeLabels = nodeLabels
}

if nodeTaints := utils.ExpandStringSlice(config["node_taints"].([]interface{})); len(*nodeTaints) > 0 {
profile.NodeTaints = nodeTaints
}
Expand Down Expand Up @@ -1312,6 +1324,7 @@ func flattenKubernetesClusterAgentPoolProfiles(profiles *[]containerservice.Mana
"max_pods": maxPods,
"min_count": minCount,
"name": name,
"node_labels": utils.FlattenMapStringPtrString(profile.NodeLabels),
"node_taints": utils.FlattenStringSlice(profile.NodeTaints),
"os_disk_size_gb": osDiskSizeGB,
"os_type": string(profile.OsType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-10-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-11-01/containerservice"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -99,6 +99,14 @@ func resourceArmKubernetesClusterNodePool() *schema.Resource {
ValidateFunc: validation.IntBetween(1, 100),
},

"node_labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"node_taints": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -213,6 +221,11 @@ func resourceArmKubernetesClusterNodePoolCreate(d *schema.ResourceData, meta int
profile.MaxPods = utils.Int32(maxPods)
}

nodeLabelsRaw := d.Get("node_labels").(map[string]interface{})
if nodeLabels := utils.ExpandMapStringPtrString(nodeLabelsRaw); len(nodeLabels) > 0 {
profile.NodeLabels = nodeLabels
}

nodeTaintsRaw := d.Get("node_taints").([]interface{})
if nodeTaints := utils.ExpandStringSlice(nodeTaintsRaw); len(*nodeTaints) > 0 {
profile.NodeTaints = nodeTaints
Expand Down Expand Up @@ -348,6 +361,12 @@ func resourceArmKubernetesClusterNodePoolUpdate(d *schema.ResourceData, meta int
props.Count = utils.Int32(int32(d.Get("node_count").(int)))
}

if d.HasChange("node_labels") {
nodeLabelsRaw := d.Get("node_labels").(map[string]interface{})
nodeLabels := utils.ExpandMapStringPtrString(nodeLabelsRaw)
props.NodeLabels = nodeLabels
}

if d.HasChange("node_taints") {
nodeTaintsRaw := d.Get("node_taints").([]interface{})
nodeTaints := utils.ExpandStringSlice(nodeTaintsRaw)
Expand Down Expand Up @@ -466,6 +485,10 @@ func resourceArmKubernetesClusterNodePoolRead(d *schema.ResourceData, meta inter
}
d.Set("node_count", count)

if err := d.Set("node_labels", props.NodeLabels); err != nil {
return fmt.Errorf("Error setting `node_labels`: %+v", err)
}

if err := d.Set("node_taints", utils.FlattenStringSlice(props.NodeTaints)); err != nil {
return fmt.Errorf("Error setting `node_taints`: %+v", err)
}
Expand Down
17 changes: 17 additions & 0 deletions azurerm/utils/common_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ func ExpandStringSlice(input []interface{}) *[]string {
return &result
}

func ExpandMapStringPtrString(input map[string]interface{}) map[string]*string {
result := make(map[string]*string)
for k, v := range input {
s := v.(string)
result[k] = &s
}
return result
}

func FlattenStringSlice(input *[]string) []interface{} {
result := make([]interface{}, 0)
if input != nil {
Expand All @@ -21,3 +30,11 @@ func FlattenStringSlice(input *[]string) []interface{} {
}
return result
}

func FlattenMapStringPtrString(input map[string]*string) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range input {
result[k] = *v
}
return result
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1477e75

Please sign in to comment.