Skip to content

Commit

Permalink
[azurerm_kubernetes_cluster ] - support for the node_taints p… (#3787)
Browse files Browse the repository at this point in the history
This small PR adds support for defining Kubernetes taints in AKS agent_pool_profiles as requested in #3755.

However the PR depends on an upgrade of the Azure Go SDK to 31.0.0 (see #3786)

(fixes #3755)
  • Loading branch information
moserda authored and katbyte committed Jul 10, 2019
1 parent c80b021 commit 901c9ef
Show file tree
Hide file tree
Showing 17 changed files with 764 additions and 84 deletions.
12 changes: 11 additions & 1 deletion azurerm/data_source_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-06-01/containerservice"
"github.com/hashicorp/terraform/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 @@ -141,6 +141,12 @@ func dataSourceArmKubernetesCluster() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},

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

if profile.NodeTaints != nil {
agentPoolProfile["node_taints"] = *profile.NodeTaints
}

agentPoolProfiles = append(agentPoolProfiles, agentPoolProfile)
}

Expand Down
36 changes: 36 additions & 0 deletions azurerm/data_source_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,30 @@ func TestAccDataSourceAzureRMKubernetesCluster_autoscalingWithAvailabilityZones(
})
}

func TestAccDataSourceAzureRMKubernetesCluster_nodeTaints(t *testing.T) {
dataSourceName := "data.azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
clientId := os.Getenv("ARM_CLIENT_ID")
clientSecret := os.Getenv("ARM_CLIENT_SECRET")

config := testAccDataSourceAzureRMKubernetesCluster_nodeTaints(ri, clientId, clientSecret, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "agent_pool_profile.1.node_taints.0", "key=value:NoSchedule"),
),
},
},
})
}

func testAccDataSourceAzureRMKubernetesCluster_basic(rInt int, clientId string, clientSecret string, location string) string {
r := testAccAzureRMKubernetesCluster_basic(rInt, clientId, clientSecret, location)
return fmt.Sprintf(`
Expand Down Expand Up @@ -666,3 +690,15 @@ data "azurerm_kubernetes_cluster" "test" {
}
`, r)
}

func testAccDataSourceAzureRMKubernetesCluster_nodeTaints(rInt int, clientId string, clientSecret string, location string) string {
r := testAccAzureRMKubernetesCluster_nodeTaints(rInt, clientId, clientSecret, location)
return fmt.Sprintf(`
%s
data "azurerm_kubernetes_cluster" "test" {
name = "${azurerm_kubernetes_cluster.test.name}"
resource_group_name = "${azurerm_kubernetes_cluster.test.resource_group_name}"
}
`, r)
}
2 changes: 1 addition & 1 deletion azurerm/internal/services/containers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package containers
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-02-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-06-01/containerservice"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_container_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"bytes"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-06-01/containerservice"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down
16 changes: 15 additions & 1 deletion azurerm/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"strings"

"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-02-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-06-01/containerservice"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
Expand Down Expand Up @@ -198,6 +198,12 @@ func resourceArmKubernetesCluster() *schema.Resource {
Computed: true,
ForceNew: true,
},

"node_taints": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
Expand Down Expand Up @@ -990,6 +996,10 @@ func expandKubernetesClusterAgentPoolProfiles(d *schema.ResourceData) ([]contain
return nil, fmt.Errorf("Can't create an AKS cluster with autoscaling enabled but not setting min_count or max_count")
}

if nodeTaints := utils.ExpandStringSlice(config["node_taints"].([]interface{})); len(*nodeTaints) > 0 {
profile.NodeTaints = nodeTaints
}

profiles = append(profiles, profile)
}

Expand Down Expand Up @@ -1057,6 +1067,10 @@ func flattenKubernetesClusterAgentPoolProfiles(profiles *[]containerservice.Mana
agentPoolProfile["max_pods"] = int(*profile.MaxPods)
}

if profile.NodeTaints != nil {
agentPoolProfile["node_taints"] = *profile.NodeTaints
}

agentPoolProfiles = append(agentPoolProfiles, agentPoolProfile)
}

Expand Down
61 changes: 61 additions & 0 deletions azurerm/resource_arm_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,29 @@ func TestAccAzureRMKubernetesCluster_multipleAgents(t *testing.T) {
})
}

func TestAccAzureRMKubernetesCluster_nodeTaints(t *testing.T) {
resourceName := "azurerm_kubernetes_cluster.test"
ri := tf.AccRandTimeInt()
clientId := os.Getenv("ARM_CLIENT_ID")
clientSecret := os.Getenv("ARM_CLIENT_SECRET")
config := testAccAzureRMKubernetesCluster_nodeTaints(ri, clientId, clientSecret, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKubernetesClusterDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKubernetesClusterExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "agent_pool_profile.1.node_taints.0", "key=value:NoSchedule"),
),
},
},
})
}

func testCheckAzureRMKubernetesClusterExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -1683,3 +1706,41 @@ resource "azurerm_kubernetes_cluster" "test" {
}
`, rInt, location, rInt, rInt, clientId, clientSecret)
}

func testAccAzureRMKubernetesCluster_nodeTaints(rInt int, clientId string, clientSecret string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_kubernetes_cluster" "test" {
name = "acctestaks%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
dns_prefix = "acctestaks%d"
agent_pool_profile {
name = "default"
count = "1"
type = "VirtualMachineScaleSets"
vm_size = "Standard_DS2_v2"
}
agent_pool_profile {
name = "pool1"
count = "1"
type = "VirtualMachineScaleSets"
vm_size = "Standard_DS2_v2"
node_taints = [
"key=value:NoSchedule"
]
}
service_principal {
client_id = "%s"
client_secret = "%s"
}
}
`, rInt, location, rInt, rInt, clientId, clientSecret)
}
Loading

0 comments on commit 901c9ef

Please sign in to comment.