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_metadata - Retrieve AKS cluster name and id #37685

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7e74460
Retrieve aks cluster name and id
MichaelKatsoulis Jan 22, 2024
7906f04
Add tests
MichaelKatsoulis Jan 24, 2024
eada5b2
Update docs
MichaelKatsoulis Jan 24, 2024
c8d065a
Lint errors and notice txt
MichaelKatsoulis Jan 25, 2024
2c1095d
make check updates
MichaelKatsoulis Jan 25, 2024
47b4fea
Update tests
MichaelKatsoulis Jan 25, 2024
d6da3c5
Merge remote-tracking branch 'upstream/main' into add-aks-cluster-nam…
MichaelKatsoulis Jan 25, 2024
775afc9
Use fake credentials
MichaelKatsoulis Jan 25, 2024
d778f99
Unused logger
MichaelKatsoulis Jan 25, 2024
2f67b55
Review comments and declare new cloud.resourceGroup.name field
MichaelKatsoulis Feb 13, 2024
f728115
Rename var cloud.resourceGroup.name to cloud.resource_group.name
MichaelKatsoulis Feb 14, 2024
df2096b
Merge remote-tracking branch 'upstream/main' into add-aks-cluster-nam…
MichaelKatsoulis Feb 14, 2024
406863c
Update CHANGELOG.next.asciidoc
MichaelKatsoulis Feb 20, 2024
c73cba1
Fix asciidoc file
MichaelKatsoulis Feb 20, 2024
9c7cd50
Merge remote-tracking branch 'upstream/main' into add-aks-cluster-nam…
MichaelKatsoulis Feb 20, 2024
0e08561
Rename new field to azure.resourcegroup.name to align with otel
MichaelKatsoulis Mar 4, 2024
ef3e8da
Update unit test
MichaelKatsoulis Mar 5, 2024
70865f3
Update changelog
MichaelKatsoulis Mar 5, 2024
d57fa5f
Add azure.resourcegroup.name field under cloud key in fields.yml
MichaelKatsoulis Mar 5, 2024
757fc01
Add fmt.Errorf in error returns
MichaelKatsoulis Mar 6, 2024
884af33
Remove azure.resourcegroup.name
MichaelKatsoulis Mar 8, 2024
e8c5400
Update libbeat/processors/add_cloud_metadata/docs/add_cloud_metadata.…
MichaelKatsoulis Mar 8, 2024
2ce7d88
Update libbeat/processors/add_cloud_metadata/docs/add_cloud_metadata.…
MichaelKatsoulis Mar 8, 2024
17a1286
Update libbeat/processors/add_cloud_metadata/provider_azure_vm.go
MichaelKatsoulis Mar 8, 2024
99db284
Update libbeat/processors/add_cloud_metadata/provider_azure_vm.go
MichaelKatsoulis Mar 8, 2024
df6f6c5
Update libbeat/processors/add_cloud_metadata/provider_azure_vm.go
MichaelKatsoulis Mar 8, 2024
c74720c
Update Id to ID
MichaelKatsoulis Mar 8, 2024
665f9d5
Update libbeat/processors/add_cloud_metadata/provider_azure_vm.go
MichaelKatsoulis Mar 11, 2024
785135f
Merge branch 'main' into add-aks-cluster-name-id
MichaelKatsoulis Mar 11, 2024
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
11 changes: 0 additions & 11 deletions auditbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2559,17 +2559,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-common]]
== Common fields

Expand Down
2 changes: 1 addition & 1 deletion auditbeat/include/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions filebeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27762,17 +27762,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-coredns]]
== Coredns fields

Expand Down
2 changes: 1 addition & 1 deletion filebeat/include/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions heartbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-common]]
== Common heartbeat monitor fields

Expand Down
2 changes: 1 addition & 1 deletion heartbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions libbeat/processors/add_cloud_metadata/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,3 @@
type: alias
path: cloud.region
migration: true

- name: azure.resourcegroup
default_field: true
type: group
fields:
- name: name
type: keyword
description: >
Name of resourceGroup the azure cloud instance belongs to.

Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ List of names the `providers` setting supports:

- "alibaba", or "ecs" for the Alibaba Cloud provider (disabled by default).
- "azure" for Azure Virtual Machine (enabled by default).
If the Virtual Machine is part of an AKS managed cluster, the fields
If the virtual machine is part of an AKS managed cluster, the fields
`orchestrator.cluster.name` and `orchestrator.cluster.id` can also be
retrieved. "TENANT_ID", "CLIENT_ID" and "CLIENT_SECRET" environment
variables need to be set for authentication purposes. If not set we
fallback to https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#2-authenticate-with-azure[DefaultAzureCredential] and user can choose different authentication methods(e.g. workload identity).
fallback to https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authentication?tabs=bash#2-authenticate-with-azure[DefaultAzureCredential] and user can choose different authentication methods (e.g. workload identity).
- "digitalocean" for Digital Ocean (enabled by default).
- "aws", or "ec2" for Amazon Web Services (enabled by default).
- "gcp" for Google Copmute Enging (enabled by default).
Expand Down Expand Up @@ -159,9 +159,6 @@ _Azure Virtual Machine_
[source,json]
-------------------------------------------------------------------------------
{
"azure": {
"resourcegroup.name": "/subscriptions/641ebacb-7743-41e7-b4fa-af1167351a61/resourcegroups/testgroup/providers/Microsoft.ContainerService/managedClusters/testcluster"
},
"cloud": {
"provider": "azure",
"instance.id": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e",
Expand Down
53 changes: 26 additions & 27 deletions libbeat/processors/add_cloud_metadata/provider_azure_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newAzureMetadataFetcher(
return azFetcher, nil
}

// NewClusterClient returns a NewManagedClustersClient
// NewClusterClient variable is assigned an anonymous function that returns a NewManagedClustersClient
MichaelKatsoulis marked this conversation as resolved.
Show resolved Hide resolved
var NewClusterClient func(clientFactory *armcontainerservice.ClientFactory) *armcontainerservice.ManagedClustersClient = func(clientFactory *armcontainerservice.ClientFactory) *armcontainerservice.ManagedClustersClient {
MichaelKatsoulis marked this conversation as resolved.
Show resolved Hide resolved
return clientFactory.NewManagedClustersClient()
}
Expand Down Expand Up @@ -83,15 +83,11 @@ var azureVMMetadataFetcher = provider{
"service": s.Object{
"name": c.Str("serviceName"),
},
"region": c.Str("location"),
"region": c.Str("location"),
"resourcegroup": c.Str("resourceGroupName"),
}.Apply(m)

azure, _ := s.Schema{
"resourcegroup": s.Object{
"name": c.Str("resourceGroupName"),
},
}.Apply(m)
return mapstr.M{"cloud": cloud, "azure": azure}
return mapstr.M{"cloud": cloud}
}

azGenSchema := func(m map[string]interface{}) mapstr.M {
Expand All @@ -108,7 +104,7 @@ var azureVMMetadataFetcher = provider{
if err != nil {
return hfetcher, fmt.Errorf("failed to create new http metadata fetcher: %w", err)
}
// fetcher represents an azure metadata fetcher. The struct includes two type of fetchers.
// fetcher represents an azure metadata fetcher. The struct includes two types of fetchers.
// 1. An http fetcher(hfetcher) which retrieves metadata from azure metadata endpoint and
// 2. A generic fetcher(gfetcher) which uses azure sdk to retrieve metadata of azure managed clusters.
fetcher, err := newAzureMetadataFetcher("azure", hfetcher)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defined in line 44. Can you also add a comment in line 44 or here?
Basic some explanation why hfetcher, fetcher and gfetcher differ?
sth like :
hfetcher: creates the handler
fetcher: the handler with the specific cloud provider is initialised
gfethcer: the actual metadata fetcher that calls fetchAzureClusterMeta

Expand All @@ -129,8 +125,8 @@ var azureVMMetadataFetcher = provider{
}

// fetchMetadata fetches azure vm metadata from
// 1. Azure metadata endpoint with httpMetadataFetcher
// 2. Azure Managed Clusters using azure sdk with genericMetadataFetcher
// 1. Azure metadata endpoint with httpMetadataFetcher
// 2. Azure Managed Clusters using azure sdk with genericMetadataFetcher
func (az *azureMetadataFetcher) fetchMetadata(ctx context.Context, client http.Client) result {
Copy link
Contributor

@gizas gizas Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment for the fetchMetadata function before the definiton?

res := result{provider: az.provider, metadata: mapstr.M{}, err: nil}
logger := logp.NewLogger("add_cloud_metadata")
Expand Down Expand Up @@ -163,8 +159,8 @@ func getAzureCredentials(logger *logp.Logger) (azcore.TokenCredential, error) {
}
}

// getAKSClusterNameId returns the AKS cluster name and Id for a given resourceGroup
func getAKSClusterNameId(ctx context.Context, logger *logp.Logger, clusterClient *armcontainerservice.ManagedClustersClient, resourceGroupName string) (string, string, error) {
// getAKSClusterNameID returns the AKS cluster name and ID for a given resourceGroup
func getAKSClusterNameID(ctx context.Context, clusterClient *armcontainerservice.ManagedClustersClient, resourceGroupName string) (string, string, error) {
pager := clusterClient.NewListPager(nil)
for pager.More() {
page, err := pager.NextPage(ctx)
Expand All @@ -188,44 +184,47 @@ func (az *azureMetadataFetcher) fetchAzureClusterMeta(
result *result,
) {
logger := logp.NewLogger("add_cloud_metadata")
subscriptionId, _ := az.httpMeta.GetValue("cloud.account.id")
resourceGroupName, _ := az.httpMeta.GetValue("azure.resourcegroup.name")
subscriptionID, _ := az.httpMeta.GetValue("cloud.account.id")
resourceGroupName, _ := az.httpMeta.GetValue("cloud.resourcegroup")
strResourceGroupName := ""
if val, ok := resourceGroupName.(string); ok {
strResourceGroupName = val
}
strSubscriptionId := ""
if val, ok := subscriptionId.(string); ok {
strSubscriptionId = val
// Drop cloud.resourcegroup field as we do not want the cloud provider to populate this field
az.httpMeta.Delete("cloud.resourcegroup")

strSubscriptionID := ""
if val, ok := subscriptionID.(string); ok {
strSubscriptionID = val
}
// if subscriptionId cannot be retrieved from metadata endpoint return an error
if strSubscriptionId == "" {
logger.Debugf("subscriptionId cannot be retrieved from metadata endpoint")
result.err = fmt.Errorf("subscriptionId is required to create a new azure client")
// if subscriptionID cannot be retrieved from metadata endpoint return an error
if strSubscriptionID == "" {
logger.Debugf("subscriptionID cannot be retrieved from metadata endpoint")
result.err = fmt.Errorf("subscriptionID is required to create a new azure client")
return
}

if strResourceGroupName == "" {
result.err = fmt.Errorf("resourceGroupName is required to fetch AKS cluster name and cluster Id")
result.err = fmt.Errorf("resourceGroupName is required to fetch AKS cluster name and cluster ID")
return
}
cred, err := getAzureCredentials(logger)
if err != nil {
result.err = fmt.Errorf("failed to obtain azure credentials: %w", err)
return
}
clientFactory, err := armcontainerservice.NewClientFactory(strSubscriptionId, cred, nil)
clientFactory, err := armcontainerservice.NewClientFactory(strSubscriptionID, cred, nil)
if err != nil {
result.err = fmt.Errorf("failed to create new armcontainerservice client factory: %w", err)
return
}

clusterClient := NewClusterClient(clientFactory)
clusterName, clusterId, err := getAKSClusterNameId(ctx, logger, clusterClient, strResourceGroupName)
clusterName, clusterID, err := getAKSClusterNameID(ctx, clusterClient, strResourceGroupName)
if err == nil {
_, _ = result.metadata.Put("orchestrator.cluster.id", clusterId)
_, _ = result.metadata.Put("orchestrator.cluster.id", clusterID)
_, _ = result.metadata.Put("orchestrator.cluster.name", clusterName)
} else {
result.err = fmt.Errorf("failed to get AKS cluster name and Id: %w", err)
result.err = fmt.Errorf("failed to get AKS cluster name and ID: %w", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import (
)

var cluster1Name = "testcluster1Name"
var cluster1Id = "testcluster1Id"
var cluster1ID = "testcluster1ID"

var cluster1 = armcontainerservice.ManagedCluster{
ID: to.Ptr(cluster1Id),
ID: to.Ptr(cluster1ID),
Name: to.Ptr(cluster1Name),
Properties: &armcontainerservice.ManagedClusterProperties{NodeResourceGroup: to.Ptr("MC_myname_group_myname_eastus")},
}
Expand Down Expand Up @@ -186,11 +186,6 @@ func TestRetrieveAzureMetadata(t *testing.T) {
}

expected := mapstr.M{
"azure": mapstr.M{
"resourcegroup": mapstr.M{
"name": "MC_myname_group_myname_eastus",
},
},
"cloud": mapstr.M{
"provider": "azure",
"instance": mapstr.M{
Expand All @@ -210,7 +205,7 @@ func TestRetrieveAzureMetadata(t *testing.T) {
},
"orchestrator": mapstr.M{
"cluster": mapstr.M{
"id": "testcluster1Id",
"id": "testcluster1ID",
"name": "testcluster1Name",
},
},
Expand Down
11 changes: 0 additions & 11 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10158,17 +10158,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-cloudfoundry]]
== Cloudfoundry fields

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/include/fields/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions packetbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1347,17 +1347,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-common]]
== Common fields

Expand Down
2 changes: 1 addition & 1 deletion packetbeat/include/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions winlogbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-docker-processor]]
== Docker fields

Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/include/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions x-pack/functionbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-docker-processor]]
== Docker fields

Expand Down
2 changes: 1 addition & 1 deletion x-pack/functionbeat/include/fields.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x-pack/heartbeat/include/fields.go

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions x-pack/osquerybeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,6 @@ alias to: cloud.region

--


*`azure.resourcegroup.name`*::
+
--
Name of resourceGroup the azure cloud instance belongs to.


type: keyword

--

[[exported-fields-docker-processor]]
== Docker fields

Expand Down
2 changes: 1 addition & 1 deletion x-pack/osquerybeat/include/fields.go

Large diffs are not rendered by default.

Loading