Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloud account ID detection in EKS environment #37179

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/eks_cloud_account_id.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: enhancement

# 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: This enhancement detects AWS EKS cloud account ID

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

# (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]
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type detectorUtils interface {
getConfigMap(ctx context.Context, namespace string, name string) (map[string]string, error)
getClusterName(ctx context.Context, logger *zap.Logger) string
getClusterNameTagFromReservations([]*ec2.Reservation) string
getCloudAccountID(ctx context.Context, logger *zap.Logger) string
}

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

d.rb.SetCloudProvider(conventions.AttributeCloudProviderAWS)
d.rb.SetCloudPlatform(conventions.AttributeCloudPlatformAWSEKS)
if d.ra.CloudAccountID.Enabled {
accountId := d.utils.getCloudAccountID(ctx, d.logger)
d.rb.SetCloudAccountID(accountId)
}

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

return ""
}

func (e eksDetectorUtils) getCloudAccountID(ctx context.Context, logger *zap.Logger) string {
defaultErrorMessage := "Unable to get EKS cluster account ID"
sess, err := session.NewSession()
if err != nil {
logger.Warn(defaultErrorMessage, zap.Error(err))
return ""
}

ec2Svc := ec2metadata.New(sess)
instanceIdentityDocument, err := ec2Svc.GetInstanceIdentityDocumentWithContext(ctx)
if err != nil {
logger.Warn(defaultErrorMessage, zap.Error(err))
return ""
}

return instanceIdentityDocument.AccountID
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

const (
clusterName = "my-cluster"
clusterName = "my-cluster"
cloudAccountId = "cloud1234"
)

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

func (detectorUtils *MockDetectorUtils) getCloudAccountID(_ context.Context, _ *zap.Logger) string {
return cloudAccountId
}

func TestNewDetector(t *testing.T) {
dcfg := CreateDefaultConfig()
detector, err := NewDetector(processortest.NewNopSettings(), dcfg)
Expand All @@ -60,8 +65,9 @@ func TestEKS(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, map[string]any{
"cloud.provider": "aws",
"cloud.platform": "aws_eks",
"cloud.provider": "aws",
"cloud.platform": "aws_eks",
"cloud.account.id": "cloud1234",
}, res.Attributes().AsRaw(), "Resource object returned is incorrect")
}

Expand All @@ -72,3 +78,57 @@ func TestNotEKS(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 0, r.Attributes().Len(), "Resource object should be empty")
}

func TestEKSResourceDetection_ForCloudAccountID(t *testing.T) {
tests := []struct {
name string
ra metadata.ResourceAttributesConfig
expectedOutput map[string]any
shouldError bool
}{
{
name: "Detects CloudAccountID when enabled",
ra: metadata.ResourceAttributesConfig{
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: true},
},
expectedOutput: map[string]any{
"cloud.account.id": "cloud1234",
},
shouldError: false,
},
{
name: "Does not detect CloudAccountID when disabled",
ra: metadata.ResourceAttributesConfig{
CloudAccountID: metadata.ResourceAttributeConfig{Enabled: false},
},
expectedOutput: map[string]any{},
shouldError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
detectorUtils := new(MockDetectorUtils)
ctx := context.Background()

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

eksResourceDetector := &detector{
utils: detectorUtils,
err: nil,
ra: tt.ra,
rb: metadata.NewResourceBuilder(tt.ra),
}
res, _, err := eksResourceDetector.Detect(ctx)

if tt.shouldError {
assert.Error(t, err)
return
}

assert.NoError(t, err)
assert.Equal(t, tt.expectedOutput, res.Attributes().AsRaw())
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| cloud.account.id | The cloud account id | Any Str | true |
| cloud.platform | The cloud.platform | Any Str | true |
| cloud.provider | The cloud.provider | Any Str | true |
| 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 |

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
default:
all_set:
resource_attributes:
cloud.account.id:
enabled: true
cloud.platform:
enabled: true
cloud.provider:
Expand All @@ -9,6 +11,8 @@ all_set:
enabled: true
none_set:
resource_attributes:
cloud.account.id:
enabled: false
cloud.platform:
enabled: false
cloud.provider:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ resource_attributes:
description: The cloud.platform
type: string
enabled: true
cloud.account.id:
description: The cloud account id
type: string
enabled: true
tilakchowdary marked this conversation as resolved.
Show resolved Hide resolved
k8s.cluster.name:
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.
type: string
Expand Down
Loading