Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurem_kubernetes_cluster/azurerm_kubernetes_cluster_node_pool: support for node_public_ip_prefix_id #11635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func dataSourceKubernetesCluster() *pluginsdk.Resource {
Computed: true,
},

"node_public_ip_prefix_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"upgrade_settings": upgradeSettingsForDataSourceSchema(),
},
},
Expand Down Expand Up @@ -970,6 +975,11 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
count = int(*profile.Count)
}

enableNodePublicIP := false
if profile.EnableNodePublicIP != nil {
enableNodePublicIP = *profile.EnableNodePublicIP
}

minCount := 0
if profile.MinCount != nil {
minCount = int(*profile.MinCount)
Expand All @@ -990,6 +1000,11 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
name = *profile.Name
}

nodePublicIPPrefixID := ""
if profile.NodePublicIPPrefixID != nil {
nodePublicIPPrefixID = *profile.NodePublicIPPrefixID
}

osDiskSizeGb := 0
if profile.OsDiskSizeGB != nil {
osDiskSizeGb = int(*profile.OsDiskSizeGB)
Expand Down Expand Up @@ -1026,35 +1041,31 @@ func flattenKubernetesClusterDataSourceAgentPoolProfiles(input *[]containerservi
nodeTaints = *profile.NodeTaints
}

enableNodePublicIP := false
if profile.EnableNodePublicIP != nil {
enableNodePublicIP = *profile.EnableNodePublicIP
}

vmSize := ""
if profile.VMSize != nil {
vmSize = *profile.VMSize
}

agentPoolProfiles = append(agentPoolProfiles, map[string]interface{}{
"availability_zones": utils.FlattenStringSlice(profile.AvailabilityZones),
"count": count,
"enable_auto_scaling": enableAutoScaling,
"enable_node_public_ip": enableNodePublicIP,
"max_count": maxCount,
"max_pods": maxPods,
"min_count": minCount,
"name": name,
"node_labels": nodeLabels,
"node_taints": nodeTaints,
"orchestrator_version": orchestratorVersion,
"os_disk_size_gb": osDiskSizeGb,
"os_type": string(profile.OsType),
"tags": tags.Flatten(profile.Tags),
"type": string(profile.Type),
"upgrade_settings": flattenUpgradeSettings(profile.UpgradeSettings),
"vm_size": vmSize,
"vnet_subnet_id": vnetSubnetId,
"availability_zones": utils.FlattenStringSlice(profile.AvailabilityZones),
"count": count,
"enable_auto_scaling": enableAutoScaling,
"enable_node_public_ip": enableNodePublicIP,
"max_count": maxCount,
"max_pods": maxPods,
"min_count": minCount,
"name": name,
"node_labels": nodeLabels,
"node_public_ip_prefix_id": nodePublicIPPrefixID,
"node_taints": nodeTaints,
"orchestrator_version": orchestratorVersion,
"os_disk_size_gb": osDiskSizeGb,
"os_type": string(profile.OsType),
"tags": tags.Flatten(profile.Tags),
"type": string(profile.Type),
"upgrade_settings": flattenUpgradeSettings(profile.UpgradeSettings),
"vm_size": vmSize,
"vnet_subnet_id": vnetSubnetId,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var kubernetesDataSourceTests = map[string]func(t *testing.T){
"autoscalingNoAvailabilityZones": testAccDataSourceKubernetesCluster_autoscalingNoAvailabilityZones,
"autoscalingWithAvailabilityZones": testAccDataSourceKubernetesCluster_autoscalingWithAvailabilityZones,
"nodeLabels": testAccDataSourceKubernetesCluster_nodeLabels,
"enableNodePublicIP": testAccDataSourceKubernetesCluster_enableNodePublicIP,
"nodePublicIP": testAccDataSourceKubernetesCluster_nodePublicIP,
"privateCluster": testAccDataSourceKubernetesCluster_privateCluster,
}

Expand Down Expand Up @@ -596,20 +596,21 @@ func testAccDataSourceKubernetesCluster_nodeLabels(t *testing.T) {
})
}

func TestAccDataSourceKubernetesCluster_enableNodePublicIP(t *testing.T) {
func TestAccDataSourceKubernetesCluster_nodePublicIP(t *testing.T) {
checkIfShouldRunTestsIndividually(t)
testAccDataSourceKubernetesCluster_enableNodePublicIP(t)
testAccDataSourceKubernetesCluster_nodePublicIP(t)
}

func testAccDataSourceKubernetesCluster_enableNodePublicIP(t *testing.T) {
func testAccDataSourceKubernetesCluster_nodePublicIP(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_kubernetes_cluster", "test")
r := KubernetesClusterDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.enableNodePublicIPConfig(data),
Config: r.nodePublicIPConfig(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("agent_pool_profile.0.enable_node_public_ip").HasValue("true"),
check.That(data.ResourceName).Key("agent_pool_profile.0.node_public_ip_prefix_id").Exists(),
),
},
})
Expand Down Expand Up @@ -857,13 +858,13 @@ data "azurerm_kubernetes_cluster" "test" {
`, KubernetesClusterResource{}.nodeLabelsConfig(data, labels))
}

func (KubernetesClusterDataSource) enableNodePublicIPConfig(data acceptance.TestData) string {
func (KubernetesClusterDataSource) nodePublicIPConfig(data acceptance.TestData) string {
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
}
`, KubernetesClusterResource{}.enableNodePublicIPConfig(data, true))
`, KubernetesClusterResource{}.nodePublicIPPrefixConfig(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,50 +232,53 @@ func testAccKubernetesCluster_enableNodePublicIP(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
// Enabled
Config: r.enableNodePublicIPConfig(data, true),
Config: r.enableNodePublicIPConfig(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("default_node_pool.0.enable_node_public_ip").HasValue("true"),
),
},
data.ImportStep(),
})
}

func TestAccKubernetesCluster_internalNetwork(t *testing.T) {
checkIfShouldRunTestsIndividually(t)
testAccKubernetesCluster_internalNetwork(t)
}

func testAccKubernetesCluster_internalNetwork(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
// Disabled
Config: r.enableNodePublicIPConfig(data, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("default_node_pool.0.enable_node_public_ip").HasValue("false"),
),
},
data.ImportStep(),
{
// Enabled
Config: r.enableNodePublicIPConfig(data, true),
Config: r.internalNetworkConfig(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("default_node_pool.0.enable_node_public_ip").HasValue("true"),
check.That(data.ResourceName).Key("default_node_pool.0.max_pods").HasValue("60"),
),
},
data.ImportStep(),
})
}

func TestAccKubernetesCluster_internalNetwork(t *testing.T) {
func TestAccKubernetesCluster_nodePublicIPPrefix(t *testing.T) {
checkIfShouldRunTestsIndividually(t)
testAccKubernetesCluster_internalNetwork(t)
testAccKubernetesCluster_nodePublicIPPrefix(t)
}

func testAccKubernetesCluster_internalNetwork(t *testing.T) {
func testAccKubernetesCluster_nodePublicIPPrefix(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kubernetes_cluster", "test")
r := KubernetesClusterResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.internalNetworkConfig(data),
Config: r.nodePublicIPPrefixConfig(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("default_node_pool.0.max_pods").HasValue("60"),
check.That(data.ResourceName).Key("default_node_pool.0.enable_node_public_ip").HasValue("true"),
check.That(data.ResourceName).Key("default_node_pool.0.node_public_ip_prefix_id").Exists(),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -971,7 +974,7 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, networkMode, networkPlugin, networkPolicy)
}

func (KubernetesClusterResource) enableNodePublicIPConfig(data acceptance.TestData, enabled bool) string {
func (KubernetesClusterResource) enableNodePublicIPConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand All @@ -992,14 +995,14 @@ resource "azurerm_kubernetes_cluster" "test" {
name = "default"
node_count = 1
vm_size = "Standard_DS2_v2"
enable_node_public_ip = %t
enable_node_public_ip = true
}

identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, enabled)
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) internalNetworkConfig(data acceptance.TestData) string {
Expand Down Expand Up @@ -1056,6 +1059,45 @@ resource "azurerm_kubernetes_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) nodePublicIPPrefixConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-aks-%d"
location = "%s"
}

resource "azurerm_public_ip_prefix" "test" {
name = "acctestpipprefix%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
prefix_length = 31
}

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"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_DS2_v2"
enable_node_public_ip = true
node_public_ip_prefix_id = azurerm_public_ip_prefix.test.id
}

identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (KubernetesClusterResource) outboundTypeLoadBalancerConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func dataSourceKubernetesClusterNodePool() *pluginsdk.Resource {
},
},

"node_public_ip_prefix_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"node_taints": {
Type: pluginsdk.TypeList,
Computed: true,
Expand Down Expand Up @@ -241,6 +246,8 @@ func dataSourceKubernetesClusterNodePoolRead(d *pluginsdk.ResourceData, meta int
return fmt.Errorf("setting `node_labels`: %+v", err)
}

d.Set("node_public_ip_prefix_id", props.NodePublicIPPrefixID)

if err := d.Set("node_taints", utils.FlattenStringSlice(props.NodeTaints)); err != nil {
return fmt.Errorf("setting `node_taints`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
"enable_node_public_ip": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
},

"eviction_policy": {
Expand Down Expand Up @@ -145,6 +146,13 @@ func resourceKubernetesClusterNodePool() *pluginsdk.Resource {
},
},

"node_public_ip_prefix_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
RequiredWith: []string{"enable_node_public_ip"},
},

"node_taints": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -345,6 +353,10 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
profile.NodeLabels = nodeLabels
}

if nodePublicIPPrefixID := d.Get("node_public_ip_prefix_id").(string); nodePublicIPPrefixID != "" {
profile.NodePublicIPPrefixID = utils.String(nodePublicIPPrefixID)
}

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

if d.HasChange("node_public_ip_prefix_id") {
props.NodePublicIPPrefixID = utils.String(d.Get("node_public_ip_prefix_id").(string))
}

if d.HasChange("orchestrator_version") {
// Spot Node pool's can't be updated - Azure Docs: https://docs.microsoft.com/en-us/azure/aks/spot-node-pool
// > You can't upgrade a spot node pool since spot node pools can't guarantee cordon and drain.
Expand Down Expand Up @@ -653,6 +669,8 @@ func resourceKubernetesClusterNodePoolRead(d *pluginsdk.ResourceData, meta inter
return fmt.Errorf("setting `node_labels`: %+v", err)
}

d.Set("node_public_ip_prefix_id", props.NodePublicIPPrefixID)

if err := d.Set("node_taints", utils.FlattenStringSlice(props.NodeTaints)); err != nil {
return fmt.Errorf("setting `node_taints`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,14 +1243,22 @@ provider "azurerm" {

%s

resource "azurerm_kubernetes_cluster_node_pool" "test" {
name = "internal"
kubernetes_cluster_id = azurerm_kubernetes_cluster.test.id
vm_size = "Standard_DS2_v2"
node_count = 1
enable_node_public_ip = true
resource "azurerm_public_ip_prefix" "test" {
name = "acctestpipprefix%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
prefix_length = 31
}
`, r.templateConfig(data))

resource "azurerm_kubernetes_cluster_node_pool" "test" {
name = "internal"
kubernetes_cluster_id = azurerm_kubernetes_cluster.test.id
vm_size = "Standard_DS2_v2"
node_count = 1
enable_node_public_ip = true
node_public_ip_prefix_id = azurerm_public_ip_prefix.test.id
}
`, r.templateConfig(data), data.RandomInteger)
}

func (r KubernetesClusterNodePoolResource) nodeTaintsConfig(data acceptance.TestData) string {
Expand Down
Loading