Skip to content

Commit 9ca905d

Browse files
authored
Merge pull request #1034 from emilymye/container_network_policy_1031
Add diff suppress for empty network policy provider in GKE cluster
2 parents 97cc6fb + 10ef26a commit 9ca905d

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

google/resource_container_cluster.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ func resourceContainerCluster() *schema.Resource {
303303
Default: false,
304304
},
305305
"provider": {
306-
Type: schema.TypeString,
307-
Default: "PROVIDER_UNSPECIFIED",
308-
Optional: true,
309-
ValidateFunc: validation.StringInSlice([]string{"PROVIDER_UNSPECIFIED", "CALICO"}, false),
306+
Type: schema.TypeString,
307+
Default: "PROVIDER_UNSPECIFIED",
308+
Optional: true,
309+
ValidateFunc: validation.StringInSlice([]string{"PROVIDER_UNSPECIFIED", "CALICO"}, false),
310+
DiffSuppressFunc: emptyOrDefaultStringSuppress("PROVIDER_UNSPECIFIED"),
310311
},
311312
},
312313
},

google/resource_container_cluster_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
func TestAccContainerCluster_basic(t *testing.T) {
2121
t.Parallel()
2222

23+
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
2324
resource.Test(t, resource.TestCase{
2425
PreCheck: func() { testAccPreCheck(t) },
2526
Providers: testAccProviders,
2627
CheckDestroy: testAccCheckContainerClusterDestroy,
2728
Steps: []resource.TestStep{
2829
{
29-
Config: testAccContainerCluster_basic(fmt.Sprintf("cluster-test-%s", acctest.RandString(10))),
30+
Config: testAccContainerCluster_basic(clusterName),
3031
Check: resource.ComposeTestCheckFunc(
3132
testAccCheckContainerCluster(
3233
"google_container_cluster.primary"),
@@ -135,6 +136,20 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
135136
"network_policy"),
136137
),
137138
},
139+
{
140+
Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName),
141+
Check: resource.ComposeTestCheckFunc(
142+
testAccCheckContainerCluster(
143+
"google_container_cluster.with_network_policy_enabled"),
144+
resource.TestCheckResourceAttr("google_container_cluster.with_network_policy_enabled",
145+
"network_policy.0.enabled", "false"),
146+
),
147+
},
148+
{
149+
Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName),
150+
PlanOnly: true,
151+
ExpectNonEmptyPlan: false,
152+
},
138153
},
139154
})
140155
}
@@ -1085,6 +1100,17 @@ resource "google_container_cluster" "with_network_policy_enabled" {
10851100
}`, clusterName)
10861101
}
10871102

1103+
func testAccContainerCluster_withNetworkPolicyDisabled(clusterName string) string {
1104+
return fmt.Sprintf(`
1105+
resource "google_container_cluster" "with_network_policy_enabled" {
1106+
name = "%s"
1107+
zone = "us-central1-a"
1108+
initial_node_count = 1
1109+
1110+
network_policy = {}
1111+
}`, clusterName)
1112+
}
1113+
10881114
func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string) string {
10891115

10901116
cidrBlocks := ""

google/utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func optionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc {
184184
}
185185
}
186186

187+
func emptyOrDefaultStringSuppress(defaultVal string) schema.SchemaDiffSuppressFunc {
188+
return func(k, old, new string, d *schema.ResourceData) bool {
189+
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)
190+
}
191+
}
192+
187193
func ipCidrRangeDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
188194
// The range may be a:
189195
// A) single IP address (e.g. 10.2.3.4)

google/utils_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,43 @@ func TestDatasourceSchemaFromResourceSchema(t *testing.T) {
404404
})
405405
}
406406
}
407+
408+
func TestEmptyOrDefaultStringSuppress(t *testing.T) {
409+
testFunc := emptyOrDefaultStringSuppress("default value")
410+
411+
cases := map[string]struct {
412+
Old, New string
413+
ExpectDiffSupress bool
414+
}{
415+
"same value, format changed from empty to default": {
416+
Old: "",
417+
New: "default value",
418+
ExpectDiffSupress: true,
419+
},
420+
"same value, format changed from default to empty": {
421+
Old: "default value",
422+
New: "",
423+
ExpectDiffSupress: true,
424+
},
425+
"different value, format changed from empty to non-default": {
426+
Old: "",
427+
New: "not default new",
428+
ExpectDiffSupress: false,
429+
},
430+
"different value, format changed from non-default to empty": {
431+
Old: "not default old",
432+
New: "",
433+
ExpectDiffSupress: false,
434+
},
435+
"different value, format changed from non-default to non-default": {
436+
Old: "not default 1",
437+
New: "not default 2",
438+
ExpectDiffSupress: false,
439+
},
440+
}
441+
for tn, tc := range cases {
442+
if testFunc("", tc.Old, tc.New, nil) != tc.ExpectDiffSupress {
443+
t.Errorf("bad: %s, '%s' => '%s' expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSupress)
444+
}
445+
}
446+
}

0 commit comments

Comments
 (0)