Skip to content

Commit 874f81d

Browse files
modular-magicianrileykarson
authored andcommitted
Handle GKE cluster client cert settings correctly (#3751)
Signed-off-by: Modular Magician <[email protected]>
1 parent 348cc1c commit 874f81d

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

google/resource_container_cluster.go

+30-43
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,16 +1663,17 @@ func expandMasterAuth(configured interface{}) *containerBeta.MasterAuth {
16601663
Username: masterAuth["username"].(string),
16611664
Password: masterAuth["password"].(string),
16621665
}
1663-
if _, ok := masterAuth["client_certificate_config"]; ok {
1664-
if len(masterAuth["client_certificate_config"].([]interface{})) > 0 {
1666+
1667+
if v, ok := masterAuth["client_certificate_config"]; ok {
1668+
if len(v.([]interface{})) > 0 {
16651669
clientCertificateConfig := masterAuth["client_certificate_config"].([]interface{})[0].(map[string]interface{})
1666-
if _, ok := clientCertificateConfig["issue_client_certificate"]; ok {
1667-
result.ClientCertificateConfig = &containerBeta.ClientCertificateConfig{
1668-
IssueClientCertificate: clientCertificateConfig["issue_client_certificate"].(bool),
1669-
}
1670+
1671+
result.ClientCertificateConfig = &containerBeta.ClientCertificateConfig{
1672+
IssueClientCertificate: clientCertificateConfig["issue_client_certificate"].(bool),
16701673
}
16711674
}
16721675
}
1676+
16731677
return result
16741678
}
16751679

@@ -1879,11 +1883,18 @@ func flattenMasterAuth(ma *containerBeta.MasterAuth) []map[string]interface{} {
18791883
"cluster_ca_certificate": ma.ClusterCaCertificate,
18801884
},
18811885
}
1882-
if len(ma.ClientCertificate) == 0 {
1883-
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
1884-
{"issue_client_certificate": false},
1885-
}
1886+
1887+
// No version of the GKE API returns the client_certificate_config value.
1888+
// Instead, we need to infer whether or not it was set based on the
1889+
// client cert being returned from the API or not.
1890+
// Previous versions of the provider didn't record anything in state when
1891+
// a client cert was enabled, only setting the block when it was false.
1892+
masterAuth[0]["client_certificate_config"] = []map[string]interface{}{
1893+
{
1894+
"issue_client_certificate": len(ma.ClientCertificate) != 0,
1895+
},
18861896
}
1897+
18871898
return masterAuth
18881899
}
18891900

@@ -1975,30 +1986,6 @@ func cidrOrSizeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
19751986
return strings.HasPrefix(new, "/") && strings.HasSuffix(old, new)
19761987
}
19771988

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-
20021989
// We want to suppress diffs for empty/disabled private cluster config.
20031990
func containerClusterPrivateClusterConfigSuppress(k, old, new string, d *schema.ResourceData) bool {
20041991
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)