Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ip aliasing in google_container_cluster #654

Merged
merged 4 commits into from
Nov 27, 2017

Conversation

davidquarles
Copy link
Contributor

Fixes #618.

Note that there are two mutually exclusive paths for GKE clusters with IP aliasing enabled:

  1. pre-allocated subnets
  2. dynamically created subnets, which are a bit more magical and strictly tied to cluster lifecycle

I chose to implement only (1), for now, because:

  • It seemed like the most flexible path
  • There are several tiers of conflicting and dependent attributes. ConflictsWith halfway solves the problem, but AFAIK there's no good abstraction on the other side for dependencies/requirements, making it difficult to implement both strategies and detect failures at plan-time. There are cases where I wasn't able to do so even with the single strategy.

@davidquarles
Copy link
Contributor Author

I can run the full acceptance suite later tonight, but for now:

$ make testacc TEST=./google TESTARGS='-run=TestAccContainerCluster_withIPAllocationPolicy'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccContainerCluster_withIPAllocationPolicy -timeout 120m
=== RUN   TestAccContainerCluster_withIPAllocationPolicy
--- PASS: TestAccContainerCluster_withIPAllocationPolicy (643.20s)
PASS
ok  github.com/terraform-providers/terraform-provider-google/google643.235s

@danawillow danawillow self-requested a review November 7, 2017 20:44
@danawillow danawillow self-assigned this Nov 7, 2017
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa, I'm surprised Terraform lets you set the default for a boolean type to nil. If you're looking for a false default, you don't really need to have one explicitly set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if new == "" {
return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why suppress the diff if new is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

if new == "" {
return true
}
return ipCidrRangeDiffSuppress(k, old, new, d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this field the name, not the range itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

return err
}

region := getRegionFromZone(zoneName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we actually need these checks inside of Terraform. If I get rid of them I get this error from the API: Google Compute Engine: services secondary range "pods" not found in subnet "container-net-asdfafw" which serves the same purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

"services_secondary_range_name": c.ServicesSecondaryRangeName,
}

//if c.UseIpAliases {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be removed :)

}

resource "google_compute_subnetwork" "container_subnetwork" {
name = "${google_compute_network.container_network.name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you fix the indentation here?

}`, rangeName, cidr))
}

var ipAllocationPolicy bytes.Buffer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine leaving this as is, but a potential alternative would be just passing in the configuration block as a string that looked like:

  ip_allocation_policy {
    use_ip_aliases                = "true"
    cluster_secondary_range_name  = "pods"
    services_secondary_range_name = "services"
  }

"ip_allocation_policy": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this is (and the others are) computed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

@davidquarles
Copy link
Contributor Author

@danawillow My apologies for the shoddy / incomplete initial PR. I was a bit sleep-deprived that week; all of your comments were right on. I made all the changes you asked for, and I removed use_ip_aliases param in favor of simply checking existence of the outer config block (as you suggested with master_authorized_networks_config). Let me know what you think. Thanks!

$ make testacc TEST=./google TESTARGS='-run=TestAccContainerCluster_withIPAllocationPolicy'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccContainerCluster_withIPAllocationPolicy -timeout 120m
=== RUN   TestAccContainerCluster_withIPAllocationPolicy
--- PASS: TestAccContainerCluster_withIPAllocationPolicy (1336.11s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	1336.136s

@@ -184,6 +186,18 @@ maintenance_policy {
}
```

The `ip_allocation_policy` block supports:

* `cluster_secondary_range_name` - (Optional) The name of the secondary range to be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last sentence of this (and the next) are no longer applicable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

resource "google_container_cluster" "with_ip_allocation_policy" {
name = "with-ip-allocation-policy"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give this a name with some randomness? We usually do that so if the test fails and the resource is lingering, we can still run the test before cleaning it up again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"ip_allocation_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it really makes a difference, but let's make this ForceNew as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -589,6 +609,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
}
d.Set("node_pool", nps)

if cluster.IpAllocationPolicy != nil && cluster.IpAllocationPolicy.UseIpAliases {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think we should set it no matter what (and this might be a bug elsewhere in the file). If I create a cluster with IP Aliasing, then remove IP Aliasing outside of Terraform, I would want my state to get updated. It's sort of a moot point since IPAllocationPolicy doesn't support update right now, but this will help future-proof it.

Here's what I think flattenIPAllocationPolicy should look like:

func flattenIPAllocationPolicy(c *container.IPAllocationPolicy) []map[string]interface{} {
	result := []map[string]interface{}{}
	if c != nil && c.UseIpAliases {
		result = append(result, map[string]interface{}{
			"cluster_secondary_range_name":  c.ClusterSecondaryRangeName,
			"services_secondary_range_name": c.ServicesSecondaryRangeName,
		})
	}
	return result
}

and then the call to set it would be:

	if err := d.Set("ip_allocation_policy", flattenIPAllocationPolicy(cluster.IpAllocationPolicy)); err != nil {
		return err
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to do the nil check, otherwise the code will panic when trying to access the struct's fields (you can prove it by running TestAccContainerCluster_basic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this comment slipped through the cracks and fixed it.

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccContainerCluster_basic -timeout 120m
=== RUN   TestAccContainerCluster_basic
--- PASS: TestAccContainerCluster_basic (356.83s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	356.853s

@@ -925,6 +949,30 @@ func expandClusterAddonsConfig(configured interface{}) *container.AddonsConfig {
return ac
}

func expandIPAllocationPolicy(configured interface{}) (*container.IPAllocationPolicy, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you accidentally got rid of the call to this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, yeah... Fixed!

},
),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add checks for ip_allocation_policy into testAccCheckContainerCluster? I also wouldn't mind seeing some resource.TestCheckResourceAttrs but I won't block on those

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and done

Copy link
Contributor Author

@davidquarles davidquarles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ make testacc TEST=./google TESTARGS='-run=TestAccContainerCluster_withIPAllocationPolicy'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./google -v -run=TestAccContainerCluster_withIPAllocationPolicy -timeout 120m
=== RUN   TestAccContainerCluster_withIPAllocationPolicy
--- PASS: TestAccContainerCluster_withIPAllocationPolicy (914.36s)
PASS
ok  	github.com/terraform-providers/terraform-provider-google/google	914.391s

"ip_allocation_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -589,6 +609,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
}
d.Set("node_pool", nps)

if cluster.IpAllocationPolicy != nil && cluster.IpAllocationPolicy.UseIpAliases {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -925,6 +949,30 @@ func expandClusterAddonsConfig(configured interface{}) *container.AddonsConfig {
return ac
}

func expandIPAllocationPolicy(configured interface{}) (*container.IPAllocationPolicy, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, yeah... Fixed!

@@ -184,6 +186,18 @@ maintenance_policy {
}
```

The `ip_allocation_policy` block supports:

* `cluster_secondary_range_name` - (Optional) The name of the secondary range to be
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@danawillow
Copy link
Contributor

Thanks @davidquarles!

@danawillow danawillow merged commit d57db91 into hashicorp:master Nov 27, 2017
chrisst pushed a commit to chrisst/terraform-provider-google that referenced this pull request Nov 9, 2018
)

* add support for ip aliasing in `google_container_cluster`

* [review] cleanup galore, infer feature enablement from `ip_allocation_policy`

* [review] cleanup, round 2

* add nil check back (when reading ip allocation policy from API)
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
…end-service-changelog

Add region_backend_service to CHANGELOG
@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for IP Aliases to google_container_cluster
3 participants