Skip to content

Commit 44393e8

Browse files
committed
Add cloud account ID detection in EKS environment
Add cloud account ID detection in EKS environment
1 parent 2aa1d00 commit 44393e8

File tree

9 files changed

+117
-6
lines changed

9 files changed

+117
-6
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type detectorUtils interface {
4343
getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error)
4444
getClusterName(ctx context.Context, logger *zap.Logger) string
4545
getClusterNameTagFromReservations([]*ec2.Reservation) string
46+
getCloudAccountID(ctx context.Context, logger *zap.Logger) string
4647
}
4748

4849
type eksDetectorUtils struct {
@@ -87,6 +88,10 @@ func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
8788

8889
d.rb.SetCloudProvider(conventions.AttributeCloudProviderAWS)
8990
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAWSEKS)
91+
if d.ra.CloudAccountID.Enabled {
92+
accountId := d.utils.getCloudAccountID(ctx, d.logger)
93+
d.rb.SetCloudAccountID(accountId)
94+
}
9095

9196
if d.ra.K8sClusterName.Enabled {
9297
clusterName := d.utils.getClusterName(ctx, d.logger)
@@ -194,3 +199,21 @@ func (e eksDetectorUtils) getClusterNameTagFromReservations(reservations []*ec2.
194199

195200
return ""
196201
}
202+
203+
func (e eksDetectorUtils) getCloudAccountID(ctx context.Context, logger *zap.Logger) string {
204+
defaultErrorMessage := "Unable to get EKS cluster account ID"
205+
sess, err := session.NewSession()
206+
if err != nil {
207+
logger.Warn(defaultErrorMessage, zap.Error(err))
208+
return ""
209+
}
210+
211+
ec2Svc := ec2metadata.New(sess)
212+
instanceIdentityDocument, err := ec2Svc.GetInstanceIdentityDocumentWithContext(ctx)
213+
if err != nil {
214+
logger.Warn(defaultErrorMessage, zap.Error(err))
215+
return ""
216+
}
217+
218+
return instanceIdentityDocument.AccountID
219+
}

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

+63-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
)
2020

2121
const (
22-
clusterName = "my-cluster"
22+
clusterName = "my-cluster"
23+
cloudAccountId = "cloud1234"
2324
)
2425

2526
type MockDetectorUtils struct {
@@ -40,6 +41,10 @@ func (detectorUtils *MockDetectorUtils) getClusterNameTagFromReservations(_ []*e
4041
return clusterName
4142
}
4243

44+
func (detectorUtils *MockDetectorUtils) getCloudAccountID(_ context.Context, _ *zap.Logger) string {
45+
return cloudAccountId
46+
}
47+
4348
func TestNewDetector(t *testing.T) {
4449
dcfg := CreateDefaultConfig()
4550
detector, err := NewDetector(processortest.NewNopSettings(), dcfg)
@@ -60,8 +65,9 @@ func TestEKS(t *testing.T) {
6065
require.NoError(t, err)
6166

6267
assert.Equal(t, map[string]any{
63-
"cloud.provider": "aws",
64-
"cloud.platform": "aws_eks",
68+
"cloud.provider": "aws",
69+
"cloud.platform": "aws_eks",
70+
"cloud.account.id": "cloud1234",
6571
}, res.Attributes().AsRaw(), "Resource object returned is incorrect")
6672
}
6773

@@ -72,3 +78,57 @@ func TestNotEKS(t *testing.T) {
7278
require.NoError(t, err)
7379
assert.Equal(t, 0, r.Attributes().Len(), "Resource object should be empty")
7480
}
81+
82+
func TestEKSResourceDetection_ForCloudAccountID(t *testing.T) {
83+
tests := []struct {
84+
name string
85+
ra metadata.ResourceAttributesConfig
86+
expectedOutput map[string]any
87+
shouldError bool
88+
}{
89+
{
90+
name: "Detects CloudAccountID when enabled",
91+
ra: metadata.ResourceAttributesConfig{
92+
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: true},
93+
},
94+
expectedOutput: map[string]any{
95+
"cloud.account.id": "cloud1234",
96+
},
97+
shouldError: false,
98+
},
99+
{
100+
name: "Does not detect CloudAccountID when disabled",
101+
ra: metadata.ResourceAttributesConfig{
102+
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: false},
103+
},
104+
expectedOutput: map[string]any{},
105+
shouldError: false,
106+
},
107+
}
108+
109+
for _, tt := range tests {
110+
t.Run(tt.name, func(t *testing.T) {
111+
detectorUtils := new(MockDetectorUtils)
112+
ctx := context.Background()
113+
114+
t.Setenv("KUBERNETES_SERVICE_HOST", "localhost")
115+
detectorUtils.On("getConfigMap", authConfigmapNS, authConfigmapName).Return(map[string]string{conventions.AttributeK8SClusterName: clusterName}, nil)
116+
117+
eksResourceDetector := &detector{
118+
utils: detectorUtils,
119+
err: nil,
120+
ra: tt.ra,
121+
rb: metadata.NewResourceBuilder(tt.ra),
122+
}
123+
res, _, err := eksResourceDetector.Detect(ctx)
124+
125+
if tt.shouldError {
126+
assert.Error(t, err)
127+
return
128+
}
129+
130+
assert.NoError(t, err)
131+
assert.Equal(t, tt.expectedOutput, res.Attributes().AsRaw())
132+
})
133+
}
134+
}

processor/resourcedetectionprocessor/internal/aws/eks/documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
| Name | Description | Values | Enabled |
1010
| ---- | ----------- | ------ | ------- |
11+
| cloud.account.id | The cloud account id | Any Str | true |
1112
| cloud.platform | The cloud.platform | Any Str | true |
1213
| cloud.provider | The cloud.provider | Any Str | true |
1314
| k8s.cluster.name | The EKS cluster name. This attribute is currently only available when running on EC2 instances, and requires permission to run the EC2:DescribeInstances action. | Any Str | false |

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_config.go

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_config_test.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_resource.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/generated_resource_test.go

+9-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/aws/eks/internal/metadata/testdata/config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
default:
22
all_set:
33
resource_attributes:
4+
cloud.account.id:
5+
enabled: true
46
cloud.platform:
57
enabled: true
68
cloud.provider:
@@ -9,6 +11,8 @@ all_set:
911
enabled: true
1012
none_set:
1113
resource_attributes:
14+
cloud.account.id:
15+
enabled: false
1216
cloud.platform:
1317
enabled: false
1418
cloud.provider:

processor/resourcedetectionprocessor/internal/aws/eks/metadata.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ resource_attributes:
1111
description: The cloud.platform
1212
type: string
1313
enabled: true
14+
cloud.account.id:
15+
description: The cloud account id
16+
type: string
17+
enabled: true
1418
k8s.cluster.name:
1519
description: The EKS cluster name. This attribute is currently only available when running on EC2 instances, and requires permission to run the EC2:DescribeInstances action.
1620
type: string

0 commit comments

Comments
 (0)