| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package eks provides a resource detector for AWS EKS. |
| 5 |
package eks // import "go.opentelemetry.io/contrib/detectors/aws/eks" |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"regexp" |
| 13 |
"strings" |
| 14 |
|
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
"go.opentelemetry.io/otel/sdk/resource" |
| 17 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 18 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 |
"k8s.io/client-go/kubernetes" |
| 20 |
"k8s.io/client-go/rest" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
k8sTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint:gosec // False positive G101: Potential hardcoded credentials. The detector only check if the token exists. |
| 25 |
k8sCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" |
| 26 |
authConfigmapNS = "kube-system" |
| 27 |
authConfigmapName = "aws-auth" |
| 28 |
cwConfigmapNS = "amazon-cloudwatch" |
| 29 |
cwConfigmapName = "cluster-info" |
| 30 |
defaultCgroupPath = "/proc/self/cgroup" |
| 31 |
containerIDLength = 64 |
| 32 |
) |
| 33 |
|
| 34 |
// detectorUtils is used for testing the resourceDetector by abstracting functions that rely on external systems. |
| 35 |
type detectorUtils interface { |
| 36 |
fileExists(filename string) bool |
| 37 |
getConfigMap(ctx context.Context, namespace, name string) (map[string]string, error) |
| 38 |
getContainerID() (string, error) |
| 39 |
} |
| 40 |
|
| 41 |
// This struct will implement the detectorUtils interface. |
| 42 |
type eksDetectorUtils struct { |
| 43 |
clientset *kubernetes.Clientset |
| 44 |
} |
| 45 |
|
| 46 |
// resourceDetector for detecting resources running on Amazon EKS. |
| 47 |
type resourceDetector struct { |
| 48 |
utils detectorUtils |
| 49 |
err error |
| 50 |
} |
| 51 |
|
| 52 |
// Compile time assertion that resourceDetector implements the resource.Detector interface. |
| 53 |
var _ resource.Detector = (*resourceDetector)(nil) |
| 54 |
|
| 55 |
// Compile time assertion that eksDetectorUtils implements the detectorUtils interface. |
| 56 |
var _ detectorUtils = (*eksDetectorUtils)(nil) |
| 57 |
|
| 58 |
// is this going to stop working with 1.20 when Docker is deprecated? |
| 59 |
var containerIDRegex = regexp.MustCompile(`^.*/docker/(.+)$`) |
| 60 |
|
| 61 |
// NewResourceDetector returns a resource detector that will detect AWS EKS resources. |
| 62 |
func NewResourceDetector() resource.Detector { |
| 63 |
utils, err := newK8sDetectorUtils() |
| 64 |
return &resourceDetector{utils: utils, err: err} |
| 65 |
} |
| 66 |
|
| 67 |
// Detect returns a Resource describing the Amazon EKS environment being run in. |
| 68 |
func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { |
| 69 |
if detector.err != nil { |
| 70 |
if errors.Is(detector.err, rest.ErrNotInCluster) { |
| 71 |
return resource.Empty(), nil |
| 72 |
} |
| 73 |
|
| 74 |
return nil, detector.err |
| 75 |
} |
| 76 |
|
| 77 |
isEks, err := isEKS(ctx, detector.utils) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
|
| 82 |
// Return empty resource object if not running in EKS |
| 83 |
if !isEks { |
| 84 |
return resource.Empty(), nil |
| 85 |
} |
| 86 |
|
| 87 |
// Create variable to hold resource attributes |
| 88 |
attributes := []attribute.KeyValue{ |
| 89 |
semconv.CloudProviderAWS, |
| 90 |
semconv.CloudPlatformAWSEKS, |
| 91 |
} |
| 92 |
|
| 93 |
// Get clusterName and append to attributes |
| 94 |
clusterName, err := getClusterName(ctx, detector.utils) |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
if clusterName != "" { |
| 99 |
attributes = append(attributes, semconv.K8SClusterName(clusterName)) |
| 100 |
} |
| 101 |
|
| 102 |
// Get containerID and append to attributes |
| 103 |
containerID, err := detector.utils.getContainerID() |
| 104 |
if err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
if containerID != "" { |
| 108 |
attributes = append(attributes, semconv.ContainerID(containerID)) |
| 109 |
} |
| 110 |
|
| 111 |
// Return new resource object with clusterName and containerID as attributes |
| 112 |
return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 113 |
} |
| 114 |
|
| 115 |
// isEKS checks if the current environment is running in EKS. |
| 116 |
func isEKS(ctx context.Context, utils detectorUtils) (bool, error) { |
| 117 |
if !isK8s(utils) { |
| 118 |
return false, nil |
| 119 |
} |
| 120 |
|
| 121 |
// Make HTTP GET request |
| 122 |
awsAuth, err := utils.getConfigMap(ctx, authConfigmapNS, authConfigmapName) |
| 123 |
if err != nil { |
| 124 |
return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err) |
| 125 |
} |
| 126 |
|
| 127 |
return awsAuth != nil, nil |
| 128 |
} |
| 129 |
|
| 130 |
// newK8sDetectorUtils creates the Kubernetes clientset. |
| 131 |
func newK8sDetectorUtils() (*eksDetectorUtils, error) { |
| 132 |
// Get cluster configuration |
| 133 |
confs, err := rest.InClusterConfig() |
| 134 |
if err != nil { |
| 135 |
return nil, fmt.Errorf("failed to create config: %w", err) |
| 136 |
} |
| 137 |
|
| 138 |
// Create clientset using generated configuration |
| 139 |
clientset, err := kubernetes.NewForConfig(confs) |
| 140 |
if err != nil { |
| 141 |
return nil, errors.New("failed to create clientset for Kubernetes client") |
| 142 |
} |
| 143 |
|
| 144 |
return &eksDetectorUtils{clientset: clientset}, nil |
| 145 |
} |
| 146 |
|
| 147 |
// isK8s checks if the current environment is running in a Kubernetes environment. |
| 148 |
func isK8s(utils detectorUtils) bool { |
| 149 |
return utils.fileExists(k8sTokenPath) && utils.fileExists(k8sCertPath) |
| 150 |
} |
| 151 |
|
| 152 |
// fileExists checks if a file with a given filename exists. |
| 153 |
func (eksDetectorUtils) fileExists(filename string) bool { |
| 154 |
info, err := os.Stat(filename) |
| 155 |
return err == nil && !info.IsDir() |
| 156 |
} |
| 157 |
|
| 158 |
// getConfigMap retrieves the configuration map from the k8s API. |
| 159 |
func (eksUtils eksDetectorUtils) getConfigMap(ctx context.Context, namespace, name string) (map[string]string, error) { |
| 160 |
cm, err := eksUtils.clientset.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{}) |
| 161 |
if err != nil { |
| 162 |
return nil, fmt.Errorf("failed to retrieve ConfigMap %s/%s: %w", namespace, name, err) |
| 163 |
} |
| 164 |
|
| 165 |
return cm.Data, nil |
| 166 |
} |
| 167 |
|
| 168 |
// getClusterName retrieves the clusterName resource attribute. |
| 169 |
func getClusterName(ctx context.Context, utils detectorUtils) (string, error) { |
| 170 |
resp, err := utils.getConfigMap(ctx, cwConfigmapNS, cwConfigmapName) |
| 171 |
if err != nil { |
| 172 |
return "", fmt.Errorf("getClusterName() error: %w", err) |
| 173 |
} |
| 174 |
|
| 175 |
return resp["cluster.name"], nil |
| 176 |
} |
| 177 |
|
| 178 |
// getContainerID returns the containerID if currently running within a container. |
| 179 |
func (eksDetectorUtils) getContainerID() (string, error) { |
| 180 |
fileData, err := os.ReadFile(defaultCgroupPath) |
| 181 |
if err != nil { |
| 182 |
return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err) |
| 183 |
} |
| 184 |
|
| 185 |
// Retrieve containerID from file |
| 186 |
splitData := strings.SplitSeq(strings.TrimSpace(string(fileData)), "\n") |
| 187 |
for str := range splitData { |
| 188 |
if containerIDRegex.MatchString(str) { |
| 189 |
return str[len(str)-containerIDLength:], nil |
| 190 |
} |
| 191 |
} |
| 192 |
return "", fmt.Errorf("getContainerID() error: cannot read containerID from file %s", defaultCgroupPath) |
| 193 |
} |