Skip to content

Commit 84a496e

Browse files
rileykarsonmodular-magician
authored andcommitted
Handle GKE cluster client cert settings correctly
Signed-off-by: Modular Magician <[email protected]>
1 parent 1f5ab66 commit 84a496e

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

google/resource_container_cluster.go

+28-39
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,22 @@ func resourceContainerCluster() *schema.Resource {
342342
Optional: true,
343343
},
344344

345+
// Ideally, this would be Optional (and not Computed).
346+
// In past versions (incl. 2.X series) of the provider
347+
// though, being unset was considered identical to set
348+
// and the issue_client_certificate value being true.
345349
"client_certificate_config": {
346-
Type: schema.TypeList,
347-
MaxItems: 1,
348-
Optional: true,
349-
DiffSuppressFunc: masterAuthClientCertCfgSuppress,
350-
ForceNew: true,
350+
Type: schema.TypeList,
351+
MaxItems: 1,
352+
Optional: true,
353+
Computed: true,
354+
ForceNew: true,
351355
Elem: &schema.Resource{
352356
Schema: map[string]*schema.Schema{
353357
"issue_client_certificate": {
354-
Type: schema.TypeBool,
355-
Required: true,
356-
ForceNew: true,
357-
DiffSuppressFunc: masterAuthClientCertCfgSuppress,
358+
Type: schema.TypeBool,
359+
Required: true,
360+
ForceNew: true,
358361
},
359362
},
360363
},
@@ -1660,12 +1663,15 @@ func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
16601663
Username: masterAuth["username"].(string),
16611664
Password: masterAuth["password"].(string),
16621665
}
1666+
16631667
if _, ok := masterAuth["client_certificate_config"]; ok {
16641668
if len(masterAuth["client_certificate_config"].([]interface{})) > 0 {
16651669
clientCertificateConfig := masterAuth["client_certificate_config"].([]interface{})[0].(map[string]interface{})
1666-
if _, ok := clientCertificateConfig["issue_client_certificate"]; ok {
1670+
1671+
// The value will be in the map whether true or false
1672+
if v, ok := clientCertificateConfig["issue_client_certificate"]; ok {
16671673
result.ClientCertificateConfig = &containerBeta.ClientCertificateConfig{
1668-
IssueClientCertificate: clientCertificateConfig["issue_client_certificate"].(bool),
1674+
IssueClientCertificate: v.(bool),
16691675
}
16701676
}
16711677
}
@@ -1879,11 +1885,18 @@ func flattenMasterAuth(ma *containerBeta.MasterAuth) []map[string]interface{} {
18791885
"cluster_ca_certificate": ma.ClusterCaCertificate,
18801886
},
18811887
}
1882-
if len(ma.ClientCertificate) == 0 {
1883-
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
1884-
{"issue_client_certificate": false},
1885-
}
1888+
1889+
// No version of the GKE API returns the client_certificate_config value.
1890+
// Instead, we need to infer whether or not it was set based on the
1891+
// client cert being returned from the API or not.
1892+
// Previous versions of the provider didn't record anything in state when
1893+
// a client cert was enabled, only setting the block when it was false.
1894+
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
1895+
{
1896+
"issue_client_certificate": len(ma.ClientCertificate) != 0,
1897+
},
18861898
}
1899+
18871900
return masterAuth
18881901
}
18891902

@@ -1975,30 +1988,6 @@ func cidrOrSizeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
19751988
return strings.HasPrefix(new, "/") && strings.HasSuffix(old, new)
19761989
}
19771990

1978-
// We want to suppress diffs for empty or default client certificate configs, i.e:
1979-
// [{ "issue_client_certificate": true}] --> []
1980-
// [] -> [{ "issue_client_certificate": true}]
1981-
func masterAuthClientCertCfgSuppress(k, old, new string, r *schema.ResourceData) bool {
1982-
var clientConfig map[string]interface{}
1983-
if v, ok := r.GetOk("master_auth"); ok {
1984-
masterAuths := v.([]interface{})
1985-
masterAuth := masterAuths[0].(map[string]interface{})
1986-
cfgs := masterAuth["client_certificate_config"].([]interface{})
1987-
if len(cfgs) > 0 {
1988-
clientConfig = cfgs[0].(map[string]interface{})
1989-
}
1990-
}
1991-
1992-
if strings.HasSuffix(k, "client_certificate_config.#") && old == "0" && new == "1" {
1993-
// nil --> { "issue_client_certificate": true }
1994-
if issueCert, ok := clientConfig["issue_client_certificate"]; ok {
1995-
return issueCert.(bool)
1996-
}
1997-
}
1998-
1999-
return strings.HasSuffix(k, ".issue_client_certificate") && old == "" && new == "true"
2000-
}
2001-
20021991
// We want to suppress diffs for empty/disabled private cluster config.
20031992
func containerClusterPrivateClusterConfigSuppress(k, old, new string, d *schema.ResourceData) bool {
20041993
o, n := d.GetChange("private_cluster_config.0.enable_private_endpoint")

website/docs/getting_started.html.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_compute_instance" "vm_instance" {
8181
network_interface {
8282
# A default network is created for all GCP projects
8383
network = "default"
84-
access_config {
84+
access_config = {
8585
}
8686
}
8787
}
@@ -135,7 +135,7 @@ network_interface {
135135
- # A default network is created for all GCP projects
136136
- network = "default"
137137
+ network = "${google_compute_network.vpc_network.self_link}"
138-
access_config {
138+
access_config = {
139139
```
140140

141141
This means that when we create the VM instance, it will use
@@ -190,7 +190,7 @@ resource "google_compute_instance" "vm_instance" {
190190
network_interface {
191191
# A default network is created for all GCP projects
192192
network = "${google_compute_network.vpc_network.self_link}"
193-
access_config {
193+
access_config = {
194194
}
195195
}
196196
}

0 commit comments

Comments
 (0)