Skip to content

Commit 2bd124c

Browse files
Add additional_pod_ranges_config field (#8622) (#6133)
* 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 38f1090 commit 2bd124c

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed

.changelog/8622.txt

Lines changed: 3 additions & 0 deletions
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-beta/services/container/resource_container_cluster.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,23 @@ func ResourceContainerCluster() *schema.Resource {
15431543
},
15441544
},
15451545
},
1546+
"additional_pod_ranges_config": {
1547+
Type: schema.TypeList,
1548+
MaxItems: 1,
1549+
Optional: true,
1550+
Description: `AdditionalPodRangesConfig is the configuration for additional pod secondary ranges supporting the ClusterUpdate message.`,
1551+
Elem: &schema.Resource{
1552+
Schema: map[string]*schema.Schema{
1553+
"pod_range_names": {
1554+
Type: schema.TypeSet,
1555+
MinItems: 1,
1556+
Required: true,
1557+
Elem: &schema.Schema{Type: schema.TypeString},
1558+
Description: `Name for pod secondary ipv4 range which has the actual range defined ahead.`,
1559+
},
1560+
},
1561+
},
1562+
},
15461563
},
15471564
},
15481565
},
@@ -2382,6 +2399,38 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
23822399
}
23832400
}
23842401

2402+
if names, ok := d.GetOk("ip_allocation_policy.0.additional_pod_ranges_config.0.pod_range_names"); ok {
2403+
name := containerClusterFullName(project, location, clusterName)
2404+
additionalPodRangesConfig := &container.AdditionalPodRangesConfig{
2405+
PodRangeNames: tpgresource.ConvertStringSet(names.(*schema.Set)),
2406+
}
2407+
2408+
req := &container.UpdateClusterRequest{
2409+
Update: &container.ClusterUpdate{
2410+
AdditionalPodRangesConfig: additionalPodRangesConfig,
2411+
},
2412+
}
2413+
2414+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
2415+
RetryFunc: func() error {
2416+
clusterUpdateCall := config.NewContainerClient(userAgent).Projects.Locations.Clusters.Update(name, req)
2417+
if config.UserProjectOverride {
2418+
clusterUpdateCall.Header().Add("X-Goog-User-Project", project)
2419+
}
2420+
op, err = clusterUpdateCall.Do()
2421+
return err
2422+
},
2423+
})
2424+
if err != nil {
2425+
return errwrap.Wrapf("Error updating AdditionalPodRangesConfig: {{err}}", err)
2426+
}
2427+
2428+
err = ContainerOperationWait(config, op, project, location, "updating AdditionalPodRangesConfig", userAgent, d.Timeout(schema.TimeoutCreate))
2429+
if err != nil {
2430+
return errwrap.Wrapf("Error while waiting to update AdditionalPodRangesConfig: {{err}}", err)
2431+
}
2432+
}
2433+
23852434
if err := resourceContainerClusterRead(d, meta); err != nil {
23862435
return err
23872436
}
@@ -3294,6 +3343,51 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
32943343

32953344
}
32963345

3346+
if d.HasChange("ip_allocation_policy.0.additional_pod_ranges_config") {
3347+
o, n := d.GetChange("ip_allocation_policy.0.additional_pod_ranges_config.0.pod_range_names")
3348+
old_names := o.(*schema.Set)
3349+
new_names := n.(*schema.Set)
3350+
3351+
// Filter unchanged names.
3352+
removed_names := old_names.Difference(new_names)
3353+
added_names := new_names.Difference(old_names)
3354+
3355+
var additional_config *container.AdditionalPodRangesConfig
3356+
var removed_config *container.AdditionalPodRangesConfig
3357+
if added_names.Len() > 0 {
3358+
var names []string
3359+
for _, name := range added_names.List() {
3360+
names = append(names, name.(string))
3361+
}
3362+
additional_config = &container.AdditionalPodRangesConfig{
3363+
PodRangeNames: names,
3364+
}
3365+
}
3366+
if removed_names.Len() > 0 {
3367+
var names []string
3368+
for _, name := range removed_names.List() {
3369+
names = append(names, name.(string))
3370+
}
3371+
removed_config = &container.AdditionalPodRangesConfig{
3372+
PodRangeNames: names,
3373+
}
3374+
}
3375+
req := &container.UpdateClusterRequest{
3376+
Update: &container.ClusterUpdate{
3377+
AdditionalPodRangesConfig: additional_config,
3378+
RemovedAdditionalPodRangesConfig: removed_config,
3379+
},
3380+
}
3381+
3382+
updateF := updateFunc(req, "updating AdditionalPodRangesConfig")
3383+
// Call update serially.
3384+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
3385+
return err
3386+
}
3387+
3388+
log.Printf("[INFO] GKE cluster %s's AdditionalPodRangesConfig has been updated", d.Id())
3389+
}
3390+
32973391
if n, ok := d.GetOk("node_pool.#"); ok {
32983392
for i := 0; i < n.(int); i++ {
32993393
nodePoolInfo, err := extractNodePoolInformationFromCluster(d, config, clusterName)
@@ -4570,6 +4664,25 @@ func flattenSecurityPostureConfig(spc *container.SecurityPostureConfig) []map[st
45704664
return []map[string]interface{}{result}
45714665
}
45724666

4667+
func flattenAdditionalPodRangesConfig(ipAllocationPolicy *container.IPAllocationPolicy) []map[string]interface{} {
4668+
if ipAllocationPolicy == nil {
4669+
return nil
4670+
}
4671+
result := make(map[string]interface{})
4672+
4673+
if aprc := ipAllocationPolicy.AdditionalPodRangesConfig; aprc != nil {
4674+
if len(aprc.PodRangeNames) > 0 {
4675+
result["pod_range_names"] = aprc.PodRangeNames
4676+
} else {
4677+
return nil
4678+
}
4679+
} else {
4680+
return nil
4681+
}
4682+
4683+
return []map[string]interface{}{result}
4684+
}
4685+
45734686
func expandNotificationConfig(configured interface{}) *container.NotificationConfig {
45744687
l := configured.([]interface{})
45754688
if len(l) == 0 || l[0] == nil {
@@ -5453,6 +5566,7 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
54535566
"services_secondary_range_name": p.ServicesSecondaryRangeName,
54545567
"stack_type": p.StackType,
54555568
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(p.PodCidrOverprovisionConfig),
5569+
"additional_pod_ranges_config": flattenAdditionalPodRangesConfig(c.IpAllocationPolicy),
54565570
},
54575571
}, nil
54585572
}

google-beta/services/container/resource_container_cluster_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,80 @@ func TestAccContainerCluster_autopilot_net_admin(t *testing.T) {
39013901
})
39023902
}
39033903

3904+
func TestAccContainerCluster_additional_pod_ranges_config_on_create(t *testing.T) {
3905+
t.Parallel()
3906+
3907+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
3908+
acctest.VcrTest(t, resource.TestCase{
3909+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3910+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3911+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3912+
Steps: []resource.TestStep{
3913+
{
3914+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 1),
3915+
},
3916+
{
3917+
ResourceName: "google_container_cluster.primary",
3918+
ImportState: true,
3919+
ImportStateVerify: true,
3920+
},
3921+
},
3922+
})
3923+
}
3924+
3925+
func TestAccContainerCluster_additional_pod_ranges_config_on_update(t *testing.T) {
3926+
t.Parallel()
3927+
3928+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
3929+
acctest.VcrTest(t, resource.TestCase{
3930+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3931+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3932+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3933+
Steps: []resource.TestStep{
3934+
{
3935+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3936+
},
3937+
{
3938+
ResourceName: "google_container_cluster.primary",
3939+
ImportState: true,
3940+
ImportStateVerify: true,
3941+
},
3942+
{
3943+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 2),
3944+
},
3945+
{
3946+
ResourceName: "google_container_cluster.primary",
3947+
ImportState: true,
3948+
ImportStateVerify: true,
3949+
},
3950+
{
3951+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3952+
},
3953+
{
3954+
ResourceName: "google_container_cluster.primary",
3955+
ImportState: true,
3956+
ImportStateVerify: true,
3957+
},
3958+
{
3959+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 1),
3960+
},
3961+
{
3962+
ResourceName: "google_container_cluster.primary",
3963+
ImportState: true,
3964+
ImportStateVerify: true,
3965+
},
3966+
{
3967+
Config: testAccContainerCluster_additional_pod_ranges_config(clusterName, 0),
3968+
},
3969+
{
3970+
ResourceName: "google_container_cluster.primary",
3971+
ImportState: true,
3972+
ImportStateVerify: true,
3973+
},
3974+
},
3975+
})
3976+
}
3977+
39043978
func testAccContainerCluster_masterAuthorizedNetworksDisabled(t *testing.T, resource_name string) resource.TestCheckFunc {
39053979
return func(s *terraform.State) error {
39063980
rs, ok := s.RootModule().Resources[resource_name]
@@ -8007,3 +8081,83 @@ resource "google_container_cluster" "cluster" {
80078081
}
80088082
}`, policyName, cluster, np)
80098083
}
8084+
8085+
func testAccContainerCluster_additional_pod_ranges_config(name string, nameCount int) string {
8086+
var podRangeNamesStr string
8087+
names := []string{"\"gke-autopilot-pods-add\",", "\"gke-autopilot-pods-add-2\""}
8088+
for i := 0; i < nameCount; i++ {
8089+
podRangeNamesStr += names[i]
8090+
}
8091+
var aprc string
8092+
if len(podRangeNamesStr) > 0 {
8093+
aprc = fmt.Sprintf(`
8094+
additional_pod_ranges_config {
8095+
pod_range_names = [%s]
8096+
}
8097+
`, podRangeNamesStr)
8098+
}
8099+
8100+
return fmt.Sprintf(`
8101+
resource "google_compute_network" "main" {
8102+
name = "%s"
8103+
auto_create_subnetworks = false
8104+
}
8105+
resource "google_compute_subnetwork" "main" {
8106+
ip_cidr_range = "10.10.0.0/16"
8107+
name = "%s"
8108+
network = google_compute_network.main.self_link
8109+
region = "us-central1"
8110+
8111+
secondary_ip_range {
8112+
range_name = "gke-autopilot-services"
8113+
ip_cidr_range = "10.11.0.0/20"
8114+
}
8115+
8116+
secondary_ip_range {
8117+
range_name = "gke-autopilot-pods"
8118+
ip_cidr_range = "10.12.0.0/16"
8119+
}
8120+
8121+
secondary_ip_range {
8122+
range_name = "gke-autopilot-pods-add"
8123+
ip_cidr_range = "10.100.0.0/16"
8124+
}
8125+
secondary_ip_range {
8126+
range_name = "gke-autopilot-pods-add-2"
8127+
ip_cidr_range = "100.0.0.0/16"
8128+
}
8129+
}
8130+
resource "google_container_cluster" "primary" {
8131+
name = "%s"
8132+
location = "us-central1"
8133+
8134+
enable_autopilot = true
8135+
8136+
release_channel {
8137+
channel = "REGULAR"
8138+
}
8139+
8140+
network = google_compute_network.main.name
8141+
subnetwork = google_compute_subnetwork.main.name
8142+
8143+
private_cluster_config {
8144+
enable_private_endpoint = false
8145+
enable_private_nodes = true
8146+
master_ipv4_cidr_block = "172.16.0.0/28"
8147+
}
8148+
8149+
# supresses permadiff
8150+
dns_config {
8151+
cluster_dns = "CLOUD_DNS"
8152+
cluster_dns_domain = "cluster.local"
8153+
cluster_dns_scope = "CLUSTER_SCOPE"
8154+
}
8155+
8156+
ip_allocation_policy {
8157+
cluster_secondary_range_name = "gke-autopilot-pods"
8158+
services_secondary_range_name = "gke-autopilot-services"
8159+
%s
8160+
}
8161+
}
8162+
`, name, name, name, aprc)
8163+
}

website/docs/r/container_cluster.html.markdown

Lines changed: 10 additions & 0 deletions
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)