Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/ResourceDetectionEKS.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: change the EKS cluster identifier and check the cluster version instead of the existence of aws-auth configmap

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39479]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user, api]
2 changes: 1 addition & 1 deletion processor/resourcedetectionprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ require (
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
k8s.io/apimachinery v0.32.3
k8s.io/client-go v0.32.3
)

Expand Down Expand Up @@ -164,6 +163,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.32.3 // indirect
k8s.io/apimachinery v0.32.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
Expand Down
33 changes: 17 additions & 16 deletions processor/resourcedetectionprocessor/internal/aws/eks/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"go.opentelemetry.io/collector/processor"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

Expand All @@ -32,19 +31,17 @@ const (

// Environment variable that is set when running on Kubernetes.
kubernetesServiceHostEnvVar = "KUBERNETES_SERVICE_HOST"
authConfigmapNS = "kube-system"
authConfigmapName = "aws-auth"

clusterNameAwsEksTag = "aws:eks:cluster-name"
clusterNameEksTag = "eks:cluster-name"
kubernetesClusterNameTag = "kubernetes.io/cluster/"
)

type detectorUtils interface {
getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error)
getClusterName(ctx context.Context, logger *zap.Logger) string
getClusterNameTagFromReservations([]types.Reservation) string
getCloudAccountID(ctx context.Context, logger *zap.Logger) string
getClusterVersion() (string, error)
}

type eksDetectorUtils struct {
Expand Down Expand Up @@ -81,11 +78,14 @@ func NewDetector(set processor.Settings, dcfg internal.DetectorConfig) (internal
// Detect returns a Resource describing the Amazon EKS environment being run in.
func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
// Check if running on EKS.
isEKS, err := isEKS(ctx, d.utils)
if !isEKS {
isEKS, err := isEKS(d.utils)
if err != nil {
d.logger.Debug("Unable to identify EKS environment", zap.Error(err))
return pcommon.NewResource(), "", err
}
if !isEKS {
return pcommon.NewResource(), "", nil
}

d.rb.SetCloudProvider(conventions.AttributeCloudProviderAWS)
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAWSEKS)
Expand All @@ -102,18 +102,19 @@ func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return d.rb.Emit(), conventions.SchemaURL, nil
}

func isEKS(ctx context.Context, utils detectorUtils) (bool, error) {
func isEKS(utils detectorUtils) (bool, error) {
if os.Getenv(kubernetesServiceHostEnvVar) == "" {
return false, nil
}

// Make HTTP GET request
awsAuth, err := utils.getConfigMap(ctx, authConfigmapNS, authConfigmapName)
clusterVersion, err := utils.getClusterVersion()
if err != nil {
return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err)
return false, fmt.Errorf("isEks() error retrieving cluster version: %w", err)
}

return awsAuth != nil, nil
if strings.Contains(clusterVersion, "-eks-") {
return true, nil
}
return false, nil
}

func newK8sDetectorUtils() (*eksDetectorUtils, error) {
Expand All @@ -132,12 +133,12 @@ func newK8sDetectorUtils() (*eksDetectorUtils, error) {
return &eksDetectorUtils{clientset: clientset}, nil
}

func (e eksDetectorUtils) getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error) {
cm, err := e.clientset.CoreV1().ConfigMaps(namespace).Get(ctx, name, metav1.GetOptions{})
func (e eksDetectorUtils) getClusterVersion() (string, error) {
serverVersion, err := e.clientset.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("failed to retrieve ConfigMap %s/%s: %w", namespace, name, err)
return "", fmt.Errorf("failed to retrieve server version: %w", err)
}
return cm.Data, nil
return serverVersion.GitVersion, nil
}

func (e eksDetectorUtils) getClusterName(ctx context.Context, logger *zap.Logger) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/processor/processortest"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata"
Expand All @@ -27,11 +26,6 @@ type MockDetectorUtils struct {
mock.Mock
}

func (detectorUtils *MockDetectorUtils) getConfigMap(_ context.Context, namespace string, name string) (map[string]string, error) {
args := detectorUtils.Called(namespace, name)
return args.Get(0).(map[string]string), args.Error(1)
}

func (detectorUtils *MockDetectorUtils) getClusterName(_ context.Context, _ *zap.Logger) string {
var reservations []types.Reservation
return detectorUtils.getClusterNameTagFromReservations(reservations)
Expand All @@ -41,6 +35,11 @@ func (detectorUtils *MockDetectorUtils) getClusterNameTagFromReservations(_ []ty
return clusterName
}

func (detectorUtils *MockDetectorUtils) getClusterVersion() (string, error) {
args := detectorUtils.Called()
return args.String(0), args.Error(1)
}

func (detectorUtils *MockDetectorUtils) getCloudAccountID(_ context.Context, _ *zap.Logger) string {
return cloudAccountID
}
Expand All @@ -58,7 +57,7 @@ func TestEKS(t *testing.T) {
ctx := context.Background()

t.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
detectorUtils.On("getConfigMap", authConfigmapNS, authConfigmapName).Return(map[string]string{conventions.AttributeK8SClusterName: clusterName}, nil)
detectorUtils.On("getClusterVersion").Return("v1.32.3-eks-d0fe756", nil)
// Call EKS Resource detector to detect resources
eksResourceDetector := &detector{utils: detectorUtils, err: nil, ra: metadata.DefaultResourceAttributesConfig(), rb: metadata.NewResourceBuilder(metadata.DefaultResourceAttributesConfig())}
res, _, err := eksResourceDetector.Detect(ctx)
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestEKSResourceDetection_ForCloudAccountID(t *testing.T) {
ctx := context.Background()

t.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
detectorUtils.On("getConfigMap", authConfigmapNS, authConfigmapName).Return(map[string]string{conventions.AttributeK8SClusterName: clusterName}, nil)
detectorUtils.On("getClusterVersion").Return("v1.32.3-eks-d0fe756", nil)

eksResourceDetector := &detector{
utils: detectorUtils,
Expand Down
Loading