From 1b839f94ede071f7b9c74053e672b621cea2f13a Mon Sep 17 00:00:00 2001 From: jsirianni Date: Fri, 27 Oct 2023 17:15:26 -0400 Subject: [PATCH 01/12] wip: detect aks cluster name --- internal/metadataproviders/azure/metadata.go | 2 ++ .../internal/azure/aks/aks.go | 30 +++++++++++++++++++ .../internal/azure/aks/aks_test.go | 28 +++++++++++++++++ .../aks/internal/metadata/generated_config.go | 8 +++-- .../metadata/generated_config_test.go | 10 ++++--- .../internal/metadata/generated_resource.go | 7 +++++ .../metadata/generated_resource_test.go | 10 +++++-- .../internal/metadata/testdata/config.yaml | 4 +++ .../internal/azure/aks/metadata.yaml | 6 +++- 9 files changed, 96 insertions(+), 9 deletions(-) diff --git a/internal/metadataproviders/azure/metadata.go b/internal/metadataproviders/azure/metadata.go index b6241feab869..5e748d104391 100644 --- a/internal/metadataproviders/azure/metadata.go +++ b/internal/metadataproviders/azure/metadata.go @@ -85,6 +85,8 @@ func (p *azureProviderImpl) Metadata(ctx context.Context) (*ComputeMetadata, err return nil, fmt.Errorf("failed to read Azure IMDS reply: %w", err) } + fmt.Println(string(respBody)) + var metadata *ComputeMetadata err = json.Unmarshal(respBody, &metadata) if err != nil { diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go index 9bdfb1e690d6..c6ee99d064dc 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go @@ -5,7 +5,9 @@ package aks // import "github.com/open-telemetry/opentelemetry-collector-contrib import ( "context" + "fmt" "os" + "strings" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/processor" @@ -53,6 +55,14 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem } if d.resourceAttributes.CloudPlatform.Enabled { attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureAKS) + + if d.resourceAttributes.K8sClusterName.Enabled { + m, err := d.provider.Metadata(ctx) + if err != nil { + return res, "", fmt.Errorf("failed to get IMDS metadata: %w", err) + } + attrs.PutStr(conventions.AttributeK8SClusterName, parseClusterName(m.ResourceGroupName)) + } } return res, conventions.SchemaURL, nil @@ -66,3 +76,23 @@ func azureMetadataAvailable(ctx context.Context, p azure.Provider) bool { _, err := p.Metadata(ctx) return err == nil } + +// parseClusterName parses the cluster name from the resource group name. +// Generally the resource group name will be of the form (MC|mc)_resource-group_cluster-name_location +// It is possible for the cluster name to have underscores. It is also possible +// for the user to override the "infrastucture resource group" which will change +// the format of the resource group name, it can also have underscores. If the +// cluster name cannot be parsed, the resource group name is returned as it can +// be used to uniquely identify the cluster. Azure will not allow the user to +// deploy multiple AKS clusters with the same "infrastructure resource group" +// name, making the resource group name a reliable way to identify the cluster. +func parseClusterName(resourceGroup string) string { + // Code inspired by https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/datadogexporter/internal/hostmetadata/internal/azure/provider.go#L36 + splitAll := strings.Split(resourceGroup, "_") + switch len(splitAll) { + case 4: + return splitAll[len(splitAll)-2] + default: + return resourceGroup + } +} diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go index 4ce567164f9b..d4c3f6d5afc6 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go @@ -34,6 +34,8 @@ func TestDetector_Detect_K8s_Azure(t *testing.T) { assert.Equal(t, map[string]any{ "cloud.provider": "azure", "cloud.platform": "azure_aks", + // Cluster name will not be detected if resource grup is not set + "k8s.cluster.name": "", }, res.Attributes().AsRaw(), "Resource attrs returned are incorrect") } @@ -64,3 +66,29 @@ func mockProvider() *azure.MockProvider { mp.On("Metadata").Return(&azure.ComputeMetadata{}, nil) return mp } + +func TestParseClusterName(t *testing.T) { + cases := []struct { + name string + input string + expected string + }{ + { + name: "parses cluster name", + input: "MC_myResourceGroup_myAKSCluster_eastus", + expected: "myAKSCluster", + }, + { + name: "returns resource group when cluster name has underscores", + input: "MC_myResourceGroup_my_AKS_Cluster_eastus", + expected: "MC_myResourceGroup_my_AKS_Cluster_eastus", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + actual := parseClusterName(tc.input) + assert.Equal(t, tc.expected, actual) + }) + } +} diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go index 5b9acb9f0fef..f22bbb0ac9d7 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go @@ -25,8 +25,9 @@ func (rac *ResourceAttributeConfig) Unmarshal(parser *confmap.Conf) error { // ResourceAttributesConfig provides config for resourcedetectionprocessor/aks resource attributes. type ResourceAttributesConfig struct { - CloudPlatform ResourceAttributeConfig `mapstructure:"cloud.platform"` - CloudProvider ResourceAttributeConfig `mapstructure:"cloud.provider"` + CloudPlatform ResourceAttributeConfig `mapstructure:"cloud.platform"` + CloudProvider ResourceAttributeConfig `mapstructure:"cloud.provider"` + K8sClusterName ResourceAttributeConfig `mapstructure:"k8s.cluster.name"` } func DefaultResourceAttributesConfig() ResourceAttributesConfig { @@ -37,5 +38,8 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig { CloudProvider: ResourceAttributeConfig{ Enabled: true, }, + K8sClusterName: ResourceAttributeConfig{ + Enabled: true, + }, } } diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config_test.go index 9ce16e7f0d6a..fa542527d5fb 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config_test.go @@ -25,15 +25,17 @@ func TestResourceAttributesConfig(t *testing.T) { { name: "all_set", want: ResourceAttributesConfig{ - CloudPlatform: ResourceAttributeConfig{Enabled: true}, - CloudProvider: ResourceAttributeConfig{Enabled: true}, + CloudPlatform: ResourceAttributeConfig{Enabled: true}, + CloudProvider: ResourceAttributeConfig{Enabled: true}, + K8sClusterName: ResourceAttributeConfig{Enabled: true}, }, }, { name: "none_set", want: ResourceAttributesConfig{ - CloudPlatform: ResourceAttributeConfig{Enabled: false}, - CloudProvider: ResourceAttributeConfig{Enabled: false}, + CloudPlatform: ResourceAttributeConfig{Enabled: false}, + CloudProvider: ResourceAttributeConfig{Enabled: false}, + K8sClusterName: ResourceAttributeConfig{Enabled: false}, }, }, } diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource.go index aff8c18f53ad..b4286d831a6b 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource.go @@ -35,6 +35,13 @@ func (rb *ResourceBuilder) SetCloudProvider(val string) { } } +// SetK8sClusterName sets provided value as "k8s.cluster.name" attribute. +func (rb *ResourceBuilder) SetK8sClusterName(val string) { + if rb.config.K8sClusterName.Enabled { + rb.res.Attributes().PutStr("k8s.cluster.name", val) + } +} + // Emit returns the built resource and resets the internal builder state. func (rb *ResourceBuilder) Emit() pcommon.Resource { r := rb.res diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go index 40fc980e81bc..f4baedf1074c 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go @@ -15,15 +15,16 @@ func TestResourceBuilder(t *testing.T) { rb := NewResourceBuilder(cfg) rb.SetCloudPlatform("cloud.platform-val") rb.SetCloudProvider("cloud.provider-val") + rb.SetK8sClusterName("k8s.cluster.name-val") res := rb.Emit() assert.Equal(t, 0, rb.Emit().Attributes().Len()) // Second call should return empty Resource switch test { case "default": - assert.Equal(t, 2, res.Attributes().Len()) + assert.Equal(t, 3, res.Attributes().Len()) case "all_set": - assert.Equal(t, 2, res.Attributes().Len()) + assert.Equal(t, 3, res.Attributes().Len()) case "none_set": assert.Equal(t, 0, res.Attributes().Len()) return @@ -41,6 +42,11 @@ func TestResourceBuilder(t *testing.T) { if ok { assert.EqualValues(t, "cloud.provider-val", val.Str()) } + val, ok = res.Attributes().Get("k8s.cluster.name") + assert.True(t, ok) + if ok { + assert.EqualValues(t, "k8s.cluster.name-val", val.Str()) + } }) } } diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/testdata/config.yaml b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/testdata/config.yaml index d00b63470c51..1b7d4c7eda41 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/testdata/config.yaml +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/testdata/config.yaml @@ -5,9 +5,13 @@ all_set: enabled: true cloud.provider: enabled: true + k8s.cluster.name: + enabled: true none_set: resource_attributes: cloud.platform: enabled: false cloud.provider: enabled: false + k8s.cluster.name: + enabled: false diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml index c0065862472b..96eb3ce15059 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml +++ b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml @@ -10,4 +10,8 @@ resource_attributes: cloud.platform: description: The cloud.platform type: string - enabled: true \ No newline at end of file + enabled: true + k8s.cluster.name: + description: The k8s.cluster.name + type: string + enabled: true From 54db3d9f46c795fc5ac5624dfe7603194f5870c6 Mon Sep 17 00:00:00 2001 From: jsirianni Date: Mon, 30 Oct 2023 16:45:55 -0400 Subject: [PATCH 02/12] re-write doc and consider the mc prefix --- .../internal/azure/aks/aks.go | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go index c6ee99d064dc..95c4d10d0585 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go @@ -77,22 +77,38 @@ func azureMetadataAvailable(ctx context.Context, p azure.Provider) bool { return err == nil } -// parseClusterName parses the cluster name from the resource group name. -// Generally the resource group name will be of the form (MC|mc)_resource-group_cluster-name_location -// It is possible for the cluster name to have underscores. It is also possible -// for the user to override the "infrastucture resource group" which will change -// the format of the resource group name, it can also have underscores. If the -// cluster name cannot be parsed, the resource group name is returned as it can -// be used to uniquely identify the cluster. Azure will not allow the user to -// deploy multiple AKS clusters with the same "infrastructure resource group" -// name, making the resource group name a reliable way to identify the cluster. +// parseClusterName parses the cluster name from the infrastructure +// resource group name. AKS IMDS returns the resource group name in +// the following formats: +// +// 1. Generated group: MC___ +// - Example: +// - Resource group: my-resource-group +// - Cluster name: my-cluster +// - Location: eastus +// - Generated name: MC_my-resource-group_my-cluster_eastus +// +// 2. Custom group: custom-infra-resource-group-name +// +// When using the generated infrastructure resource group, the resource +// group will include the cluster name. If the cluster's resource group +// or cluster name contains underscores, parsing will fall back on the +// unparsed infrastructure resource group name. +// +// When using a custom infrastructure resource group, the resource group name +// does not contain the cluster name. The custom infrastructure resource group +// name is returned instead. +// +// It is safe to use the infrastructure resource group name as a unique identifier +// because Azure will not allow the user to create multiple AKS clusters with the same +// infrastructure resource group name. func parseClusterName(resourceGroup string) string { // Code inspired by https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/datadogexporter/internal/hostmetadata/internal/azure/provider.go#L36 splitAll := strings.Split(resourceGroup, "_") - switch len(splitAll) { - case 4: + + if len(splitAll) == 4 && strings.ToLower(splitAll[0]) == "mc" { return splitAll[len(splitAll)-2] - default: - return resourceGroup } + + return resourceGroup } From 74a2cc7b098771ad12d9ff3875108985455b452a Mon Sep 17 00:00:00 2001 From: jsirianni Date: Mon, 30 Oct 2023 16:46:12 -0400 Subject: [PATCH 03/12] add tests for happy path and common failures --- .../internal/azure/aks/aks_test.go | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go index d4c3f6d5afc6..bc1e4f98bdf1 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go @@ -69,25 +69,48 @@ func mockProvider() *azure.MockProvider { func TestParseClusterName(t *testing.T) { cases := []struct { - name string - input string - expected string + name string + resourceGroup string + expected string }{ { - name: "parses cluster name", - input: "MC_myResourceGroup_myAKSCluster_eastus", - expected: "myAKSCluster", + name: "Return cluster name", + resourceGroup: "MC_myResourceGroup_AKSCluster_eastus", + expected: "AKSCluster", }, { - name: "returns resource group when cluster name has underscores", - input: "MC_myResourceGroup_my_AKS_Cluster_eastus", - expected: "MC_myResourceGroup_my_AKS_Cluster_eastus", + name: "Return resource group name, resource group contains underscores", + resourceGroup: "MC_Resource_Group_AKSCluster_eastus", + expected: "MC_Resource_Group_AKSCluster_eastus", + }, + { + name: "Return resource group name, cluster name contains underscores", + resourceGroup: "MC_myResourceGroup_AKS_Cluster_eastus", + expected: "MC_myResourceGroup_AKS_Cluster_eastus", + }, + { + name: "Custom infrastructure resource group name, return resource group name", + resourceGroup: "infra-group_name", + expected: "infra-group_name", + }, + { + name: "Custom infrastructure resource group name with four underscores, return resource group name", + resourceGroup: "dev_infra_group_name", + expected: "dev_infra_group_name", + }, + // This case is unlikely because it would require the user to create + // a custom infrastructure resource group with the MC prefix and the + // correct number of underscores. + { + name: "Custom infrastructure resource group name with MC prefix", + resourceGroup: "MC_group_name_location", + expected: "name", }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - actual := parseClusterName(tc.input) + actual := parseClusterName(tc.resourceGroup) assert.Equal(t, tc.expected, actual) }) } From b03daa9315dacb9acb0667f372136f720020c515 Mon Sep 17 00:00:00 2001 From: jsirianni Date: Mon, 30 Oct 2023 16:47:04 -0400 Subject: [PATCH 04/12] remove debug print --- internal/metadataproviders/azure/metadata.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/metadataproviders/azure/metadata.go b/internal/metadataproviders/azure/metadata.go index 5e748d104391..b6241feab869 100644 --- a/internal/metadataproviders/azure/metadata.go +++ b/internal/metadataproviders/azure/metadata.go @@ -85,8 +85,6 @@ func (p *azureProviderImpl) Metadata(ctx context.Context) (*ComputeMetadata, err return nil, fmt.Errorf("failed to read Azure IMDS reply: %w", err) } - fmt.Println(string(respBody)) - var metadata *ComputeMetadata err = json.Unmarshal(respBody, &metadata) if err != nil { From 2ea69432956376966728250aab4b6558069620bc Mon Sep 17 00:00:00 2001 From: jsirianni Date: Thu, 16 Nov 2023 12:36:45 -0500 Subject: [PATCH 05/12] add changelog entry --- ...detectionprocessor-azure-cluster-name.yaml | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/resourcedetectionprocessor-azure-cluster-name.yaml diff --git a/.chloggen/resourcedetectionprocessor-azure-cluster-name.yaml b/.chloggen/resourcedetectionprocessor-azure-cluster-name.yaml new file mode 100644 index 000000000000..168388405a27 --- /dev/null +++ b/.chloggen/resourcedetectionprocessor-azure-cluster-name.yaml @@ -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: processor/resourcedetectionprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Detect Azure cluster name from IMDS metadata + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [26794] + +# (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: [] From b08a5f6ada971aa8518e5a670e89d84b935bc282 Mon Sep 17 00:00:00 2001 From: jsirianni Date: Thu, 16 Nov 2023 12:41:40 -0500 Subject: [PATCH 06/12] update aks resource attribute list --- processor/resourcedetectionprocessor/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/processor/resourcedetectionprocessor/README.md b/processor/resourcedetectionprocessor/README.md index 691fc84d4c8f..12d32dc803fc 100644 --- a/processor/resourcedetectionprocessor/README.md +++ b/processor/resourcedetectionprocessor/README.md @@ -400,6 +400,7 @@ processors: * cloud.provider ("azure") * cloud.platform ("azure_aks") + * k8s.cluster.name ```yaml processors: From 6feb5e05ce66e3c8f88b6236db822e3e6ec875ce Mon Sep 17 00:00:00 2001 From: jsirianni Date: Thu, 16 Nov 2023 14:05:04 -0500 Subject: [PATCH 07/12] unnest k8s.cluster.name --- .../internal/azure/aks/aks.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go index 95c4d10d0585..67a52f5c9e67 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go @@ -55,14 +55,13 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem } if d.resourceAttributes.CloudPlatform.Enabled { attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureAKS) - - if d.resourceAttributes.K8sClusterName.Enabled { - m, err := d.provider.Metadata(ctx) - if err != nil { - return res, "", fmt.Errorf("failed to get IMDS metadata: %w", err) - } - attrs.PutStr(conventions.AttributeK8SClusterName, parseClusterName(m.ResourceGroupName)) + } + if d.resourceAttributes.K8sClusterName.Enabled { + m, err := d.provider.Metadata(ctx) + if err != nil { + return res, "", fmt.Errorf("failed to get IMDS metadata: %w", err) } + attrs.PutStr(conventions.AttributeK8SClusterName, parseClusterName(m.ResourceGroupName)) } return res, conventions.SchemaURL, nil From c7445d09ff3dbad1b5dd24ddc585dd7483cdd439 Mon Sep 17 00:00:00 2001 From: jsirianni Date: Thu, 16 Nov 2023 14:07:10 -0500 Subject: [PATCH 08/12] refactor to call imds endpoint once --- .../internal/azure/aks/aks.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go index 67a52f5c9e67..0619ab2708ec 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks.go @@ -5,7 +5,6 @@ package aks // import "github.com/open-telemetry/opentelemetry-collector-contrib import ( "context" - "fmt" "os" "strings" @@ -44,8 +43,9 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem return res, "", nil } + m, err := d.provider.Metadata(ctx) // If we can't get a response from the metadata endpoint, we're not running in Azure - if !azureMetadataAvailable(ctx, d.provider) { + if err != nil { return res, "", nil } @@ -57,10 +57,6 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem attrs.PutStr(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformAzureAKS) } if d.resourceAttributes.K8sClusterName.Enabled { - m, err := d.provider.Metadata(ctx) - if err != nil { - return res, "", fmt.Errorf("failed to get IMDS metadata: %w", err) - } attrs.PutStr(conventions.AttributeK8SClusterName, parseClusterName(m.ResourceGroupName)) } @@ -71,11 +67,6 @@ func onK8s() bool { return os.Getenv(kubernetesServiceHostEnvVar) != "" } -func azureMetadataAvailable(ctx context.Context, p azure.Provider) bool { - _, err := p.Metadata(ctx) - return err == nil -} - // parseClusterName parses the cluster name from the infrastructure // resource group name. AKS IMDS returns the resource group name in // the following formats: From a9be94bcd91bee207125fc72e5d9ba7ba6e02e27 Mon Sep 17 00:00:00 2001 From: jsirianni Date: Tue, 21 Nov 2023 09:54:33 -0500 Subject: [PATCH 09/12] document aks cluster name parsing --- processor/resourcedetectionprocessor/README.md | 8 ++++++++ .../internal/azure/aks/metadata.yaml | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/processor/resourcedetectionprocessor/README.md b/processor/resourcedetectionprocessor/README.md index 12d32dc803fc..c9c5ee0d8605 100644 --- a/processor/resourcedetectionprocessor/README.md +++ b/processor/resourcedetectionprocessor/README.md @@ -410,6 +410,14 @@ processors: override: false ``` +#### Cluster Name + +Azure AKS cluster name is derived from the Azure Instance Metadata Service's (IMDS) infrastructure resource group field. This field contains the resource group and name of the cluster, separated by underscores. e.g: `MC___`. + +The cluster name is accurately detected if underscores are not present in the resource group name or the cluster name. + +If accurate parsing cannot be performed, the infrastructure resource group value is returned. This value can be used to accurately identify the cluster, as Azure will not allow users to create multiple clusters with the same infrastructure resource group. + ### Consul Queries a [consul agent](https://www.consul.io/docs/agent) and reads its' [configuration endpoint](https://www.consul.io/api-docs/agent#read-configuration) to retrieve the following resource attributes: diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml index 96eb3ce15059..b3930de3e7a1 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml +++ b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml @@ -12,6 +12,6 @@ resource_attributes: type: string enabled: true k8s.cluster.name: - description: The k8s.cluster.name + description: The k8s.cluster.name parsed from the Azure Instance Metadata Service's infrastructure resource group field type: string enabled: true From f6e6efb1f3d9c088d18ed611abda926ae9d5aa91 Mon Sep 17 00:00:00 2001 From: Joe Sirianni Date: Mon, 18 Dec 2023 10:45:21 -0500 Subject: [PATCH 10/12] pr feedback: Disable cluster name by default. Misc spelling / wording. --- .../resourcedetectionprocessor/README.md | 24 +++++++++++++++++-- .../internal/azure/aks/metadata.yaml | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/processor/resourcedetectionprocessor/README.md b/processor/resourcedetectionprocessor/README.md index c9c5ee0d8605..05a75bdc8bf4 100644 --- a/processor/resourcedetectionprocessor/README.md +++ b/processor/resourcedetectionprocessor/README.md @@ -412,11 +412,31 @@ processors: #### Cluster Name +Cluster name detection is disabled by default, and can be enabled with the +following configuration: + +```yaml +processors: + resourcedetection/aks: + detectors: [aks] + timeout: 2s + override: false + aks: + resource_attributes: + k8s.cluster.name: true +``` + Azure AKS cluster name is derived from the Azure Instance Metadata Service's (IMDS) infrastructure resource group field. This field contains the resource group and name of the cluster, separated by underscores. e.g: `MC___`. -The cluster name is accurately detected if underscores are not present in the resource group name or the cluster name. +Example: + - Resource group: my-resource-group + - Cluster name: my-cluster + - Location: eastus + - Generated name: MC_my-resource-group_my-cluster_eastus + +The cluster name is detected if it does not contain underscores and if a custom infrastructure resource group name was not used. -If accurate parsing cannot be performed, the infrastructure resource group value is returned. This value can be used to accurately identify the cluster, as Azure will not allow users to create multiple clusters with the same infrastructure resource group. +If accurate parsing cannot be performed, the infrastructure resource group value is returned. This value can be used to uniquely identify the cluster, as Azure will not allow users to create multiple clusters with the same infrastructure resource group name. ### Consul diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml index b3930de3e7a1..c5c1af7fbc15 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml +++ b/processor/resourcedetectionprocessor/internal/azure/aks/metadata.yaml @@ -14,4 +14,4 @@ resource_attributes: k8s.cluster.name: description: The k8s.cluster.name parsed from the Azure Instance Metadata Service's infrastructure resource group field type: string - enabled: true + enabled: false From a68f8b0b94bc61afe2881dd4864f2951642674eb Mon Sep 17 00:00:00 2001 From: jsirianni Date: Fri, 12 Jan 2024 11:09:58 -0500 Subject: [PATCH 11/12] Re generate --- .../internal/azure/aks/internal/metadata/generated_config.go | 2 +- .../azure/aks/internal/metadata/generated_resource_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go index f22bbb0ac9d7..2eb05eb09483 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_config.go @@ -39,7 +39,7 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig { Enabled: true, }, K8sClusterName: ResourceAttributeConfig{ - Enabled: true, + Enabled: false, }, } } diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go index f4baedf1074c..a467659755bc 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/internal/metadata/generated_resource_test.go @@ -22,7 +22,7 @@ func TestResourceBuilder(t *testing.T) { switch test { case "default": - assert.Equal(t, 3, res.Attributes().Len()) + assert.Equal(t, 2, res.Attributes().Len()) case "all_set": assert.Equal(t, 3, res.Attributes().Len()) case "none_set": @@ -43,7 +43,7 @@ func TestResourceBuilder(t *testing.T) { assert.EqualValues(t, "cloud.provider-val", val.Str()) } val, ok = res.Attributes().Get("k8s.cluster.name") - assert.True(t, ok) + assert.Equal(t, test == "all_set", ok) if ok { assert.EqualValues(t, "k8s.cluster.name-val", val.Str()) } From 3f0c0e39957493087c243a3287084e541cd79c6d Mon Sep 17 00:00:00 2001 From: jsirianni Date: Thu, 25 Jan 2024 15:59:38 -0500 Subject: [PATCH 12/12] fix TestDetector_Detect_K8s_Azure --- .../resourcedetectionprocessor/internal/azure/aks/aks_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go index bc1e4f98bdf1..56a6ccf51d01 100644 --- a/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go +++ b/processor/resourcedetectionprocessor/internal/azure/aks/aks_test.go @@ -34,8 +34,6 @@ func TestDetector_Detect_K8s_Azure(t *testing.T) { assert.Equal(t, map[string]any{ "cloud.provider": "azure", "cloud.platform": "azure_aks", - // Cluster name will not be detected if resource grup is not set - "k8s.cluster.name": "", }, res.Attributes().AsRaw(), "Resource attrs returned are incorrect") }