Skip to content

Commit 2a9c4ed

Browse files
telemetry integration support added (#3585) (#2149)
* telemetry integration support added * sentence corrected * merge conflict fixed * doc fixed as per PR comments * indentation fixed * implemented PR comments * added beta only block to fix the tests failing in tpg * Update third_party/terraform/website/docs/r/container_cluster.html.markdown Co-authored-by: Dana Hoffman <[email protected]> * Update third_party/terraform/website/docs/r/container_cluster.html.markdown Co-authored-by: Dana Hoffman <[email protected]> * cluster telemtery doc reference pointed Co-authored-by: Dana Hoffman <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Dana Hoffman <[email protected]>
1 parent b143eb6 commit 2a9c4ed

File tree

4 files changed

+155
-12
lines changed

4 files changed

+155
-12
lines changed

.changelog/3585.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note: enhancement
2+
container : Added cluster_telemetry attribute to `google_container_cluster`
3+
```

google-beta/resource_container_cluster.go

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,12 @@ func resourceContainerCluster() *schema.Resource {
539539
},
540540

541541
"logging_service": {
542-
Type: schema.TypeString,
543-
Optional: true,
544-
Default: "logging.googleapis.com/kubernetes",
545-
ValidateFunc: validation.StringInSlice([]string{"logging.googleapis.com", "logging.googleapis.com/kubernetes", "none"}, false),
546-
Description: `The logging service that the cluster should write logs to. Available options include logging.googleapis.com(Legacy Stackdriver), logging.googleapis.com/kubernetes(Stackdriver Kubernetes Engine Logging), and none. Defaults to logging.googleapis.com/kubernetes.`,
542+
Type: schema.TypeString,
543+
Optional: true,
544+
Computed: true,
545+
ConflictsWith: []string{"cluster_telemetry"},
546+
ValidateFunc: validation.StringInSlice([]string{"logging.googleapis.com", "logging.googleapis.com/kubernetes", "none"}, false),
547+
Description: `The logging service that the cluster should write logs to. Available options include logging.googleapis.com(Legacy Stackdriver), logging.googleapis.com/kubernetes(Stackdriver Kubernetes Engine Logging), and none. Defaults to logging.googleapis.com/kubernetes.`,
547548
},
548549

549550
"maintenance_policy": {
@@ -694,11 +695,12 @@ func resourceContainerCluster() *schema.Resource {
694695
},
695696

696697
"monitoring_service": {
697-
Type: schema.TypeString,
698-
Optional: true,
699-
Default: "monitoring.googleapis.com/kubernetes",
700-
ValidateFunc: validation.StringInSlice([]string{"monitoring.googleapis.com", "monitoring.googleapis.com/kubernetes", "none"}, false),
701-
Description: `The monitoring service that the cluster should write metrics to. Automatically send metrics from pods in the cluster to the Google Cloud Monitoring API. VM metrics will be collected by Google Compute Engine regardless of this setting Available options include monitoring.googleapis.com(Legacy Stackdriver), monitoring.googleapis.com/kubernetes(Stackdriver Kubernetes Engine Monitoring), and none. Defaults to monitoring.googleapis.com/kubernetes.`,
698+
Type: schema.TypeString,
699+
Optional: true,
700+
Computed: true,
701+
ConflictsWith: []string{"cluster_telemetry"},
702+
ValidateFunc: validation.StringInSlice([]string{"monitoring.googleapis.com", "monitoring.googleapis.com/kubernetes", "none"}, false),
703+
Description: `The monitoring service that the cluster should write metrics to. Automatically send metrics from pods in the cluster to the Google Cloud Monitoring API. VM metrics will be collected by Google Compute Engine regardless of this setting Available options include monitoring.googleapis.com(Legacy Stackdriver), monitoring.googleapis.com/kubernetes(Stackdriver Kubernetes Engine Monitoring), and none. Defaults to monitoring.googleapis.com/kubernetes.`,
702704
},
703705

704706
"network": {
@@ -1036,6 +1038,22 @@ func resourceContainerCluster() *schema.Resource {
10361038
},
10371039
},
10381040

1041+
"cluster_telemetry": {
1042+
Type: schema.TypeList,
1043+
Optional: true,
1044+
Computed: true,
1045+
MaxItems: 1,
1046+
Elem: &schema.Resource{
1047+
Schema: map[string]*schema.Schema{
1048+
"type": {
1049+
Type: schema.TypeString,
1050+
Required: true,
1051+
ValidateFunc: validation.StringInSlice([]string{"DISABLED", "ENABLED", "SYSTEM_ONLY"}, false),
1052+
},
1053+
},
1054+
},
1055+
},
1056+
10391057
"resource_usage_export_config": {
10401058
Type: schema.TypeList,
10411059
MaxItems: 1,
@@ -1184,8 +1202,9 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
11841202
Enabled: d.Get("enable_shielded_nodes").(bool),
11851203
ForceSendFields: []string{"Enabled"},
11861204
},
1187-
ReleaseChannel: expandReleaseChannel(d.Get("release_channel")),
1188-
EnableTpu: d.Get("enable_tpu").(bool),
1205+
ReleaseChannel: expandReleaseChannel(d.Get("release_channel")),
1206+
ClusterTelemetry: expandClusterTelemetry(d.Get("cluster_telemetry")),
1207+
EnableTpu: d.Get("enable_tpu").(bool),
11891208
NetworkConfig: &containerBeta.NetworkConfig{
11901209
EnableIntraNodeVisibility: d.Get("enable_intranode_visibility").(bool),
11911210
},
@@ -1503,6 +1522,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
15031522
return err
15041523
}
15051524

1525+
if err := d.Set("cluster_telemetry", flattenClusterTelemetry(cluster.ClusterTelemetry)); err != nil {
1526+
return err
1527+
}
1528+
15061529
d.Set("resource_labels", cluster.ResourceLabels)
15071530
d.Set("label_fingerprint", cluster.LabelFingerprint)
15081531

@@ -2168,6 +2191,36 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
21682191

21692192
d.Partial(false)
21702193

2194+
if d.HasChange("cluster_telemetry") {
2195+
req := &containerBeta.UpdateClusterRequest{
2196+
Update: &containerBeta.ClusterUpdate{
2197+
DesiredClusterTelemetry: expandClusterTelemetry(d.Get("cluster_telemetry")),
2198+
},
2199+
}
2200+
updateF := func() error {
2201+
log.Println("[DEBUG] updating cluster_telemetry")
2202+
name := containerClusterFullName(project, location, clusterName)
2203+
op, err := config.clientContainerBeta.Projects.Locations.Clusters.Update(name, req).Do()
2204+
if err != nil {
2205+
return err
2206+
}
2207+
2208+
// Wait until it's updated
2209+
err = containerOperationWait(config, op, project, location, "updating Cluster Telemetry", d.Timeout(schema.TimeoutUpdate))
2210+
log.Println("[DEBUG] done updating cluster_telemetry")
2211+
return err
2212+
}
2213+
2214+
// Call update serially.
2215+
if err := lockedCall(lockKey, updateF); err != nil {
2216+
return err
2217+
}
2218+
2219+
log.Printf("[INFO] GKE cluster %s Cluster Telemetry has been updated to %#v", d.Id(), req.Update.DesiredClusterTelemetry)
2220+
2221+
d.SetPartial("cluster_telemetry")
2222+
}
2223+
21712224
if _, err := containerClusterAwaitRestingState(config, project, location, clusterName, d.Timeout(schema.TimeoutUpdate)); err != nil {
21722225
return err
21732226
}
@@ -2690,6 +2743,17 @@ func expandDatabaseEncryption(configured interface{}) *containerBeta.DatabaseEnc
26902743
}
26912744
}
26922745

2746+
func expandClusterTelemetry(configured interface{}) *containerBeta.ClusterTelemetry {
2747+
l := configured.([]interface{})
2748+
if len(l) == 0 || l[0] == nil {
2749+
return nil
2750+
}
2751+
config := l[0].(map[string]interface{})
2752+
return &containerBeta.ClusterTelemetry{
2753+
Type: config["type"].(string),
2754+
}
2755+
}
2756+
26932757
func expandWorkloadIdentityConfig(configured interface{}) *containerBeta.WorkloadIdentityConfig {
26942758
l := configured.([]interface{})
26952759
if len(l) == 0 || l[0] == nil {
@@ -2913,6 +2977,16 @@ func flattenReleaseChannel(c *containerBeta.ReleaseChannel) []map[string]interfa
29132977
return result
29142978
}
29152979

2980+
func flattenClusterTelemetry(c *containerBeta.ClusterTelemetry) []map[string]interface{} {
2981+
result := []map[string]interface{}{}
2982+
if c != nil {
2983+
result = append(result, map[string]interface{}{
2984+
"type": c.Type,
2985+
})
2986+
}
2987+
return result
2988+
}
2989+
29162990
func flattenWorkloadIdentityConfig(c *containerBeta.WorkloadIdentityConfig) []map[string]interface{} {
29172991
if c == nil {
29182992
return nil

google-beta/resource_container_cluster_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,48 @@ func TestAccContainerCluster_withInvalidReleaseChannel(t *testing.T) {
416416
})
417417
}
418418

419+
func TestAccContainerCluster_withTelemetryEnabled(t *testing.T) {
420+
t.Parallel()
421+
clusterName := fmt.Sprintf("tf-test-cluster-%s", randString(t, 10))
422+
vcrTest(t, resource.TestCase{
423+
PreCheck: func() { testAccPreCheck(t) },
424+
Providers: testAccProviders,
425+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
426+
Steps: []resource.TestStep{
427+
{
428+
Config: testAccContainerCluster_withTelemetryEnabled(clusterName, "ENABLED"),
429+
},
430+
{
431+
ResourceName: "google_container_cluster.with_cluster_telemetry",
432+
ImportStateIdPrefix: "us-central1-a/",
433+
ImportState: true,
434+
ImportStateVerify: true,
435+
ImportStateVerifyIgnore: []string{"min_master_version"},
436+
},
437+
{
438+
Config: testAccContainerCluster_withTelemetryEnabled(clusterName, "DISABLED"),
439+
},
440+
{
441+
ResourceName: "google_container_cluster.with_cluster_telemetry",
442+
ImportStateIdPrefix: "us-central1-a/",
443+
ImportState: true,
444+
ImportStateVerify: true,
445+
ImportStateVerifyIgnore: []string{"min_master_version"},
446+
},
447+
{
448+
Config: testAccContainerCluster_withTelemetryEnabled(clusterName, "SYSTEM_ONLY"),
449+
},
450+
{
451+
ResourceName: "google_container_cluster.with_cluster_telemetry",
452+
ImportStateIdPrefix: "us-central1-a/",
453+
ImportState: true,
454+
ImportStateVerify: true,
455+
ImportStateVerifyIgnore: []string{"min_master_version"},
456+
},
457+
},
458+
})
459+
}
460+
419461
func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) {
420462
t.Parallel()
421463

@@ -2211,6 +2253,21 @@ resource "google_container_cluster" "with_release_channel" {
22112253
`, clusterName, channel)
22122254
}
22132255

2256+
func testAccContainerCluster_withTelemetryEnabled(clusterName string, telemetryType string) string {
2257+
return fmt.Sprintf(`
2258+
resource "google_container_cluster" "with_cluster_telemetry" {
2259+
name = "%s"
2260+
location = "us-central1-a"
2261+
initial_node_count = 1
2262+
min_master_version = "1.15"
2263+
2264+
cluster_telemetry {
2265+
type = "%s"
2266+
}
2267+
}
2268+
`, clusterName, telemetryType)
2269+
}
2270+
22142271
func testAccContainerCluster_removeNetworkPolicy(clusterName string) string {
22152272
return fmt.Sprintf(`
22162273
resource "google_container_cluster" "with_network_policy_enabled" {

website/docs/r/container_cluster.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ region are guaranteed to support the same version.
268268
* `private_cluster_config` - (Optional) Configuration for [private clusters](https://cloud.google.com/kubernetes-engine/docs/how-to/private-clusters),
269269
clusters with private nodes. Structure is documented below.
270270

271+
* `cluster_telemetry` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Configuration for
272+
[ClusterTelemetry](https://cloud.google.com/monitoring/kubernetes-engine/installing#controlling_the_collection_of_application_logs) feature,
273+
Structure is documented below.
274+
271275
* `project` - (Optional) The ID of the project in which the resource belongs. If it
272276
is not provided, the provider project is used.
273277

@@ -308,6 +312,11 @@ subnetwork in which the cluster's instances are launched.
308312
* `enable_intranode_visibility` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
309313
Whether Intra-node visibility is enabled for this cluster. This makes same node pod to pod traffic visible for VPC network.
310314

315+
The `cluster_telemetry` blocks supports
316+
317+
* `type` - Telemetry integration for the cluster. Supported values (`ENABLE, DISABLE, SYSTEM_ONLY`);
318+
`SYSTEM_ONLY` (Only system components are monitored and logged) is only available in GKE versions 1.15 and later.
319+
311320
The `addons_config` block supports:
312321

313322
* `horizontal_pod_autoscaling` - (Optional) The status of the Horizontal Pod Autoscaling

0 commit comments

Comments
 (0)