Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Remove support for legacy GKE monitoring resource model (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
csbell authored Jun 11, 2021
1 parent bf9def0 commit b7f4654
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 96 deletions.
28 changes: 1 addition & 27 deletions monitoredresource/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ||
Expand Down
28 changes: 3 additions & 25 deletions monitoredresource/gcp/gcp_metadata_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
18 changes: 4 additions & 14 deletions monitoredresource/gcp/monitored_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 0 additions & 30 deletions monitoredresource/gcp/monitored_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit b7f4654

Please sign in to comment.