Skip to content

Commit abbafb1

Browse files
authored
fix: change aks_pod_cidr based on aks_network_plugin (PSKD-1458) (#513)
* chore: add aks_network_plugin to outputs Signed-off-by: chjmil <[email protected]> * chore: change aks_pod_cidr based on network_plugin Signed-off-by: chjmil <[email protected]> * test: adding azure network plugin tests Signed-off-by: chjmil <[email protected]> * fix: change from vnet_address space to aks subnet address space Signed-off-by: chjmil <[email protected]> --------- Signed-off-by: chjmil <[email protected]>
1 parent 43ae26b commit abbafb1

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

outputs.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ output "aks_cluster_password" {
2727
}
2828

2929
output "aks_pod_cidr" {
30-
value = var.aks_pod_cidr
30+
# If the aks_network_plugin is set to azure, use the aks subnet address space as the pod CIDR.
31+
value = var.aks_network_plugin == "kubenet" ? var.aks_pod_cidr : module.vnet.subnets["aks"].address_prefixes[0]
3132
}
3233

3334
# postgres
@@ -148,3 +149,7 @@ output "cluster_node_pool_mode" {
148149
output "cluster_api_mode" {
149150
value = var.cluster_api_mode
150151
}
152+
153+
output "aks_network_plugin" {
154+
value = var.aks_network_plugin
155+
}

test/defaultplan/network_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func TestPlanNetwork(t *testing.T) {
4242
ResourceMapName: "module.aks.azurerm_kubernetes_cluster.aks",
4343
AttributeJsonPath: "{$.expressions.aks_network_plugin_mode.reference[0]}",
4444
},
45+
"kubeletPluginAksPodCidrTest": {
46+
Expected: "10.244.0.0/16",
47+
ResourceMapName: "aks_pod_cidr",
48+
Retriever: helpers.RetrieveFromRawPlanOutputChanges,
49+
},
4550
}
4651

4752
helpers.RunTests(t, tests, helpers.GetDefaultPlan(t))

test/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ go 1.23.0
55
toolchain go1.23.2
66

77
require (
8+
github.com/Azure/azure-sdk-for-go v51.0.0+incompatible
9+
github.com/Azure/go-autorest/autorest/to v0.4.0
810
github.com/gruntwork-io/terratest v0.48.2
911
github.com/hashicorp/terraform-json v0.23.0
1012
github.com/stretchr/testify v1.10.0
@@ -13,7 +15,6 @@ require (
1315

1416
require (
1517
filippo.io/edwards25519 v1.1.0 // indirect
16-
github.com/Azure/azure-sdk-for-go v51.0.0+incompatible // indirect
1718
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
1819
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 // indirect
1920
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
@@ -24,7 +25,6 @@ require (
2425
github.com/Azure/go-autorest/autorest/azure/auth v0.5.8 // indirect
2526
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 // indirect
2627
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
27-
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
2828
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
2929
github.com/Azure/go-autorest/logger v0.2.1 // indirect
3030
github.com/Azure/go-autorest/tracing v0.6.0 // indirect

test/helpers/test_case.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package helpers
55

66
import (
77
"fmt"
8+
"testing"
9+
810
"github.com/gruntwork-io/terratest/modules/terraform"
911
"github.com/stretchr/testify/assert"
1012
"github.com/stretchr/testify/require"
11-
"testing"
1213
)
1314

1415
// TupleTestCase struct which encapsulates a range of tests against a single resource map.
@@ -45,6 +46,16 @@ func RetrieveFromRawPlan(plan *terraform.PlanStruct, outputName string, jsonPath
4546
return value, nil
4647
}
4748

49+
// RetrieveFromRawPlan Retriever that gets a value from the raw plan variables
50+
func RetrieveFromRawPlanOutputChanges(plan *terraform.PlanStruct, outputName string, jsonPath string) (string, error) {
51+
output, exists := plan.RawPlan.OutputChanges[outputName]
52+
if !exists {
53+
return "nil", nil
54+
}
55+
value := fmt.Sprintf("%v", output.After)
56+
return value, nil
57+
}
58+
4859
// RetrieveFromResourcePlannedValuesMap Retriever that gets the value of a jsonpath query on a given *terraform.PlanStruct
4960
func RetrieveFromResourcePlannedValuesMap(plan *terraform.PlanStruct, resourceMapName string, jsonPath string) (string, error) {
5061
valuesMap, exists := plan.ResourcePlannedValuesMap[resourceMapName]

test/nondefaultplan/azure_policy_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestPlanAzurePolicy(t *testing.T) {
1515

1616
variables := helpers.GetDefaultPlanVars(t)
1717
variables["aks_azure_policy_enabled"] = true
18+
variables["aks_network_plugin"] = "azure"
1819

1920
tests := map[string]helpers.TestCase{
2021
"azurePolicyEnabledTest": {
@@ -23,6 +24,69 @@ func TestPlanAzurePolicy(t *testing.T) {
2324
AttributeJsonPath: "{$.azure_policy_enabled}",
2425
Message: "Unexpected azure_policy_enabled value",
2526
},
27+
"networkPluginTest": {
28+
Expected: "azure",
29+
ResourceMapName: "module.aks.azurerm_kubernetes_cluster.aks",
30+
AttributeJsonPath: "{$.network_profile[0].network_plugin}",
31+
},
32+
"azurePluginAksPodCidrTest": {
33+
Expected: "192.168.0.0/23",
34+
ResourceMapName: "aks_pod_cidr",
35+
Retriever: helpers.RetrieveFromRawPlanOutputChanges,
36+
},
37+
}
38+
39+
plan := helpers.GetPlan(t, variables)
40+
helpers.RunTests(t, tests, plan)
41+
}
42+
43+
// Test the default variables when using the sample-input-defaults.tfvars file
44+
// with aks_network_plugin set to azure and custom subnets.
45+
func TestPlanCustomSubnets(t *testing.T) {
46+
t.Parallel()
47+
48+
variables := helpers.GetDefaultPlanVars(t)
49+
variables["aks_network_plugin"] = "azure"
50+
variables["subnets"] = map[string]interface{}{
51+
"aks": map[string]interface{}{
52+
"prefixes": []string{"123.12.0.0/21"},
53+
"service_endpoints": []string{"Microsoft.Sql"},
54+
"private_endpoint_network_policies": "Disabled",
55+
"private_link_service_network_policies_enabled": false,
56+
"service_delegations": map[string]interface{}{},
57+
},
58+
"misc": map[string]interface{}{
59+
"prefixes": []string{"123.12.8.0/24"},
60+
"service_endpoints": []string{"Microsoft.Sql"},
61+
"private_endpoint_network_policies": "Disabled",
62+
"private_link_service_network_policies_enabled": false,
63+
"service_delegations": map[string]interface{}{},
64+
},
65+
"netapp": map[string]interface{}{
66+
"prefixes": []string{"123.12.9.0/24"},
67+
"service_endpoints": []string{""},
68+
"private_endpoint_network_policies": "Disabled",
69+
"private_link_service_network_policies_enabled": false,
70+
"service_delegations": map[string]interface{}{
71+
"netapp": map[string]interface{}{
72+
"name": "Microsoft.Netapp/volumes",
73+
"actions": []string{"Microsoft.Network/networkinterfaces/*", "Microsoft.Network/virtualNetworks/subnets/join/action"},
74+
},
75+
},
76+
},
77+
}
78+
79+
tests := map[string]helpers.TestCase{
80+
"networkPluginTest": {
81+
Expected: "azure",
82+
ResourceMapName: "module.aks.azurerm_kubernetes_cluster.aks",
83+
AttributeJsonPath: "{$.network_profile[0].network_plugin}",
84+
},
85+
"azurePluginAksPodCidrTest": {
86+
Expected: "123.12.0.0/21",
87+
ResourceMapName: "aks_pod_cidr",
88+
Retriever: helpers.RetrieveFromRawPlanOutputChanges,
89+
},
2690
}
2791

2892
plan := helpers.GetPlan(t, variables)

0 commit comments

Comments
 (0)