Skip to content

Commit

Permalink
add networking_mode to google_container_cluster (#3709) (#2243)
Browse files Browse the repository at this point in the history
* add  to

* review comment updates

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jun 27, 2020
1 parent 2bff074 commit a6bd926
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/3709.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
container: added `networking_mode` to `google_container_cluster` (TPGB-only)
```
30 changes: 25 additions & 5 deletions google-beta/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,15 @@ func resourceContainerCluster() *schema.Resource {
},
},

"networking_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"VPC_NATIVE", "ROUTES"}, false),
Description: `Determines whether alias IPs or routes will be used for pod IPs in the cluster.`,
},

"remove_default_node_pool": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1190,6 +1199,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er

clusterName := d.Get("name").(string)

ipAllocationBlock, err := expandIPAllocationPolicy(d.Get("ip_allocation_policy"), d.Get("networking_mode").(string))
if err != nil {
return err
}

cluster := &containerBeta.Cluster{
Name: clusterName,
InitialNodeCount: int64(d.Get("initial_node_count").(int)),
Expand All @@ -1207,7 +1221,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
NetworkPolicy: expandNetworkPolicy(d.Get("network_policy")),
AddonsConfig: expandClusterAddonsConfig(d.Get("addons_config")),
EnableKubernetesAlpha: d.Get("enable_kubernetes_alpha").(bool),
IpAllocationPolicy: expandIPAllocationPolicy(d.Get("ip_allocation_policy")),
IpAllocationPolicy: ipAllocationBlock,
PodSecurityPolicyConfig: expandPodSecurityPolicyConfig(d.Get("pod_security_policy_config")),
Autoscaling: expandClusterAutoscaling(d.Get("cluster_autoscaling"), d),
BinaryAuthorization: &containerBeta.BinaryAuthorization{
Expand Down Expand Up @@ -2480,25 +2494,29 @@ func expandClusterAddonsConfig(configured interface{}) *containerBeta.AddonsConf
return ac
}

func expandIPAllocationPolicy(configured interface{}) *containerBeta.IPAllocationPolicy {
func expandIPAllocationPolicy(configured interface{}, networking_mode string) (*containerBeta.IPAllocationPolicy, error) {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
if networking_mode == "VPC_NATIVE" {
return nil, fmt.Errorf("`ip_allocation_policy` block is required for VPC_NATIVE clusters.")
}
return &containerBeta.IPAllocationPolicy{
UseIpAliases: false,
ForceSendFields: []string{"UseIpAliases"},
}
}, nil
}

config := l[0].(map[string]interface{})
return &containerBeta.IPAllocationPolicy{
UseIpAliases: true,
UseIpAliases: networking_mode == "VPC_NATIVE",
ClusterIpv4CidrBlock: config["cluster_ipv4_cidr_block"].(string),
ServicesIpv4CidrBlock: config["services_ipv4_cidr_block"].(string),

ClusterSecondaryRangeName: config["cluster_secondary_range_name"].(string),
ServicesSecondaryRangeName: config["services_secondary_range_name"].(string),
ForceSendFields: []string{"UseIpAliases"},
}
UseRoutes: networking_mode == "ROUTES",
}, nil
}

func expandMaintenancePolicy(d *schema.ResourceData, meta interface{}) *containerBeta.MaintenancePolicy {
Expand Down Expand Up @@ -3042,8 +3060,10 @@ func flattenWorkloadIdentityConfig(c *containerBeta.WorkloadIdentityConfig) []ma
func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData, config *Config) []map[string]interface{} {
// 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 {
d.Set("networking_mode", "ROUTES")
return nil
}
d.Set("networking_mode", "VPC_NATIVE")

p := c.IpAllocationPolicy
return []map[string]interface{}{
Expand Down
26 changes: 20 additions & 6 deletions google-beta/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,7 @@ resource "google_container_cluster" "with_authenticator_groups" {
security_group = "gke-security-groups@mydomain.tld"
}
networking_mode = "VPC_NATIVE"
ip_allocation_policy {
cluster_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[0].range_name
services_secondary_range_name = google_compute_subnetwork.container_subnetwork.secondary_ip_range[1].range_name
Expand Down Expand Up @@ -2483,8 +2484,9 @@ resource "google_container_cluster" "with_tpu" {
enable_tpu = true
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
networking_mode = "VPC_NATIVE"
private_cluster_config {
enable_private_endpoint = true
Expand Down Expand Up @@ -3319,6 +3321,7 @@ resource "google_container_cluster" "with_ip_allocation_policy" {
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
networking_mode = "VPC_NATIVE"
initial_node_count = 1
ip_allocation_policy {
cluster_secondary_range_name = "pods"
Expand Down Expand Up @@ -3350,6 +3353,8 @@ resource "google_container_cluster" "with_ip_allocation_policy" {
subnetwork = google_compute_subnetwork.container_subnetwork.name
initial_node_count = 1
networking_mode = "VPC_NATIVE"
ip_allocation_policy {
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
Expand Down Expand Up @@ -3380,6 +3385,8 @@ resource "google_container_cluster" "with_ip_allocation_policy" {
subnetwork = google_compute_subnetwork.container_subnetwork.name
initial_node_count = 1
networking_mode = "VPC_NATIVE"
ip_allocation_policy {
cluster_ipv4_cidr_block = "/16"
services_ipv4_cidr_block = "/22"
Expand Down Expand Up @@ -3457,6 +3464,7 @@ resource "google_container_cluster" "with_private_cluster" {
location = "us-central1-a"
initial_node_count = 1
networking_mode = "VPC_NATIVE"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
Expand Down Expand Up @@ -3504,6 +3512,7 @@ resource "google_container_cluster" "with_private_cluster" {
location = "us-central1-a"
initial_node_count = 1
networking_mode = "VPC_NATIVE"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
Expand Down Expand Up @@ -3673,8 +3682,9 @@ resource "google_container_cluster" "shared_vpc_cluster" {
initial_node_count = 1
project = google_compute_shared_vpc_service_project.service_project.service_project
network = google_compute_network.shared_network.self_link
subnetwork = google_compute_subnetwork.shared_subnetwork.self_link
networking_mode = "VPC_NATIVE"
network = google_compute_network.shared_network.self_link
subnetwork = google_compute_subnetwork.shared_subnetwork.self_link
ip_allocation_policy {
cluster_secondary_range_name = google_compute_subnetwork.shared_subnetwork.secondary_ip_range[0].range_name
Expand Down Expand Up @@ -3732,8 +3742,9 @@ resource "google_container_cluster" "with_flexible_cidr" {
location = "us-central1-a"
initial_node_count = 3
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
networking_mode = "VPC_NATIVE"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
private_cluster_config {
enable_private_endpoint = true
Expand Down Expand Up @@ -3771,6 +3782,7 @@ resource "google_container_cluster" "cidr_error_preempt" {
name = "%s"
location = "us-central1-a"
networking_mode = "VPC_NATIVE"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
Expand All @@ -3797,6 +3809,7 @@ resource "google_container_cluster" "cidr_error_overlap" {
initial_node_count = 1
networking_mode = "VPC_NATIVE"
ip_allocation_policy {
cluster_ipv4_cidr_block = "10.0.0.0/16"
services_ipv4_cidr_block = "10.1.0.0/16"
Expand Down Expand Up @@ -3878,6 +3891,7 @@ resource "google_container_cluster" "with_private_cluster" {
location = "us-central1-a"
initial_node_count = 1
networking_mode = "VPC_NATIVE"
network = google_compute_network.container_network.name
subnetwork = google_compute_subnetwork.container_subnetwork.name
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ VPC-native clusters. Adding this block enables [IP aliasing](https://cloud.googl
making the cluster VPC-native instead of routes-based. Structure is documented
below.

* `networking_mode` - (Optional, [Beta]) Determines whether alias IPs or routes will be used for pod IPs in the cluster.
Options are `VPC_NATIVE` or `ROUTES`. `VPC_NATIVE` enables [IP aliasing](https://cloud.google.com/kubernetes-engine/docs/how-to/ip-aliases),
and requires the `ip_allocation_policy` block to be defined. By default when this field is unspecified, GKE will create a `ROUTES`-based cluster.

* `logging_service` - (Optional) The logging service that the cluster should
write logs to. Available options include `logging.googleapis.com`(Legacy Stackdriver),
`logging.googleapis.com/kubernetes`(Stackdriver Kubernetes Engine Logging), and `none`. Defaults to `logging.googleapis.com/kubernetes`
Expand Down

0 comments on commit a6bd926

Please sign in to comment.