Skip to content

Commit 2782347

Browse files
bradhoekstraniharika-98
authored andcommitted
Add enterprise_config field to GKE cluster object (GoogleCloudPlatform#12430)
Signed-off-by: Brad Hoekstra <[email protected]>
1 parent 6ba2313 commit 2782347

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

mmv1/third_party/terraform/services/container/resource_container_cluster.go.tmpl

+79
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,30 @@ func ResourceContainerCluster() *schema.Resource {
23232323
},
23242324
},
23252325
{{- end }}
2326+
"enterprise_config": {
2327+
Type: schema.TypeList,
2328+
Optional: true,
2329+
MaxItems: 1,
2330+
Computed: true,
2331+
Description: `Defines the config needed to enable/disable GKE Enterprise`,
2332+
Elem: &schema.Resource{
2333+
Schema: map[string]*schema.Schema{
2334+
"cluster_tier": {
2335+
Type: schema.TypeString,
2336+
Computed: true,
2337+
Description: `Indicates the effective cluster tier. Available options include STANDARD and ENTERPRISE.`,
2338+
},
2339+
"desired_tier": {
2340+
Type: schema.TypeString,
2341+
Optional: true,
2342+
Computed: true,
2343+
ValidateFunc: validation.StringInSlice([]string{"STANDARD", "ENTERPRISE"}, false),
2344+
Description: `Indicates the desired cluster tier. Available options include STANDARD and ENTERPRISE.`,
2345+
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("CLUSTER_TIER_UNSPECIFIED"),
2346+
},
2347+
},
2348+
},
2349+
},
23262350
},
23272351
}
23282352
}
@@ -2645,6 +2669,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
26452669
cluster.SecurityPostureConfig = expandSecurityPostureConfig(v)
26462670
}
26472671

2672+
if v, ok := d.GetOk("enterprise_config"); ok {
2673+
cluster.EnterpriseConfig = expandEnterpriseConfig(v)
2674+
}
2675+
26482676
needUpdateAfterCreate := false
26492677

26502678
// For now PSC based cluster don't support `enable_private_endpoint` on `create`, but only on `update` API call.
@@ -3208,6 +3236,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
32083236
}
32093237
{{- end }}
32103238

3239+
if err := d.Set("enterprise_config", flattenEnterpriseConfig(cluster.EnterpriseConfig)); err != nil {
3240+
return err
3241+
}
3242+
32113243
return nil
32123244
}
32133245

@@ -4532,6 +4564,23 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
45324564
log.Printf("[INFO] GKE cluster %s node pool auto config linux_node_config parameters have been updated", d.Id())
45334565
}
45344566

4567+
if d.HasChange("enterprise_config") && d.HasChange("enterprise_config.0.desired_tier") {
4568+
req := &container.UpdateClusterRequest{
4569+
Update: &container.ClusterUpdate{
4570+
DesiredEnterpriseConfig: &container.DesiredEnterpriseConfig{
4571+
DesiredTier: d.Get("enterprise_config.0.desired_tier").(string),
4572+
},
4573+
},
4574+
}
4575+
updateF := updateFunc(req, "updating GKE cluster Enterprise Config")
4576+
// Call update serially.
4577+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4578+
return err
4579+
}
4580+
4581+
log.Printf("[INFO] GKE cluster %s Enterprise Config has been updated to %#v", d.Id(), req.Update.DesiredSecurityPostureConfig)
4582+
}
4583+
45354584
d.Partial(false)
45364585

45374586
{{ if ne $.TargetVersionName `ga` -}}
@@ -5267,6 +5316,36 @@ func flattenSecurityPostureConfig(spc *container.SecurityPostureConfig) []map[st
52675316
return []map[string]interface{}{result}
52685317
}
52695318

5319+
func expandEnterpriseConfig(configured interface{}) *container.EnterpriseConfig {
5320+
l := configured.([]interface{})
5321+
if len(l) == 0 {
5322+
return nil
5323+
}
5324+
5325+
ec := &container.EnterpriseConfig{}
5326+
enterpriseConfig := l[0].(map[string]interface{})
5327+
if v, ok := enterpriseConfig["cluster_tier"]; ok {
5328+
ec.ClusterTier = v.(string)
5329+
}
5330+
5331+
if v, ok := enterpriseConfig["desired_tier"]; ok {
5332+
ec.DesiredTier = v.(string)
5333+
}
5334+
return ec
5335+
}
5336+
5337+
func flattenEnterpriseConfig(ec *container.EnterpriseConfig) []map[string]interface{} {
5338+
if ec == nil {
5339+
return nil
5340+
}
5341+
result := make(map[string]interface{})
5342+
5343+
result["cluster_tier"] = ec.ClusterTier
5344+
result["desired_tier"] = ec.DesiredTier
5345+
5346+
return []map[string]interface{}{result}
5347+
}
5348+
52705349
func flattenAdditionalPodRangesConfig(ipAllocationPolicy *container.IPAllocationPolicy) []map[string]interface{} {
52715350
if ipAllocationPolicy == nil {
52725351
return nil

mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl

+84
Original file line numberDiff line numberDiff line change
@@ -12802,3 +12802,87 @@ resource "google_container_cluster" "primary" {
1280212802
}
1280312803
`, name, cgroupMode)
1280412804
}
12805+
12806+
func TestAccContainerCluster_withEnterpriseConfig(t *testing.T) {
12807+
t.Parallel()
12808+
12809+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
12810+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
12811+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
12812+
pid := envvar.GetTestProjectFromEnv()
12813+
12814+
acctest.VcrTest(t, resource.TestCase{
12815+
PreCheck: func() { acctest.AccTestPreCheck(t) },
12816+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
12817+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
12818+
Steps: []resource.TestStep{
12819+
{
12820+
Config: testAccContainerCluster_updateEnterpriseConfig(pid, clusterName, networkName, subnetworkName, "STANDARD"),
12821+
},
12822+
{
12823+
ResourceName: "google_container_cluster.with_enterprise_config",
12824+
ImportState: true,
12825+
ImportStateVerify: true,
12826+
ImportStateVerifyIgnore: []string{"deletion_protection"},
12827+
},
12828+
{
12829+
Config: testAccContainerCluster_updateEnterpriseConfig(pid, clusterName, networkName, subnetworkName, "ENTERPRISE"),
12830+
},
12831+
{
12832+
ResourceName: "google_container_cluster.with_enterprise_config",
12833+
ImportState: true,
12834+
ImportStateVerify: true,
12835+
ImportStateVerifyIgnore: []string{"deletion_protection"},
12836+
},
12837+
{
12838+
Config: testAccContainerCluster_removeEnterpriseConfig(pid, clusterName, networkName, subnetworkName),
12839+
},
12840+
{
12841+
ResourceName: "google_container_cluster.with_enterprise_config",
12842+
ImportState: true,
12843+
ImportStateVerify: true,
12844+
ImportStateVerifyIgnore: []string{"deletion_protection"},
12845+
},
12846+
},
12847+
})
12848+
}
12849+
12850+
func testAccContainerCluster_updateEnterpriseConfig(projectID, clusterName, networkName, subnetworkName string, desiredTier string) string {
12851+
return fmt.Sprintf(`
12852+
data "google_project" "project" {
12853+
project_id = "%s"
12854+
}
12855+
12856+
resource "google_container_cluster" "with_enterprise_config" {
12857+
name = "%s"
12858+
location = "us-central1-a"
12859+
initial_node_count = 1
12860+
enterprise_config {
12861+
desired_tier = "%s"
12862+
}
12863+
network = "%s"
12864+
subnetwork = "%s"
12865+
12866+
deletion_protection = false
12867+
}
12868+
`, projectID, clusterName, desiredTier, networkName, subnetworkName)
12869+
}
12870+
12871+
func testAccContainerCluster_removeEnterpriseConfig(projectID, clusterName, networkName, subnetworkName string) string {
12872+
return fmt.Sprintf(`
12873+
data "google_project" "project" {
12874+
project_id = "%s"
12875+
}
12876+
12877+
resource "google_container_cluster" "with_enterprise_config" {
12878+
name = "%s"
12879+
location = "us-central1-a"
12880+
initial_node_count = 1
12881+
network = "%s"
12882+
subnetwork = "%s"
12883+
12884+
deletion_protection = false
12885+
}
12886+
`, projectID, clusterName, networkName, subnetworkName)
12887+
}
12888+

mmv1/third_party/terraform/website/docs/r/container_cluster.html.markdown

+11
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ Fleet configuration for the cluster. Structure is [documented below](#nested_fle
405405
* `workload_alts_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
406406
Configuration for [direct-path (via ALTS) with workload identity.](https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1beta1/projects.locations.clusters#workloadaltsconfig). Structure is [documented below](#nested_workload_alts_config).
407407

408+
* `enterprise_config` - (Optional)
409+
Configuration for [Enterprise edition].(https://cloud.google.com/kubernetes-engine/enterprise/docs/concepts/gke-editions). Structure is [documented below](#nested_enterprise_config).
410+
411+
408412
<a name="nested_default_snat_status"></a>The `default_snat_status` block supports
409413

410414
* `disabled` - (Required) Whether the cluster disables default in-node sNAT rules. In-node sNAT rules will be disabled when defaultSnatStatus is disabled.When disabled is set to false, default IP masquerade rules will be applied to the nodes to prevent sNAT on cluster internal traffic
@@ -1433,6 +1437,11 @@ linux_node_config {
14331437

14341438
* `enable_alts` - (Required) Whether the alts handshaker should be enabled or not for direct-path. Requires Workload Identity ([workloadPool]((#nested_workload_identity_config)) must be non-empty).
14351439

1440+
<a name="nested_enterprise_config"></a>The `enterprise_config` block supports:
1441+
1442+
* `desired_tier` - (Optional) Sets the tier of the cluster. Available options include `STANDARD` and `ENTERPRISE`.
1443+
1444+
14361445
## Attributes Reference
14371446

14381447
In addition to the arguments listed above, the following computed attributes are
@@ -1482,6 +1491,8 @@ exported:
14821491

14831492
* `fleet.0.membership_location` - The location of the fleet membership, extracted from `fleet.0.membership`. You can use this field to configure `membership_location` under [google_gkehub_feature_membership](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/gke_hub_feature_membership).
14841493

1494+
* `enterprise_config.0.cluster_tier` - The effective tier of the cluster.
1495+
14851496
## Timeouts
14861497

14871498
This resource provides the following

0 commit comments

Comments
 (0)