Skip to content

Commit 13537de

Browse files
authored
[processor/resourcedetection] Use cluster version for EKS identifier (#39486)
#### Description `-eks-` substring exists on all eks releases from what we have seen and I also verified by checking the repo https://github.com/aws/eks-distro/tree/main This PR changes the EKS logic to check the cluster version and check if it contains the substring `_eks_` to flag the cluster as EKS. Unit tests are updated and code cleaned up. #### Link to tracking issue Fixes: #39479 #### Testing Unit test ``` 2025-04-18T15:44:02.285Z info internal/resourcedetection.go:188 detected resource information {"resource": {"cloud.account.id":"906383545488","cloud.availability_zone":"us-west-2a","cloud.platform":"aws_eks","cloud.provider":"aws","cloud.region":"us-west-2","host.id":"i-091f2d176accf9126","host.image.id":"ami-05dbe843e7dc0237c","host.name":"ip-192-168-103-137.us-west-2.compute.internal","host.type":"c6a.large","k8s.cluster.name":"OTL-UI-Cluster","os.type":"linux"}} ``` Signed-off-by: Dani Louca <[email protected]>
1 parent cc3bf7d commit 13537de

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

.chloggen/ResourceDetectionEKS.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: resourcedetectionprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: change the EKS cluster identifier and check the cluster version instead of the existence of aws-auth configmap
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39479]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user, api]

processor/resourcedetectionprocessor/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ require (
4040
go.uber.org/goleak v1.3.0
4141
go.uber.org/multierr v1.11.0
4242
go.uber.org/zap v1.27.0
43-
k8s.io/apimachinery v0.32.3
4443
k8s.io/client-go v0.32.3
4544
)
4645

@@ -167,6 +166,7 @@ require (
167166
gopkg.in/inf.v0 v0.9.1 // indirect
168167
gopkg.in/yaml.v3 v3.0.1 // indirect
169168
k8s.io/api v0.32.3 // indirect
169+
k8s.io/apimachinery v0.32.3 // indirect
170170
k8s.io/klog/v2 v2.130.1 // indirect
171171
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
172172
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect

processor/resourcedetectionprocessor/internal/aws/eks/detector.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"go.opentelemetry.io/collector/processor"
1919
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
2020
"go.uber.org/zap"
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221
"k8s.io/client-go/kubernetes"
2322
"k8s.io/client-go/rest"
2423

@@ -32,19 +31,17 @@ const (
3231

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

3835
clusterNameAwsEksTag = "aws:eks:cluster-name"
3936
clusterNameEksTag = "eks:cluster-name"
4037
kubernetesClusterNameTag = "kubernetes.io/cluster/"
4138
)
4239

4340
type detectorUtils interface {
44-
getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error)
4541
getClusterName(ctx context.Context, logger *zap.Logger) string
4642
getClusterNameTagFromReservations([]types.Reservation) string
4743
getCloudAccountID(ctx context.Context, logger *zap.Logger) string
44+
getClusterVersion() (string, error)
4845
}
4946

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

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

105-
func isEKS(ctx context.Context, utils detectorUtils) (bool, error) {
105+
func isEKS(utils detectorUtils) (bool, error) {
106106
if os.Getenv(kubernetesServiceHostEnvVar) == "" {
107107
return false, nil
108108
}
109109

110-
// Make HTTP GET request
111-
awsAuth, err := utils.getConfigMap(ctx, authConfigmapNS, authConfigmapName)
110+
clusterVersion, err := utils.getClusterVersion()
112111
if err != nil {
113-
return false, fmt.Errorf("isEks() error retrieving auth configmap: %w", err)
112+
return false, fmt.Errorf("isEks() error retrieving cluster version: %w", err)
114113
}
115-
116-
return awsAuth != nil, nil
114+
if strings.Contains(clusterVersion, "-eks-") {
115+
return true, nil
116+
}
117+
return false, nil
117118
}
118119

119120
func newK8sDetectorUtils() (*eksDetectorUtils, error) {
@@ -132,12 +133,12 @@ func newK8sDetectorUtils() (*eksDetectorUtils, error) {
132133
return &eksDetectorUtils{clientset: clientset}, nil
133134
}
134135

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

143144
func (e eksDetectorUtils) getClusterName(ctx context.Context, logger *zap.Logger) string {

processor/resourcedetectionprocessor/internal/aws/eks/detector_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/stretchr/testify/mock"
1313
"github.com/stretchr/testify/require"
1414
"go.opentelemetry.io/collector/processor/processortest"
15-
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
1615
"go.uber.org/zap"
1716

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

30-
func (detectorUtils *MockDetectorUtils) getConfigMap(_ context.Context, namespace string, name string) (map[string]string, error) {
31-
args := detectorUtils.Called(namespace, name)
32-
return args.Get(0).(map[string]string), args.Error(1)
33-
}
34-
3529
func (detectorUtils *MockDetectorUtils) getClusterName(_ context.Context, _ *zap.Logger) string {
3630
var reservations []types.Reservation
3731
return detectorUtils.getClusterNameTagFromReservations(reservations)
@@ -41,6 +35,11 @@ func (detectorUtils *MockDetectorUtils) getClusterNameTagFromReservations(_ []ty
4135
return clusterName
4236
}
4337

38+
func (detectorUtils *MockDetectorUtils) getClusterVersion() (string, error) {
39+
args := detectorUtils.Called()
40+
return args.String(0), args.Error(1)
41+
}
42+
4443
func (detectorUtils *MockDetectorUtils) getCloudAccountID(_ context.Context, _ *zap.Logger) string {
4544
return cloudAccountID
4645
}
@@ -58,7 +57,7 @@ func TestEKS(t *testing.T) {
5857
ctx := context.Background()
5958

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

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

116115
eksResourceDetector := &detector{
117116
utils: detectorUtils,

0 commit comments

Comments
 (0)