Skip to content

Commit 16cceaa

Browse files
Add additional_pod_ranges_config field (#8622) (#15600)
* support additional_pod_ranges_config field * fix update upon creation logic * finalize tests, suppress permadiff, add docs * nest within ip_allocation_policy block + update docs * minor docs fix Signed-off-by: Modular Magician <[email protected]>
1 parent bee7121 commit 16cceaa

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed

.changelog/8622.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
container: added `additional_pod_ranges_config` field to `google_container_cluster` resource
3+
```

google/services/container/resource_container_cluster.go

+114
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,23 @@ func ResourceContainerCluster() *schema.Resource {
13691369
},
13701370
},
13711371
},
1372+
"additional_pod_ranges_config": {
1373+
Type: schema.TypeList,
1374+
MaxItems: 1,
1375+
Optional: true,
1376+
Description: `AdditionalPodRangesConfig is the configuration for additional pod secondary ranges supporting the ClusterUpdate message.`,
1377+
Elem: &schema.Resource{
1378+
Schema: map[string]*schema.Schema{
1379+
"pod_range_names": {
1380+
Type: schema.TypeSet,
1381+
MinItems: 1,
1382+
Required: true,
1383+
Elem: &schema.Schema{Type: schema.TypeString},
1384+
Description: `Name for pod secondary ipv4 range which has the actual range defined ahead.`,
1385+
},
1386+
},
1387+
},
1388+
},
13721389
},
13731390
},
13741391
},
@@ -2149,6 +2166,38 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
21492166
}
21502167
}
21512168

2169+
if names, ok := d.GetOk("ip_allocation_policy.0.additional_pod_ranges_config.0.pod_range_names"); ok {
2170+
name := containerClusterFullName(project, location, clusterName)
2171+
additionalPodRangesConfig := &container.AdditionalPodRangesConfig{
2172+
PodRangeNames: tpgresource.ConvertStringSet(names.(*schema.Set)),
2173+
}
2174+
2175+
req := &container.UpdateClusterRequest{
2176+
Update: &container.ClusterUpdate{
2177+
AdditionalPodRangesConfig: additionalPodRangesConfig,
2178+
},
2179+
}
2180+
2181+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
2182+
RetryFunc: func() error {
2183+
clusterUpdateCall := config.NewContainerClient(userAgent).Projects.Locations.Clusters.Update(name, req)
2184+
if config.UserProjectOverride {
2185+
clusterUpdateCall.Header().Add("X-Goog-User-Project", project)
2186+
}
2187+
op, err = clusterUpdateCall.Do()
2188+
return err
2189+
},
2190+
})
2191+
if err != nil {
2192+
return errwrap.Wrapf("Error updating AdditionalPodRangesConfig: {{err}}", err)
2193+
}
2194+
2195+
err = ContainerOperationWait(config, op, project, location, "updating AdditionalPodRangesConfig", userAgent, d.Timeout(schema.TimeoutCreate))
2196+
if err != nil {
2197+
return errwrap.Wrapf("Error while waiting to update AdditionalPodRangesConfig: {{err}}", err)
2198+
}
2199+
}
2200+
21522201
if err := resourceContainerClusterRead(d, meta); err != nil {
21532202
return err
21542203
}
@@ -3038,6 +3087,51 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
30383087

30393088
}
30403089

3090+
if d.HasChange("ip_allocation_policy.0.additional_pod_ranges_config") {
3091+
o, n := d.GetChange("ip_allocation_policy.0.additional_pod_ranges_config.0.pod_range_names")
3092+
old_names := o.(*schema.Set)
3093+
new_names := n.(*schema.Set)
3094+
3095+
// Filter unchanged names.
3096+
removed_names := old_names.Difference(new_names)
3097+
added_names := new_names.Difference(old_names)
3098+
3099+
var additional_config *container.AdditionalPodRangesConfig
3100+
var removed_config *container.AdditionalPodRangesConfig
3101+
if added_names.Len() > 0 {
3102+
var names []string
3103+
for _, name := range added_names.List() {
3104+
names = append(names, name.(string))
3105+
}
3106+
additional_config = &container.AdditionalPodRangesConfig{
3107+
PodRangeNames: names,
3108+
}
3109+
}
3110+
if removed_names.Len() > 0 {
3111+
var names []string
3112+
for _, name := range removed_names.List() {
3113+
names = append(names, name.(string))
3114+
}
3115+
removed_config = &container.AdditionalPodRangesConfig{
3116+
PodRangeNames: names,
3117+
}
3118+
}
3119+
req := &container.UpdateClusterRequest{
3120+
Update: &container.ClusterUpdate{
3121+
AdditionalPodRangesConfig: additional_config,
3122+
RemovedAdditionalPodRangesConfig: removed_config,
3123+
},
3124+
}
3125+
3126+
updateF := updateFunc(req, "updating AdditionalPodRangesConfig")
3127+
// Call update serially.
3128+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
3129+
return err
3130+
}
3131+
3132+
log.Printf("[INFO] GKE cluster %s's AdditionalPodRangesConfig has been updated", d.Id())
3133+
}
3134+
30413135
if n, ok := d.GetOk("node_pool.#"); ok {
30423136
for i := 0; i < n.(int); i++ {
30433137
nodePoolInfo, err := extractNodePoolInformationFromCluster(d, config, clusterName)
@@ -4108,6 +4202,25 @@ func flattenSecurityPostureConfig(spc *container.SecurityPostureConfig) []map[st
41084202
return []map[string]interface{}{result}
41094203
}
41104204

4205+
func flattenAdditionalPodRangesConfig(ipAllocationPolicy *container.IPAllocationPolicy) []map[string]interface{} {
4206+
if ipAllocationPolicy == nil {
4207+
return nil
4208+
}
4209+
result := make(map[string]interface{})
4210+
4211+
if aprc := ipAllocationPolicy.AdditionalPodRangesConfig; aprc != nil {
4212+
if len(aprc.PodRangeNames) > 0 {
4213+
result["pod_range_names"] = aprc.PodRangeNames
4214+
} else {
4215+
return nil
4216+
}
4217+
} else {
4218+
return nil
4219+
}
4220+
4221+
return []map[string]interface{}{result}
4222+
}
4223+
41114224
func expandNotificationConfig(configured interface{}) *container.NotificationConfig {
41124225
l := configured.([]interface{})
41134226
if len(l) == 0 || l[0] == nil {
@@ -4878,6 +4991,7 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
48784991
"services_secondary_range_name": p.ServicesSecondaryRangeName,
48794992
"stack_type": p.StackType,
48804993
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(p.PodCidrOverprovisionConfig),
4994+
"additional_pod_ranges_config": flattenAdditionalPodRangesConfig(c.IpAllocationPolicy),
48814995
},
48824996
}, nil
48834997
}

google/services/container/resource_container_cluster_test.go

+154
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,80 @@ func TestAccContainerCluster_autopilot_net_admin(t *testing.T) {
32673267
})
32683268
}
32693269

3270+
func TestAccContainerCluster_additional_pod_ranges_config_on_create(t *testing.T) {
3271+
t.Parallel()
3272+
3273+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
3274+
acctest.VcrTest(t, resource.TestCase{
3275+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3276+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3277+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3278+
Steps: []resource.TestStep{
3279+
{
3280+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 1),
3281+
},
3282+
{
3283+
ResourceName: "google_container_cluster.primary",
3284+
ImportState: true,
3285+
ImportStateVerify: true,
3286+
},
3287+
},
3288+
})
3289+
}
3290+
3291+
func TestAccContainerCluster_additional_pod_ranges_config_on_update(t *testing.T) {
3292+
t.Parallel()
3293+
3294+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
3295+
acctest.VcrTest(t, resource.TestCase{
3296+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3297+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3298+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3299+
Steps: []resource.TestStep{
3300+
{
3301+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3302+
},
3303+
{
3304+
ResourceName: "google_container_cluster.primary",
3305+
ImportState: true,
3306+
ImportStateVerify: true,
3307+
},
3308+
{
3309+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 2),
3310+
},
3311+
{
3312+
ResourceName: "google_container_cluster.primary",
3313+
ImportState: true,
3314+
ImportStateVerify: true,
3315+
},
3316+
{
3317+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3318+
},
3319+
{
3320+
ResourceName: "google_container_cluster.primary",
3321+
ImportState: true,
3322+
ImportStateVerify: true,
3323+
},
3324+
{
3325+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 1),
3326+
},
3327+
{
3328+
ResourceName: "google_container_cluster.primary",
3329+
ImportState: true,
3330+
ImportStateVerify: true,
3331+
},
3332+
{
3333+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3334+
},
3335+
{
3336+
ResourceName: "google_container_cluster.primary",
3337+
ImportState: true,
3338+
ImportStateVerify: true,
3339+
},
3340+
},
3341+
})
3342+
}
3343+
32703344
func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
32713345
return func(s *terraform.State) error {
32723346
rs, ok := s.RootModule().Resources[resource_name]
@@ -6858,3 +6932,83 @@ resource "google_container_cluster" "cluster" {
68586932
}
68596933
}`, policyName, cluster, np)
68606934
}
6935+
6936+
func testAccContainerCluster_additional_pod_ranges_config(name string, nameCount int) string {
6937+
var podRangeNamesStr string
6938+
names := []string{"\"gke-autopilot-pods-add\",", "\"gke-autopilot-pods-add-2\""}
6939+
for i := 0; i < nameCount; i++ {
6940+
podRangeNamesStr += names[i]
6941+
}
6942+
var aprc string
6943+
if len(podRangeNamesStr) > 0 {
6944+
aprc = fmt.Sprintf(`
6945+
additional_pod_ranges_config {
6946+
pod_range_names = [%s]
6947+
}
6948+
`, podRangeNamesStr)
6949+
}
6950+
6951+
return fmt.Sprintf(`
6952+
resource "google_compute_network" "main" {
6953+
name = "%s"
6954+
auto_create_subnetworks = false
6955+
}
6956+
resource "google_compute_subnetwork" "main" {
6957+
ip_cidr_range = "10.10.0.0/16"
6958+
name = "%s"
6959+
network = google_compute_network.main.self_link
6960+
region = "us-central1"
6961+
6962+
secondary_ip_range {
6963+
range_name = "gke-autopilot-services"
6964+
ip_cidr_range = "10.11.0.0/20"
6965+
}
6966+
6967+
secondary_ip_range {
6968+
range_name = "gke-autopilot-pods"
6969+
ip_cidr_range = "10.12.0.0/16"
6970+
}
6971+
6972+
secondary_ip_range {
6973+
range_name = "gke-autopilot-pods-add"
6974+
ip_cidr_range = "10.100.0.0/16"
6975+
}
6976+
secondary_ip_range {
6977+
range_name = "gke-autopilot-pods-add-2"
6978+
ip_cidr_range = "100.0.0.0/16"
6979+
}
6980+
}
6981+
resource "google_container_cluster" "primary" {
6982+
name = "%s"
6983+
location = "us-central1"
6984+
6985+
enable_autopilot = true
6986+
6987+
release_channel {
6988+
channel = "REGULAR"
6989+
}
6990+
6991+
network = google_compute_network.main.name
6992+
subnetwork = google_compute_subnetwork.main.name
6993+
6994+
private_cluster_config {
6995+
enable_private_endpoint = false
6996+
enable_private_nodes = true
6997+
master_ipv4_cidr_block = "172.16.0.0/28"
6998+
}
6999+
7000+
# supresses permadiff
7001+
dns_config {
7002+
cluster_dns = "CLOUD_DNS"
7003+
cluster_dns_domain = "cluster.local"
7004+
cluster_dns_scope = "CLUSTER_SCOPE"
7005+
}
7006+
7007+
ip_allocation_policy {
7008+
cluster_secondary_range_name = "gke-autopilot-pods"
7009+
services_secondary_range_name = "gke-autopilot-services"
7010+
%s
7011+
}
7012+
}
7013+
`, name, name, name, aprc)
7014+
}

website/docs/r/container_cluster.html.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,16 @@ pick a specific range to use.
723723
Default value is `IPV4`.
724724
Possible values are `IPV4` and `IPV4_IPV6`.
725725

726+
* `additional_pod_ranges_config` - (Optional) The configuration for additional pod secondary ranges at
727+
the cluster level. Used for Autopilot clusters and Standard clusters with which control of the
728+
secondary Pod IP address assignment to node pools isn't needed. Structure is [documented below](#nested_additional_pod_ranges_config).
729+
730+
731+
<a name="nested_additional_pod_ranges_config"></a>The `additional_pod_ranges_config` block supports:
732+
733+
* `pod_range_names` - (Required) The names of the Pod ranges to add to the cluster.
734+
735+
726736
<a name="nested_master_auth"></a>The `master_auth` block supports:
727737

728738
* `client_certificate_config` - (Required) Whether client certificate authorization is enabled for this cluster. For example:

0 commit comments

Comments
 (0)