From b7f465487cb6d6e40f341911850c2cd1894a117b Mon Sep 17 00:00:00 2001 From: Christian Bell Date: Fri, 11 Jun 2021 13:07:31 -0400 Subject: [PATCH] Remove support for legacy GKE monitoring resource model (#289) Details: https://cloud.google.com/stackdriver/docs/deprecations/legacy --- monitoredresource/deprecated_test.go | 28 +---------------- monitoredresource/gcp/gcp_metadata_config.go | 28 ++--------------- monitoredresource/gcp/monitored_resources.go | 18 +++-------- .../gcp/monitored_resources_test.go | 30 ------------------- 4 files changed, 8 insertions(+), 96 deletions(-) diff --git a/monitoredresource/deprecated_test.go b/monitoredresource/deprecated_test.go index 4f46763..ecb6045 100644 --- a/monitoredresource/deprecated_test.go +++ b/monitoredresource/deprecated_test.go @@ -29,7 +29,7 @@ const ( GKEClusterNameStr = "cluster" ) -func TestGKEContainerMonitoredResources(t *testing.T) { +func TestGKEContainerMonitoredResourcesV2(t *testing.T) { os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1") autoDetected := GKEContainer{ InstanceID: GCPInstanceIDStr, @@ -41,32 +41,6 @@ func TestGKEContainerMonitoredResources(t *testing.T) { PodID: GKEPodIDStr, } - resType, labels := autoDetected.MonitoredResource() - if resType != "gke_container" || - labels["instance_id"] != GCPInstanceIDStr || - labels["project_id"] != GCPProjectIDStr || - labels["cluster_name"] != GKEClusterNameStr || - labels["container_name"] != GKEContainerNameStr || - labels["zone"] != GCPZoneStr || - labels["namespace_id"] != GKENamespaceStr || - labels["pod_id"] != GKEPodIDStr { - t.Errorf("GKEContainerMonitoredResource Failed: %v", autoDetected) - } -} - -func TestGKEContainerMonitoredResourcesV2(t *testing.T) { - os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1") - autoDetected := GKEContainer{ - InstanceID: GCPInstanceIDStr, - ProjectID: GCPProjectIDStr, - Zone: GCPZoneStr, - ClusterName: GKEClusterNameStr, - ContainerName: GKEContainerNameStr, - NamespaceID: GKENamespaceStr, - PodID: GKEPodIDStr, - LoggingMonitoringV2Enabled: true, - } - resType, labels := autoDetected.MonitoredResource() if resType != "k8s_container" || labels["project_id"] != GCPProjectIDStr || diff --git a/monitoredresource/gcp/gcp_metadata_config.go b/monitoredresource/gcp/gcp_metadata_config.go index ccaade6..b9c6d13 100644 --- a/monitoredresource/gcp/gcp_metadata_config.go +++ b/monitoredresource/gcp/gcp_metadata_config.go @@ -15,15 +15,11 @@ package gcp import ( - "context" - "fmt" "log" "os" "strings" "cloud.google.com/go/compute/metadata" - container "cloud.google.com/go/container/apiv1" - containerpb "google.golang.org/genproto/googleapis/container/v1" ) // gcpMetadata represents metadata retrieved from GCP (GKE and GCE) environment. @@ -50,6 +46,7 @@ type gcpMetadata struct { // zone is the Compute Engine zone in which the VM is running. zone string + // monitoringV2 is currently always set to true as v1 has been deprecated. monitoringV2 bool } @@ -76,33 +73,14 @@ func retrieveGCPMetadata() *gcpMetadata { logError(err) gcpMetadata.clusterName = strings.TrimSpace(clusterName) - clusterLocation, err := metadata.InstanceAttributeValue("cluster-location") - logError(err) - // Following attributes are derived from environment variables. They are configured // via yaml file. For details refer to: // https://cloud.google.com/kubernetes-engine/docs/tutorials/custom-metrics-autoscaling#exporting_metrics_from_the_application gcpMetadata.namespaceID = os.Getenv("NAMESPACE") gcpMetadata.containerName = os.Getenv("CONTAINER_NAME") gcpMetadata.podID = os.Getenv("HOSTNAME") - - // Monitoring API version can be obtained from cluster info.q - if gcpMetadata.clusterName != "" { - ctx := context.Background() - c, err := container.NewClusterManagerClient(ctx) - logError(err) - if c != nil { - req := &containerpb.GetClusterRequest{ - Name: fmt.Sprintf("projects/%s/locations/%s/clusters/%s", gcpMetadata.projectID, strings.TrimSpace(clusterLocation), gcpMetadata.clusterName), - } - resp, err := c.GetCluster(ctx, req) - logError(err) - if resp != nil && resp.GetMonitoringService() == "monitoring.googleapis.com/kubernetes" && - resp.GetLoggingService() == "logging.googleapis.com/kubernetes" { - gcpMetadata.monitoringV2 = true - } - } - } + // Monitoring API v2 is now default. + gcpMetadata.monitoringV2 = true return &gcpMetadata } diff --git a/monitoredresource/gcp/monitored_resources.go b/monitoredresource/gcp/monitored_resources.go index 1a08b1c..d736a1f 100644 --- a/monitoredresource/gcp/monitored_resources.go +++ b/monitoredresource/gcp/monitored_resources.go @@ -62,21 +62,11 @@ func (gke *GKEContainer) MonitoredResource() (resType string, labels map[string] "project_id": gke.ProjectID, "cluster_name": gke.ClusterName, "container_name": gke.ContainerName, + "pod_name": gke.PodID, + "namespace_name": gke.NamespaceID, + "location": gke.Zone, } - var typ string - if gke.LoggingMonitoringV2Enabled { - typ = "k8s_container" - labels["pod_name"] = gke.PodID - labels["namespace_name"] = gke.NamespaceID - labels["location"] = gke.Zone - } else { - typ = "gke_container" - labels["pod_id"] = gke.PodID - labels["namespace_id"] = gke.NamespaceID - labels["zone"] = gke.Zone - labels["instance_id"] = gke.InstanceID - } - return typ, labels + return "k8s_container", labels } // GCEInstance represents gce_instance type monitored resource. diff --git a/monitoredresource/gcp/monitored_resources_test.go b/monitoredresource/gcp/monitored_resources_test.go index 7d27c6c..3475265 100644 --- a/monitoredresource/gcp/monitored_resources_test.go +++ b/monitoredresource/gcp/monitored_resources_test.go @@ -42,36 +42,6 @@ func TestGKEContainerMonitoredResources(t *testing.T) { } autoDetected := detectResourceType(&gcpMetadata) - if autoDetected == nil { - t.Fatal("GKEContainerMonitoredResource nil") - } - resType, labels := autoDetected.MonitoredResource() - if resType != "gke_container" || - labels["instance_id"] != GCPInstanceIDStr || - labels["project_id"] != GCPProjectIDStr || - labels["cluster_name"] != GKEClusterNameStr || - labels["container_name"] != GKEContainerNameStr || - labels["zone"] != GCPZoneStr || - labels["namespace_id"] != GKENamespaceStr || - labels["pod_id"] != GKEPodIDStr { - t.Errorf("GKEContainerMonitoredResource Failed: %v", autoDetected) - } -} - -func TestGKEContainerMonitoredResourcesV2(t *testing.T) { - os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1") - gcpMetadata := gcpMetadata{ - instanceID: GCPInstanceIDStr, - projectID: GCPProjectIDStr, - zone: GCPZoneStr, - clusterName: GKEClusterNameStr, - containerName: GKEContainerNameStr, - namespaceID: GKENamespaceStr, - podID: GKEPodIDStr, - monitoringV2: true, - } - autoDetected := detectResourceType(&gcpMetadata) - if autoDetected == nil { t.Fatal("GKEContainerMonitoredResource nil") }