From 217dc61e65e23d08fdfe901856e4c9acda08fe75 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 20 Nov 2020 03:21:16 -0800 Subject: [PATCH 01/48] Add EKS Resource Detector --- .github/dependabot.yml | 10 + detectors/aws/eks/eks_resource_detector.go | 248 ++++++++++++++++++ .../aws/eks/eks_resource_detector_test.go | 110 ++++++++ detectors/aws/eks/go.mod | 11 + detectors/aws/eks/go.sum | 30 +++ 5 files changed, 409 insertions(+) create mode 100644 detectors/aws/eks/eks_resource_detector.go create mode 100644 detectors/aws/eks/eks_resource_detector_test.go create mode 100644 detectors/aws/eks/go.mod create mode 100644 detectors/aws/eks/go.sum diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 79bc51eb221..934e63c8c91 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -27,6 +27,16 @@ updates: schedule: interval: "weekly" day: "sunday" + - + package-ecosystem: "gomod" + directory: "/detectors/aws/eks" + labels: + - dependencies + - go + - "Skip Changelog" + schedule: + interval: "weekly" + day: "sunday" - package-ecosystem: "gomod" directory: "/detectors/aws" diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go new file mode 100644 index 00000000000..dc992fd5849 --- /dev/null +++ b/detectors/aws/eks/eks_resource_detector.go @@ -0,0 +1,248 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package eks + +import ( + "bufio" + "context" + "crypto/tls" + "crypto/x509" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "time" + + "go.opentelemetry.io/otel/label" + "go.opentelemetry.io/otel/sdk/resource" + "go.opentelemetry.io/otel/semconv" +) + +const ( + k8sSvcURL = "https://kubernetes.default.svc" + k8sTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" + k8sCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + authConfigmapPath = "/api/v1/namespaces/kube-system/configmaps/aws-auth" + cwConfigmapPath = "/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info" + defaultCgroupPath = "/proc/self/cgroup" + containerIDLength = 64 + millisecondTimeOut = 2000 +) + +// Create interface for functions that need to be mocked +type detectorUtils interface { + fileExists(filename string) bool + fetchString(httpMethod string, URL string) (string, error) + getK8sCredHeader() (string, error) + getContainerID() (string, error) +} + +type eksDetectorUtils struct{} + +// ResourceDetector for detecting resources running on EKS +type ResourceDetector struct { + utils detectorUtils +} + +// JSONResponse is used to parse the JSON response returned from calling HTTP GET to EKS +type JSONResponse struct { + Data DataObject `json:"data"` +} + +// DataObject is used to parse the data attribute inside the JSON response returned from calling HTTP GET to EKS +type DataObject struct { + ClusterName string `json:"cluster.name"` +} + +// Compile time assertion that ResourceDetector implements the resource.Detector interface. +var _ resource.Detector = (*ResourceDetector)(nil) + +// Compile time assertion that eksDetectorUtils implements the detectorUtils interface. +var _ detectorUtils = (*eksDetectorUtils)(nil) + +// Detect detects associated resources when running with AWS EKS. +func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { + + labels := []label.KeyValue{} + + isEks, err := isEks(detector.utils) + if hasProblem(err) { + return nil, err + } + + if !isEks { + return resource.New(labels...), nil + } + + clusterName, err := getClusterName(detector.utils) + if hasProblem(err) { + return nil, err + } + if clusterName != "" { + labels = append(labels, semconv.K8SClusterNameKey.String(clusterName)) + } + + containerID, err := detector.utils.getContainerID() + if hasProblem(err) { + return nil, err + } + if containerID != "" { + labels = append(labels, semconv.ContainerIDKey.String(containerID)) + } + + return resource.New(labels...), nil + +} + +func isEks(utils detectorUtils) (bool, error) { + if !isK8s(utils) { + return false, nil + } + + awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) + if hasProblem(err) { + return false, err + } + + return awsAuth != "", nil +} + +func isK8s(utils detectorUtils) bool { + return utils.fileExists(k8sTokenPath) && utils.fileExists(k8sCertPath) +} + +// eksDetectorUtils is implementing the detectorUtils interface +func (eksUtils eksDetectorUtils) fileExists(filename string) bool { + fmt.Println("REAL HIT") + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +// fetchString is implementing the detectorUtils interface +func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { + request, err := http.NewRequest(httpMethod, URL, nil) + if hasProblem(err) { + return "", err + } + + authHeader, err := eksUtils.getK8sCredHeader() + if hasProblem(err) { + return "", err + } + request.Header.Set("Authorization", authHeader) + + caCert, err := ioutil.ReadFile(k8sCertPath) + if hasProblem(err) { + return "", err + } + + caCertPool := x509.NewCertPool() + caCertPool.AppendCertsFromPEM(caCert) + + client := &http.Client{ + Timeout: millisecondTimeOut * time.Millisecond, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: caCertPool, + }, + }, + } + + response, err := client.Do(request) + if hasProblem(err) { + return "", err + } + + body, err := ioutil.ReadAll(response.Body) + if hasProblem(err) { + return "", err + } + + return string(body), nil +} + +// getK8sCredHeader is implementing the detectorUtils interface +func (eksUtils eksDetectorUtils) getK8sCredHeader() (string, error) { + content, err := ioutil.ReadFile(k8sTokenPath) + if hasProblem(err) { + return "", err + } + + return "Bearer " + string(content), nil +} + +func getClusterName(utils detectorUtils) (string, error) { + resp, err := utils.fetchString("GET", k8sSvcURL+cwConfigmapPath) + if hasProblem(err) { + return "", err + } + + var parsedResp JSONResponse + err = json.Unmarshal([]byte(resp), &parsedResp) + if hasProblem(err) { + return "", err + } + clusterName := parsedResp.Data.ClusterName + + return clusterName, nil +} + +// getContainerID is implementing the detectorUtils interface +func (eksUtils eksDetectorUtils) getContainerID() (string, error) { + content, err := readFile(defaultCgroupPath) + if hasProblem(err) { + return "", err + } + + for _, line := range content { + if lineLength := len(line); lineLength > containerIDLength { + return line[lineLength-containerIDLength:], nil + } + } + + return "", nil +} + +func readFile(fileName string) ([]string, error) { + file, err := os.Open(fileName) + if hasProblem(err) { + return nil, err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + + var text []string + + for scanner.Scan() { + text = append(text, scanner.Text()) + } + + return text, nil +} + +func hasProblem(err error) bool { + if err == nil { + return false + } + log.Fatalln(err) + return true +} diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go new file mode 100644 index 00000000000..27340590adf --- /dev/null +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -0,0 +1,110 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package eks + +import ( + "context" + "testing" + + "github.com/bmizerany/assert" + "github.com/stretchr/testify/mock" + + "go.opentelemetry.io/otel/label" + "go.opentelemetry.io/otel/sdk/resource" + "go.opentelemetry.io/otel/semconv" +) + +type MockDetectorUtils struct { + mock.Mock +} + +// Mock function for fileExists() +func (detectorUtils *MockDetectorUtils) fileExists(filename string) bool { + args := detectorUtils.Called(filename) + return args.Bool(0) +} + +// Mock function for fetchString() +func (detectorUtils *MockDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { + args := detectorUtils.Called(httpMethod, URL) + return args.String(0), args.Error(1) +} + +// Mock function for getK8sCredHeader() +func (detectorUtils *MockDetectorUtils) getK8sCredHeader() (string, error) { + args := detectorUtils.Called() + return args.String(0), args.Error(1) +} + +// Mock function for getContainerID() +func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { + args := detectorUtils.Called() + return args.String(0), args.Error(1) +} + +func TestEks(t *testing.T) { + + detectorUtils := new(MockDetectorUtils) + + // Set EKS resource detector variables + k8sTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token" + k8sCertPath := "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + k8sSvcURL := "https://kubernetes.default.svc" + authConfigmapPath := "/api/v1/namespaces/kube-system/configmaps/aws-auth" + cwConfigmapPath := "/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info" + + // Mock functions and set expectations + detectorUtils.On("fileExists", k8sTokenPath).Return(true) + detectorUtils.On("fileExists", k8sCertPath).Return(true) + detectorUtils.On("fetchString", "GET", k8sSvcURL+authConfigmapPath).Return("not empty", nil) + detectorUtils.On("fetchString", "GET", k8sSvcURL+cwConfigmapPath).Return(`{"data":{"cluster.name":"my-cluster"}}`, nil) + detectorUtils.On("getContainerID").Return("0123456789A", nil) + + // Expected resource object + eksResourceLabels := []label.KeyValue{ + semconv.K8SClusterNameKey.String("my-cluster"), + semconv.ContainerIDKey.String("0123456789A"), + } + expectedResource := resource.New(eksResourceLabels...) + + // Call EKS Resource detector to detect resources + eksResourceDetector := ResourceDetector{detectorUtils} + resourceObj, _ := eksResourceDetector.Detect(context.Background()) + + assert.Equal(t, expectedResource, resourceObj, "Resource object returned is incorrect") + detectorUtils.AssertExpectations(t) +} + +func TestNotEks(t *testing.T) { + + detectorUtils := new(MockDetectorUtils) + + // Set EKS resource detector variables + k8sTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token" + + // Mock functions and set expectations + detectorUtils.On("fileExists", k8sTokenPath).Return(false) + + // Expected resource object + eksResourceLabels := []label.KeyValue{} + expectedResource := resource.New(eksResourceLabels...) + + // Call EKS Resource detector to detect resources + eksResourceDetector := ResourceDetector{detectorUtils} + resourceObj, _ := eksResourceDetector.Detect(context.Background()) + + assert.Equal(t, expectedResource, resourceObj, "Resource object should be empty") + detectorUtils.AssertExpectations(t) +} diff --git a/detectors/aws/eks/go.mod b/detectors/aws/eks/go.mod new file mode 100644 index 00000000000..11e72c605a9 --- /dev/null +++ b/detectors/aws/eks/go.mod @@ -0,0 +1,11 @@ +module github.com/wilguo/opentelemetry-go-contrib/detectors/aws/eks + +go 1.15 + +require ( + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 + github.com/kr/pretty v0.2.1 // indirect + github.com/stretchr/testify v1.6.1 + go.opentelemetry.io/otel v0.13.0 + go.opentelemetry.io/otel/sdk v0.13.0 +) diff --git a/detectors/aws/eks/go.sum b/detectors/aws/eks/go.sum new file mode 100644 index 00000000000..ef0460941ac --- /dev/null +++ b/detectors/aws/eks/go.sum @@ -0,0 +1,30 @@ +github.com/DataDog/sketches-go v0.0.1/go.mod h1:Q5DbzQ+3AkgGwymQO7aZFNP7ns2lZKGtvRBzRXfdi60= +github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +go.opentelemetry.io/otel v0.13.0 h1:2isEnyzjjJZq6r2EKMsFj4TxiQiexsM04AVhwbR/oBA= +go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY= +go.opentelemetry.io/otel/sdk v0.13.0 h1:4VCfpKamZ8GtnepXxMRurSpHpMKkcxhtO33z1S4rGDQ= +go.opentelemetry.io/otel/sdk v0.13.0/go.mod h1:dKvLH8Uu8LcEPlSAUsfW7kMGaJBhk/1NYvpPZ6wIMbU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 96439acf666cab703fe0e91703ef117afde5f8f3 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 20 Nov 2020 04:46:33 -0800 Subject: [PATCH 02/48] Remove getK8sCredHeader() from detectorUtils interface --- detectors/aws/eks/eks_resource_detector.go | 6 ++---- detectors/aws/eks/eks_resource_detector_test.go | 6 ------ detectors/aws/go.sum | 1 + 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index dc992fd5849..d8771e3fcd0 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -47,7 +47,6 @@ const ( type detectorUtils interface { fileExists(filename string) bool fetchString(httpMethod string, URL string) (string, error) - getK8sCredHeader() (string, error) getContainerID() (string, error) } @@ -142,7 +141,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str return "", err } - authHeader, err := eksUtils.getK8sCredHeader() + authHeader, err := getK8sCredHeader() if hasProblem(err) { return "", err } @@ -178,8 +177,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str return string(body), nil } -// getK8sCredHeader is implementing the detectorUtils interface -func (eksUtils eksDetectorUtils) getK8sCredHeader() (string, error) { +func getK8sCredHeader() (string, error) { content, err := ioutil.ReadFile(k8sTokenPath) if hasProblem(err) { return "", err diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index 27340590adf..e54626f7f23 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -42,12 +42,6 @@ func (detectorUtils *MockDetectorUtils) fetchString(httpMethod string, URL strin return args.String(0), args.Error(1) } -// Mock function for getK8sCredHeader() -func (detectorUtils *MockDetectorUtils) getK8sCredHeader() (string, error) { - args := detectorUtils.Called() - return args.String(0), args.Error(1) -} - // Mock function for getContainerID() func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { args := detectorUtils.Called() diff --git a/detectors/aws/go.sum b/detectors/aws/go.sum index 9e66dc4678e..468f662d62e 100644 --- a/detectors/aws/go.sum +++ b/detectors/aws/go.sum @@ -15,6 +15,7 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= From fc9da579a8c99d15d007f901ed056af38d0cab98 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 20 Nov 2020 16:46:17 -0800 Subject: [PATCH 03/48] Change implementation for getContainerId() to be consistent with ecs resource detector --- detectors/aws/eks/eks_resource_detector.go | 36 +++++----------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index d8771e3fcd0..b8414de6199 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -15,7 +15,6 @@ package eks import ( - "bufio" "context" "crypto/tls" "crypto/x509" @@ -25,6 +24,7 @@ import ( "log" "net/http" "os" + "strings" "time" "go.opentelemetry.io/otel/label" @@ -204,37 +204,17 @@ func getClusterName(utils detectorUtils) (string, error) { // getContainerID is implementing the detectorUtils interface func (eksUtils eksDetectorUtils) getContainerID() (string, error) { - content, err := readFile(defaultCgroupPath) - if hasProblem(err) { + fileData, err := ioutil.ReadFile(defaultCgroupPath) + if err != nil { return "", err } - - for _, line := range content { - if lineLength := len(line); lineLength > containerIDLength { - return line[lineLength-containerIDLength:], nil + splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") + for _, str := range splitData { + if len(str) > containerIDLength { + return str[len(str)-containerIDLength:], nil } } - - return "", nil -} - -func readFile(fileName string) ([]string, error) { - file, err := os.Open(fileName) - if hasProblem(err) { - return nil, err - } - defer file.Close() - - scanner := bufio.NewScanner(file) - scanner.Split(bufio.ScanLines) - - var text []string - - for scanner.Scan() { - text = append(text, scanner.Text()) - } - - return text, nil + return "", err } func hasProblem(err error) bool { From 1938d61fc571d90ea6e7d24af660fbf835515c2b Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 20 Nov 2020 16:53:54 -0800 Subject: [PATCH 04/48] Change code to use resource.Empty() --- detectors/aws/eks/eks_resource_detector.go | 6 +++--- detectors/aws/eks/eks_resource_detector_test.go | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index b8414de6199..ed35a829a93 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -76,17 +76,17 @@ var _ detectorUtils = (*eksDetectorUtils)(nil) // Detect detects associated resources when running with AWS EKS. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { - labels := []label.KeyValue{} - isEks, err := isEks(detector.utils) if hasProblem(err) { return nil, err } if !isEks { - return resource.New(labels...), nil + return resource.Empty(), nil } + labels := []label.KeyValue{} + clusterName, err := getClusterName(detector.utils) if hasProblem(err) { return nil, err diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index e54626f7f23..af3ccdd2fac 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -92,8 +92,7 @@ func TestNotEks(t *testing.T) { detectorUtils.On("fileExists", k8sTokenPath).Return(false) // Expected resource object - eksResourceLabels := []label.KeyValue{} - expectedResource := resource.New(eksResourceLabels...) + expectedResource := resource.Empty() // Call EKS Resource detector to detect resources eksResourceDetector := ResourceDetector{detectorUtils} From 52dbceba040edee0ae972d73c0442156f702ae11 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 19:51:29 -0800 Subject: [PATCH 05/48] Add comments to functions --- detectors/aws/eks/eks_resource_detector.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index ed35a829a93..70e66079c55 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -50,19 +50,22 @@ type detectorUtils interface { getContainerID() (string, error) } +// This struct will implement the detectorUtils interface type eksDetectorUtils struct{} -// ResourceDetector for detecting resources running on EKS +// ResourceDetector for detecting resources running on Amazon EKS type ResourceDetector struct { utils detectorUtils } // JSONResponse is used to parse the JSON response returned from calling HTTP GET to EKS +// eg. {"data":{"cluster.name":"my-cluster"}} type JSONResponse struct { Data DataObject `json:"data"` } // DataObject is used to parse the data attribute inside the JSON response returned from calling HTTP GET to EKS +// eg. {"data":{"cluster.name":"my-cluster"}} type DataObject struct { ClusterName string `json:"cluster.name"` } @@ -73,7 +76,7 @@ var _ resource.Detector = (*ResourceDetector)(nil) // Compile time assertion that eksDetectorUtils implements the detectorUtils interface. var _ detectorUtils = (*eksDetectorUtils)(nil) -// Detect detects associated resources when running with AWS EKS. +// Detect function detects associated resources when running in Amazon EKS environment. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { isEks, err := isEks(detector.utils) @@ -81,12 +84,15 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc return nil, err } + // Return empty resource object if not running in EKS if !isEks { return resource.Empty(), nil } + // Create variable to hold resource attributes labels := []label.KeyValue{} + // Get clusterName and append to labels clusterName, err := getClusterName(detector.utils) if hasProblem(err) { return nil, err @@ -95,6 +101,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc labels = append(labels, semconv.K8SClusterNameKey.String(clusterName)) } + // Get containerID and append to labels containerID, err := detector.utils.getContainerID() if hasProblem(err) { return nil, err @@ -103,15 +110,18 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc labels = append(labels, semconv.ContainerIDKey.String(containerID)) } + // Return new resource object with clusterName and containerID as attributes return resource.New(labels...), nil } +// isEks checks if the current environment is running in EKS func isEks(utils detectorUtils) (bool, error) { if !isK8s(utils) { return false, nil } + // Make HTTP GET request awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) if hasProblem(err) { return false, err @@ -120,6 +130,7 @@ func isEks(utils detectorUtils) (bool, error) { return awsAuth != "", nil } +// isK8s checks if the current environment is running in a Kubernetes environment func isK8s(utils detectorUtils) bool { return utils.fileExists(k8sTokenPath) && utils.fileExists(k8sCertPath) } From d6135de5822f657b405c1770fb33dd042f9192cf Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 20:44:34 -0800 Subject: [PATCH 06/48] Update error handling with useful messages --- detectors/aws/eks/eks_resource_detector.go | 55 +++++++++------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 70e66079c55..af731d80907 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "os" "strings" @@ -80,7 +79,7 @@ var _ detectorUtils = (*eksDetectorUtils)(nil) func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { isEks, err := isEks(detector.utils) - if hasProblem(err) { + if err != nil { return nil, err } @@ -94,7 +93,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc // Get clusterName and append to labels clusterName, err := getClusterName(detector.utils) - if hasProblem(err) { + if err != nil { return nil, err } if clusterName != "" { @@ -103,7 +102,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc // Get containerID and append to labels containerID, err := detector.utils.getContainerID() - if hasProblem(err) { + if err != nil { return nil, err } if containerID != "" { @@ -123,8 +122,8 @@ func isEks(utils detectorUtils) (bool, error) { // Make HTTP GET request awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) - if hasProblem(err) { - return false, err + if err != nil { + return false, fmt.Errorf("isEks() error: %s", err.Error()) } return awsAuth != "", nil @@ -137,7 +136,6 @@ func isK8s(utils detectorUtils) bool { // eksDetectorUtils is implementing the detectorUtils interface func (eksUtils eksDetectorUtils) fileExists(filename string) bool { - fmt.Println("REAL HIT") info, err := os.Stat(filename) if os.IsNotExist(err) { return false @@ -148,19 +146,20 @@ func (eksUtils eksDetectorUtils) fileExists(filename string) bool { // fetchString is implementing the detectorUtils interface func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { request, err := http.NewRequest(httpMethod, URL, nil) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("failed to create new HTTP request with method=%s, URL=%s", httpMethod, URL) } authHeader, err := getK8sCredHeader() - if hasProblem(err) { + if err != nil { return "", err } + request.Header.Set("Authorization", authHeader) caCert, err := ioutil.ReadFile(k8sCertPath) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("failed to read file with path %s", k8sCertPath) } caCertPool := x509.NewCertPool() @@ -176,13 +175,13 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str } response, err := client.Do(request) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s", httpMethod, URL) } body, err := ioutil.ReadAll(response.Body) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("failed to read response from HTTP request with method=%s, URL=%s", httpMethod, URL) } return string(body), nil @@ -190,8 +189,8 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str func getK8sCredHeader() (string, error) { content, err := ioutil.ReadFile(k8sTokenPath) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("getK8sCredHeader() error: cannot read file with path %s", k8sTokenPath) } return "Bearer " + string(content), nil @@ -199,14 +198,14 @@ func getK8sCredHeader() (string, error) { func getClusterName(utils detectorUtils) (string, error) { resp, err := utils.fetchString("GET", k8sSvcURL+cwConfigmapPath) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("getClusterName() error: %s", err.Error()) } var parsedResp JSONResponse err = json.Unmarshal([]byte(resp), &parsedResp) - if hasProblem(err) { - return "", err + if err != nil { + return "", fmt.Errorf("getClusterName() error: cannot parse JSON %s", resp) } clusterName := parsedResp.Data.ClusterName @@ -217,7 +216,7 @@ func getClusterName(utils detectorUtils) (string, error) { func (eksUtils eksDetectorUtils) getContainerID() (string, error) { fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { - return "", err + return "", fmt.Errorf("getContainerID() error: cannot read file with path %s", defaultCgroupPath) } splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { @@ -225,13 +224,5 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { return str[len(str)-containerIDLength:], nil } } - return "", err -} - -func hasProblem(err error) bool { - if err == nil { - return false - } - log.Fatalln(err) - return true + return "", fmt.Errorf("getContainerID() error: cannot read containerID from file %s", defaultCgroupPath) } From 687f99d7ad891c61c36fb109920381488bef5381 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 20:48:36 -0800 Subject: [PATCH 07/48] Delete unused variables in EKS resource detector tests --- detectors/aws/eks/eks_resource_detector_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index af3ccdd2fac..e422bf82166 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -52,13 +52,6 @@ func TestEks(t *testing.T) { detectorUtils := new(MockDetectorUtils) - // Set EKS resource detector variables - k8sTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token" - k8sCertPath := "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" - k8sSvcURL := "https://kubernetes.default.svc" - authConfigmapPath := "/api/v1/namespaces/kube-system/configmaps/aws-auth" - cwConfigmapPath := "/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info" - // Mock functions and set expectations detectorUtils.On("fileExists", k8sTokenPath).Return(true) detectorUtils.On("fileExists", k8sCertPath).Return(true) From 049a54726a586e25a97388b198d9d5275f1e7ec8 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 20:52:17 -0800 Subject: [PATCH 08/48] Add whitespace around operators --- detectors/aws/eks/eks_resource_detector.go | 4 ++-- detectors/aws/eks/eks_resource_detector_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index af731d80907..d24404afc28 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -121,7 +121,7 @@ func isEks(utils detectorUtils) (bool, error) { } // Make HTTP GET request - awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) + awsAuth, err := utils.fetchString("GET", k8sSvcURL + authConfigmapPath) if err != nil { return false, fmt.Errorf("isEks() error: %s", err.Error()) } @@ -197,7 +197,7 @@ func getK8sCredHeader() (string, error) { } func getClusterName(utils detectorUtils) (string, error) { - resp, err := utils.fetchString("GET", k8sSvcURL+cwConfigmapPath) + resp, err := utils.fetchString("GET", k8sSvcURL + cwConfigmapPath) if err != nil { return "", fmt.Errorf("getClusterName() error: %s", err.Error()) } diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index e422bf82166..0fad2457c70 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -55,8 +55,8 @@ func TestEks(t *testing.T) { // Mock functions and set expectations detectorUtils.On("fileExists", k8sTokenPath).Return(true) detectorUtils.On("fileExists", k8sCertPath).Return(true) - detectorUtils.On("fetchString", "GET", k8sSvcURL+authConfigmapPath).Return("not empty", nil) - detectorUtils.On("fetchString", "GET", k8sSvcURL+cwConfigmapPath).Return(`{"data":{"cluster.name":"my-cluster"}}`, nil) + detectorUtils.On("fetchString", "GET", k8sSvcURL + authConfigmapPath).Return("not empty", nil) + detectorUtils.On("fetchString", "GET", k8sSvcURL + cwConfigmapPath).Return(`{"data":{"cluster.name":"my-cluster"}}`, nil) detectorUtils.On("getContainerID").Return("0123456789A", nil) // Expected resource object From 0f7a91fee949b21a544337f4b6e7c37d631457a3 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 20:56:36 -0800 Subject: [PATCH 09/48] Add space around "-" operator --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index d24404afc28..c7e3e304fbb 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -221,7 +221,7 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { if len(str) > containerIDLength { - return str[len(str)-containerIDLength:], nil + return str[len(str) - containerIDLength:], nil } } return "", fmt.Errorf("getContainerID() error: cannot read containerID from file %s", defaultCgroupPath) From cc784e520e2d708a764893ed95f0e22b37923196 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Sat, 21 Nov 2020 21:06:49 -0800 Subject: [PATCH 10/48] Update comments for functions to be more descriptive --- detectors/aws/eks/eks_resource_detector.go | 27 ++++++++++++++----- .../aws/eks/eks_resource_detector_test.go | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index c7e3e304fbb..7eff1778baf 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -75,7 +75,7 @@ var _ resource.Detector = (*ResourceDetector)(nil) // Compile time assertion that eksDetectorUtils implements the detectorUtils interface. var _ detectorUtils = (*eksDetectorUtils)(nil) -// Detect function detects associated resources when running in Amazon EKS environment. +// Detect function collects associated resource attributes when running in Amazon EKS environment. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { isEks, err := isEks(detector.utils) @@ -134,7 +134,8 @@ func isK8s(utils detectorUtils) bool { return utils.fileExists(k8sTokenPath) && utils.fileExists(k8sCertPath) } -// eksDetectorUtils is implementing the detectorUtils interface +// fileExists checks if a file with a given filename exists +// this function implements the detectorUtils interface func (eksUtils eksDetectorUtils) fileExists(filename string) bool { info, err := os.Stat(filename) if os.IsNotExist(err) { @@ -143,28 +144,32 @@ func (eksUtils eksDetectorUtils) fileExists(filename string) bool { return !info.IsDir() } -// fetchString is implementing the detectorUtils interface +// fetchString executes an HTTP request with a given HTTP Method and URL string +// this function implements the detectorUtils interface func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { + + // Create new HTTP request object request, err := http.NewRequest(httpMethod, URL, nil) if err != nil { return "", fmt.Errorf("failed to create new HTTP request with method=%s, URL=%s", httpMethod, URL) } + // Set HTTP request header with authentication credentials authHeader, err := getK8sCredHeader() if err != nil { return "", err } - request.Header.Set("Authorization", authHeader) + // Get certificate caCert, err := ioutil.ReadFile(k8sCertPath) if err != nil { return "", fmt.Errorf("failed to read file with path %s", k8sCertPath) } - caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) + // Set HTTP request timeout and add certificate client := &http.Client{ Timeout: millisecondTimeOut * time.Millisecond, Transport: &http.Transport{ @@ -174,11 +179,13 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str }, } + // Execute HTTP request response, err := client.Do(request) if err != nil { return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s", httpMethod, URL) } + // Retrieve response body from HTTP request body, err := ioutil.ReadAll(response.Body) if err != nil { return "", fmt.Errorf("failed to read response from HTTP request with method=%s, URL=%s", httpMethod, URL) @@ -187,6 +194,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str return string(body), nil } +// getK8sCredHeader retrieves the kubernetes credential information func getK8sCredHeader() (string, error) { content, err := ioutil.ReadFile(k8sTokenPath) if err != nil { @@ -196,12 +204,14 @@ func getK8sCredHeader() (string, error) { return "Bearer " + string(content), nil } +// getClusterName retrieves the clusterName resource attribute func getClusterName(utils detectorUtils) (string, error) { resp, err := utils.fetchString("GET", k8sSvcURL + cwConfigmapPath) if err != nil { return "", fmt.Errorf("getClusterName() error: %s", err.Error()) } + // parse JSON object returned from HTTP request var parsedResp JSONResponse err = json.Unmarshal([]byte(resp), &parsedResp) if err != nil { @@ -212,12 +222,17 @@ func getClusterName(utils detectorUtils) (string, error) { return clusterName, nil } -// getContainerID is implementing the detectorUtils interface +// getContainerID retrieves the containerID resource attribute +// this function implements the detectorUtils interface func (eksUtils eksDetectorUtils) getContainerID() (string, error) { + + // Read file fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { return "", fmt.Errorf("getContainerID() error: cannot read file with path %s", defaultCgroupPath) } + + // Retrieve containerID from file splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { if len(str) > containerIDLength { diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index 0fad2457c70..82242c34a8a 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -48,6 +48,7 @@ func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { return args.String(0), args.Error(1) } +// Tests EKS resource detector running in EKS environment func TestEks(t *testing.T) { detectorUtils := new(MockDetectorUtils) @@ -74,6 +75,7 @@ func TestEks(t *testing.T) { detectorUtils.AssertExpectations(t) } +// Tests EKS resource detector not running in EKS environment func TestNotEks(t *testing.T) { detectorUtils := new(MockDetectorUtils) From 1bae800173a67735ddf8a19f8cd2764d5a9ba898 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Mon, 23 Nov 2020 11:25:37 -0800 Subject: [PATCH 11/48] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44a3a939aa3..480cfdcaacf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +- Add Amazon EKS resource detector. (#465) + ### Added - `otelhttp.{Get,Head,Post,PostForm}` convenience wrappers for their `http` counterparts. (#390) From 26a9129c5c71448f56f39bd937e6559c29b93707 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Mon, 23 Nov 2020 12:46:11 -0800 Subject: [PATCH 12/48] Fix spaces around operators. --- detectors/aws/eks/eks_resource_detector.go | 6 +++--- detectors/aws/eks/eks_resource_detector_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 7eff1778baf..c88abbaf74f 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -121,7 +121,7 @@ func isEks(utils detectorUtils) (bool, error) { } // Make HTTP GET request - awsAuth, err := utils.fetchString("GET", k8sSvcURL + authConfigmapPath) + awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) if err != nil { return false, fmt.Errorf("isEks() error: %s", err.Error()) } @@ -206,7 +206,7 @@ func getK8sCredHeader() (string, error) { // getClusterName retrieves the clusterName resource attribute func getClusterName(utils detectorUtils) (string, error) { - resp, err := utils.fetchString("GET", k8sSvcURL + cwConfigmapPath) + resp, err := utils.fetchString("GET", k8sSvcURL+cwConfigmapPath) if err != nil { return "", fmt.Errorf("getClusterName() error: %s", err.Error()) } @@ -236,7 +236,7 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { if len(str) > containerIDLength { - return str[len(str) - containerIDLength:], nil + return str[len(str)-containerIDLength:], nil } } return "", fmt.Errorf("getContainerID() error: cannot read containerID from file %s", defaultCgroupPath) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index 82242c34a8a..eb7387a2264 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -56,8 +56,8 @@ func TestEks(t *testing.T) { // Mock functions and set expectations detectorUtils.On("fileExists", k8sTokenPath).Return(true) detectorUtils.On("fileExists", k8sCertPath).Return(true) - detectorUtils.On("fetchString", "GET", k8sSvcURL + authConfigmapPath).Return("not empty", nil) - detectorUtils.On("fetchString", "GET", k8sSvcURL + cwConfigmapPath).Return(`{"data":{"cluster.name":"my-cluster"}}`, nil) + detectorUtils.On("fetchString", "GET", k8sSvcURL+authConfigmapPath).Return("not empty", nil) + detectorUtils.On("fetchString", "GET", k8sSvcURL+cwConfigmapPath).Return(`{"data":{"cluster.name":"my-cluster"}}`, nil) detectorUtils.On("getContainerID").Return("0123456789A", nil) // Expected resource object From 8e43f348830c4900b64c46292ef4fe0fefd1e449 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:04:03 -0800 Subject: [PATCH 13/48] Change "GET" to use http.MethodGet Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index c88abbaf74f..0b82372a5a6 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -121,7 +121,7 @@ func isEks(utils detectorUtils) (bool, error) { } // Make HTTP GET request - awsAuth, err := utils.fetchString("GET", k8sSvcURL+authConfigmapPath) + awsAuth, err := utils.fetchString(http.MethodGet, k8sSvcURL+authConfigmapPath) if err != nil { return false, fmt.Errorf("isEks() error: %s", err.Error()) } From be46135a40d88dbd2b85f3871d2cdc76898ddc18 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:04:39 -0800 Subject: [PATCH 14/48] Update error message for retrieving auth configmap Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 0b82372a5a6..1cdb0b3be50 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -123,7 +123,7 @@ func isEks(utils detectorUtils) (bool, error) { // Make HTTP GET request awsAuth, err := utils.fetchString(http.MethodGet, k8sSvcURL+authConfigmapPath) if err != nil { - return false, fmt.Errorf("isEks() error: %s", err.Error()) + return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err.Error()) } return awsAuth != "", nil From 1bcfc40aca32d247644334b1720eac16b9c3542b Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:04:56 -0800 Subject: [PATCH 15/48] Update error message for HTTP request Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 1cdb0b3be50..71e5e2eb79d 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -151,7 +151,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str // Create new HTTP request object request, err := http.NewRequest(httpMethod, URL, nil) if err != nil { - return "", fmt.Errorf("failed to create new HTTP request with method=%s, URL=%s", httpMethod, URL) + return "", fmt.Errorf("failed to create new HTTP request with method=%s, URL=%s: %w", httpMethod, URL, err) } // Set HTTP request header with authentication credentials From 388dbe20e991bc07ded2ec04066eb4ff86cde36e Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:05:38 -0800 Subject: [PATCH 16/48] Update error message for executing HTTP request Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 71e5e2eb79d..b2b485d1c0e 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -182,7 +182,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str // Execute HTTP request response, err := client.Do(request) if err != nil { - return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s", httpMethod, URL) + return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s: %w", httpMethod, URL, err) } // Retrieve response body from HTTP request From 0cf714453d195b59b5c916de457e1ea9c75aa1d9 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:06:02 -0800 Subject: [PATCH 17/48] Update error message for HTTP response Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index b2b485d1c0e..cf1ba61016e 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -188,7 +188,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str // Retrieve response body from HTTP request body, err := ioutil.ReadAll(response.Body) if err != nil { - return "", fmt.Errorf("failed to read response from HTTP request with method=%s, URL=%s", httpMethod, URL) + return "", fmt.Errorf("failed to read response from HTTP request with method=%s, URL=%s: %w", httpMethod, URL, err) } return string(body), nil From a549e85cb1be3c31a30964abe3cca51bdd43b767 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:57:47 -0800 Subject: [PATCH 18/48] Update error message for getClusterName Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index cf1ba61016e..0672f4acdcf 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -208,7 +208,7 @@ func getK8sCredHeader() (string, error) { func getClusterName(utils detectorUtils) (string, error) { resp, err := utils.fetchString("GET", k8sSvcURL+cwConfigmapPath) if err != nil { - return "", fmt.Errorf("getClusterName() error: %s", err.Error()) + return "", fmt.Errorf("getClusterName() error: %w", err) } // parse JSON object returned from HTTP request From d9b7a506fcfaf685031429be2396f1fb91fe69ae Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:58:27 -0800 Subject: [PATCH 19/48] Update error message for parsing JSON for clusterName Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 0672f4acdcf..29df8fcd475 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -215,7 +215,7 @@ func getClusterName(utils detectorUtils) (string, error) { var parsedResp JSONResponse err = json.Unmarshal([]byte(resp), &parsedResp) if err != nil { - return "", fmt.Errorf("getClusterName() error: cannot parse JSON %s", resp) + return "", fmt.Errorf("getClusterName() error: cannot parse JSON: %w", w) } clusterName := parsedResp.Data.ClusterName From 666ce5ffd7c5bc207ea0bf69267a320705387b48 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 12:59:19 -0800 Subject: [PATCH 20/48] Update error message for reading file Co-authored-by: Anthony Mirabella --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 29df8fcd475..f84ad404639 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -229,7 +229,7 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { // Read file fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { - return "", fmt.Errorf("getContainerID() error: cannot read file with path %s", defaultCgroupPath) + return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err) } // Retrieve containerID from file From aeb0708f0264a15f999b5770fb96ff68df7cc457 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 13:07:48 -0800 Subject: [PATCH 21/48] Fix build errors --- detectors/aws/eks/eks_resource_detector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index f84ad404639..5cf9ff14f08 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -123,7 +123,7 @@ func isEks(utils detectorUtils) (bool, error) { // Make HTTP GET request awsAuth, err := utils.fetchString(http.MethodGet, k8sSvcURL+authConfigmapPath) if err != nil { - return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err.Error()) + return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err) } return awsAuth != "", nil @@ -215,7 +215,7 @@ func getClusterName(utils detectorUtils) (string, error) { var parsedResp JSONResponse err = json.Unmarshal([]byte(resp), &parsedResp) if err != nil { - return "", fmt.Errorf("getClusterName() error: cannot parse JSON: %w", w) + return "", fmt.Errorf("getClusterName() error: cannot parse JSON: %w", err) } clusterName := parsedResp.Data.ClusterName From 376940d36d3640b8c150a739dc392cd48546d26e Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 14:00:55 -0800 Subject: [PATCH 22/48] Change JSON parsing to use map[string]interface{} --- detectors/aws/eks/eks_resource_detector.go | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 5cf9ff14f08..fb4079df02d 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -57,15 +57,8 @@ type ResourceDetector struct { utils detectorUtils } -// JSONResponse is used to parse the JSON response returned from calling HTTP GET to EKS -// eg. {"data":{"cluster.name":"my-cluster"}} -type JSONResponse struct { - Data DataObject `json:"data"` -} - -// DataObject is used to parse the data attribute inside the JSON response returned from calling HTTP GET to EKS -// eg. {"data":{"cluster.name":"my-cluster"}} -type DataObject struct { +// This struct will help unmarshal clustername from JSON response +type data struct { ClusterName string `json:"cluster.name"` } @@ -212,12 +205,18 @@ func getClusterName(utils detectorUtils) (string, error) { } // parse JSON object returned from HTTP request - var parsedResp JSONResponse - err = json.Unmarshal([]byte(resp), &parsedResp) + var respmap map[string]json.RawMessage + err = json.Unmarshal([]byte(resp), &respmap) + if err != nil { + return "", fmt.Errorf("getClusterName() error: cannot parse JSON: %w", err) + } + var d data + err = json.Unmarshal(respmap["data"], &d) if err != nil { return "", fmt.Errorf("getClusterName() error: cannot parse JSON: %w", err) } - clusterName := parsedResp.Data.ClusterName + + clusterName := d.ClusterName return clusterName, nil } From 9d5e8334c39d9cc04533ccb75e4b8845b8893dca Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 14:08:07 -0800 Subject: [PATCH 23/48] Add check for HTTP response status code in error handling --- detectors/aws/eks/eks_resource_detector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index fb4079df02d..5ef1d272f69 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -174,8 +174,8 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str // Execute HTTP request response, err := client.Do(request) - if err != nil { - return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s: %w", httpMethod, URL, err) + if err != nil || response.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s, Status Code=%d: %w", httpMethod, URL, response.StatusCode, err) } // Retrieve response body from HTTP request From 7cda33ba3f772281d240d11b4a089809945b59d4 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 27 Nov 2020 14:11:40 -0800 Subject: [PATCH 24/48] Refactor millisecondTimeOut variable name to timeoutMillis --- detectors/aws/eks/eks_resource_detector.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 5ef1d272f69..58b7f766ef7 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -32,14 +32,14 @@ import ( ) const ( - k8sSvcURL = "https://kubernetes.default.svc" - k8sTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" - k8sCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" - authConfigmapPath = "/api/v1/namespaces/kube-system/configmaps/aws-auth" - cwConfigmapPath = "/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info" - defaultCgroupPath = "/proc/self/cgroup" - containerIDLength = 64 - millisecondTimeOut = 2000 + k8sSvcURL = "https://kubernetes.default.svc" + k8sTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" + k8sCertPath = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + authConfigmapPath = "/api/v1/namespaces/kube-system/configmaps/aws-auth" + cwConfigmapPath = "/api/v1/namespaces/amazon-cloudwatch/configmaps/cluster-info" + defaultCgroupPath = "/proc/self/cgroup" + containerIDLength = 64 + timeoutMillis = 2000 ) // Create interface for functions that need to be mocked @@ -164,7 +164,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str // Set HTTP request timeout and add certificate client := &http.Client{ - Timeout: millisecondTimeOut * time.Millisecond, + Timeout: timeoutMillis * time.Millisecond, Transport: &http.Transport{ TLSClientConfig: &tls.Config{ RootCAs: caCertPool, From 6d4b6502853d46614c90dcad6f8233d3463fe4ad Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:19:48 -0800 Subject: [PATCH 25/48] Update CHANGELOG.md Co-authored-by: Tyler Yahn --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1e36f65e8e..f7485597a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] -- Add Amazon EKS resource detector. (#465) +### Added + +- Amazon EKS resource detector. (#465) ## [0.14.0] - 2020-11-20 From 6159f52f5a31a02b4c62351e9b734d539659c258 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:25:00 -0800 Subject: [PATCH 26/48] Update detectors/aws/eks/go.mod Co-authored-by: Tyler Yahn --- detectors/aws/eks/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/go.mod b/detectors/aws/eks/go.mod index 11e72c605a9..83cd9794926 100644 --- a/detectors/aws/eks/go.mod +++ b/detectors/aws/eks/go.mod @@ -1,4 +1,4 @@ -module github.com/wilguo/opentelemetry-go-contrib/detectors/aws/eks +module go.opentelemetry.io/contrib/detectors/aws/eks go 1.15 From a90923639d35755277eb3737cabcfbcefba3d09c Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:25:13 -0800 Subject: [PATCH 27/48] Update detectors/aws/go.sum Co-authored-by: Tyler Yahn --- detectors/aws/go.sum | 1 - 1 file changed, 1 deletion(-) diff --git a/detectors/aws/go.sum b/detectors/aws/go.sum index 2bae0c29e45..34fe7ddb3cd 100644 --- a/detectors/aws/go.sum +++ b/detectors/aws/go.sum @@ -15,7 +15,6 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= From 2a369d01b0e029b640ae306ed3d2762bb168b355 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:26:13 -0800 Subject: [PATCH 28/48] Update detectors/aws/eks/go.mod Co-authored-by: Tyler Yahn --- detectors/aws/eks/go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/detectors/aws/eks/go.mod b/detectors/aws/eks/go.mod index 83cd9794926..4b94fa6b955 100644 --- a/detectors/aws/eks/go.mod +++ b/detectors/aws/eks/go.mod @@ -6,6 +6,6 @@ require ( github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 github.com/kr/pretty v0.2.1 // indirect github.com/stretchr/testify v1.6.1 - go.opentelemetry.io/otel v0.13.0 - go.opentelemetry.io/otel/sdk v0.13.0 + go.opentelemetry.io/otel v0.14.0 + go.opentelemetry.io/otel/sdk v0.14.0 ) From a7101cdb91c503b3480a23e5d73f8e9196692b1f Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:26:29 -0800 Subject: [PATCH 29/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 58b7f766ef7..78aaf61d6ec 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -42,7 +42,7 @@ const ( timeoutMillis = 2000 ) -// Create interface for functions that need to be mocked +// detectorUtils is used for testing the ResourceDetector by abstracting functions that rely on external systems. type detectorUtils interface { fileExists(filename string) bool fetchString(httpMethod string, URL string) (string, error) From 58cbe994b64101fb8430583337eb2812f0cef1d3 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:27:19 -0800 Subject: [PATCH 30/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 78aaf61d6ec..6609a20d53f 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -108,7 +108,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc } // isEks checks if the current environment is running in EKS -func isEks(utils detectorUtils) (bool, error) { +func isEKS(utils detectorUtils) (bool, error) { if !isK8s(utils) { return false, nil } From 2f7cf164be6e9ccd568ad4e4ff73fd41acd8a491 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:27:43 -0800 Subject: [PATCH 31/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 6609a20d53f..abf42377f74 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -107,7 +107,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc } -// isEks checks if the current environment is running in EKS +// isEKS checks if the current environment is running in EKS. func isEKS(utils detectorUtils) (bool, error) { if !isK8s(utils) { return false, nil From 22268b717e19755bc5bfeed71a35b642bc581296 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:28:20 -0800 Subject: [PATCH 32/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index abf42377f74..020f9b5ee6f 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -68,7 +68,7 @@ var _ resource.Detector = (*ResourceDetector)(nil) // Compile time assertion that eksDetectorUtils implements the detectorUtils interface. var _ detectorUtils = (*eksDetectorUtils)(nil) -// Detect function collects associated resource attributes when running in Amazon EKS environment. +// Detect returns a Resource describing the Amazon EKS environment being run in. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { isEks, err := isEks(detector.utils) From 74229e2dd17f9297dded035672eca5a13956b35b Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:28:43 -0800 Subject: [PATCH 33/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 020f9b5ee6f..d0b97e19487 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -127,8 +127,7 @@ func isK8s(utils detectorUtils) bool { return utils.fileExists(k8sTokenPath) && utils.fileExists(k8sCertPath) } -// fileExists checks if a file with a given filename exists -// this function implements the detectorUtils interface +// fileExists checks if a file with a given filename exists. func (eksUtils eksDetectorUtils) fileExists(filename string) bool { info, err := os.Stat(filename) if os.IsNotExist(err) { From c23b0a1a6217cab10a123c8057e57ea9914db0f0 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:29:16 -0800 Subject: [PATCH 34/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index d0b97e19487..abe0d143559 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -130,10 +130,7 @@ func isK8s(utils detectorUtils) bool { // fileExists checks if a file with a given filename exists. func (eksUtils eksDetectorUtils) fileExists(filename string) bool { info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() + return err == nil && !info.IsDir() } // fetchString executes an HTTP request with a given HTTP Method and URL string From b4181f0dc1d1ae2299b4ad692d58d3841c74a2d8 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:29:36 -0800 Subject: [PATCH 35/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index abe0d143559..376d46892cb 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -133,8 +133,7 @@ func (eksUtils eksDetectorUtils) fileExists(filename string) bool { return err == nil && !info.IsDir() } -// fetchString executes an HTTP request with a given HTTP Method and URL string -// this function implements the detectorUtils interface +// fetchString executes an HTTP request with a given HTTP Method and URL string. func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { // Create new HTTP request object From 88b49809f415c766942a07cd8c4d056ca4c084af Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:30:01 -0800 Subject: [PATCH 36/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 376d46892cb..a3fa0f2b8d9 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -135,8 +135,6 @@ func (eksUtils eksDetectorUtils) fileExists(filename string) bool { // fetchString executes an HTTP request with a given HTTP Method and URL string. func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (string, error) { - - // Create new HTTP request object request, err := http.NewRequest(httpMethod, URL, nil) if err != nil { return "", fmt.Errorf("failed to create new HTTP request with method=%s, URL=%s: %w", httpMethod, URL, err) From f2616cb21deb796262d48be31df372bed9148ea7 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:30:15 -0800 Subject: [PATCH 37/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 1 - 1 file changed, 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index a3fa0f2b8d9..26e9ecf0921 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -165,7 +165,6 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str }, } - // Execute HTTP request response, err := client.Do(request) if err != nil || response.StatusCode != http.StatusOK { return "", fmt.Errorf("failed to execute HTTP request with method=%s, URL=%s, Status Code=%d: %w", httpMethod, URL, response.StatusCode, err) From 7208ab040fa53afeb8c92d0e4406ddfc5160e343 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:30:29 -0800 Subject: [PATCH 38/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 26e9ecf0921..95061084633 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -179,7 +179,7 @@ func (eksUtils eksDetectorUtils) fetchString(httpMethod string, URL string) (str return string(body), nil } -// getK8sCredHeader retrieves the kubernetes credential information +// getK8sCredHeader retrieves the kubernetes credential information. func getK8sCredHeader() (string, error) { content, err := ioutil.ReadFile(k8sTokenPath) if err != nil { From 48b97254621f28dd5806669dc04cdc74d2265904 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:31:14 -0800 Subject: [PATCH 39/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 95061084633..1f72d6cdf99 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -213,8 +213,7 @@ func getClusterName(utils detectorUtils) (string, error) { return clusterName, nil } -// getContainerID retrieves the containerID resource attribute -// this function implements the detectorUtils interface +// getContainerID returns the containerID if currently running within a container. func (eksUtils eksDetectorUtils) getContainerID() (string, error) { // Read file From 0d6d0c9cba7e82d6dac8ca74d8535d34fcca0c8e Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:31:32 -0800 Subject: [PATCH 40/48] Update detectors/aws/eks/eks_resource_detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/eks_resource_detector.go index 1f72d6cdf99..11efc5b84d1 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/eks_resource_detector.go @@ -215,8 +215,6 @@ func getClusterName(utils detectorUtils) (string, error) { // getContainerID returns the containerID if currently running within a container. func (eksUtils eksDetectorUtils) getContainerID() (string, error) { - - // Read file fileData, err := ioutil.ReadFile(defaultCgroupPath) if err != nil { return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err) From 16c436d62a4cb623187d79b2144fd718d524d803 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:32:21 -0800 Subject: [PATCH 41/48] Update detectors/aws/eks/eks_resource_detector_test.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index eb7387a2264..e67ccf20894 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -69,7 +69,8 @@ func TestEks(t *testing.T) { // Call EKS Resource detector to detect resources eksResourceDetector := ResourceDetector{detectorUtils} - resourceObj, _ := eksResourceDetector.Detect(context.Background()) + resourceObj, err := eksResourceDetector.Detect(context.Background()) + require.NoError(t, err) assert.Equal(t, expectedResource, resourceObj, "Resource object returned is incorrect") detectorUtils.AssertExpectations(t) From 6306b4fb7b8f4183b154ee32d19392361a77e1fc Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:32:47 -0800 Subject: [PATCH 42/48] Update detectors/aws/eks/eks_resource_detector_test.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index e67ccf20894..45a4c02a1ef 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -77,7 +77,7 @@ func TestEks(t *testing.T) { } // Tests EKS resource detector not running in EKS environment -func TestNotEks(t *testing.T) { +func TestNotEKS(t *testing.T) { detectorUtils := new(MockDetectorUtils) From 4f304a60a835966d155e74ac6f7782a7eee2a081 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:33:03 -0800 Subject: [PATCH 43/48] Update detectors/aws/eks/eks_resource_detector_test.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index 45a4c02a1ef..1bc3928ebc6 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -81,7 +81,6 @@ func TestNotEKS(t *testing.T) { detectorUtils := new(MockDetectorUtils) - // Set EKS resource detector variables k8sTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token" // Mock functions and set expectations From 9c042fc63d9f55571a39158d6c1cde92e0ae2c20 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:33:22 -0800 Subject: [PATCH 44/48] Update detectors/aws/eks/eks_resource_detector_test.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/eks_resource_detector_test.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/eks_resource_detector_test.go index 1bc3928ebc6..5252d87d971 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/eks_resource_detector_test.go @@ -86,13 +86,8 @@ func TestNotEKS(t *testing.T) { // Mock functions and set expectations detectorUtils.On("fileExists", k8sTokenPath).Return(false) - // Expected resource object - expectedResource := resource.Empty() - - // Call EKS Resource detector to detect resources - eksResourceDetector := ResourceDetector{detectorUtils} - resourceObj, _ := eksResourceDetector.Detect(context.Background()) - - assert.Equal(t, expectedResource, resourceObj, "Resource object should be empty") + r, err := ResourceDetector{detectorUtils}.Detect(context.Background()) + require.NoError(t, err) + assert.Equal(t, resource.Empty(), r, "Resource object should be empty") detectorUtils.AssertExpectations(t) } From ed13bcee97cc4e305a41dad9b5d92f863014dd0e Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 12:45:11 -0800 Subject: [PATCH 45/48] Fix build error and address changes to PR --- .../eks/{eks_resource_detector.go => detector.go} | 4 ++-- ...ks_resource_detector_test.go => detector_test.go} | 6 ++++-- detectors/aws/eks/go.sum | 12 ++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) rename detectors/aws/eks/{eks_resource_detector.go => detector.go} (98%) rename detectors/aws/eks/{eks_resource_detector_test.go => detector_test.go} (93%) diff --git a/detectors/aws/eks/eks_resource_detector.go b/detectors/aws/eks/detector.go similarity index 98% rename from detectors/aws/eks/eks_resource_detector.go rename to detectors/aws/eks/detector.go index 11efc5b84d1..21629386353 100644 --- a/detectors/aws/eks/eks_resource_detector.go +++ b/detectors/aws/eks/detector.go @@ -71,7 +71,7 @@ var _ detectorUtils = (*eksDetectorUtils)(nil) // Detect returns a Resource describing the Amazon EKS environment being run in. func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resource, error) { - isEks, err := isEks(detector.utils) + isEks, err := isEKS(detector.utils) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func (detector *ResourceDetector) Detect(ctx context.Context) (*resource.Resourc } // Return new resource object with clusterName and containerID as attributes - return resource.New(labels...), nil + return resource.NewWithAttributes(labels...), nil } diff --git a/detectors/aws/eks/eks_resource_detector_test.go b/detectors/aws/eks/detector_test.go similarity index 93% rename from detectors/aws/eks/eks_resource_detector_test.go rename to detectors/aws/eks/detector_test.go index 5252d87d971..94b29d097df 100644 --- a/detectors/aws/eks/eks_resource_detector_test.go +++ b/detectors/aws/eks/detector_test.go @@ -20,6 +20,7 @@ import ( "github.com/bmizerany/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/label" "go.opentelemetry.io/otel/sdk/resource" @@ -65,7 +66,7 @@ func TestEks(t *testing.T) { semconv.K8SClusterNameKey.String("my-cluster"), semconv.ContainerIDKey.String("0123456789A"), } - expectedResource := resource.New(eksResourceLabels...) + expectedResource := resource.NewWithAttributes(eksResourceLabels...) // Call EKS Resource detector to detect resources eksResourceDetector := ResourceDetector{detectorUtils} @@ -86,7 +87,8 @@ func TestNotEKS(t *testing.T) { // Mock functions and set expectations detectorUtils.On("fileExists", k8sTokenPath).Return(false) - r, err := ResourceDetector{detectorUtils}.Detect(context.Background()) + detector := ResourceDetector{detectorUtils} + r, err := detector.Detect(context.Background()) require.NoError(t, err) assert.Equal(t, resource.Empty(), r, "Resource object should be empty") detectorUtils.AssertExpectations(t) diff --git a/detectors/aws/eks/go.sum b/detectors/aws/eks/go.sum index ef0460941ac..80396ac2251 100644 --- a/detectors/aws/eks/go.sum +++ b/detectors/aws/eks/go.sum @@ -4,8 +4,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -18,10 +18,10 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -go.opentelemetry.io/otel v0.13.0 h1:2isEnyzjjJZq6r2EKMsFj4TxiQiexsM04AVhwbR/oBA= -go.opentelemetry.io/otel v0.13.0/go.mod h1:dlSNewoRYikTkotEnxdmuBHgzT+k/idJSfDv/FxEnOY= -go.opentelemetry.io/otel/sdk v0.13.0 h1:4VCfpKamZ8GtnepXxMRurSpHpMKkcxhtO33z1S4rGDQ= -go.opentelemetry.io/otel/sdk v0.13.0/go.mod h1:dKvLH8Uu8LcEPlSAUsfW7kMGaJBhk/1NYvpPZ6wIMbU= +go.opentelemetry.io/otel v0.14.0 h1:YFBEfjCk9MTjaytCNSUkp9Q8lF7QJezA06T71FbQxLQ= +go.opentelemetry.io/otel v0.14.0/go.mod h1:vH5xEuwy7Rts0GNtsCW3HYQoZDY+OmBJ6t1bFGGlxgw= +go.opentelemetry.io/otel/sdk v0.14.0 h1:Pqgd85y5XhyvHQlOxkKW+FD4DAX7AoeaNIDKC2VhfHQ= +go.opentelemetry.io/otel/sdk v0.14.0/go.mod h1:kGO5pEMSNqSJppHAm8b73zztLxB5fgDQnD56/dl5xqE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= From 52f25a6e277cf4e371add330386034b379c67a69 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Thu, 3 Dec 2020 14:48:35 -0800 Subject: [PATCH 46/48] Change getContainerID() to use regex --- detectors/aws/eks/detector.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/detectors/aws/eks/detector.go b/detectors/aws/eks/detector.go index 21629386353..30a15d978d4 100644 --- a/detectors/aws/eks/detector.go +++ b/detectors/aws/eks/detector.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "net/http" "os" + "regexp" "strings" "time" @@ -220,10 +221,12 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err) } + r, _ := regexp.Compile(`^.*/docker/(.+)$`) + // Retrieve containerID from file splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { - if len(str) > containerIDLength { + if isMatch := r.MatchString(str); isMatch { return str[len(str)-containerIDLength:], nil } } From ff1170dc32b15fd3a00cb15242d747f95848cc94 Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 4 Dec 2020 10:59:12 -0800 Subject: [PATCH 47/48] Update detectors/aws/eks/detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/detector.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/detectors/aws/eks/detector.go b/detectors/aws/eks/detector.go index 30a15d978d4..ef64f5ccdaa 100644 --- a/detectors/aws/eks/detector.go +++ b/detectors/aws/eks/detector.go @@ -221,7 +221,10 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { return "", fmt.Errorf("getContainerID() error: cannot read file with path %s: %w", defaultCgroupPath, err) } - r, _ := regexp.Compile(`^.*/docker/(.+)$`) + r, err := regexp.Compile(`^.*/docker/(.+)$`) + if err != nil { + return "", err + } // Retrieve containerID from file splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") From 861b905fc03f5bfec0d5fd186604ae22f4868e6f Mon Sep 17 00:00:00 2001 From: Wilbert Guo Date: Fri, 4 Dec 2020 10:59:21 -0800 Subject: [PATCH 48/48] Update detectors/aws/eks/detector.go Co-authored-by: Tyler Yahn --- detectors/aws/eks/detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/aws/eks/detector.go b/detectors/aws/eks/detector.go index ef64f5ccdaa..5ea57cabc60 100644 --- a/detectors/aws/eks/detector.go +++ b/detectors/aws/eks/detector.go @@ -229,7 +229,7 @@ func (eksUtils eksDetectorUtils) getContainerID() (string, error) { // Retrieve containerID from file splitData := strings.Split(strings.TrimSpace(string(fileData)), "\n") for _, str := range splitData { - if isMatch := r.MatchString(str); isMatch { + if r.MatchString(str) { return str[len(str)-containerIDLength:], nil } }