func (gke *GKE) Detect(ctx context.Context) (*resource.Resource, error) {
	gcpDetecor := GCE{}
	gceLablRes, err := gcpDetecor.Detect(ctx)
	if os.Getenv("KUBERNETES_SERVICE_HOST") == "" {
		return gceLablRes, err
	}
	var errInfo []string
	if err != nil {
		errInfo = append(errInfo, err.Error())
	}
	attributes := []attribute.KeyValue{
		semconv.K8SNamespaceNameKey.String(os.Getenv("NAMESPACE")),
		semconv.K8SPodNameKey.String(os.Getenv("HOSTNAME")),
	}
	if containerName := os.Getenv("CONTAINER_NAME"); containerName != "" {
		attributes = append(attributes, semconv.ContainerNameKey.String(containerName))
	}
	if clusterName, err := metadata.InstanceAttributeValue("cluster-name"); hasProblem(err) {
		errInfo = append(errInfo, err.Error())
	} else if clusterName != "" {
		attributes = append(attributes, semconv.K8SClusterNameKey.String(clusterName))
	}
	k8sattributeRes := resource.NewWithAttributes(semconv.SchemaURL, attributes...)
	res, err := resource.Merge(gceLablRes, k8sattributeRes)
	if err != nil {
		errInfo = append(errInfo, err.Error())
	}
	var aggregatedErr error
	if len(errInfo) > 0 {
		aggregatedErr = fmt.Errorf("detecting GKE resources: %s", errInfo)
	}
	return res, aggregatedErr
}
Detect detects associated resources when running in GKE environment.
package gcp
import (
	"context"
	"fmt"
	"os"
	"cloud.google.com/go/compute/metadata"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
type GKE struct{}
var _ resource.Detector = (*GKE)(nil)