Skip to content

Commit

Permalink
Support for gke pod cidr overprovision config (#7673) (#5468)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
Co-authored-by: Scott Suarez <ScottSuarez@google.com>
  • Loading branch information
modular-magician and ScottSuarez authored Apr 11, 2023
1 parent a6baaf8 commit 4e81650
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .changelog/7673.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `pod_cidr_overprovision_config` field to `google_container_cluster` and `google_container_node_pool` resources.
```
28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/hashstructure v1.1.0
github.com/sirupsen/logrus v1.8.1
golang.org/x/net v0.8.0
golang.org/x/oauth2 v0.6.0
google.golang.org/api v0.114.0
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.29.1
golang.org/x/net v0.9.0
golang.org/x/oauth2 v0.7.0
google.golang.org/api v0.117.0
google.golang.org/genproto v0.0.0-20230403163135-c38d8f061ccd
google.golang.org/grpc v1.54.0
google.golang.org/protobuf v1.30.0
)

require (
bitbucket.org/creachadair/stringset v0.0.8 // indirect
cloud.google.com/go v0.110.0 // indirect
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute v1.19.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
Expand All @@ -51,12 +51,13 @@ require (
github.com/gammazero/deque v0.0.0-20180920172122-f6adf94963e4 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 // indirect
github.com/google/s2a-go v0.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/googleapis/gax-go/v2 v2.8.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
Expand All @@ -69,7 +70,6 @@ require (
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/jhump/protoreflect v1.6.1 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -84,9 +84,9 @@ require (
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.11.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
63 changes: 33 additions & 30 deletions go.sum

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,22 @@ func ResourceContainerCluster() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV4_IPV6"}, false),
Description: `The IP Stack type of the cluster. Choose between IPV4 and IPV4_IPV6. Default type is IPV4 Only if not set`,
},
"pod_cidr_overprovision_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Description: `Configuration for cluster level pod cidr overprovision. Default is disabled=false.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disabled": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -3898,6 +3914,18 @@ func expandClusterAddonsConfig(configured interface{}) *container.AddonsConfig {
return ac
}

func expandPodCidrOverprovisionConfig(configured interface{}) *container.PodCIDROverprovisionConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}
config := l[0].(map[string]interface{})
return &container.PodCIDROverprovisionConfig{
Disable: config["disabled"].(bool),
ForceSendFields: []string{"Disable"},
}
}

func expandIPAllocationPolicy(configured interface{}, networkingMode string) (*container.IPAllocationPolicy, error) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand All @@ -3924,6 +3952,7 @@ func expandIPAllocationPolicy(configured interface{}, networkingMode string) (*c
ForceSendFields: []string{"UseIpAliases"},
UseRoutes: networkingMode == "ROUTES",
StackType: stackType,
PodCidrOverprovisionConfig: expandPodCidrOverprovisionConfig(config["pod_cidr_overprovision_config"]),
}, nil
}

Expand Down Expand Up @@ -5041,6 +5070,18 @@ func flattenIdentityServiceConfig(c *container.IdentityServiceConfig, d *schema.
}
}

func flattenPodCidrOverprovisionConfig(c *container.PodCIDROverprovisionConfig) []map[string]interface{} {
if c == nil {
return nil
}

return []map[string]interface{}{
{
"disabled": c.Disable,
},
}
}

func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, config *Config) ([]map[string]interface{}, error) {
// If IP aliasing isn't enabled, none of the values in this block can be set.
if c == nil || c.IpAllocationPolicy == nil || !c.IpAllocationPolicy.UseIpAliases {
Expand Down Expand Up @@ -5069,6 +5110,7 @@ func flattenIPAllocationPolicy(c *container.Cluster, d *schema.ResourceData, con
"cluster_secondary_range_name": p.ClusterSecondaryRangeName,
"services_secondary_range_name": p.ServicesSecondaryRangeName,
"stack_type": p.StackType,
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(p.PodCidrOverprovisionConfig),
},
}, nil
}
Expand Down
64 changes: 64 additions & 0 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,34 @@ func TestAccContainerCluster_stackType_withSingleStack(t *testing.T) {
})
}

func TestAccContainerCluster_with_PodCIDROverprovisionDisabled(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
containerNetName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
resourceName := "google_container_cluster.with_pco_disabled"

VcrTest(t, resource.TestCase{
PreCheck: func() { AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_with_PodCIDROverprovisionDisabled(containerNetName, clusterName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "ip_allocation_policy.0.pod_cidr_overprovision_config.0.disabled", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"min_master_version"},
},
},
})
}

func TestAccContainerCluster_nodeAutoprovisioning(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -6322,6 +6350,42 @@ resource "google_container_cluster" "with_stack_type" {
`, containerNetName, clusterName)
}

func testAccContainerCluster_with_PodCIDROverprovisionDisabled(containerNetName string, clusterName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "container_network" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "container_subnetwork" {
name = google_compute_network.container_network.name
network = google_compute_network.container_network.name
region = "us-central1"
ip_cidr_range = "10.0.0.0/16"
}
resource "google_container_cluster" "with_pco_disabled" {
name = "%s"
location = "us-central1-a"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
min_master_version = "1.23"
initial_node_count = 1
datapath_provider = "ADVANCED_DATAPATH"
ip_allocation_policy {
cluster_ipv4_cidr_block = "10.1.0.0/16"
services_ipv4_cidr_block = "10.2.0.0/16"
pod_cidr_overprovision_config {
disabled = true
}
}
}
`, containerNetName, clusterName)
}

func testAccContainerCluster_withResourceUsageExportConfig(clusterName, datasetId, enableMetering string) string {
return fmt.Sprintf(`
provider "google" {
Expand Down
28 changes: 24 additions & 4 deletions google-beta/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ var schemaNodePool = map[string]*schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `The ID of the secondary range for pod IPs. If create_pod_range is true, this ID is used for the new range. If create_pod_range is false, uses an existing secondary range with this ID.`,
},
"pod_ipv4_cidr_block": {
Expand All @@ -352,6 +353,22 @@ var schemaNodePool = map[string]*schema.Schema{
ValidateFunc: validateIpCidrRange,
Description: `The IP address range for pod IPs in this node pool. Only applicable if create_pod_range is true. Set to blank to have a range chosen with the default size. Set to /netmask (e.g. /14) to have a range chosen with a specific netmask. Set to a CIDR notation (e.g. 10.96.0.0/14) to pick a specific range to use.`,
},
"pod_cidr_overprovision_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Description: `Configuration for node-pool level pod cidr overprovision. If not set, the cluster level setting will be inherited`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"disabled": {
Type: schema.TypeBool,
Required: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -1068,10 +1085,11 @@ func flattenNodeNetworkConfig(c *container.NodeNetworkConfig, d *schema.Resource
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"create_pod_range": d.Get(prefix + "network_config.0.create_pod_range"), // API doesn't return this value so we set the old one. Field is ForceNew + Required
"pod_ipv4_cidr_block": c.PodIpv4CidrBlock,
"pod_range": c.PodRange,
"enable_private_nodes": c.EnablePrivateNodes,
"create_pod_range": d.Get(prefix + "network_config.0.create_pod_range"), // API doesn't return this value so we set the old one. Field is ForceNew + Required
"pod_ipv4_cidr_block": c.PodIpv4CidrBlock,
"pod_range": c.PodRange,
"enable_private_nodes": c.EnablePrivateNodes,
"pod_cidr_overprovision_config": flattenPodCidrOverprovisionConfig(c.PodCidrOverprovisionConfig),
})
}
return result
Expand Down Expand Up @@ -1105,6 +1123,8 @@ func expandNodeNetworkConfig(v interface{}) *container.NodeNetworkConfig {
nnc.ForceSendFields = []string{"EnablePrivateNodes"}
}

nnc.PodCidrOverprovisionConfig = expandPodCidrOverprovisionConfig(networkNodeConfig["pod_cidr_overprovision_config"])

return nnc
}

Expand Down
23 changes: 22 additions & 1 deletion google-beta/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ func TestAccContainerNodePool_withNetworkConfig(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccContainerNodePool_withNetworkConfig(cluster, np, network),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_container_node_pool.with_pco_disabled", "network_config.0.pod_cidr_overprovision_config.0.disabled", "true"),
),
},
{
ResourceName: "google_container_node_pool.with_manual_pod_cidr",
Expand Down Expand Up @@ -2541,7 +2545,24 @@ resource "google_container_node_pool" "with_auto_pod_cidr" {
}
}
`, network, cluster, np, np)
resource "google_container_node_pool" "with_pco_disabled" {
name = "%s-pco"
location = "us-central1"
cluster = google_container_cluster.cluster.name
node_count = 1
network_config {
pod_cidr_overprovision_config {
disabled = true
}
}
node_config {
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
]
}
}
`, network, cluster, np, np, np)
}

func testAccContainerNodePool_withBootDiskKmsKey(cluster, np string) string {
Expand Down

0 comments on commit 4e81650

Please sign in to comment.