From cb847b619d4865dafd5ae4da9ab9485bae962436 Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Fri, 10 Jan 2025 13:45:53 -0500 Subject: [PATCH 1/6] Add resource labels and annotations as tags to orchestrator check --- .../orchestrator/collectors/collector.go | 2 + .../orchestrator/collectors/ecs/task.go | 2 +- .../collectors/inventory/inventory.go | 50 ++++---- .../collectors/k8s/clusterrole.go | 20 ++- .../collectors/k8s/clusterrolebinding.go | 17 ++- .../orchestrator/collectors/k8s/crd.go | 4 +- .../orchestrator/collectors/k8s/cronjob.go | 7 +- .../orchestrator/collectors/k8s/cronjob_v1.go | 13 +- .../collectors/k8s/cronjob_v1beta1.go | 13 +- .../orchestrator/collectors/k8s/daemonset.go | 17 ++- .../orchestrator/collectors/k8s/deployment.go | 17 ++- .../collectors/k8s/horizontalpodautoscaler.go | 17 ++- .../orchestrator/collectors/k8s/ingress.go | 17 ++- .../orchestrator/collectors/k8s/job.go | 17 ++- .../orchestrator/collectors/k8s/limitrange.go | 17 ++- .../orchestrator/collectors/k8s/namespace.go | 17 ++- .../collectors/k8s/networkpolicy.go | 17 ++- .../orchestrator/collectors/k8s/node.go | 17 ++- .../collectors/k8s/persistentvolume.go | 17 ++- .../collectors/k8s/persistentvolumeclaim.go | 17 ++- .../collectors/k8s/pod_unassigned.go | 17 ++- .../collectors/k8s/poddisruptionbudget.go | 17 ++- .../orchestrator/collectors/k8s/replicaset.go | 17 ++- .../orchestrator/collectors/k8s/role.go | 17 ++- .../collectors/k8s/rolebinding.go | 17 ++- .../orchestrator/collectors/k8s/service.go | 17 ++- .../collectors/k8s/serviceaccount.go | 17 ++- .../collectors/k8s/statefulset.go | 17 ++- .../collectors/k8s/storageclass.go | 17 ++- .../collectors/k8s/verticalpodautoscaler.go | 17 ++- .../orchestrator/collectors/k8scollector.go | 2 + .../cluster/orchestrator/collectors/types.go | 103 +++++++++++++++ .../processors/k8s/clusterrole.go | 2 +- .../processors/k8s/clusterrolebinding.go | 2 +- .../orchestrator/processors/k8s/cronjob_v1.go | 2 +- .../processors/k8s/cronjob_v1beta1.go | 2 +- .../orchestrator/processors/k8s/daemonset.go | 2 +- .../orchestrator/processors/k8s/deployment.go | 2 +- .../processors/k8s/horizontalpodautoscaler.go | 2 +- .../orchestrator/processors/k8s/ingress.go | 2 +- .../orchestrator/processors/k8s/job.go | 2 +- .../orchestrator/processors/k8s/limitrange.go | 2 +- .../orchestrator/processors/k8s/namespace.go | 2 +- .../processors/k8s/networkpolicy.go | 2 +- .../orchestrator/processors/k8s/node.go | 2 +- .../processors/k8s/persistentvolume.go | 2 +- .../processors/k8s/persistentvolumeclaim.go | 2 +- .../orchestrator/processors/k8s/pod.go | 2 +- .../processors/k8s/poddisruptionbudget.go | 4 +- .../orchestrator/processors/k8s/replicaset.go | 2 +- .../orchestrator/processors/k8s/role.go | 2 +- .../processors/k8s/rolebinding.go | 2 +- .../orchestrator/processors/k8s/service.go | 2 +- .../processors/k8s/serviceaccount.go | 2 +- .../processors/k8s/statefulset.go | 2 +- .../processors/k8s/storageclass.go | 2 +- .../processors/k8s/verticalpodautoscaler.go | 2 +- .../orchestrator/processors/processor.go | 3 + .../transformers/k8s/clusterrole.go | 5 +- .../transformers/k8s/clusterrole_test.go | 27 +++- .../transformers/k8s/clusterrolebinding.go | 5 +- .../k8s/clusterrolebinding_test.go | 27 +++- .../transformers/k8s/cronjob_v1.go | 6 +- .../transformers/k8s/cronjob_v1_test.go | 27 +++- .../transformers/k8s/cronjob_v1beta1.go | 7 +- .../transformers/k8s/cronjob_v1beta1_test.go | 27 +++- .../transformers/k8s/daemonset.go | 9 +- .../transformers/k8s/daemonset_test.go | 44 +++++-- .../transformers/k8s/deployment.go | 6 +- .../transformers/k8s/deployment_test.go | 29 ++++- .../k8s/horizontalpodautoscaler.go | 5 +- .../k8s/horizontalpodautoscaler_test.go | 25 +++- .../orchestrator/transformers/k8s/ingress.go | 5 +- .../transformers/k8s/ingress_test.go | 33 ++++- .../orchestrator/transformers/k8s/job.go | 9 +- .../orchestrator/transformers/k8s/job_test.go | 41 ++++-- .../transformers/k8s/limitrange.go | 7 +- .../transformers/k8s/limitrange_test.go | 25 +++- .../transformers/k8s/namespace.go | 5 +- .../transformers/k8s/namespace_test.go | 25 +++- .../transformers/k8s/networkpolicy.go | 5 +- .../transformers/k8s/networkpolicy_test.go | 31 ++++- .../orchestrator/transformers/k8s/node.go | 5 +- .../transformers/k8s/node_test.go | 43 +++++-- .../transformers/k8s/persistentvolume.go | 5 +- .../transformers/k8s/persistentvolume_test.go | 17 ++- .../transformers/k8s/persistentvolumeclaim.go | 6 +- .../k8s/persistentvolumeclaim_test.go | 27 +++- .../orchestrator/transformers/k8s/pod.go | 7 +- .../orchestrator/transformers/k8s/pod_test.go | 41 ++++-- .../transformers/k8s/poddisruptionbudget.go | 6 +- .../k8s/poddisruptionbudget_test.go | 27 +++- .../transformers/k8s/replicaset.go | 6 +- .../transformers/k8s/replicaset_test.go | 32 ++++- .../orchestrator/transformers/k8s/role.go | 6 +- .../transformers/k8s/role_test.go | 27 +++- .../transformers/k8s/rolebinding.go | 5 +- .../transformers/k8s/rolebinding_test.go | 27 +++- .../orchestrator/transformers/k8s/service.go | 5 +- .../transformers/k8s/service_test.go | 27 +++- .../transformers/k8s/serviceaccount.go | 5 +- .../transformers/k8s/serviceaccount_test.go | 27 +++- .../transformers/k8s/statefulset.go | 8 +- .../transformers/k8s/statefulset_test.go | 32 ++++- .../transformers/k8s/storageclass.go | 7 +- .../transformers/k8s/storageclass_test.go | 24 +++- .../transformers/k8s/verticalpodautoscaler.go | 6 +- .../k8s/verticalpodautoscaler_test.go | 25 +++- .../transformers/{ust.go => resourcetags.go} | 24 +++- .../transformers/resourcetags_test.go | 121 ++++++++++++++++++ .../orchestrator/transformers/ust_test.go | 47 ------- 111 files changed, 1361 insertions(+), 364 deletions(-) create mode 100644 pkg/collector/corechecks/cluster/orchestrator/collectors/types.go rename pkg/collector/corechecks/cluster/orchestrator/transformers/{ust.go => resourcetags.go} (75%) create mode 100644 pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags_test.go delete mode 100644 pkg/collector/corechecks/cluster/orchestrator/transformers/ust_test.go diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go index b85af3b72f338..3e3eaf057b673 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go @@ -48,6 +48,8 @@ type CollectorMetadata struct { Version string IsSkipped bool SkippedReason string + LabelsAsTags map[string]string + AnnotationsAsTags map[string]string } // FullName returns a string that contains the collector name and version. diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go index d87c43156d107..2278fbb86b2fb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go @@ -34,7 +34,7 @@ func NewTaskCollector(tagger tagger.Component) *TaskCollector { IsStable: false, IsMetadataProducer: true, IsManifestProducer: false, - Name: "ecstasks", + Name: collectors.ECSTaskName, NodeType: orchestrator.ECSTask, }, processor: processors.NewProcessor(ecs.NewTaskHandlers(tagger)), diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go index 6372a6ce10f25..b4ada4c57191a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go @@ -16,6 +16,7 @@ import ( workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" k8sCollectors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" ) // CollectorInventory is used to store and retrieve available collectors. @@ -26,34 +27,35 @@ type CollectorInventory struct { // NewCollectorInventory returns a new inventory containing all known // collectors. func NewCollectorInventory(cfg config.Component, store workloadmeta.Component, tagger tagger.Component) *CollectorInventory { + metadataAsTags := utils.GetMetadataAsTags(cfg) return &CollectorInventory{ collectors: []collectors.CollectorVersions{ k8sCollectors.NewCRDCollectorVersions(), k8sCollectors.NewClusterCollectorVersions(), - k8sCollectors.NewClusterRoleBindingCollectorVersions(), - k8sCollectors.NewClusterRoleCollectorVersions(), - k8sCollectors.NewCronJobCollectorVersions(), - k8sCollectors.NewDaemonSetCollectorVersions(), - k8sCollectors.NewDeploymentCollectorVersions(), - k8sCollectors.NewHorizontalPodAutoscalerCollectorVersions(), - k8sCollectors.NewIngressCollectorVersions(), - k8sCollectors.NewJobCollectorVersions(), - k8sCollectors.NewLimitRangeCollectorVersions(), - k8sCollectors.NewNamespaceCollectorVersions(), - k8sCollectors.NewNetworkPolicyCollectorVersions(), - k8sCollectors.NewNodeCollectorVersions(), - k8sCollectors.NewPersistentVolumeClaimCollectorVersions(), - k8sCollectors.NewPersistentVolumeCollectorVersions(), - k8sCollectors.NewPodDisruptionBudgetCollectorVersions(), - k8sCollectors.NewReplicaSetCollectorVersions(), - k8sCollectors.NewRoleBindingCollectorVersions(), - k8sCollectors.NewRoleCollectorVersions(), - k8sCollectors.NewServiceAccountCollectorVersions(), - k8sCollectors.NewServiceCollectorVersions(), - k8sCollectors.NewStatefulSetCollectorVersions(), - k8sCollectors.NewStorageClassCollectorVersions(), - k8sCollectors.NewUnassignedPodCollectorVersions(cfg, store, tagger), - k8sCollectors.NewVerticalPodAutoscalerCollectorVersions(), + k8sCollectors.NewClusterRoleBindingCollectorVersions(metadataAsTags), + k8sCollectors.NewClusterRoleCollectorVersions(metadataAsTags), + k8sCollectors.NewCronJobCollectorVersions(metadataAsTags), + k8sCollectors.NewDaemonSetCollectorVersions(metadataAsTags), + k8sCollectors.NewDeploymentCollectorVersions(metadataAsTags), + k8sCollectors.NewHorizontalPodAutoscalerCollectorVersions(metadataAsTags), + k8sCollectors.NewIngressCollectorVersions(metadataAsTags), + k8sCollectors.NewJobCollectorVersions(metadataAsTags), + k8sCollectors.NewLimitRangeCollectorVersions(metadataAsTags), + k8sCollectors.NewNamespaceCollectorVersions(metadataAsTags), + k8sCollectors.NewNetworkPolicyCollectorVersions(metadataAsTags), + k8sCollectors.NewNodeCollectorVersions(metadataAsTags), + k8sCollectors.NewPersistentVolumeClaimCollectorVersions(metadataAsTags), + k8sCollectors.NewPersistentVolumeCollectorVersions(metadataAsTags), + k8sCollectors.NewPodDisruptionBudgetCollectorVersions(metadataAsTags), + k8sCollectors.NewReplicaSetCollectorVersions(metadataAsTags), + k8sCollectors.NewRoleBindingCollectorVersions(metadataAsTags), + k8sCollectors.NewRoleCollectorVersions(metadataAsTags), + k8sCollectors.NewServiceAccountCollectorVersions(metadataAsTags), + k8sCollectors.NewServiceCollectorVersions(metadataAsTags), + k8sCollectors.NewStatefulSetCollectorVersions(metadataAsTags), + k8sCollectors.NewStorageClassCollectorVersions(metadataAsTags), + k8sCollectors.NewUnassignedPodCollectorVersions(cfg, store, tagger, metadataAsTags), + k8sCollectors.NewVerticalPodAutoscalerCollectorVersions(metadataAsTags), }, } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go index 1cdc6518ddc4f..9aec83d8b6d25 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go @@ -11,7 +11,9 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" + "github.com/DataDog/datadog-agent/pkg/util/log" "k8s.io/apimachinery/pkg/labels" rbacv1Informers "k8s.io/client-go/informers/rbac/v1" @@ -20,9 +22,9 @@ import ( ) // NewClusterRoleCollectorVersions builds the group of collector versions. -func NewClusterRoleCollectorVersions() collectors.CollectorVersions { +func NewClusterRoleCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewClusterRoleCollector(), + NewClusterRoleCollector(metadataAsTags), ) } @@ -36,7 +38,13 @@ type ClusterRoleCollector struct { // NewClusterRoleCollector creates a new collector for the Kubernetes // ClusterRole resource. -func NewClusterRoleCollector() *ClusterRoleCollector { +func NewClusterRoleCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleCollector { + resourceType := collectors.GetResourceType(collectors.K8sClusterRoleName, collectors.K8sClusterRoleVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + + log.Infof("ethan_test_log resourceType: %v labelsAsTags: %v annotationsAsTags: %v", resourceType, labelsAsTags, annotationsAsTags) + return &ClusterRoleCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +52,11 @@ func NewClusterRoleCollector() *ClusterRoleCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "clusterroles", + Name: collectors.K8sClusterRoleName, NodeType: orchestrator.K8sClusterRole, - Version: "rbac.authorization.k8s.io/v1", + Version: collectors.K8sClusterRoleVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.ClusterRoleHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go index 715289b771dc8..438c6c8e97770 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewClusterRoleBindingCollectorVersions builds the group of collector versions. -func NewClusterRoleBindingCollectorVersions() collectors.CollectorVersions { +func NewClusterRoleBindingCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewClusterRoleBindingCollector(), + NewClusterRoleBindingCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type ClusterRoleBindingCollector struct { // NewClusterRoleBindingCollector creates a new collector for the Kubernetes // ClusterRoleBinding resource. -func NewClusterRoleBindingCollector() *ClusterRoleBindingCollector { +func NewClusterRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleBindingCollector { + resourceType := collectors.GetResourceType(collectors.K8sClusterRoleBindingName, collectors.K8sClusterRoleBindingVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &ClusterRoleBindingCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewClusterRoleBindingCollector() *ClusterRoleBindingCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "clusterrolebindings", + Name: collectors.K8sClusterRoleBindingName, NodeType: orchestrator.K8sClusterRoleBinding, - Version: "rbac.authorization.k8s.io/v1", + Version: collectors.K8sClusterRoleBindingVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.ClusterRoleBindingHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go index b2f5f5463ec58..3d4a9a912e66b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go @@ -45,9 +45,9 @@ func NewCRDCollector() *CRDCollector { IsManifestProducer: true, IsMetadataProducer: false, SupportsManifestBuffering: false, - Name: "customresourcedefinitions", + Name: collectors.K8sCRDName, NodeType: orchestrator.K8sCRD, - Version: "apiextensions.k8s.io/v1", + Version: collectors.K8sCRDVersion, }, processor: processors.NewProcessor(new(k8sProcessors.CRDHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob.go index 5d2fbcfa980ea..c91082b01fcf2 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob.go @@ -9,12 +9,13 @@ package k8s import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" + "github.com/DataDog/datadog-agent/pkg/config/utils" ) // NewCronJobCollectorVersions builds the group of collector versions for -func NewCronJobCollectorVersions() collectors.CollectorVersions { +func NewCronJobCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewCronJobV1Collector(), - NewCronJobV1Beta1Collector(), + NewCronJobV1Collector(metadataAsTags), + NewCronJobV1Beta1Collector(metadataAsTags), ) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go index 72551c168ee3d..ff80260117bf5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -28,7 +29,11 @@ type CronJobV1Collector struct { } // NewCronJobV1Collector creates a new collector for the Kubernetes Job resource. -func NewCronJobV1Collector() *CronJobV1Collector { +func NewCronJobV1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1Collector { + resourceType := collectors.GetResourceType(collectors.K8sCronJobName, collectors.K8sCronJobVersionV1) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &CronJobV1Collector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -36,9 +41,11 @@ func NewCronJobV1Collector() *CronJobV1Collector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "cronjobs", + Name: collectors.K8sCronJobName, NodeType: orchestrator.K8sCronJob, - Version: "batch/v1", + Version: collectors.K8sCronJobVersionV1, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.CronJobV1Handlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go index 000fa354b5b81..cf09113a03677 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -28,7 +29,11 @@ type CronJobV1Beta1Collector struct { } // NewCronJobV1Beta1Collector creates a new collector for the Kubernetes Job resource. -func NewCronJobV1Beta1Collector() *CronJobV1Beta1Collector { +func NewCronJobV1Beta1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1Beta1Collector { + resourceType := collectors.GetResourceType(collectors.K8sCronJobName, collectors.K8sCronJobVersionV1Beta1) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &CronJobV1Beta1Collector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: false, @@ -36,9 +41,11 @@ func NewCronJobV1Beta1Collector() *CronJobV1Beta1Collector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "cronjobs", + Name: collectors.K8sCronJobName, NodeType: orchestrator.K8sCronJob, - Version: "batch/v1beta1", + Version: collectors.K8sCronJobVersionV1Beta1, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.CronJobV1Beta1Handlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go index bc01177cbb564..be7fca6dcd6e8 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewDaemonSetCollectorVersions builds the group of collector versions. -func NewDaemonSetCollectorVersions() collectors.CollectorVersions { +func NewDaemonSetCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewDaemonSetCollector(), + NewDaemonSetCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type DaemonSetCollector struct { // NewDaemonSetCollector creates a new collector for the Kubernetes DaemonSet // resource. -func NewDaemonSetCollector() *DaemonSetCollector { +func NewDaemonSetCollector(metadataAsTags utils.MetadataAsTags) *DaemonSetCollector { + resourceType := collectors.GetResourceType(collectors.K8sDaemonSetName, collectors.K8sDaemonSetVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &DaemonSetCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewDaemonSetCollector() *DaemonSetCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "daemonsets", + Name: collectors.K8sDaemonSetName, NodeType: orchestrator.K8sDaemonSet, - Version: "apps/v1", + Version: collectors.K8sDaemonSetVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.DaemonSetHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go index 668603d8a7e4c..5f57f2179683e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewDeploymentCollectorVersions builds the group of collector versions. -func NewDeploymentCollectorVersions() collectors.CollectorVersions { +func NewDeploymentCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewDeploymentCollector(), + NewDeploymentCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type DeploymentCollector struct { // NewDeploymentCollector creates a new collector for the Kubernetes Deployment // resource. -func NewDeploymentCollector() *DeploymentCollector { +func NewDeploymentCollector(metadataAsTags utils.MetadataAsTags) *DeploymentCollector { + resourceType := collectors.GetResourceType(collectors.K8sDeploymentName, collectors.K8sDeploymentVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &DeploymentCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewDeploymentCollector() *DeploymentCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "deployments", + Name: collectors.K8sDeploymentName, NodeType: orchestrator.K8sDeployment, - Version: "apps/v1", + Version: collectors.K8sDeploymentVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.DeploymentHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go index 2f6565ca7647d..ffbadf5068bab 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" v2Informers "k8s.io/client-go/informers/autoscaling/v2" @@ -21,9 +22,9 @@ import ( ) // NewHorizontalPodAutoscalerCollectorVersions builds the group of collector versions. -func NewHorizontalPodAutoscalerCollectorVersions() collectors.CollectorVersions { +func NewHorizontalPodAutoscalerCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewHorizontalPodAutoscalerCollector(), + NewHorizontalPodAutoscalerCollector(metadataAsTags), ) } @@ -37,7 +38,11 @@ type HorizontalPodAutoscalerCollector struct { // NewHorizontalPodAutoscalerCollector creates a new collector for the Kubernetes // HorizontalPodAutoscaler resource. -func NewHorizontalPodAutoscalerCollector() *HorizontalPodAutoscalerCollector { +func NewHorizontalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *HorizontalPodAutoscalerCollector { + resourceType := collectors.GetResourceType(collectors.K8sHPAName, collectors.K8sHPAVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &HorizontalPodAutoscalerCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -45,9 +50,11 @@ func NewHorizontalPodAutoscalerCollector() *HorizontalPodAutoscalerCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "horizontalpodautoscalers", + Name: collectors.K8sHPAName, NodeType: orchestrator.K8sHorizontalPodAutoscaler, - Version: "autoscaling/v2", + Version: collectors.K8sHPAVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.HorizontalPodAutoscalerHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go index 197dea30727ed..6ed6f59aec9e6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewIngressCollectorVersions builds the group of collector versions. -func NewIngressCollectorVersions() collectors.CollectorVersions { +func NewIngressCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewIngressCollector(), + NewIngressCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type IngressCollector struct { // NewIngressCollector creates a new collector for the Kubernetes Ingress // resource. -func NewIngressCollector() *IngressCollector { +func NewIngressCollector(metadataAsTags utils.MetadataAsTags) *IngressCollector { + resourceType := collectors.GetResourceType(collectors.K8sIngressName, collectors.K8sIngressVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &IngressCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewIngressCollector() *IngressCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "ingresses", + Name: collectors.K8sIngressName, NodeType: orchestrator.K8sIngress, - Version: "networking.k8s.io/v1", + Version: collectors.K8sIngressVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.IngressHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go index 6a66eed934877..48abdccf13bc1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewJobCollectorVersions builds the group of collector versions. -func NewJobCollectorVersions() collectors.CollectorVersions { +func NewJobCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewJobCollector(), + NewJobCollector(metadataAsTags), ) } @@ -35,7 +36,11 @@ type JobCollector struct { } // NewJobCollector creates a new collector for the Kubernetes Job resource. -func NewJobCollector() *JobCollector { +func NewJobCollector(metadataAsTags utils.MetadataAsTags) *JobCollector { + resourceType := collectors.GetResourceType(collectors.K8sJobName, collectors.K8sJobVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &JobCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -43,9 +48,11 @@ func NewJobCollector() *JobCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "jobs", + Name: collectors.K8sJobName, NodeType: orchestrator.K8sJob, - Version: "batch/v1", + Version: collectors.K8sJobVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.JobHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go index 1104c13c3524b..bf92fd6dcb000 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewLimitRangeCollectorVersions builds the group of collector versions. -func NewLimitRangeCollectorVersions() collectors.CollectorVersions { +func NewLimitRangeCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewLimitRangeCollector(), + NewLimitRangeCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type LimitRangeCollector struct { // NewLimitRangeCollector creates a new collector for the Kubernetes // LimitRange resource. -func NewLimitRangeCollector() *LimitRangeCollector { +func NewLimitRangeCollector(metadataAsTags utils.MetadataAsTags) *LimitRangeCollector { + resourceType := collectors.GetResourceType(collectors.K8sLimitRangeName, collectors.K8sLimitRangeVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &LimitRangeCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewLimitRangeCollector() *LimitRangeCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "limitranges", + Name: collectors.K8sLimitRangeName, NodeType: orchestrator.K8sLimitRange, - Version: "v1", + Version: collectors.K8sLimitRangeVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.LimitRangeHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go index e635d10e762d6..95c0abde1b3bc 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewNamespaceCollectorVersions builds the group of collector versions. -func NewNamespaceCollectorVersions() collectors.CollectorVersions { +func NewNamespaceCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewNamespaceCollector(), + NewNamespaceCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type NamespaceCollector struct { // NewNamespaceCollector creates a new collector for the Kubernetes // Namespace resource. -func NewNamespaceCollector() *NamespaceCollector { +func NewNamespaceCollector(metadataAsTags utils.MetadataAsTags) *NamespaceCollector { + resourceType := collectors.GetResourceType(collectors.K8sNamespaceName, collectors.K8sNamespaceVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &NamespaceCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewNamespaceCollector() *NamespaceCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "namespaces", + Name: collectors.K8sNamespaceName, NodeType: orchestrator.K8sNamespace, - Version: "v1", + Version: collectors.K8sNamespaceVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.NamespaceHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go index bfd568fb52a2d..91b41af47498f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/config/utils" "k8s.io/apimachinery/pkg/labels" networkingv1Informers "k8s.io/client-go/informers/networking/v1" networkingv1Listers "k8s.io/client-go/listers/networking/v1" @@ -20,9 +21,9 @@ import ( ) // NewNetworkPolicyCollectorVersions builds the group of collector versions. -func NewNetworkPolicyCollectorVersions() collectors.CollectorVersions { +func NewNetworkPolicyCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewNetworkPolicyCollector(), + NewNetworkPolicyCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type NetworkPolicyCollector struct { // NewNetworkPolicyCollector creates a new collector for the Kubernetes // NetworkPolicy resource. -func NewNetworkPolicyCollector() *NetworkPolicyCollector { +func NewNetworkPolicyCollector(metadataAsTags utils.MetadataAsTags) *NetworkPolicyCollector { + resourceType := collectors.GetResourceType(collectors.K8sNetworkPolicyName, collectors.K8sNetworkPolicyVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &NetworkPolicyCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewNetworkPolicyCollector() *NetworkPolicyCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "networkpolicies", + Name: collectors.K8sNetworkPolicyName, NodeType: orchestrator.K8sNetworkPolicy, - Version: "networking.k8s.io/v1", + Version: collectors.K8sNetworkPolicyVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.NetworkPolicyHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go index 991f52ae253fa..4ae01b4264787 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewNodeCollectorVersions builds the group of collector versions. -func NewNodeCollectorVersions() collectors.CollectorVersions { +func NewNodeCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewNodeCollector(), + NewNodeCollector(metadataAsTags), ) } @@ -35,7 +36,11 @@ type NodeCollector struct { } // NewNodeCollector creates a new collector for the Kubernetes Node resource. -func NewNodeCollector() *NodeCollector { +func NewNodeCollector(metadataAsTags utils.MetadataAsTags) *NodeCollector { + resourceType := collectors.GetResourceType(collectors.K8sNodeName, collectors.K8sNodeVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &NodeCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -43,9 +48,11 @@ func NewNodeCollector() *NodeCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "nodes", + Name: collectors.K8sNodeName, NodeType: orchestrator.K8sNode, - Version: "v1", + Version: collectors.K8sNodeVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.NodeHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go index 7c78f6af2efdc..1b429e9c5f43b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewPersistentVolumeCollectorVersions builds the group of collector versions. -func NewPersistentVolumeCollectorVersions() collectors.CollectorVersions { +func NewPersistentVolumeCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewPersistentVolumeCollector(), + NewPersistentVolumeCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type PersistentVolumeCollector struct { // NewPersistentVolumeCollector creates a new collector for the Kubernetes // PersistentVolume resource. -func NewPersistentVolumeCollector() *PersistentVolumeCollector { +func NewPersistentVolumeCollector(metadataAsTags utils.MetadataAsTags) *PersistentVolumeCollector { + resourceType := collectors.GetResourceType(collectors.K8sPersistentVolumeName, collectors.K8sPersistentVolumeVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &PersistentVolumeCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewPersistentVolumeCollector() *PersistentVolumeCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "persistentvolumes", + Name: collectors.K8sPersistentVolumeName, NodeType: orchestrator.K8sPersistentVolume, - Version: "v1", + Version: collectors.K8sPersistentVolumeVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.PersistentVolumeHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go index e5bf23dadade3..33b008a14b80f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewPersistentVolumeClaimCollectorVersions builds the group of collector versions. -func NewPersistentVolumeClaimCollectorVersions() collectors.CollectorVersions { +func NewPersistentVolumeClaimCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewPersistentVolumeClaimCollector(), + NewPersistentVolumeClaimCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type PersistentVolumeClaimCollector struct { // NewPersistentVolumeClaimCollector creates a new collector for the Kubernetes // PersistentVolumeClaim resource. -func NewPersistentVolumeClaimCollector() *PersistentVolumeClaimCollector { +func NewPersistentVolumeClaimCollector(metadataAsTags utils.MetadataAsTags) *PersistentVolumeClaimCollector { + resourceType := collectors.GetResourceType(collectors.K8sPersistentVolumeClaimName, collectors.K8sPersistentVolumeClaimVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &PersistentVolumeClaimCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewPersistentVolumeClaimCollector() *PersistentVolumeClaimCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "persistentvolumeclaims", + Name: collectors.K8sPersistentVolumeClaimName, NodeType: orchestrator.K8sPersistentVolumeClaim, - Version: "v1", + Version: collectors.K8sPersistentVolumeClaimVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.PersistentVolumeClaimHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go index de25efa0b01e4..e9b8a490c236c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go @@ -14,6 +14,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -23,9 +24,9 @@ import ( ) // NewUnassignedPodCollectorVersions builds the group of collector versions. -func NewUnassignedPodCollectorVersions(cfg config.Component, store workloadmeta.Component, tagger tagger.Component) collectors.CollectorVersions { +func NewUnassignedPodCollectorVersions(cfg config.Component, store workloadmeta.Component, tagger tagger.Component, metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewUnassignedPodCollector(cfg, store, tagger), + NewUnassignedPodCollector(cfg, store, tagger, metadataAsTags), ) } @@ -40,7 +41,11 @@ type UnassignedPodCollector struct { // NewUnassignedPodCollector creates a new collector for the Kubernetes Pod // resource that is not assigned to any node. -func NewUnassignedPodCollector(cfg config.Component, store workloadmeta.Component, tagger tagger.Component) *UnassignedPodCollector { +func NewUnassignedPodCollector(cfg config.Component, store workloadmeta.Component, tagger tagger.Component, metadataAsTags utils.MetadataAsTags) *UnassignedPodCollector { + resourceType := collectors.GetResourceType(collectors.K8sPodName, collectors.K8sPodVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &UnassignedPodCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -48,9 +53,11 @@ func NewUnassignedPodCollector(cfg config.Component, store workloadmeta.Componen IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "pods", + Name: collectors.K8sPodName, NodeType: orchestrator.K8sPod, - Version: "v1", + Version: collectors.K8sPodVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(k8sProcessors.NewPodHandlers(cfg, store, tagger)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go index 8567b95ca4974..cb4b65bd63cf5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/config/utils" "k8s.io/apimachinery/pkg/labels" v1policyinformer "k8s.io/client-go/informers/policy/v1" v1policylister "k8s.io/client-go/listers/policy/v1" @@ -20,9 +21,9 @@ import ( ) // NewPodDisruptionBudgetCollectorVersions builds the group of collector versions. -func NewPodDisruptionBudgetCollectorVersions() collectors.CollectorVersions { +func NewPodDisruptionBudgetCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewPodDisruptionBudgetCollectorVersion(), + NewPodDisruptionBudgetCollectorVersion(metadataAsTags), ) } @@ -36,7 +37,11 @@ type PodDisruptionBudgetCollector struct { // NewPodDisruptionBudgetCollectorVersion creates a new collector for the Kubernetes Pod Disruption Budget // resource. -func NewPodDisruptionBudgetCollectorVersion() *PodDisruptionBudgetCollector { +func NewPodDisruptionBudgetCollectorVersion(metadataAsTags utils.MetadataAsTags) *PodDisruptionBudgetCollector { + resourceType := collectors.GetResourceType(collectors.K8sPodDisruptionBudgetName, collectors.K8sPodDisruptionBudgetVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &PodDisruptionBudgetCollector{ informer: nil, lister: nil, @@ -46,9 +51,11 @@ func NewPodDisruptionBudgetCollectorVersion() *PodDisruptionBudgetCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "poddisruptionbudgets", + Name: collectors.K8sPodDisruptionBudgetName, NodeType: orchestrator.K8sPodDisruptionBudget, - Version: "policy/v1", + Version: collectors.K8sPodDisruptionBudgetVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.PodDisruptionBudgetHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go index 7fa0e593c7564..03ebb9d2322bc 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewReplicaSetCollectorVersions builds the group of collector versions. -func NewReplicaSetCollectorVersions() collectors.CollectorVersions { +func NewReplicaSetCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewReplicaSetCollector(), + NewReplicaSetCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type ReplicaSetCollector struct { // NewReplicaSetCollector creates a new collector for the Kubernetes ReplicaSet // resource. -func NewReplicaSetCollector() *ReplicaSetCollector { +func NewReplicaSetCollector(metadataAsTags utils.MetadataAsTags) *ReplicaSetCollector { + resourceType := collectors.GetResourceType(collectors.K8sReplicaSetName, collectors.K8sReplicaSetVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &ReplicaSetCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewReplicaSetCollector() *ReplicaSetCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "replicasets", + Name: collectors.K8sReplicaSetName, NodeType: orchestrator.K8sReplicaSet, - Version: "apps/v1", + Version: collectors.K8sReplicaSetVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.ReplicaSetHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go index 5d0999f5bd59d..4f4d60b057099 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewRoleCollectorVersions builds the group of collector versions. -func NewRoleCollectorVersions() collectors.CollectorVersions { +func NewRoleCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewRoleCollector(), + NewRoleCollector(metadataAsTags), ) } @@ -35,7 +36,11 @@ type RoleCollector struct { } // NewRoleCollector creates a new collector for the Kubernetes Role resource. -func NewRoleCollector() *RoleCollector { +func NewRoleCollector(metadataAsTags utils.MetadataAsTags) *RoleCollector { + resourceType := collectors.GetResourceType(collectors.K8sRoleName, collectors.K8sRoleVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &RoleCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -43,9 +48,11 @@ func NewRoleCollector() *RoleCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "roles", + Name: collectors.K8sRoleName, NodeType: orchestrator.K8sRole, - Version: "rbac.authorization.k8s.io/v1", + Version: collectors.K8sRoleVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.RoleHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go index 6a499a0faa151..56c868f762fe1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewRoleBindingCollectorVersions builds the group of collector versions. -func NewRoleBindingCollectorVersions() collectors.CollectorVersions { +func NewRoleBindingCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewRoleBindingCollector(), + NewRoleBindingCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type RoleBindingCollector struct { // NewRoleBindingCollector creates a new collector for the Kubernetes // RoleBinding resource. -func NewRoleBindingCollector() *RoleBindingCollector { +func NewRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *RoleBindingCollector { + resourceType := collectors.GetResourceType(collectors.K8sRoleBindingName, collectors.K8sRoleBindingVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &RoleBindingCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewRoleBindingCollector() *RoleBindingCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "rolebindings", + Name: collectors.K8sRoleBindingName, NodeType: orchestrator.K8sRoleBinding, - Version: "rbac.authorization.k8s.io/v1", + Version: collectors.K8sRoleBindingVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.RoleBindingHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go index 434a5243736a0..fc1d66f315efd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewServiceCollectorVersions builds the group of collector versions. -func NewServiceCollectorVersions() collectors.CollectorVersions { +func NewServiceCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewServiceCollector(), + NewServiceCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type ServiceCollector struct { // NewServiceCollector creates a new collector for the Kubernetes Service // resource. -func NewServiceCollector() *ServiceCollector { +func NewServiceCollector(metadataAsTags utils.MetadataAsTags) *ServiceCollector { + resourceType := collectors.GetResourceType(collectors.K8sServiceName, collectors.K8sServiceVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &ServiceCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewServiceCollector() *ServiceCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "services", + Name: collectors.K8sServiceName, NodeType: orchestrator.K8sService, - Version: "v1", + Version: collectors.K8sServiceVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.ServiceHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go index 1bb1d5cd92cf0..6b10e1f91f965 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewServiceAccountCollectorVersions builds the group of collector versions. -func NewServiceAccountCollectorVersions() collectors.CollectorVersions { +func NewServiceAccountCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewServiceAccountCollector(), + NewServiceAccountCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type ServiceAccountCollector struct { // NewServiceAccountCollector creates a new collector for the Kubernetes // ServiceAccount resource. -func NewServiceAccountCollector() *ServiceAccountCollector { +func NewServiceAccountCollector(metadataAsTags utils.MetadataAsTags) *ServiceAccountCollector { + resourceType := collectors.GetResourceType(collectors.K8sServiceAccountName, collectors.K8sServiceAccountVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &ServiceAccountCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewServiceAccountCollector() *ServiceAccountCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "serviceaccounts", + Name: collectors.K8sServiceAccountName, NodeType: orchestrator.K8sServiceAccount, - Version: "v1", + Version: collectors.K8sServiceAccountVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.ServiceAccountHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go index 2c1d839882847..ade033877db8a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewStatefulSetCollectorVersions builds the group of collector versions. -func NewStatefulSetCollectorVersions() collectors.CollectorVersions { +func NewStatefulSetCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewStatefulSetCollector(), + NewStatefulSetCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type StatefulSetCollector struct { // NewStatefulSetCollector creates a new collector for the Kubernetes // StatefulSet resource. -func NewStatefulSetCollector() *StatefulSetCollector { +func NewStatefulSetCollector(metadataAsTags utils.MetadataAsTags) *StatefulSetCollector { + resourceType := collectors.GetResourceType(collectors.K8sStatefulSetName, collectors.K8sStatefulSetVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &StatefulSetCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewStatefulSetCollector() *StatefulSetCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "statefulsets", + Name: collectors.K8sStatefulSetName, NodeType: orchestrator.K8sStatefulSet, - Version: "apps/v1", + Version: collectors.K8sStatefulSetVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.StatefulSetHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go index e789f2273b0ac..f39b1937bd0f8 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" "k8s.io/apimachinery/pkg/labels" @@ -20,9 +21,9 @@ import ( ) // NewStorageClassCollectorVersions builds the group of collector versions. -func NewStorageClassCollectorVersions() collectors.CollectorVersions { +func NewStorageClassCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewStorageClassCollector(), + NewStorageClassCollector(metadataAsTags), ) } @@ -36,7 +37,11 @@ type StorageClassCollector struct { // NewStorageClassCollector creates a new collector for the Kubernetes // StorageClass resource. -func NewStorageClassCollector() *StorageClassCollector { +func NewStorageClassCollector(metadataAsTags utils.MetadataAsTags) *StorageClassCollector { + resourceType := collectors.GetResourceType(collectors.K8sStorageClassName, collectors.K8sStorageClassVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &StorageClassCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -44,9 +49,11 @@ func NewStorageClassCollector() *StorageClassCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "storageclasses", + Name: collectors.K8sStorageClassName, NodeType: orchestrator.K8sStorageClass, - Version: "storage.k8s.io/v1", + Version: collectors.K8sStorageClassVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.StorageClassHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go index e7008e9e04b8c..bb20d48a07487 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go @@ -11,6 +11,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" v1Informers "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/informers/externalversions/autoscaling.k8s.io/v1" @@ -21,9 +22,9 @@ import ( ) // NewVerticalPodAutoscalerCollectorVersions builds the group of collector versions. -func NewVerticalPodAutoscalerCollectorVersions() collectors.CollectorVersions { +func NewVerticalPodAutoscalerCollectorVersions(metadataAsTags utils.MetadataAsTags) collectors.CollectorVersions { return collectors.NewCollectorVersions( - NewVerticalPodAutoscalerCollector(), + NewVerticalPodAutoscalerCollector(metadataAsTags), ) } @@ -37,7 +38,11 @@ type VerticalPodAutoscalerCollector struct { // NewVerticalPodAutoscalerCollector creates a new collector for the Kubernetes // VerticalPodAutoscaler resource. -func NewVerticalPodAutoscalerCollector() *VerticalPodAutoscalerCollector { +func NewVerticalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *VerticalPodAutoscalerCollector { + resourceType := collectors.GetResourceType(collectors.K8sVPAName, collectors.K8sVPAVersion) + labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] + annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] + return &VerticalPodAutoscalerCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true, @@ -45,9 +50,11 @@ func NewVerticalPodAutoscalerCollector() *VerticalPodAutoscalerCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "verticalpodautoscalers", + Name: collectors.K8sVPAName, NodeType: orchestrator.K8sVerticalPodAutoscaler, - Version: "autoscaling.k8s.io/v1", + Version: collectors.K8sVPAVersion, + LabelsAsTags: labelsAsTags, + AnnotationsAsTags: annotationsAsTags, }, processor: processors.NewProcessor(new(k8sProcessors.VerticalPodAutoscalerHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go index b7c3051a39377..e47ea97ec95b3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go @@ -72,5 +72,7 @@ func NewK8sProcessorContext(rcfg *CollectorRunConfig, metadata *CollectorMetadat }, APIClient: rcfg.APIClient, ApiGroupVersionTag: fmt.Sprintf("kube_api_version:%s", metadata.Version), + LabelsAsTags: metadata.LabelsAsTags, + AnnotationsAsTags: metadata.AnnotationsAsTags, } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go new file mode 100644 index 0000000000000..b0af56765b8d7 --- /dev/null +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go @@ -0,0 +1,103 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +package collectors + +import ( + "fmt" + "strings" +) + +const ( + ECSTaskName = "ecstasks" + + K8sClusterRoleName = "clusterroles" + K8sClusterRoleVersion = "rbac.authorization.k8s.io/v1" + + K8sClusterRoleBindingName = "clusterrolebindings" + K8sClusterRoleBindingVersion = "rbac.authorization.k8s.io/v1" + + K8sCRDName = "customresourcedefinitions" + K8sCRDVersion = "apiextensions.k8s.io/v1" + + K8sCronJobName = "cronjobs" + K8sCronJobVersionV1 = "batch/v1" + K8sCronJobVersionV1Beta1 = "batch/v1beta1" + + K8sDaemonSetName = "daemonsets" + K8sDaemonSetVersion = "apps/v1" + + K8sDeploymentName = "deployments" + K8sDeploymentVersion = "apps/v1" + + K8sHPAName = "horizontalpodautoscalers" + K8sHPAVersion = "autoscaling/v2" + + K8sIngressName = "ingresses" + K8sIngressVersion = "networking.k8s.io/v1" + + K8sJobName = "jobs" + K8sJobVersion = "batch/v1" + + K8sLimitRangeName = "limitranges" + K8sLimitRangeVersion = "v1" + + K8sNamespaceName = "namespaces" + K8sNamespaceVersion = "v1" + + K8sNetworkPolicyName = "networkpolicies" + K8sNetworkPolicyVersion = "networking.k8s.io/v1" + + K8sNodeName = "nodes" + K8sNodeVersion = "v1" + + K8sPersistentVolumeName = "persistentvolumes" + K8sPersistentVolumeVersion = "v1" + + K8sPersistentVolumeClaimName = "persistentvolumeclaims" + K8sPersistentVolumeClaimVersion = "v1" + + K8sPodName = "pods" + K8sPodVersion = "v1" + + K8sPodDisruptionBudgetName = "poddisruptionbudgets" + K8sPodDisruptionBudgetVersion = "policy/v1" + + K8sReplicaSetName = "replicasets" + K8sReplicaSetVersion = "apps/v1" + + K8sRoleName = "roles" + K8sRoleVersion = "rbac.authorization.k8s.io/v1" + + K8sRoleBindingName = "rolebindings" + K8sRoleBindingVersion = "rbac.authorization.k8s.io/v1" + + K8sServiceName = "services" + K8sServiceVersion = "v1" + + K8sServiceAccountName = "serviceaccounts" + K8sServiceAccountVersion = "v1" + + K8sStatefulSetName = "statefulsets" + K8sStatefulSetVersion = "apps/v1" + + K8sStorageClassName = "storageclasses" + K8sStorageClassVersion = "storage.k8s.io/v1" + + K8sVPAName = "verticalpodautoscalers" + K8sVPAVersion = "autoscaling.k8s.io/v1" +) + +// GetResourceType returns a string in the format "name.apiGroup" if an API group is present in the version. +// Otherwise, it returns the name. +func GetResourceType(name string, version string) string { + var apiGroup string + apiVersionParts := strings.Split(version, "/") + if len(apiVersionParts) == 2 { + apiGroup = apiVersionParts[0] + return fmt.Sprintf("%s.%s", name, apiGroup) + } + return name +} diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go index f4bbbc2cf7b22..bd6e644afa7b0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go @@ -57,7 +57,7 @@ func (h *ClusterRoleHandlers) BuildMessageBody(ctx processors.ProcessorContext, //nolint:revive // TODO(CAPP) Fix revive linter func (h *ClusterRoleHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.ClusterRole) - return k8sTransformers.ExtractClusterRole(r) + return k8sTransformers.ExtractClusterRole(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go index 9aaa212513fe5..c190070a7563b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go @@ -58,7 +58,7 @@ func (h *ClusterRoleBindingHandlers) BuildMessageBody(ctx processors.ProcessorCo //nolint:revive // TODO(CAPP) Fix revive linter func (h *ClusterRoleBindingHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.ClusterRoleBinding) - return k8sTransformers.ExtractClusterRoleBinding(r) + return k8sTransformers.ExtractClusterRoleBinding(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go index 7e9108ee3d4ee..36e112f9888b6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go @@ -57,7 +57,7 @@ func (h *CronJobV1Handlers) BuildMessageBody(ctx processors.ProcessorContext, re //nolint:revive // TODO(CAPP) Fix revive linter func (h *CronJobV1Handlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1.CronJob) - return k8sTransformers.ExtractCronJobV1(r) + return k8sTransformers.ExtractCronJobV1(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.go index 2d17b60004518..db10ba31c283d 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.go @@ -58,7 +58,7 @@ func (h *CronJobV1Beta1Handlers) BuildMessageBody(ctx processors.ProcessorContex //nolint:revive // TODO(CAPP) Fix revive linter func (h *CronJobV1Beta1Handlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1beta1.CronJob) - return k8sTransformers.ExtractCronJobV1Beta1(r) + return k8sTransformers.ExtractCronJobV1Beta1(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go index b6ec8ade9089b..e9cf6af26f6ab 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go @@ -58,7 +58,7 @@ func (h *DaemonSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, re //nolint:revive // TODO(CAPP) Fix revive linter func (h *DaemonSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.DaemonSet) - return k8sTransformers.ExtractDaemonSet(r) + return k8sTransformers.ExtractDaemonSet(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go index aed5e1a42f4cd..a97f01e764b20 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go @@ -58,7 +58,7 @@ func (h *DeploymentHandlers) BuildMessageBody(ctx processors.ProcessorContext, r //nolint:revive // TODO(CAPP) Fix revive linter func (h *DeploymentHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.Deployment) - return k8sTransformers.ExtractDeployment(r) + return k8sTransformers.ExtractDeployment(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go index cf74dd8cdb155..3f74007d17490 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go @@ -57,7 +57,7 @@ func (h *HorizontalPodAutoscalerHandlers) BuildMessageBody(ctx processors.Proces //nolint:revive // TODO(CAPP) Fix revive linter func (h *HorizontalPodAutoscalerHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (horizontalPodAutoscalerModel interface{}) { r := resource.(*v2.HorizontalPodAutoscaler) - return k8sTransformers.ExtractHorizontalPodAutoscaler(r) + return k8sTransformers.ExtractHorizontalPodAutoscaler(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go index 13e65ea2917ef..7c9c990191a69 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go @@ -58,7 +58,7 @@ func (h *IngressHandlers) BuildMessageBody(ctx processors.ProcessorContext, reso //nolint:revive // TODO(CAPP) Fix revive linter func (h *IngressHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*netv1.Ingress) - return k8sTransformers.ExtractIngress(r) + return k8sTransformers.ExtractIngress(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go index 64328ef34518b..05920c71690d5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go @@ -58,7 +58,7 @@ func (h *JobHandlers) BuildMessageBody(ctx processors.ProcessorContext, resource //nolint:revive // TODO(CAPP) Fix revive linter func (h *JobHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1.Job) - return k8sTransformers.ExtractJob(r) + return k8sTransformers.ExtractJob(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/limitrange.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/limitrange.go index 9effd8f943287..45127ea2cb9a1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/limitrange.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/limitrange.go @@ -57,7 +57,7 @@ func (h *LimitRangeHandlers) BuildMessageBody(ctx processors.ProcessorContext, r //nolint:revive func (h *LimitRangeHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (LimitRangeModel interface{}) { r := resource.(*corev1.LimitRange) - return k8sTransformers.ExtractLimitRange(r) + return k8sTransformers.ExtractLimitRange(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go index 9921fe7a706e5..a4e0bd85d2bfd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go @@ -59,7 +59,7 @@ func (h *NamespaceHandlers) BuildMessageBody(ctx processors.ProcessorContext, re //nolint:revive // TODO(CAPP) Fix revive linter func (h *NamespaceHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (namespaceModel interface{}) { r := resource.(*corev1.Namespace) - return k8sTransformers.ExtractNamespace(r) + return k8sTransformers.ExtractNamespace(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go index dd16c8031e36a..27417e39e14ea 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go @@ -57,7 +57,7 @@ func (h *NetworkPolicyHandlers) BuildMessageBody(ctx processors.ProcessorContext //nolint:revive // TODO(CAPP) Fix revive linter func (h *NetworkPolicyHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*netv1.NetworkPolicy) - return k8sTransformers.ExtractNetworkPolicy(r) + return k8sTransformers.ExtractNetworkPolicy(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go index e3348f896265b..3d9ec43407564 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go @@ -58,7 +58,7 @@ func (h *NodeHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourc //nolint:revive // TODO(CAPP) Fix revive linter func (h *NodeHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Node) - return k8sTransformers.ExtractNode(r) + return k8sTransformers.ExtractNode(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go index 991ceb7c2dc7a..fde55ca2e7181 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go @@ -58,7 +58,7 @@ func (h *PersistentVolumeHandlers) BuildMessageBody(ctx processors.ProcessorCont //nolint:revive // TODO(CAPP) Fix revive linter func (h *PersistentVolumeHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.PersistentVolume) - return k8sTransformers.ExtractPersistentVolume(r) + return k8sTransformers.ExtractPersistentVolume(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go index b6e9aeee58d0a..fd47e3751ec12 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go @@ -58,7 +58,7 @@ func (h *PersistentVolumeClaimHandlers) BuildMessageBody(ctx processors.Processo //nolint:revive // TODO(CAPP) Fix revive linter func (h *PersistentVolumeClaimHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.PersistentVolumeClaim) - return k8sTransformers.ExtractPersistentVolumeClaim(r) + return k8sTransformers.ExtractPersistentVolumeClaim(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go index db54519fbc90b..a6b9c77f318eb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go @@ -132,7 +132,7 @@ func (h *PodHandlers) BuildMessageBody(ctx processors.ProcessorContext, resource //nolint:revive // TODO(CAPP) Fix revive linter func (h *PodHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Pod) - return k8sTransformers.ExtractPod(r) + return k8sTransformers.ExtractPod(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/poddisruptionbudget.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/poddisruptionbudget.go index 3dc6fdb837358..f3b2d65c6fa44 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/poddisruptionbudget.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/poddisruptionbudget.go @@ -49,9 +49,9 @@ func (h *PodDisruptionBudgetHandlers) BuildMessageBody(ctx processors.ProcessorC } // ExtractResource is a handler called to extract the resource model out of a raw resource. -func (h *PodDisruptionBudgetHandlers) ExtractResource(_ processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *PodDisruptionBudgetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*policyv1.PodDisruptionBudget) - return k8sTransformers.ExtractPodDisruptionBudget(r) + return k8sTransformers.ExtractPodDisruptionBudget(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go index feeb0ee9e626f..475be9b42c0ef 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go @@ -58,7 +58,7 @@ func (h *ReplicaSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, r //nolint:revive // TODO(CAPP) Fix revive linter func (h *ReplicaSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.ReplicaSet) - return k8sTransformers.ExtractReplicaSet(r) + return k8sTransformers.ExtractReplicaSet(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go index 6fc61bd55ec24..1f67313346f7f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go @@ -58,7 +58,7 @@ func (h *RoleHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourc //nolint:revive // TODO(CAPP) Fix revive linter func (h *RoleHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.Role) - return k8sTransformers.ExtractRole(r) + return k8sTransformers.ExtractRole(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go index 07ffa2c7bc8f5..1ee555239ead2 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go @@ -57,7 +57,7 @@ func (h *RoleBindingHandlers) BuildMessageBody(ctx processors.ProcessorContext, //nolint:revive // TODO(CAPP) Fix revive linter func (h *RoleBindingHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.RoleBinding) - return k8sTransformers.ExtractRoleBinding(r) + return k8sTransformers.ExtractRoleBinding(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go index 25eeecf69a561..11dd7de339aed 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go @@ -58,7 +58,7 @@ func (h *ServiceHandlers) BuildMessageBody(ctx processors.ProcessorContext, reso //nolint:revive // TODO(CAPP) Fix revive linter func (h *ServiceHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Service) - return k8sTransformers.ExtractService(r) + return k8sTransformers.ExtractService(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go index e25e3d3b0e743..8e87974ba0b4a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go @@ -58,7 +58,7 @@ func (h *ServiceAccountHandlers) BuildMessageBody(ctx processors.ProcessorContex //nolint:revive // TODO(CAPP) Fix revive linter func (h *ServiceAccountHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.ServiceAccount) - return k8sTransformers.ExtractServiceAccount(r) + return k8sTransformers.ExtractServiceAccount(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go index c6fa1b0589eab..683ff29b59699 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go @@ -57,7 +57,7 @@ func (h *StatefulSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, //nolint:revive // TODO(CAPP) Fix revive linter func (h *StatefulSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.StatefulSet) - return k8sTransformers.ExtractStatefulSet(r) + return k8sTransformers.ExtractStatefulSet(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/storageclass.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/storageclass.go index d72d99fad6435..b6dac3dcbb25b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/storageclass.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/storageclass.go @@ -57,7 +57,7 @@ func (h *StorageClassHandlers) BuildMessageBody(ctx processors.ProcessorContext, //nolint:revive func (h *StorageClassHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (StorageClassModel interface{}) { r := resource.(*storagev1.StorageClass) - return k8sTransformers.ExtractStorageClass(r) + return k8sTransformers.ExtractStorageClass(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go index 1f6a9643538f6..29752c871bcac 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go @@ -57,7 +57,7 @@ func (h *VerticalPodAutoscalerHandlers) BuildMessageBody(ctx processors.Processo //nolint:revive // TODO(CAPP) Fix revive linter func (h *VerticalPodAutoscalerHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (verticalPodAutoscalerModel interface{}) { r := resource.(*v1.VerticalPodAutoscaler) - return k8sTransformers.ExtractVerticalPodAutoscaler(r) + return k8sTransformers.ExtractVerticalPodAutoscaler(ctx, r) } // ResourceList is a handler called to convert a list passed as a generic diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go b/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go index 65e5619fef47a..85ec518cc0518 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go @@ -75,6 +75,9 @@ type K8sProcessorContext struct { //nolint:revive // TODO(CAPP) Fix revive linter ApiGroupVersionTag string SystemInfo *model.SystemInfo + ResourceType string + LabelsAsTags map[string]string + AnnotationsAsTags map[string]string } // ECSProcessorContext holds ECS resource processing attributes diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go index 56402e8122664..7127c46f129c0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" rbacv1 "k8s.io/api/rbac/v1" @@ -16,7 +17,7 @@ import ( // ExtractClusterRole returns the protobuf model corresponding to a // Kubernetes ClusterRole resource. -func ExtractClusterRole(cr *rbacv1.ClusterRole) *model.ClusterRole { +func ExtractClusterRole(ctx processors.ProcessorContext, cr *rbacv1.ClusterRole) *model.ClusterRole { clusterRole := &model.ClusterRole{ Metadata: extractMetadata(&cr.ObjectMeta), Rules: extractPolicyRules(cr.Rules), @@ -27,7 +28,9 @@ func ExtractClusterRole(cr *rbacv1.ClusterRole) *model.ClusterRole { } } + pctx := ctx.(*processors.K8sProcessorContext) clusterRole.Tags = append(clusterRole.Tags, transformers.RetrieveUnifiedServiceTags(cr.ObjectMeta.Labels)...) + clusterRole.Tags = append(clusterRole.Tags, transformers.RetrieveMetadataTags(cr.ObjectMeta.Labels, cr.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return clusterRole } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole_test.go index 31405a7ce1cc1..fe4a62b93a6fa 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" rbacv1 "k8s.io/api/rbac/v1" @@ -23,8 +25,10 @@ func TestExtractClusterRole(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input rbacv1.ClusterRole - expected model.ClusterRole + input rbacv1.ClusterRole + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.ClusterRole }{ "standard": { input: rbacv1.ClusterRole{ @@ -73,6 +77,12 @@ func TestExtractClusterRole(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.ClusterRole{ AggregationRules: []*model.LabelSelectorRequirement{ { @@ -112,12 +122,23 @@ func TestExtractClusterRole(t *testing.T) { Verbs: []string{"create"}, }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractClusterRole(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractClusterRole(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding.go index 5902de6b0dfdc..34081aaf87c2b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" rbacv1 "k8s.io/api/rbac/v1" @@ -16,14 +17,16 @@ import ( // ExtractClusterRoleBinding returns the protobuf model corresponding to a // Kubernetes ClusterRoleBinding resource. -func ExtractClusterRoleBinding(crb *rbacv1.ClusterRoleBinding) *model.ClusterRoleBinding { +func ExtractClusterRoleBinding(ctx processors.ProcessorContext, crb *rbacv1.ClusterRoleBinding) *model.ClusterRoleBinding { c := &model.ClusterRoleBinding{ Metadata: extractMetadata(&crb.ObjectMeta), RoleRef: extractRoleRef(&crb.RoleRef), Subjects: extractSubjects(crb.Subjects), } + pctx := ctx.(*processors.K8sProcessorContext) c.Tags = append(c.Tags, transformers.RetrieveUnifiedServiceTags(crb.ObjectMeta.Labels)...) + c.Tags = append(c.Tags, transformers.RetrieveMetadataTags(crb.ObjectMeta.Labels, crb.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return c } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding_test.go index 0ffaf44d751cc..2be8ce5f9e8ed 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrolebinding_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" rbacv1 "k8s.io/api/rbac/v1" @@ -23,8 +25,10 @@ func TestExtractClusterRoleBinding(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input rbacv1.ClusterRoleBinding - expected model.ClusterRoleBinding + input rbacv1.ClusterRoleBinding + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.ClusterRoleBinding }{ "standard": { input: rbacv1.ClusterRoleBinding{ @@ -54,6 +58,12 @@ func TestExtractClusterRoleBinding(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.ClusterRoleBinding{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -76,12 +86,23 @@ func TestExtractClusterRoleBinding(t *testing.T) { Name: "firstname.lastname@company.com", }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractClusterRoleBinding(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractClusterRoleBinding(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1.go index 82e09d8c3754e..b90db424b885b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" batchv1 "k8s.io/api/batch/v1" @@ -16,7 +17,7 @@ import ( // ExtractCronJobV1 returns the protobuf model corresponding to a Kubernetes // CronJob resource. -func ExtractCronJobV1(cj *batchv1.CronJob) *model.CronJob { +func ExtractCronJobV1(ctx processors.ProcessorContext, cj *batchv1.CronJob) *model.CronJob { cronJob := model.CronJob{ Metadata: extractMetadata(&cj.ObjectMeta), Spec: &model.CronJobSpec{ @@ -58,7 +59,10 @@ func ExtractCronJobV1(cj *batchv1.CronJob) *model.CronJob { } cronJob.Spec.ResourceRequirements = ExtractPodTemplateResourceRequirements(cj.Spec.JobTemplate.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) cronJob.Tags = append(cronJob.Tags, transformers.RetrieveUnifiedServiceTags(cj.ObjectMeta.Labels)...) + cronJob.Tags = append(cronJob.Tags, transformers.RetrieveMetadataTags(cj.ObjectMeta.Labels, cj.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &cronJob } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1_test.go index 2d174f7592a82..22b30ed31243e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -27,8 +29,10 @@ func TestExtractCronJobV1(t *testing.T) { lastSuccessfulTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input batchv1.CronJob - expected model.CronJob + input batchv1.CronJob + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.CronJob }{ "full cron job (active)": { input: batchv1.CronJob{ @@ -68,6 +72,12 @@ func TestExtractCronJobV1(t *testing.T) { LastSuccessfulTime: &lastSuccessfulTime, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.CronJob{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -100,12 +110,23 @@ func TestExtractCronJobV1(t *testing.T) { LastScheduleTime: lastScheduleTime.Unix(), LastSuccessfulTime: lastSuccessfulTime.Unix(), }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractCronJobV1(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractCronJobV1(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1.go index 66818141aff07..3018f6477360a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1.go @@ -9,14 +9,14 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" - batchv1beta1 "k8s.io/api/batch/v1beta1" ) // ExtractCronJobV1Beta1 returns the protobuf model corresponding to a Kubernetes // CronJob resource. -func ExtractCronJobV1Beta1(cj *batchv1beta1.CronJob) *model.CronJob { +func ExtractCronJobV1Beta1(ctx processors.ProcessorContext, cj *batchv1beta1.CronJob) *model.CronJob { cronJob := model.CronJob{ Metadata: extractMetadata(&cj.ObjectMeta), Spec: &model.CronJobSpec{ @@ -55,7 +55,10 @@ func ExtractCronJobV1Beta1(cj *batchv1beta1.CronJob) *model.CronJob { } cronJob.Spec.ResourceRequirements = ExtractPodTemplateResourceRequirements(cj.Spec.JobTemplate.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) cronJob.Tags = append(cronJob.Tags, transformers.RetrieveUnifiedServiceTags(cj.ObjectMeta.Labels)...) + cronJob.Tags = append(cronJob.Tags, transformers.RetrieveMetadataTags(cj.ObjectMeta.Labels, cj.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &cronJob } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1_test.go index 2cd556ad3dc77..82e3fe78c90dd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/cronjob_v1beta1_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -27,8 +29,10 @@ func TestExtractCronJobV1Beta1(t *testing.T) { lastScheduleTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input batchv1beta1.CronJob - expected model.CronJob + input batchv1beta1.CronJob + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.CronJob }{ "full cron job (active)": { input: batchv1beta1.CronJob{ @@ -67,6 +71,12 @@ func TestExtractCronJobV1Beta1(t *testing.T) { LastScheduleTime: &lastScheduleTime, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.CronJob{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -98,6 +108,10 @@ func TestExtractCronJobV1Beta1(t *testing.T) { }, LastScheduleTime: lastScheduleTime.Unix(), }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, "cronjob with resources": { @@ -119,7 +133,14 @@ func TestExtractCronJobV1Beta1(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractCronJobV1Beta1(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractCronJobV1Beta1(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset.go index 7d9164db536dd..ba649fb17935a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset.go @@ -9,14 +9,14 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" - appsv1 "k8s.io/api/apps/v1" - + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" + appsv1 "k8s.io/api/apps/v1" ) // ExtractDaemonSet returns the protobuf model corresponding to a Kubernetes // DaemonSet resource. -func ExtractDaemonSet(ds *appsv1.DaemonSet) *model.DaemonSet { +func ExtractDaemonSet(ctx processors.ProcessorContext, ds *appsv1.DaemonSet) *model.DaemonSet { daemonSet := model.DaemonSet{ Metadata: extractMetadata(&ds.ObjectMeta), Spec: &model.DaemonSetSpec{ @@ -55,7 +55,10 @@ func ExtractDaemonSet(ds *appsv1.DaemonSet) *model.DaemonSet { } daemonSet.Spec.ResourceRequirements = ExtractPodTemplateResourceRequirements(ds.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) daemonSet.Tags = append(daemonSet.Tags, transformers.RetrieveUnifiedServiceTags(ds.ObjectMeta.Labels)...) + daemonSet.Tags = append(daemonSet.Tags, transformers.RetrieveMetadataTags(ds.ObjectMeta.Labels, ds.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &daemonSet } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset_test.go index 5aaf8a256e1c2..7d63e5c5c64d7 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/daemonset_test.go @@ -8,6 +8,7 @@ package k8s import ( + "sort" "time" "github.com/stretchr/testify/assert" @@ -17,6 +18,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "testing" ) @@ -27,8 +29,10 @@ func TestExtractDaemonset(t *testing.T) { timestamp := metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)) // 1389744000 tests := map[string]struct { - input v1.DaemonSet - expected model.DaemonSet + input v1.DaemonSet + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.DaemonSet }{ "empty ds": {input: v1.DaemonSet{}, expected: model.DaemonSet{Metadata: &model.Metadata{}, Spec: &model.DaemonSetSpec{}, Status: &model.DaemonSetStatus{}}}, "ds with resources": { @@ -63,8 +67,10 @@ func TestExtractDaemonset(t *testing.T) { "partial ds": { input: v1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ - Name: "daemonset", - Namespace: "namespace", + Name: "daemonset", + Namespace: "namespace", + Labels: map[string]string{"app": "my-app"}, + Annotations: map[string]string{"annotation": "my-annotation"}, }, Spec: v1.DaemonSetSpec{ UpdateStrategy: v1.DaemonSetUpdateStrategy{ @@ -87,10 +93,19 @@ func TestExtractDaemonset(t *testing.T) { CurrentNumberScheduled: 1, NumberReady: 1, }, - }, expected: model.DaemonSet{ + }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.DaemonSet{ Metadata: &model.Metadata{ - Name: "daemonset", - Namespace: "namespace", + Name: "daemonset", + Namespace: "namespace", + Labels: []string{"app:my-app"}, + Annotations: []string{"annotation:my-annotation"}, }, Conditions: []*model.DaemonSetCondition{ { @@ -101,7 +116,11 @@ func TestExtractDaemonset(t *testing.T) { Message: "test message", }, }, - Tags: []string{"kube_condition_test:false"}, + Tags: []string{ + "kube_condition_test:false", + "application:my-app", + "annotation_key:my-annotation", + }, Spec: &model.DaemonSetSpec{ DeploymentStrategy: "RollingUpdate", MaxUnavailable: "1%", @@ -115,7 +134,14 @@ func TestExtractDaemonset(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractDaemonSet(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractDaemonSet(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment.go index 30b9d154ed1b3..bc9f45a379a6a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" @@ -18,7 +19,7 @@ import ( // ExtractDeployment returns the protobuf model corresponding to a Kubernetes // Deployment resource. -func ExtractDeployment(d *appsv1.Deployment) *model.Deployment { +func ExtractDeployment(ctx processors.ProcessorContext, d *appsv1.Deployment) *model.Deployment { deploy := model.Deployment{ Metadata: extractMetadata(&d.ObjectMeta), } @@ -56,7 +57,10 @@ func ExtractDeployment(d *appsv1.Deployment) *model.Deployment { } deploy.ResourceRequirements = ExtractPodTemplateResourceRequirements(d.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) deploy.Tags = append(deploy.Tags, transformers.RetrieveUnifiedServiceTags(d.ObjectMeta.Labels)...) + deploy.Tags = append(deploy.Tags, transformers.RetrieveMetadataTags(d.ObjectMeta.Labels, d.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &deploy } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment_test.go index 2a96c6e29d639..d5dc893103b5d 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/deployment_test.go @@ -9,11 +9,12 @@ package k8s import ( "fmt" + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" - + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" "github.com/stretchr/testify/assert" @@ -30,8 +31,10 @@ func TestExtractDeployment(t *testing.T) { testIntOrStrNumber := intstr.FromInt(1) testInt32 := int32(2) tests := map[string]struct { - input appsv1.Deployment - expected model.Deployment + input appsv1.Deployment + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Deployment }{ "full deploy": { input: appsv1.Deployment{ @@ -87,7 +90,14 @@ func TestExtractDeployment(t *testing.T) { }, }, }, - }, expected: model.Deployment{ + }, + labelsAsTags: map[string]string{ + "label": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.Deployment{ Metadata: &model.Metadata{ Name: "deploy", Namespace: "namespace", @@ -132,6 +142,8 @@ func TestExtractDeployment(t *testing.T) { Tags: []string{ "kube_condition_available:false", "kube_condition_progressing:false", + "application:foo", + "annotation_key:bar", }, }, }, @@ -213,7 +225,14 @@ func TestExtractDeployment(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractDeployment(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractDeployment(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler.go index 353850e5ee46c..877ffd2c0851d 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" v2 "k8s.io/api/autoscaling/v2" model "github.com/DataDog/agent-payload/v5/process" @@ -17,7 +18,7 @@ import ( // ExtractHorizontalPodAutoscaler returns the protobuf model corresponding to a Kubernetes Horizontal Pod Autoscaler resource. // https://github.com/kubernetes/api/blob/v0.23.15/autoscaling/v2/types.go#L33 -func ExtractHorizontalPodAutoscaler(v *v2.HorizontalPodAutoscaler) *model.HorizontalPodAutoscaler { +func ExtractHorizontalPodAutoscaler(ctx processors.ProcessorContext, v *v2.HorizontalPodAutoscaler) *model.HorizontalPodAutoscaler { if v == nil { return &model.HorizontalPodAutoscaler{} } @@ -34,7 +35,9 @@ func ExtractHorizontalPodAutoscaler(v *v2.HorizontalPodAutoscaler) *model.Horizo m.Tags = append(m.Tags, conditionTags...) } + pctx := ctx.(*processors.K8sProcessorContext) m.Tags = append(m.Tags, transformers.RetrieveUnifiedServiceTags(v.ObjectMeta.Labels)...) + m.Tags = append(m.Tags, transformers.RetrieveMetadataTags(v.ObjectMeta.Labels, v.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return m } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler_test.go index 9ed9bfb707c1b..635326438d706 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/horizontalpodautoscaler_test.go @@ -8,6 +8,7 @@ package k8s import ( + "sort" "testing" "time" @@ -18,6 +19,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" ) func TestExtractHorizontalPodAutoscaler(t *testing.T) { @@ -34,8 +36,10 @@ func TestExtractHorizontalPodAutoscaler(t *testing.T) { *averageUtilization = 60 tests := map[string]struct { - input v2.HorizontalPodAutoscaler - expected model.HorizontalPodAutoscaler + input v2.HorizontalPodAutoscaler + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.HorizontalPodAutoscaler }{ "standard": { input: v2.HorizontalPodAutoscaler{ @@ -258,6 +262,12 @@ func TestExtractHorizontalPodAutoscaler(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.HorizontalPodAutoscaler{ Metadata: &model.Metadata{ Name: "HPATest", @@ -475,6 +485,8 @@ func TestExtractHorizontalPodAutoscaler(t *testing.T) { }, }, Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", "kube_condition_abletoscale:true", "kube_condition_scalingactive:true", "kube_condition_scalinglimited:false", @@ -897,7 +909,14 @@ func TestExtractHorizontalPodAutoscaler(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractHorizontalPodAutoscaler(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractHorizontalPodAutoscaler(pctx, &tc.input) + sort.Strings(tc.expected.Tags) + sort.Strings(actual.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress.go index 30d41a54dc642..a99bea3e2a7a6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" netv1 "k8s.io/api/networking/v1" @@ -16,7 +17,7 @@ import ( // ExtractIngress returns the protobuf model corresponding to a Kubernetes // Ingress resource. -func ExtractIngress(in *netv1.Ingress) *model.Ingress { +func ExtractIngress(ctx processors.ProcessorContext, in *netv1.Ingress) *model.Ingress { ingress := model.Ingress{ Metadata: extractMetadata(&in.ObjectMeta), Spec: &model.IngressSpec{}, @@ -43,7 +44,9 @@ func ExtractIngress(in *netv1.Ingress) *model.Ingress { ingress.Status = extractIngressStatus(in.Status) } + pctx := ctx.(*processors.K8sProcessorContext) ingress.Tags = append(ingress.Tags, transformers.RetrieveUnifiedServiceTags(in.ObjectMeta.Labels)...) + ingress.Tags = append(ingress.Tags, transformers.RetrieveMetadataTags(in.ObjectMeta.Labels, in.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &ingress } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress_test.go index 6841efe59fca8..41de57919fc4e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/ingress_test.go @@ -8,9 +8,11 @@ package k8s import ( + "sort" "testing" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -23,8 +25,10 @@ func TestExtractIngress(t *testing.T) { pathType := netv1.PathTypeImplementationSpecific tests := map[string]struct { - input netv1.Ingress - expected model.Ingress + input netv1.Ingress + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Ingress }{ "empty": {input: netv1.Ingress{}, expected: model.Ingress{Metadata: &model.Metadata{}, Spec: &model.IngressSpec{}, Status: &model.IngressStatus{}}}, "with spec and status": { @@ -32,7 +36,8 @@ func TestExtractIngress(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "ingress", Namespace: "namespace", - Annotations: map[string]string{"key": "val"}, + Annotations: map[string]string{"annotation": "my-annotation"}, + Labels: map[string]string{"app": "my-app"}, }, Spec: netv1.IngressSpec{ Rules: []netv1.IngressRule{ @@ -80,11 +85,18 @@ func TestExtractIngress(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.Ingress{ Metadata: &model.Metadata{ Name: "ingress", Namespace: "namespace", - Annotations: []string{"key:val"}, + Annotations: []string{"annotation:my-annotation"}, + Labels: []string{"app:my-app"}, }, Spec: &model.IngressSpec{ DefaultBackend: &model.IngressBackend{ @@ -122,12 +134,23 @@ func TestExtractIngress(t *testing.T) { {Hostname: "foo.us-east-1.elb.amazonaws.com"}, }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractIngress(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractIngress(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job.go index 3227b52a5c839..e4c34e0abeafb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job.go @@ -9,14 +9,14 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" - batchv1 "k8s.io/api/batch/v1" - + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" + batchv1 "k8s.io/api/batch/v1" ) // ExtractJob returns the protobuf model corresponding to a Kubernetes Job // resource. -func ExtractJob(j *batchv1.Job) *model.Job { +func ExtractJob(ctx processors.ProcessorContext, j *batchv1.Job) *model.Job { job := model.Job{ Metadata: extractMetadata(&j.ObjectMeta), Spec: &model.JobSpec{}, @@ -61,7 +61,10 @@ func ExtractJob(j *batchv1.Job) *model.Job { } job.Spec.ResourceRequirements = ExtractPodTemplateResourceRequirements(j.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) job.Tags = append(job.Tags, transformers.RetrieveUnifiedServiceTags(j.ObjectMeta.Labels)...) + job.Tags = append(job.Tags, transformers.RetrieveMetadataTags(j.ObjectMeta.Labels, j.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &job } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job_test.go index 57d7777bc23ef..d051d239a08ce 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/job_test.go @@ -8,11 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" - + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -29,8 +30,10 @@ func TestExtractJob(t *testing.T) { lastTransitionTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 35, 0, 0, time.UTC)) tests := map[string]struct { - input batchv1.Job - expected model.Job + input batchv1.Job + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Job }{ "job started by cronjob (in progress)": { input: batchv1.Job{ @@ -39,9 +42,12 @@ func TestExtractJob(t *testing.T) { "annotation": "my-annotation", }, CreationTimestamp: creationTime, - Labels: map[string]string{"controller-uid": "43739057-c6d7-4a5e-ab63-d0c8844e5272"}, - Name: "job", - Namespace: "project", + Labels: map[string]string{ + "controller-uid": "43739057-c6d7-4a5e-ab63-d0c8844e5272", + "app": "my-app", + }, + Name: "job", + Namespace: "project", OwnerReferences: []metav1.OwnerReference{ { APIVersion: "batch/v1beta1", @@ -69,11 +75,17 @@ func TestExtractJob(t *testing.T) { StartTime: &startTime, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.Job{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, CreationTimestamp: creationTime.Unix(), - Labels: []string{"controller-uid:43739057-c6d7-4a5e-ab63-d0c8844e5272"}, + Labels: []string{"controller-uid:43739057-c6d7-4a5e-ab63-d0c8844e5272", "app:my-app"}, Name: "job", Namespace: "project", OwnerReferences: []*model.OwnerReference{ @@ -102,6 +114,10 @@ func TestExtractJob(t *testing.T) { Active: 1, StartTime: startTime.Unix(), }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, "job started by cronjob (completed)": { @@ -308,7 +324,16 @@ func TestExtractJob(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractJob(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractJob(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + sort.Strings(actual.Metadata.Labels) + sort.Strings(tc.expected.Metadata.Labels) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange.go index 86951274127a0..ba787ea65a20a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange.go @@ -8,6 +8,8 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -15,7 +17,7 @@ import ( ) // ExtractLimitRange returns the protobuf model corresponding to a Kubernetes LimitRange resource. -func ExtractLimitRange(lr *corev1.LimitRange) *model.LimitRange { +func ExtractLimitRange(ctx processors.ProcessorContext, lr *corev1.LimitRange) *model.LimitRange { msg := &model.LimitRange{ Metadata: extractMetadata(&lr.ObjectMeta), Spec: &model.LimitRangeSpec{}, @@ -34,6 +36,9 @@ func ExtractLimitRange(lr *corev1.LimitRange) *model.LimitRange { msg.Spec.Limits = append(msg.Spec.Limits, limit) } + pctx := ctx.(*processors.K8sProcessorContext) + msg.Tags = append(msg.Tags, transformers.RetrieveMetadataTags(lr.ObjectMeta.Labels, lr.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) + return msg } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange_test.go index 091a4eeba21c9..deff3d9bae956 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/limitrange_test.go @@ -8,6 +8,7 @@ package k8s import ( + "sort" "testing" "github.com/stretchr/testify/assert" @@ -16,12 +17,15 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" ) func TestExtractResourceLimit(t *testing.T) { input := corev1.LimitRange{ ObjectMeta: metav1.ObjectMeta{ - Name: "limit-range", + Name: "limit-range", + Labels: map[string]string{"app": "my-app"}, + Annotations: map[string]string{"annotation": "my-annotation"}, }, Spec: corev1.LimitRangeSpec{ Limits: []corev1.LimitRangeItem{ @@ -55,7 +59,9 @@ func TestExtractResourceLimit(t *testing.T) { expected := &model.LimitRange{ LimitTypes: []string{"Container"}, Metadata: &model.Metadata{ - Name: "limit-range", + Name: "limit-range", + Labels: []string{"app:my-app"}, + Annotations: []string{"annotation:my-annotation"}, }, Spec: &model.LimitRangeSpec{ Limits: []*model.LimitRangeItem{ @@ -84,8 +90,17 @@ func TestExtractResourceLimit(t *testing.T) { }, }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, } - - out := ExtractLimitRange(&input) - assert.Equal(t, expected, out) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: map[string]string{"app": "application"}, + AnnotationsAsTags: map[string]string{"annotation": "annotation_key"}, + } + actual := ExtractLimitRange(pctx, &input) + sort.Strings(actual.Tags) + sort.Strings(expected.Tags) + assert.Equal(t, expected, actual) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace.go index 44e5dc4eef397..49bb18f96d7b1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace.go @@ -9,13 +9,14 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" corev1 "k8s.io/api/core/v1" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" ) // ExtractNamespace returns the protobuf model corresponding to a Kubernetes Namespace resource. -func ExtractNamespace(ns *corev1.Namespace) *model.Namespace { +func ExtractNamespace(ctx processors.ProcessorContext, ns *corev1.Namespace) *model.Namespace { n := &model.Namespace{ Metadata: extractMetadata(&ns.ObjectMeta), // status value based on https://github.com/kubernetes/kubernetes/blob/1e12d92a5179dbfeb455c79dbf9120c8536e5f9c/pkg/printers/internalversion/printers.go#L1350 @@ -28,7 +29,9 @@ func ExtractNamespace(ns *corev1.Namespace) *model.Namespace { n.Tags = append(n.Tags, conditionTags...) } + pctx := ctx.(*processors.K8sProcessorContext) n.Tags = append(n.Tags, transformers.RetrieveUnifiedServiceTags(ns.ObjectMeta.Labels)...) + n.Tags = append(n.Tags, transformers.RetrieveMetadataTags(ns.ObjectMeta.Labels, ns.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return n } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace_test.go index 0b024acc57c8f..1869ccc3435fc 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/namespace_test.go @@ -8,12 +8,14 @@ package k8s import ( + "sort" "testing" "time" corev1 "k8s.io/api/core/v1" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -24,8 +26,10 @@ func TestExtractNamespace(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input corev1.Namespace - expected model.Namespace + input corev1.Namespace + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Namespace }{ "standard": { input: corev1.Namespace{ @@ -64,6 +68,12 @@ func TestExtractNamespace(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.Namespace{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -98,6 +108,8 @@ func TestExtractNamespace(t *testing.T) { "kube_condition_namespacefinalizersremaining:false", "kube_condition_namespacedeletioncontentfailure:true", "kube_condition_namespacedeletiondiscoveryfailure:true", + "application:my-app", + "annotation_key:my-annotation", }, }, }, @@ -115,7 +127,14 @@ func TestExtractNamespace(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractNamespace(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractNamespace(pctx, &tc.input) + sort.Strings(tc.expected.Tags) + sort.Strings(actual.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy.go index f21b09a19397f..2812a901c3e43 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" @@ -16,13 +17,15 @@ import ( ) // ExtractNetworkPolicy returns the protobuf model corresponding to a Kubernetes -func ExtractNetworkPolicy(n *networkingv1.NetworkPolicy) *model.NetworkPolicy { +func ExtractNetworkPolicy(ctx processors.ProcessorContext, n *networkingv1.NetworkPolicy) *model.NetworkPolicy { networkPolicy := model.NetworkPolicy{ Metadata: extractMetadata(&n.ObjectMeta), Spec: extractNetworkPolicySpec(&n.Spec), } + pctx := ctx.(*processors.K8sProcessorContext) networkPolicy.Tags = append(networkPolicy.Tags, transformers.RetrieveUnifiedServiceTags(n.ObjectMeta.Labels)...) + networkPolicy.Tags = append(networkPolicy.Tags, transformers.RetrieveMetadataTags(n.ObjectMeta.Labels, n.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &networkPolicy } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy_test.go index 5895e29d6dda7..bd71ddb7e03d4 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/networkpolicy_test.go @@ -8,9 +8,11 @@ package k8s import ( + "sort" "testing" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" @@ -22,8 +24,10 @@ import ( func TestExtractNetworkPolicy(t *testing.T) { protocol := v1.Protocol("TCP") tests := map[string]struct { - input networkingv1.NetworkPolicy - expected *model.NetworkPolicy + input networkingv1.NetworkPolicy + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected *model.NetworkPolicy }{ "standard": { input: networkingv1.NetworkPolicy{ @@ -31,6 +35,9 @@ func TestExtractNetworkPolicy(t *testing.T) { Annotations: map[string]string{ "annotation": "my-annotation", }, + Labels: map[string]string{ + "app": "my-app", + }, }, Spec: networkingv1.NetworkPolicySpec{ Ingress: []networkingv1.NetworkPolicyIngressRule{ @@ -52,9 +59,16 @@ func TestExtractNetworkPolicy(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: &model.NetworkPolicy{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, + Labels: []string{"app:my-app"}, }, Spec: &model.NetworkPolicySpec{ Ingress: []*model.NetworkPolicyIngressRule{ @@ -75,6 +89,10 @@ func TestExtractNetworkPolicy(t *testing.T) { }, }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, "nil-safety": { @@ -90,7 +108,14 @@ func TestExtractNetworkPolicy(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, tc.expected, ExtractNetworkPolicy(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractNetworkPolicy(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node.go index 2e9fafd648068..08da9ab0d35a3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node.go @@ -9,6 +9,7 @@ package k8s import ( "fmt" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "strings" model "github.com/DataDog/agent-payload/v5/process" @@ -23,7 +24,7 @@ import ( // ExtractNode returns the protobuf model corresponding to a Kubernetes Node // resource. -func ExtractNode(n *corev1.Node) *model.Node { +func ExtractNode(ctx processors.ProcessorContext, n *corev1.Node) *model.Node { msg := &model.Node{ Metadata: extractMetadata(&n.ObjectMeta), PodCIDR: n.Spec.PodCIDR, @@ -88,7 +89,9 @@ func ExtractNode(n *corev1.Node) *model.Node { addAdditionalNodeTags(msg) + pctx := ctx.(*processors.K8sProcessorContext) msg.Tags = append(msg.Tags, transformers.RetrieveUnifiedServiceTags(n.ObjectMeta.Labels)...) + msg.Tags = append(msg.Tags, transformers.RetrieveMetadataTags(n.ObjectMeta.Labels, n.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return msg } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node_test.go index 118b0f5d4e5f1..2932c9d4b0064 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/node_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -23,8 +25,10 @@ import ( func TestExtractNode(t *testing.T) { timestamp := metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)) // 1389744000 tests := map[string]struct { - input corev1.Node - expected model.Node + input corev1.Node + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Node }{ "full node": { input: corev1.Node{ @@ -34,9 +38,10 @@ func TestExtractNode(t *testing.T) { CreationTimestamp: timestamp, Labels: map[string]string{ "kubernetes.io/role": "data", + "app": "my-app", }, Annotations: map[string]string{ - "annotation": "bar", + "annotation": "my-annotation", }, ResourceVersion: "1234", }, @@ -86,13 +91,20 @@ func TestExtractNode(t *testing.T) { Message: "ready", }}, }, - }, expected: model.Node{ + }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.Node{ Metadata: &model.Metadata{ Name: "node", Uid: "e42e5adc-0749-11e8-a2b8-000c29dea4f6", CreationTimestamp: 1389744000, - Labels: []string{"kubernetes.io/role:data"}, - Annotations: []string{"annotation:bar"}, + Labels: []string{"kubernetes.io/role:data", "app:my-app"}, + Annotations: []string{"annotation:my-annotation"}, ResourceVersion: "1234", }, Status: &model.NodeStatus{ @@ -128,7 +140,13 @@ func TestExtractNode(t *testing.T) { }, PodCIDR: "1234-5678-90", Unschedulable: true, - Tags: []string{"node_status:ready", "node_schedulable:false", "kube_node_role:data"}, + Tags: []string{ + "node_status:ready", + "node_schedulable:false", + "kube_node_role:data", + "application:my-app", + "annotation_key:my-annotation", + }, Taints: []*model.Taint{{ Key: "taint2NoTimeStamp", Value: "val1", @@ -209,7 +227,16 @@ func TestExtractNode(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractNode(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractNode(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + sort.Strings(actual.Metadata.Labels) + sort.Strings(tc.expected.Metadata.Labels) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume.go index d7b9a059942e1..bbd24a6e43a3e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "strings" corev1 "k8s.io/api/core/v1" @@ -18,7 +19,7 @@ import ( // ExtractPersistentVolume returns the protobuf model corresponding to a Kubernetes // PersistentVolume resource. -func ExtractPersistentVolume(pv *corev1.PersistentVolume) *model.PersistentVolume { +func ExtractPersistentVolume(ctx processors.ProcessorContext, pv *corev1.PersistentVolume) *model.PersistentVolume { message := &model.PersistentVolume{ Metadata: extractMetadata(&pv.ObjectMeta), Spec: &model.PersistentVolumeSpec{ @@ -82,7 +83,9 @@ func ExtractPersistentVolume(pv *corev1.PersistentVolume) *model.PersistentVolum addAdditionalPersistentVolumeTags(message) + pctx := ctx.(*processors.K8sProcessorContext) message.Tags = append(message.Tags, transformers.RetrieveUnifiedServiceTags(pv.ObjectMeta.Labels)...) + message.Tags = append(message.Tags, transformers.RetrieveMetadataTags(pv.ObjectMeta.Labels, pv.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return message } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume_test.go index 07cf4591941bf..8e39aa68c2821 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolume_test.go @@ -8,11 +8,13 @@ package k8s import ( + "sort" "strings" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -167,11 +169,18 @@ func TestExtractPersistentVolume(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: map[string]string{"app": "application"}, + AnnotationsAsTags: map[string]string{"annotation": "annotation_key"}, + } tc.basicInputPV.Spec.PersistentVolumeSource = tc.inputSource tc.basicExpectedPV.Spec.PersistentVolumeType = tc.expectedType tc.basicExpectedPV.Spec.PersistentVolumeSource = tc.expectedSource tc.basicExpectedPV.Tags = append(tc.basicExpectedPV.Tags, "pv_type:"+strings.ToLower(tc.expectedType)) - assert.Equal(t, &tc.basicExpectedPV, ExtractPersistentVolume(&tc.basicInputPV)) + actual := ExtractPersistentVolume(pctx, &tc.basicInputPV) + sort.Strings(actual.Tags) + sort.Strings(tc.basicExpectedPV.Tags) + assert.Equal(t, &tc.basicExpectedPV, actual) }) } } @@ -307,7 +316,11 @@ func newExpectedPV() model.PersistentVolume { Message: "test", Reason: "test", }, - Tags: []string{"pv_phase:pending"}, + Tags: []string{ + "pv_phase:pending", + "application:my-app", + "annotation_key:my-annotation", + }, } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim.go index aa172ff30d9e0..bcc3a330e8738 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" corev1 "k8s.io/api/core/v1" @@ -16,7 +17,7 @@ import ( // ExtractPersistentVolumeClaim returns the protobuf model corresponding to a // Kubernetes PersistentVolumeClaim resource. -func ExtractPersistentVolumeClaim(pvc *corev1.PersistentVolumeClaim) *model.PersistentVolumeClaim { +func ExtractPersistentVolumeClaim(ctx processors.ProcessorContext, pvc *corev1.PersistentVolumeClaim) *model.PersistentVolumeClaim { message := &model.PersistentVolumeClaim{ Metadata: extractMetadata(&pvc.ObjectMeta), Spec: &model.PersistentVolumeClaimSpec{ @@ -30,7 +31,10 @@ func ExtractPersistentVolumeClaim(pvc *corev1.PersistentVolumeClaim) *model.Pers } extractSpec(pvc, message) extractStatus(pvc, message) + + pctx := ctx.(*processors.K8sProcessorContext) message.Tags = append(message.Tags, transformers.RetrieveUnifiedServiceTags(pvc.ObjectMeta.Labels)...) + message.Tags = append(message.Tags, transformers.RetrieveMetadataTags(pvc.ObjectMeta.Labels, pvc.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return message } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim_test.go index 600159f833f52..d710a5374aa5c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/persistentvolumeclaim_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -27,8 +29,10 @@ func TestExtractPersistentVolumeClaim(t *testing.T) { parsedResource := resource.MustParse("2Gi") tests := map[string]struct { - input corev1.PersistentVolumeClaim - expected model.PersistentVolumeClaim + input corev1.PersistentVolumeClaim + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.PersistentVolumeClaim }{ "full pvc": { input: corev1.PersistentVolumeClaim{ @@ -77,6 +81,12 @@ func TestExtractPersistentVolumeClaim(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.PersistentVolumeClaim{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -114,12 +124,23 @@ func TestExtractPersistentVolumeClaim(t *testing.T) { Reason: "OfflineResize", }}, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractPersistentVolumeClaim(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractPersistentVolumeClaim(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod.go index 013a110b28c52..8f9eb74df5377 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod.go @@ -9,6 +9,8 @@ package k8s import ( "fmt" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" "hash/fnv" "sort" "strconv" @@ -30,7 +32,7 @@ const ( // ExtractPod returns the protobuf model corresponding to a Kubernetes Pod // resource. -func ExtractPod(p *corev1.Pod) *model.Pod { +func ExtractPod(ctx processors.ProcessorContext, p *corev1.Pod) *model.Pod { podModel := model.Pod{ Metadata: extractMetadata(&p.ObjectMeta), } @@ -81,6 +83,9 @@ func ExtractPod(p *corev1.Pod) *model.Pod { } } + pctx := ctx.(*processors.K8sProcessorContext) + podModel.Tags = append(podModel.Tags, transformers.RetrieveMetadataTags(p.ObjectMeta.Labels, p.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) + return &podModel } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod_test.go index f5d48219b3a8c..3de2e71ff1fdd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/pod_test.go @@ -10,10 +10,12 @@ package k8s import ( "fmt" "reflect" + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" @@ -73,8 +75,10 @@ func TestExtractPod(t *testing.T) { parseRequests := resource.MustParse("250M") parseLimits := resource.MustParse("550M") tests := map[string]struct { - input v1.Pod - expected model.Pod + input v1.Pod + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Pod }{ "full pod with containers without resourceRequirements": { input: v1.Pod{ @@ -143,10 +147,10 @@ func TestExtractPod(t *testing.T) { Namespace: "namespace", CreationTimestamp: timestamp, Labels: map[string]string{ - "label": "foo", + "app": "my-app", }, Annotations: map[string]string{ - "annotation": "bar", + "annotation": "my-annotation", }, OwnerReferences: []metav1.OwnerReference{ { @@ -166,14 +170,21 @@ func TestExtractPod(t *testing.T) { }, PriorityClassName: "high-priority", }, - }, expected: model.Pod{ + }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.Pod{ Metadata: &model.Metadata{ Name: "pod", Namespace: "namespace", Uid: "e42e5adc-0749-11e8-a2b8-000c29dea4f6", CreationTimestamp: 1389744000, - Labels: []string{"label:foo"}, - Annotations: []string{"annotation:bar"}, + Labels: []string{"app:my-app"}, + Annotations: []string{"annotation:my-annotation"}, OwnerReferences: []*model.OwnerReference{ { Name: "test-controller", @@ -231,7 +242,12 @@ func TestExtractPod(t *testing.T) { LastTransitionTime: timestamp.Unix(), }, }, - Tags: []string{"kube_condition_ready:true", "kube_condition_podscheduled:true"}, + Tags: []string{ + "kube_condition_ready:true", + "kube_condition_podscheduled:true", + "application:my-app", + "annotation_key:my-annotation", + }, ResourceRequirements: []*model.ResourceRequirements{ { Limits: map[string]int64{}, @@ -524,7 +540,14 @@ func TestExtractPod(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractPod(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractPod(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget.go index 3838c30ef0f7f..e4d8def87f3f9 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget.go @@ -9,15 +9,15 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" - policyv1 "k8s.io/api/policy/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) // ExtractPodDisruptionBudget returns the protobuf model corresponding to a Kubernetes -func ExtractPodDisruptionBudget(pdb *policyv1.PodDisruptionBudget) *model.PodDisruptionBudget { +func ExtractPodDisruptionBudget(ctx processors.ProcessorContext, pdb *policyv1.PodDisruptionBudget) *model.PodDisruptionBudget { if pdb == nil { return nil } @@ -26,7 +26,9 @@ func ExtractPodDisruptionBudget(pdb *policyv1.PodDisruptionBudget) *model.PodDis Spec: extractPodDisruptionBudgetSpec(&pdb.Spec), Status: extractPodDisruptionBudgetStatus(&pdb.Status), } + pctx := ctx.(*processors.K8sProcessorContext) result.Tags = append(result.Tags, transformers.RetrieveUnifiedServiceTags(pdb.ObjectMeta.Labels)...) + result.Tags = append(result.Tags, transformers.RetrieveMetadataTags(pdb.ObjectMeta.Labels, pdb.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &result } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget_test.go index ba32a4b1bd36b..00593e77a96ef 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/poddisruptionbudget_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/assert" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" policyv1 "k8s.io/api/policy/v1" @@ -174,8 +175,10 @@ func TestExtractPodDisruptionBudget(t *testing.T) { t1 := t0.Add(time.Minute) for name, tc := range map[string]struct { - in *policyv1.PodDisruptionBudget - expect *model.PodDisruptionBudget + in *policyv1.PodDisruptionBudget + labelsAsTags map[string]string + annotationsAsTags map[string]string + expect *model.PodDisruptionBudget }{ "nil": { in: nil, @@ -210,6 +213,10 @@ func TestExtractPodDisruptionBudget(t *testing.T) { Labels: map[string]string{ kubernetes.VersionTagLabelKey: "ultimate", kubernetes.ServiceTagLabelKey: "honorable", + "app": "my-app", + }, + Annotations: map[string]string{ + "annotation": "my-annotation", }, }, Spec: policyv1.PodDisruptionBudgetSpec{ @@ -239,6 +246,12 @@ func TestExtractPodDisruptionBudget(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expect: &model.PodDisruptionBudget{ Metadata: &model.Metadata{ Name: "gwern", @@ -250,7 +263,9 @@ func TestExtractPodDisruptionBudget(t *testing.T) { Labels: []string{ fmt.Sprintf("%s:ultimate", kubernetes.VersionTagLabelKey), fmt.Sprintf("%s:honorable", kubernetes.ServiceTagLabelKey), + "app:my-app", }, + Annotations: []string{"annotation:my-annotation"}, }, Spec: &model.PodDisruptionBudgetSpec{ MinAvailable: &model.IntOrString{ @@ -289,12 +304,18 @@ func TestExtractPodDisruptionBudget(t *testing.T) { Tags: []string{ "version:ultimate", "service:honorable", + "application:my-app", + "annotation_key:my-annotation", }, }, }, } { t.Run(name, func(t *testing.T) { - got := ExtractPodDisruptionBudget(tc.in) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + got := ExtractPodDisruptionBudget(pctx, tc.in) if tc.expect == nil { assert.Nil(t, got) } else { diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset.go index 66219033bcbc1..eefde04dab03f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" appsv1 "k8s.io/api/apps/v1" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" @@ -16,7 +17,7 @@ import ( // ExtractReplicaSet returns the protobuf model corresponding to a Kubernetes // ReplicaSet resource. -func ExtractReplicaSet(rs *appsv1.ReplicaSet) *model.ReplicaSet { +func ExtractReplicaSet(ctx processors.ProcessorContext, rs *appsv1.ReplicaSet) *model.ReplicaSet { replicaSet := model.ReplicaSet{ Metadata: extractMetadata(&rs.ObjectMeta), } @@ -42,7 +43,10 @@ func ExtractReplicaSet(rs *appsv1.ReplicaSet) *model.ReplicaSet { } replicaSet.ResourceRequirements = ExtractPodTemplateResourceRequirements(rs.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) replicaSet.Tags = append(replicaSet.Tags, transformers.RetrieveUnifiedServiceTags(rs.ObjectMeta.Labels)...) + replicaSet.Tags = append(replicaSet.Tags, transformers.RetrieveMetadataTags(rs.ObjectMeta.Labels, rs.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &replicaSet } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset_test.go index c5ab710aeb3b8..907caccb7ad90 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/replicaset_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" appsv1 "k8s.io/api/apps/v1" @@ -24,8 +26,10 @@ func TestExtractReplicaSet(t *testing.T) { timestamp := metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)) // 1389744000 testInt32 := int32(2) tests := map[string]struct { - input appsv1.ReplicaSet - expected model.ReplicaSet + input appsv1.ReplicaSet + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.ReplicaSet }{ "full rs": { input: appsv1.ReplicaSet{ @@ -72,7 +76,14 @@ func TestExtractReplicaSet(t *testing.T) { }, }, }, - }, expected: model.ReplicaSet{ + }, + labelsAsTags: map[string]string{ + "label": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.ReplicaSet{ Metadata: &model.Metadata{ Name: "replicaset", Namespace: "namespace", @@ -91,7 +102,11 @@ func TestExtractReplicaSet(t *testing.T) { Message: "test message", }, }, - Tags: []string{"kube_condition_replicafailure:false"}, + Tags: []string{ + "kube_condition_replicafailure:false", + "application:foo", + "annotation_key:bar", + }, Selectors: []*model.LabelSelectorRequirement{ { Key: "app", @@ -144,7 +159,14 @@ func TestExtractReplicaSet(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractReplicaSet(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractReplicaSet(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role.go index 3f8d32dd85376..4ea33e4906c93 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" rbacv1 "k8s.io/api/rbac/v1" @@ -16,13 +17,14 @@ import ( // ExtractRole returns the protobuf model corresponding to a Kubernetes Role // resource. -func ExtractRole(r *rbacv1.Role) *model.Role { +func ExtractRole(ctx processors.ProcessorContext, r *rbacv1.Role) *model.Role { msg := &model.Role{ Metadata: extractMetadata(&r.ObjectMeta), Rules: extractPolicyRules(r.Rules), } + pctx := ctx.(*processors.K8sProcessorContext) msg.Tags = append(msg.Tags, transformers.RetrieveUnifiedServiceTags(r.ObjectMeta.Labels)...) - + msg.Tags = append(msg.Tags, transformers.RetrieveMetadataTags(r.ObjectMeta.Labels, r.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return msg } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role_test.go index 36ee55a9843a9..9277e851a026a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/role_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" rbacv1 "k8s.io/api/rbac/v1" @@ -23,8 +25,10 @@ func TestExtractRole(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input rbacv1.Role - expected model.Role + input rbacv1.Role + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Role }{ "standard": { input: rbacv1.Role{ @@ -59,6 +63,12 @@ func TestExtractRole(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.Role{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -86,12 +96,23 @@ func TestExtractRole(t *testing.T) { Verbs: []string{"create"}, }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractRole(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractRole(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding.go index b6f50d941a3a6..14c8eac529868 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" rbacv1 "k8s.io/api/rbac/v1" @@ -16,14 +17,16 @@ import ( // ExtractRoleBinding returns the protobuf model corresponding to a Kubernetes // RoleBinding resource. -func ExtractRoleBinding(rb *rbacv1.RoleBinding) *model.RoleBinding { +func ExtractRoleBinding(ctx processors.ProcessorContext, rb *rbacv1.RoleBinding) *model.RoleBinding { msg := &model.RoleBinding{ Metadata: extractMetadata(&rb.ObjectMeta), RoleRef: extractRoleRef(&rb.RoleRef), Subjects: extractSubjects(rb.Subjects), } + pctx := ctx.(*processors.K8sProcessorContext) msg.Tags = append(msg.Tags, transformers.RetrieveUnifiedServiceTags(rb.ObjectMeta.Labels)...) + msg.Tags = append(msg.Tags, transformers.RetrieveMetadataTags(rb.ObjectMeta.Labels, rb.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return msg } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding_test.go index 7ea856b651ece..f6c3370af301e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/rolebinding_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" rbacv1 "k8s.io/api/rbac/v1" @@ -23,8 +25,10 @@ func TestExtractRoleBinding(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input rbacv1.RoleBinding - expected model.RoleBinding + input rbacv1.RoleBinding + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.RoleBinding }{ "standard": { input: rbacv1.RoleBinding{ @@ -54,6 +58,12 @@ func TestExtractRoleBinding(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.RoleBinding{ Metadata: &model.Metadata{ Annotations: []string{"annotation:my-annotation"}, @@ -76,12 +86,23 @@ func TestExtractRoleBinding(t *testing.T) { Name: "firstname.lastname@company.com", }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractRoleBinding(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractRoleBinding(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service.go index 23f346c5897a2..8444aca6bcac1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "strings" model "github.com/DataDog/agent-payload/v5/process" @@ -18,7 +19,7 @@ import ( // ExtractService returns the protobuf model corresponding to a Kubernetes // Service resource. -func ExtractService(s *corev1.Service) *model.Service { +func ExtractService(ctx processors.ProcessorContext, s *corev1.Service) *model.Service { message := &model.Service{ Metadata: extractMetadata(&s.ObjectMeta), Spec: &model.ServiceSpec{ @@ -79,7 +80,9 @@ func ExtractService(s *corev1.Service) *model.Service { }) } + pctx := ctx.(*processors.K8sProcessorContext) message.Tags = append(message.Tags, transformers.RetrieveUnifiedServiceTags(s.ObjectMeta.Labels)...) + message.Tags = append(message.Tags, transformers.RetrieveMetadataTags(s.ObjectMeta.Labels, s.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return message } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service_test.go index ad8bc2b23487f..5daf4a33ee3e0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/service_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" @@ -21,8 +23,10 @@ import ( func TestExtractService(t *testing.T) { tests := map[string]struct { - input corev1.Service - expected model.Service + input corev1.Service + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.Service }{ "ClusterIP": { input: corev1.Service{ @@ -56,6 +60,12 @@ func TestExtractService(t *testing.T) { }, Status: corev1.ServiceStatus{}, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "prefix/name": "annotation_key", + }, expected: model.Service{ Metadata: &model.Metadata{ Annotations: []string{"prefix/name:annotation-value"}, @@ -88,6 +98,10 @@ func TestExtractService(t *testing.T) { Type: "ClusterIP", }, Status: &model.ServiceStatus{}, + Tags: []string{ + "application:app-1", + "annotation_key:annotation-value", + }, }, }, "ExternalName": { @@ -292,6 +306,13 @@ func TestExtractService(t *testing.T) { }, } for _, test := range tests { - assert.Equal(t, &test.expected, ExtractService(&test.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: test.labelsAsTags, + AnnotationsAsTags: test.annotationsAsTags, + } + actual := ExtractService(pctx, &test.input) + sort.Strings(actual.Tags) + sort.Strings(test.expected.Tags) + assert.Equal(t, &test.expected, actual) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount.go index 9a03d49f435cd..8b5d24d764367 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount.go @@ -9,6 +9,7 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" corev1 "k8s.io/api/core/v1" @@ -17,7 +18,7 @@ import ( // ExtractServiceAccount returns the protobuf model corresponding to a // Kubernetes ServiceAccount resource. -func ExtractServiceAccount(sa *corev1.ServiceAccount) *model.ServiceAccount { +func ExtractServiceAccount(ctx processors.ProcessorContext, sa *corev1.ServiceAccount) *model.ServiceAccount { serviceAccount := &model.ServiceAccount{ Metadata: extractMetadata(&sa.ObjectMeta), } @@ -43,7 +44,9 @@ func ExtractServiceAccount(sa *corev1.ServiceAccount) *model.ServiceAccount { }) } + pctx := ctx.(*processors.K8sProcessorContext) serviceAccount.Tags = append(serviceAccount.Tags, transformers.RetrieveUnifiedServiceTags(sa.ObjectMeta.Labels)...) + serviceAccount.Tags = append(serviceAccount.Tags, transformers.RetrieveMetadataTags(sa.ObjectMeta.Labels, sa.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return serviceAccount } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount_test.go index 9204ea20a3a3e..1f50ec0005545 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/serviceaccount_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" "github.com/stretchr/testify/assert" @@ -24,8 +26,10 @@ func TestExtractServiceAccount(t *testing.T) { creationTime := metav1.NewTime(time.Date(2021, time.April, 16, 14, 30, 0, 0, time.UTC)) tests := map[string]struct { - input corev1.ServiceAccount - expected model.ServiceAccount + input corev1.ServiceAccount + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.ServiceAccount }{ "standard": { input: corev1.ServiceAccount{ @@ -54,6 +58,12 @@ func TestExtractServiceAccount(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.ServiceAccount{ AutomountServiceAccountToken: true, ImagePullSecrets: []*model.TypedLocalObjectReference{ @@ -75,12 +85,23 @@ func TestExtractServiceAccount(t *testing.T) { Name: "default-token-uudge", }, }, + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, }, }, } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractServiceAccount(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractServiceAccount(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset.go index 13b05a5b6a24d..afa555506a5a5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset.go @@ -9,15 +9,14 @@ package k8s import ( model "github.com/DataDog/agent-payload/v5/process" - + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" - v1 "k8s.io/api/apps/v1" ) // ExtractStatefulSet returns the protobuf model corresponding to a // Kubernetes StatefulSet resource. -func ExtractStatefulSet(sts *v1.StatefulSet) *model.StatefulSet { +func ExtractStatefulSet(ctx processors.ProcessorContext, sts *v1.StatefulSet) *model.StatefulSet { statefulSet := model.StatefulSet{ Metadata: extractMetadata(&sts.ObjectMeta), Spec: &model.StatefulSetSpec{ @@ -54,7 +53,10 @@ func ExtractStatefulSet(sts *v1.StatefulSet) *model.StatefulSet { } statefulSet.Spec.ResourceRequirements = ExtractPodTemplateResourceRequirements(sts.Spec.Template) + + pctx := ctx.(*processors.K8sProcessorContext) statefulSet.Tags = append(statefulSet.Tags, transformers.RetrieveUnifiedServiceTags(sts.ObjectMeta.Labels)...) + statefulSet.Tags = append(statefulSet.Tags, transformers.RetrieveMetadataTags(sts.ObjectMeta.Labels, sts.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) return &statefulSet } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset_test.go index bb24525b9c22e..cbcf2924951f0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/statefulset_test.go @@ -8,10 +8,12 @@ package k8s import ( + "sort" "testing" "time" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/stretchr/testify/assert" appsv1 "k8s.io/api/apps/v1" @@ -24,8 +26,10 @@ func TestExtractStatefulSet(t *testing.T) { timestamp := metav1.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)) // 1389744000 testInt32 := int32(2) tests := map[string]struct { - input appsv1.StatefulSet - expected model.StatefulSet + input appsv1.StatefulSet + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.StatefulSet }{ "full sts": { input: appsv1.StatefulSet{ @@ -72,7 +76,14 @@ func TestExtractStatefulSet(t *testing.T) { Replicas: 2, UpdatedReplicas: 2, }, - }, expected: model.StatefulSet{ + }, + labelsAsTags: map[string]string{ + "label": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, + expected: model.StatefulSet{ Metadata: &model.Metadata{ Name: "sts", Namespace: "namespace", @@ -91,7 +102,11 @@ func TestExtractStatefulSet(t *testing.T) { Message: "123", }, }, - Tags: []string{"kube_condition_test:false"}, + Tags: []string{ + "kube_condition_test:false", + "application:foo", + "annotation_key:bar", + }, Spec: &model.StatefulSetSpec{ DesiredReplicas: 2, UpdateStrategy: "RollingUpdate", @@ -144,7 +159,14 @@ func TestExtractStatefulSet(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractStatefulSet(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractStatefulSet(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass.go index 479330f512db6..941ac73aba19a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass.go @@ -8,6 +8,8 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers" corev1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" @@ -15,7 +17,7 @@ import ( ) // ExtractStorageClass returns the protobuf model corresponding to a Kubernetes StorageClass resource. -func ExtractStorageClass(sc *storagev1.StorageClass) *model.StorageClass { +func ExtractStorageClass(ctx processors.ProcessorContext, sc *storagev1.StorageClass) *model.StorageClass { msg := &model.StorageClass{ AllowedTopologies: extractStorageClassTopologies(sc.AllowedTopologies), Metadata: extractMetadata(&sc.ObjectMeta), @@ -39,6 +41,9 @@ func ExtractStorageClass(sc *storagev1.StorageClass) *model.StorageClass { msg.VolumeBindingMode = string(*sc.VolumeBindingMode) } + pctx := ctx.(*processors.K8sProcessorContext) + msg.Tags = append(msg.Tags, transformers.RetrieveMetadataTags(sc.ObjectMeta.Labels, sc.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) + return msg } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass_test.go index af542eb820ba6..79de2ed872f1b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/storageclass_test.go @@ -8,6 +8,7 @@ package k8s import ( + "sort" "testing" "time" @@ -18,6 +19,7 @@ import ( "github.com/stretchr/testify/assert" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/util/pointer" ) @@ -80,9 +82,17 @@ func TestExtractStorageClass(t *testing.T) { Provisioner: "provisioner", ReclaimPolicy: string(corev1.PersistentVolumeReclaimDelete), VolumeBindingMode: string(storagev1.VolumeBindingImmediate), + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, } - actual := ExtractStorageClass(sc) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: map[string]string{"app": "application"}, + AnnotationsAsTags: map[string]string{"annotation": "annotation_key"}, + } + actual := ExtractStorageClass(pctx, sc) assert.Equal(t, expected, actual) }) t.Run("standard", func(t *testing.T) { @@ -162,9 +172,19 @@ func TestExtractStorageClass(t *testing.T) { Provisioner: "provisioner", ReclaimPolicy: string(corev1.PersistentVolumeReclaimRetain), VolumeBindingMode: string(storagev1.VolumeBindingWaitForFirstConsumer), + Tags: []string{ + "application:my-app", + "annotation_key:my-annotation", + }, } - actual := ExtractStorageClass(sc) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: map[string]string{"app": "application"}, + AnnotationsAsTags: map[string]string{"annotation": "annotation_key"}, + } + actual := ExtractStorageClass(pctx, sc) + sort.Strings(actual.Tags) + sort.Strings(expected.Tags) assert.Equal(t, expected, actual) }) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler.go index 6d1ce327aa06c..22aa5bacfcaa6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler.go @@ -8,6 +8,7 @@ package k8s import ( + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" corev1 "k8s.io/api/core/v1" v1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" @@ -17,7 +18,7 @@ import ( ) // ExtractVerticalPodAutoscaler returns the protobuf model corresponding to a Kubernetes Vertical Pod Autoscaler resource. -func ExtractVerticalPodAutoscaler(v *v1.VerticalPodAutoscaler) *model.VerticalPodAutoscaler { +func ExtractVerticalPodAutoscaler(ctx processors.ProcessorContext, v *v1.VerticalPodAutoscaler) *model.VerticalPodAutoscaler { if v == nil { return &model.VerticalPodAutoscaler{} } @@ -35,7 +36,10 @@ func ExtractVerticalPodAutoscaler(v *v1.VerticalPodAutoscaler) *model.VerticalPo m.Tags = append(m.Tags, conditionTags...) } + pctx := ctx.(*processors.K8sProcessorContext) m.Tags = append(m.Tags, transformers.RetrieveUnifiedServiceTags(v.ObjectMeta.Labels)...) + m.Tags = append(m.Tags, transformers.RetrieveMetadataTags(v.ObjectMeta.Labels, v.ObjectMeta.Annotations, pctx.LabelsAsTags, pctx.AnnotationsAsTags)...) + return m } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler_test.go index 6054c58092b67..7892adea05104 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/verticalpodautoscaler_test.go @@ -8,6 +8,7 @@ package k8s import ( + "sort" "testing" "time" @@ -19,6 +20,7 @@ import ( v1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" ) func TestExtractVerticalPodAutoscaler(t *testing.T) { @@ -28,8 +30,10 @@ func TestExtractVerticalPodAutoscaler(t *testing.T) { controlledValues := v1.ContainerControlledValuesRequestsAndLimits tests := map[string]struct { - input v1.VerticalPodAutoscaler - expected model.VerticalPodAutoscaler + input v1.VerticalPodAutoscaler + labelsAsTags map[string]string + annotationsAsTags map[string]string + expected model.VerticalPodAutoscaler }{ "standard": { input: v1.VerticalPodAutoscaler{ @@ -116,6 +120,12 @@ func TestExtractVerticalPodAutoscaler(t *testing.T) { }, }, }, + labelsAsTags: map[string]string{ + "app": "application", + }, + annotationsAsTags: map[string]string{ + "annotation": "annotation_key", + }, expected: model.VerticalPodAutoscaler{ Metadata: &model.Metadata{ Name: "VPATest", @@ -211,6 +221,8 @@ func TestExtractVerticalPodAutoscaler(t *testing.T) { Tags: []string{ "kube_condition_recommendationprovided:true", "kube_condition_nopodsmatched:true", + "application:my-app", + "annotation_key:my-annotation", }, }, }, @@ -348,7 +360,14 @@ func TestExtractVerticalPodAutoscaler(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - assert.Equal(t, &tc.expected, ExtractVerticalPodAutoscaler(&tc.input)) + pctx := &processors.K8sProcessorContext{ + LabelsAsTags: tc.labelsAsTags, + AnnotationsAsTags: tc.annotationsAsTags, + } + actual := ExtractVerticalPodAutoscaler(pctx, &tc.input) + sort.Strings(actual.Tags) + sort.Strings(tc.expected.Tags) + assert.Equal(t, &tc.expected, actual) }) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/ust.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags.go similarity index 75% rename from pkg/collector/corechecks/cluster/orchestrator/transformers/ust.go rename to pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags.go index fe0974a49fac2..7d45639ff70d3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/ust.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags.go @@ -10,7 +10,6 @@ package transformers import ( "fmt" - pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" "github.com/DataDog/datadog-agent/pkg/util/kubernetes" ) @@ -49,3 +48,26 @@ func RetrieveUnifiedServiceTags(labels map[string]string) []string { } return tags } + +func RetrieveMetadataTags( + labels map[string]string, + annotations map[string]string, + labelsAsTags map[string]string, + annotationsAsTags map[string]string, +) []string { + tags := []string{} + + for name, value := range labels { + if tagKey, ok := labelsAsTags[name]; ok { + tags = append(tags, fmt.Sprintf("%s:%s", tagKey, value)) + } + } + + for name, value := range annotations { + if tagKey, ok := annotationsAsTags[name]; ok { + tags = append(tags, fmt.Sprintf("%s:%s", tagKey, value)) + } + } + + return tags +} diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags_test.go new file mode 100644 index 0000000000000..d397a03eb1b8f --- /dev/null +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/resourcetags_test.go @@ -0,0 +1,121 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +//go:build kubeapiserver && orchestrator + +package transformers + +import ( + "reflect" + "testing" + + configmock "github.com/DataDog/datadog-agent/pkg/config/mock" + "github.com/DataDog/datadog-agent/pkg/util/kubernetes" + + "github.com/stretchr/testify/assert" +) + +func TestRetrieveUST(t *testing.T) { + cfg := configmock.New(t) + cfg.SetWithoutSource("env", "staging") + cfg.SetWithoutSource(tagKeyService, "not-applied") + cfg.SetWithoutSource(tagKeyVersion, "not-applied") + + tests := []struct { + name string + labels map[string]string + want []string + }{ + { + name: "label contains ust, labels ust takes precedence", + labels: map[string]string{kubernetes.EnvTagLabelKey: "prod", kubernetes.VersionTagLabelKey: "123", kubernetes.ServiceTagLabelKey: "app"}, + want: []string{"env:prod", "version:123", "service:app"}, + }, + { + name: "label does not contain env, takes from config", + labels: map[string]string{}, + want: []string{"env:staging"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RetrieveUnifiedServiceTags(tt.labels); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RetrieveUnifiedServiceTags() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestRetrieveMetadataTags(t *testing.T) { + tests := []struct { + name string + labels map[string]string + annotations map[string]string + labelsAsTags map[string]string + annotationsAsTags map[string]string + want []string + }{ + { + name: "labels and annotations have matching tags", + labels: map[string]string{ + "app": "my-app", + "team": "my-team", + }, + annotations: map[string]string{ + "annotation-key": "annotation-value", + }, + labelsAsTags: map[string]string{ + "app": "application", + "team": "team-name", + }, + annotationsAsTags: map[string]string{ + "annotation-key": "annotation_key", + }, + want: []string{"application:my-app", "team-name:my-team", "annotation_key:annotation-value"}, + }, + { + name: "no matching labels or annotations", + labels: map[string]string{ + "random": "value", + }, + annotations: map[string]string{ + "another-random": "value", + }, + labelsAsTags: map[string]string{"app": "application"}, + annotationsAsTags: map[string]string{"annotation-key": "annotation_key"}, + want: []string{}, + }, + { + name: "only annotations match", + labels: map[string]string{ + "random": "value", + }, + annotations: map[string]string{ + "annotation-key": "annotation-value", + }, + labelsAsTags: map[string]string{"app": "application"}, + annotationsAsTags: map[string]string{"annotation-key": "annotation_key"}, + want: []string{"annotation_key:annotation-value"}, + }, + { + name: "only labels match", + labels: map[string]string{ + "app": "my-app", + }, + annotations: map[string]string{ + "random-annotation": "value", + }, + labelsAsTags: map[string]string{"app": "application"}, + annotationsAsTags: map[string]string{"annotation-key": "annotation_key"}, + want: []string{"application:my-app"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := RetrieveMetadataTags(tt.labels, tt.annotations, tt.labelsAsTags, tt.annotationsAsTags) + assert.ElementsMatch(t, tt.want, got) + }) + } +} diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/ust_test.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/ust_test.go deleted file mode 100644 index 530eab9ce178a..0000000000000 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/ust_test.go +++ /dev/null @@ -1,47 +0,0 @@ -// Unless explicitly stated otherwise all files in this repository are licensed -// under the Apache License Version 2.0. -// This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2016-present Datadog, Inc. - -//go:build kubeapiserver && orchestrator - -package transformers - -import ( - "reflect" - "testing" - - configmock "github.com/DataDog/datadog-agent/pkg/config/mock" - "github.com/DataDog/datadog-agent/pkg/util/kubernetes" -) - -func TestRetrieveUST(t *testing.T) { - cfg := configmock.New(t) - cfg.SetWithoutSource("env", "staging") - cfg.SetWithoutSource(tagKeyService, "not-applied") - cfg.SetWithoutSource(tagKeyVersion, "not-applied") - - tests := []struct { - name string - labels map[string]string - want []string - }{ - { - name: "label contains ust, labels ust takes precedence", - labels: map[string]string{kubernetes.EnvTagLabelKey: "prod", kubernetes.VersionTagLabelKey: "123", kubernetes.ServiceTagLabelKey: "app"}, - want: []string{"env:prod", "version:123", "service:app"}, - }, - { - name: "label does not contain env, takes from config", - labels: map[string]string{}, - want: []string{"env:staging"}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := RetrieveUnifiedServiceTags(tt.labels); !reflect.DeepEqual(got, tt.want) { - t.Errorf("RetrieveUnifiedServiceTags() = %v, want %v", got, tt.want) - } - }) - } -} From 8124537b32b1a2b42a3b4cefda2a3d88288e9b88 Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Fri, 10 Jan 2025 13:52:02 -0500 Subject: [PATCH 2/6] Added comments for exported constants --- .../cluster/orchestrator/collectors/types.go | 104 +++++++++++++----- 1 file changed, 78 insertions(+), 26 deletions(-) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go index b0af56765b8d7..70dd31c952ff6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go @@ -11,82 +11,134 @@ import ( ) const ( + // ECSTaskName represents the name for ECS tasks. ECSTaskName = "ecstasks" - K8sClusterRoleName = "clusterroles" + // K8sClusterRoleName represents the name for Kubernetes ClusterRoles. + K8sClusterRoleName = "clusterroles" + // K8sClusterRoleVersion represents the API version for Kubernetes ClusterRoles. K8sClusterRoleVersion = "rbac.authorization.k8s.io/v1" - K8sClusterRoleBindingName = "clusterrolebindings" + // K8sClusterRoleBindingName represents the name for Kubernetes ClusterRoleBindings. + K8sClusterRoleBindingName = "clusterrolebindings" + // K8sClusterRoleBindingVersion represents the API version for Kubernetes ClusterRoleBindings. K8sClusterRoleBindingVersion = "rbac.authorization.k8s.io/v1" - K8sCRDName = "customresourcedefinitions" + // K8sCRDName represents the name for Kubernetes CustomResourceDefinitions. + K8sCRDName = "customresourcedefinitions" + // K8sCRDVersion represents the API version for Kubernetes CustomResourceDefinitions. K8sCRDVersion = "apiextensions.k8s.io/v1" - K8sCronJobName = "cronjobs" - K8sCronJobVersionV1 = "batch/v1" + // K8sCronJobName represents the name for Kubernetes CronJobs. + K8sCronJobName = "cronjobs" + // K8sCronJobVersionV1 represents the v1 API version for Kubernetes CronJobs. + K8sCronJobVersionV1 = "batch/v1" + // K8sCronJobVersionV1Beta1 represents the v1beta1 API version for Kubernetes CronJobs. K8sCronJobVersionV1Beta1 = "batch/v1beta1" - K8sDaemonSetName = "daemonsets" + // K8sDaemonSetName represents the name for Kubernetes DaemonSets. + K8sDaemonSetName = "daemonsets" + // K8sDaemonSetVersion represents the API version for Kubernetes DaemonSets. K8sDaemonSetVersion = "apps/v1" - K8sDeploymentName = "deployments" + // K8sDeploymentName represents the name for Kubernetes Deployments. + K8sDeploymentName = "deployments" + // K8sDeploymentVersion represents the API version for Kubernetes Deployments. K8sDeploymentVersion = "apps/v1" - K8sHPAName = "horizontalpodautoscalers" + // K8sHPAName represents the name for Kubernetes HorizontalPodAutoscalers. + K8sHPAName = "horizontalpodautoscalers" + // K8sHPAVersion represents the API version for Kubernetes HorizontalPodAutoscalers. K8sHPAVersion = "autoscaling/v2" - K8sIngressName = "ingresses" + // K8sIngressName represents the name for Kubernetes Ingresses. + K8sIngressName = "ingresses" + // K8sIngressVersion represents the API version for Kubernetes Ingresses. K8sIngressVersion = "networking.k8s.io/v1" - K8sJobName = "jobs" + // K8sJobName represents the name for Kubernetes Jobs. + K8sJobName = "jobs" + // K8sJobVersion represents the API version for Kubernetes Jobs. K8sJobVersion = "batch/v1" - K8sLimitRangeName = "limitranges" + // K8sLimitRangeName represents the name for Kubernetes LimitRanges. + K8sLimitRangeName = "limitranges" + // K8sLimitRangeVersion represents the API version for Kubernetes LimitRanges. K8sLimitRangeVersion = "v1" - K8sNamespaceName = "namespaces" + // K8sNamespaceName represents the name for Kubernetes Namespaces. + K8sNamespaceName = "namespaces" + // K8sNamespaceVersion represents the API version for Kubernetes Namespaces. K8sNamespaceVersion = "v1" - K8sNetworkPolicyName = "networkpolicies" + // K8sNetworkPolicyName represents the name for Kubernetes NetworkPolicies. + K8sNetworkPolicyName = "networkpolicies" + // K8sNetworkPolicyVersion represents the API version for Kubernetes NetworkPolicies. K8sNetworkPolicyVersion = "networking.k8s.io/v1" - K8sNodeName = "nodes" + // K8sNodeName represents the name for Kubernetes Nodes. + K8sNodeName = "nodes" + // K8sNodeVersion represents the API version for Kubernetes Nodes. K8sNodeVersion = "v1" - K8sPersistentVolumeName = "persistentvolumes" + // K8sPersistentVolumeName represents the name for Kubernetes PersistentVolumes. + K8sPersistentVolumeName = "persistentvolumes" + // K8sPersistentVolumeVersion represents the API version for Kubernetes PersistentVolumes. K8sPersistentVolumeVersion = "v1" - K8sPersistentVolumeClaimName = "persistentvolumeclaims" + // K8sPersistentVolumeClaimName represents the name for Kubernetes PersistentVolumeClaims. + K8sPersistentVolumeClaimName = "persistentvolumeclaims" + // K8sPersistentVolumeClaimVersion represents the API version for Kubernetes PersistentVolumeClaims. K8sPersistentVolumeClaimVersion = "v1" - K8sPodName = "pods" + // K8sPodName represents the name for Kubernetes Pods. + K8sPodName = "pods" + // K8sPodVersion represents the API version for Kubernetes Pods. K8sPodVersion = "v1" - K8sPodDisruptionBudgetName = "poddisruptionbudgets" + // K8sPodDisruptionBudgetName represents the name for Kubernetes PodDisruptionBudgets. + K8sPodDisruptionBudgetName = "poddisruptionbudgets" + // K8sPodDisruptionBudgetVersion represents the API version for Kubernetes PodDisruptionBudgets. K8sPodDisruptionBudgetVersion = "policy/v1" - K8sReplicaSetName = "replicasets" + // K8sReplicaSetName represents the name for Kubernetes ReplicaSets. + K8sReplicaSetName = "replicasets" + // K8sReplicaSetVersion represents the API version for Kubernetes ReplicaSets. K8sReplicaSetVersion = "apps/v1" - K8sRoleName = "roles" + // K8sRoleName represents the name for Kubernetes Roles. + K8sRoleName = "roles" + // K8sRoleVersion represents the API version for Kubernetes Roles. K8sRoleVersion = "rbac.authorization.k8s.io/v1" - K8sRoleBindingName = "rolebindings" + // K8sRoleBindingName represents the name for Kubernetes RoleBindings. + K8sRoleBindingName = "rolebindings" + // K8sRoleBindingVersion represents the API version for Kubernetes RoleBindings. K8sRoleBindingVersion = "rbac.authorization.k8s.io/v1" - K8sServiceName = "services" + // K8sServiceName represents the name for Kubernetes Services. + K8sServiceName = "services" + // K8sServiceVersion represents the API version for Kubernetes Services. K8sServiceVersion = "v1" - K8sServiceAccountName = "serviceaccounts" + // K8sServiceAccountName represents the name for Kubernetes ServiceAccounts. + K8sServiceAccountName = "serviceaccounts" + // K8sServiceAccountVersion represents the API version for Kubernetes ServiceAccounts. K8sServiceAccountVersion = "v1" - K8sStatefulSetName = "statefulsets" + // K8sStatefulSetName represents the name for Kubernetes StatefulSets. + K8sStatefulSetName = "statefulsets" + // K8sStatefulSetVersion represents the API version for Kubernetes StatefulSets. K8sStatefulSetVersion = "apps/v1" - K8sStorageClassName = "storageclasses" + // K8sStorageClassName represents the name for Kubernetes StorageClasses. + K8sStorageClassName = "storageclasses" + // K8sStorageClassVersion represents the API version for Kubernetes StorageClasses. K8sStorageClassVersion = "storage.k8s.io/v1" - K8sVPAName = "verticalpodautoscalers" + // K8sVPAName represents the name for Kubernetes VerticalPodAutoscalers. + K8sVPAName = "verticalpodautoscalers" + // K8sVPAVersion represents the API version for Kubernetes VerticalPodAutoscalers. K8sVPAVersion = "autoscaling.k8s.io/v1" ) From 4a737027481be9da9a7555fe71f014e2cc9544bc Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Fri, 10 Jan 2025 14:04:03 -0500 Subject: [PATCH 3/6] Added release note --- ...annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml diff --git a/releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml b/releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml new file mode 100644 index 0000000000000..94ccd1dcc7f13 --- /dev/null +++ b/releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml @@ -0,0 +1,5 @@ +enhancements: + - | + Added support for `kubernetesResourcesLabelsAsTags` and `kubernetesResourcesAnnotationsAsTags` in the + orchestrator check. Kubernetes resources processed by the orchestrator check can now include labels + and annotations as tags, improving consistency with existing tagging configurations. From 6e2e2c509233afc500375a33d852009cdb41510e Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Fri, 10 Jan 2025 14:36:32 -0500 Subject: [PATCH 4/6] Moved types file --- .../orchestrator/collectors/ecs/task.go | 2 +- .../orchestrator/collectors/k8s/cluster.go | 2 +- .../collectors/k8s/clusterrole.go | 6 +- .../collectors/k8s/clusterrolebinding.go | 6 +- .../orchestrator/collectors/k8s/crd.go | 4 +- .../orchestrator/collectors/k8s/cronjob_v1.go | 6 +- .../collectors/k8s/cronjob_v1beta1.go | 6 +- .../orchestrator/collectors/k8s/daemonset.go | 6 +- .../orchestrator/collectors/k8s/deployment.go | 6 +- .../collectors/k8s/horizontalpodautoscaler.go | 6 +- .../orchestrator/collectors/k8s/ingress.go | 6 +- .../orchestrator/collectors/k8s/job.go | 6 +- .../orchestrator/collectors/k8s/limitrange.go | 6 +- .../orchestrator/collectors/k8s/namespace.go | 6 +- .../collectors/k8s/networkpolicy.go | 6 +- .../orchestrator/collectors/k8s/node.go | 6 +- .../collectors/k8s/persistentvolume.go | 6 +- .../collectors/k8s/persistentvolumeclaim.go | 6 +- .../collectors/k8s/pod_unassigned.go | 6 +- .../collectors/k8s/poddisruptionbudget.go | 6 +- .../orchestrator/collectors/k8s/replicaset.go | 6 +- .../orchestrator/collectors/k8s/role.go | 6 +- .../collectors/k8s/rolebinding.go | 6 +- .../orchestrator/collectors/k8s/service.go | 6 +- .../collectors/k8s/serviceaccount.go | 6 +- .../collectors/k8s/statefulset.go | 6 +- .../collectors/k8s/storageclass.go | 6 +- .../orchestrator/collectors/k8s/types.go | 104 ++++++++++++ .../collectors/k8s/verticalpodautoscaler.go | 6 +- .../cluster/orchestrator/collectors/types.go | 155 ------------------ 30 files changed, 183 insertions(+), 234 deletions(-) create mode 100644 pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/types.go delete mode 100644 pkg/collector/corechecks/cluster/orchestrator/collectors/types.go diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go index 2278fbb86b2fb..d87c43156d107 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/ecs/task.go @@ -34,7 +34,7 @@ func NewTaskCollector(tagger tagger.Component) *TaskCollector { IsStable: false, IsMetadataProducer: true, IsManifestProducer: false, - Name: collectors.ECSTaskName, + Name: "ecstasks", NodeType: orchestrator.ECSTask, }, processor: processors.NewProcessor(ecs.NewTaskHandlers(tagger)), diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go index 916ab29d9eb73..660391f24d406 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go @@ -44,7 +44,7 @@ func NewClusterCollector() *ClusterCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: "clusters", + Name: clusterName, NodeType: orchestrator.K8sCluster, }, processor: k8sProcessors.NewClusterProcessor(), diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go index 9aec83d8b6d25..45f4fa662e1b5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go @@ -39,7 +39,7 @@ type ClusterRoleCollector struct { // NewClusterRoleCollector creates a new collector for the Kubernetes // ClusterRole resource. func NewClusterRoleCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleCollector { - resourceType := collectors.GetResourceType(collectors.K8sClusterRoleName, collectors.K8sClusterRoleVersion) + resourceType := getResourceType(clusterRoleName, clusterRoleVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -52,9 +52,9 @@ func NewClusterRoleCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleCo IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sClusterRoleName, + Name: clusterRoleName, NodeType: orchestrator.K8sClusterRole, - Version: collectors.K8sClusterRoleVersion, + Version: clusterRoleVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go index 438c6c8e97770..c865892622dd2 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go @@ -38,7 +38,7 @@ type ClusterRoleBindingCollector struct { // NewClusterRoleBindingCollector creates a new collector for the Kubernetes // ClusterRoleBinding resource. func NewClusterRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleBindingCollector { - resourceType := collectors.GetResourceType(collectors.K8sClusterRoleBindingName, collectors.K8sClusterRoleBindingVersion) + resourceType := getResourceType(clusterRoleBindingName, clusterRoleBindingVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewClusterRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *Cluste IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sClusterRoleBindingName, + Name: clusterRoleBindingName, NodeType: orchestrator.K8sClusterRoleBinding, - Version: collectors.K8sClusterRoleBindingVersion, + Version: clusterRoleBindingVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go index 3d4a9a912e66b..c97e8c2934c90 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go @@ -45,9 +45,9 @@ func NewCRDCollector() *CRDCollector { IsManifestProducer: true, IsMetadataProducer: false, SupportsManifestBuffering: false, - Name: collectors.K8sCRDName, + Name: crdName, NodeType: orchestrator.K8sCRD, - Version: collectors.K8sCRDVersion, + Version: crdVersion, }, processor: processors.NewProcessor(new(k8sProcessors.CRDHandlers)), } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go index ff80260117bf5..6f11fac59bbfb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go @@ -30,7 +30,7 @@ type CronJobV1Collector struct { // NewCronJobV1Collector creates a new collector for the Kubernetes Job resource. func NewCronJobV1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1Collector { - resourceType := collectors.GetResourceType(collectors.K8sCronJobName, collectors.K8sCronJobVersionV1) + resourceType := getResourceType(cronJobName, cronJobVersionV1) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -41,9 +41,9 @@ func NewCronJobV1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1Collec IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sCronJobName, + Name: cronJobName, NodeType: orchestrator.K8sCronJob, - Version: collectors.K8sCronJobVersionV1, + Version: cronJobVersionV1, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go index cf09113a03677..329c8d9ac7013 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go @@ -30,7 +30,7 @@ type CronJobV1Beta1Collector struct { // NewCronJobV1Beta1Collector creates a new collector for the Kubernetes Job resource. func NewCronJobV1Beta1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1Beta1Collector { - resourceType := collectors.GetResourceType(collectors.K8sCronJobName, collectors.K8sCronJobVersionV1Beta1) + resourceType := getResourceType(cronJobName, cronJobVersionV1Beta1) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -41,9 +41,9 @@ func NewCronJobV1Beta1Collector(metadataAsTags utils.MetadataAsTags) *CronJobV1B IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sCronJobName, + Name: cronJobName, NodeType: orchestrator.K8sCronJob, - Version: collectors.K8sCronJobVersionV1Beta1, + Version: cronJobVersionV1Beta1, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go index be7fca6dcd6e8..554237656d8f2 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go @@ -38,7 +38,7 @@ type DaemonSetCollector struct { // NewDaemonSetCollector creates a new collector for the Kubernetes DaemonSet // resource. func NewDaemonSetCollector(metadataAsTags utils.MetadataAsTags) *DaemonSetCollector { - resourceType := collectors.GetResourceType(collectors.K8sDaemonSetName, collectors.K8sDaemonSetVersion) + resourceType := getResourceType(daemonSetName, daemonSetVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewDaemonSetCollector(metadataAsTags utils.MetadataAsTags) *DaemonSetCollec IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sDaemonSetName, + Name: daemonSetName, NodeType: orchestrator.K8sDaemonSet, - Version: collectors.K8sDaemonSetVersion, + Version: daemonSetVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go index 5f57f2179683e..4b25960f7af9b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go @@ -38,7 +38,7 @@ type DeploymentCollector struct { // NewDeploymentCollector creates a new collector for the Kubernetes Deployment // resource. func NewDeploymentCollector(metadataAsTags utils.MetadataAsTags) *DeploymentCollector { - resourceType := collectors.GetResourceType(collectors.K8sDeploymentName, collectors.K8sDeploymentVersion) + resourceType := getResourceType(deploymentName, deploymentVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewDeploymentCollector(metadataAsTags utils.MetadataAsTags) *DeploymentColl IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sDeploymentName, + Name: deploymentName, NodeType: orchestrator.K8sDeployment, - Version: collectors.K8sDeploymentVersion, + Version: deploymentVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go index ffbadf5068bab..711f39c064a68 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go @@ -39,7 +39,7 @@ type HorizontalPodAutoscalerCollector struct { // NewHorizontalPodAutoscalerCollector creates a new collector for the Kubernetes // HorizontalPodAutoscaler resource. func NewHorizontalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *HorizontalPodAutoscalerCollector { - resourceType := collectors.GetResourceType(collectors.K8sHPAName, collectors.K8sHPAVersion) + resourceType := getResourceType(hpaName, hpaVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -50,9 +50,9 @@ func NewHorizontalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *H IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sHPAName, + Name: hpaName, NodeType: orchestrator.K8sHorizontalPodAutoscaler, - Version: collectors.K8sHPAVersion, + Version: hpaVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go index 6ed6f59aec9e6..5bf43c1a7fa2e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go @@ -38,7 +38,7 @@ type IngressCollector struct { // NewIngressCollector creates a new collector for the Kubernetes Ingress // resource. func NewIngressCollector(metadataAsTags utils.MetadataAsTags) *IngressCollector { - resourceType := collectors.GetResourceType(collectors.K8sIngressName, collectors.K8sIngressVersion) + resourceType := getResourceType(ingressName, ingressVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewIngressCollector(metadataAsTags utils.MetadataAsTags) *IngressCollector IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sIngressName, + Name: ingressName, NodeType: orchestrator.K8sIngress, - Version: collectors.K8sIngressVersion, + Version: ingressVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go index 48abdccf13bc1..7899cacc6e8e3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go @@ -37,7 +37,7 @@ type JobCollector struct { // NewJobCollector creates a new collector for the Kubernetes Job resource. func NewJobCollector(metadataAsTags utils.MetadataAsTags) *JobCollector { - resourceType := collectors.GetResourceType(collectors.K8sJobName, collectors.K8sJobVersion) + resourceType := getResourceType(jobName, jobVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -48,9 +48,9 @@ func NewJobCollector(metadataAsTags utils.MetadataAsTags) *JobCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sJobName, + Name: jobName, NodeType: orchestrator.K8sJob, - Version: collectors.K8sJobVersion, + Version: jobVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go index bf92fd6dcb000..b26d5faa70386 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/limitrange.go @@ -38,7 +38,7 @@ type LimitRangeCollector struct { // NewLimitRangeCollector creates a new collector for the Kubernetes // LimitRange resource. func NewLimitRangeCollector(metadataAsTags utils.MetadataAsTags) *LimitRangeCollector { - resourceType := collectors.GetResourceType(collectors.K8sLimitRangeName, collectors.K8sLimitRangeVersion) + resourceType := getResourceType(limitRangeName, limitRangeVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewLimitRangeCollector(metadataAsTags utils.MetadataAsTags) *LimitRangeColl IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sLimitRangeName, + Name: limitRangeName, NodeType: orchestrator.K8sLimitRange, - Version: collectors.K8sLimitRangeVersion, + Version: limitRangeVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go index 95c0abde1b3bc..d2c0fabf9406a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go @@ -38,7 +38,7 @@ type NamespaceCollector struct { // NewNamespaceCollector creates a new collector for the Kubernetes // Namespace resource. func NewNamespaceCollector(metadataAsTags utils.MetadataAsTags) *NamespaceCollector { - resourceType := collectors.GetResourceType(collectors.K8sNamespaceName, collectors.K8sNamespaceVersion) + resourceType := getResourceType(namespaceName, namespaceVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewNamespaceCollector(metadataAsTags utils.MetadataAsTags) *NamespaceCollec IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sNamespaceName, + Name: namespaceName, NodeType: orchestrator.K8sNamespace, - Version: collectors.K8sNamespaceVersion, + Version: namespaceVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go index 91b41af47498f..f401f06fb58ff 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go @@ -38,7 +38,7 @@ type NetworkPolicyCollector struct { // NewNetworkPolicyCollector creates a new collector for the Kubernetes // NetworkPolicy resource. func NewNetworkPolicyCollector(metadataAsTags utils.MetadataAsTags) *NetworkPolicyCollector { - resourceType := collectors.GetResourceType(collectors.K8sNetworkPolicyName, collectors.K8sNetworkPolicyVersion) + resourceType := getResourceType(networkPolicyName, networkPolicyVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewNetworkPolicyCollector(metadataAsTags utils.MetadataAsTags) *NetworkPoli IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sNetworkPolicyName, + Name: networkPolicyName, NodeType: orchestrator.K8sNetworkPolicy, - Version: collectors.K8sNetworkPolicyVersion, + Version: networkPolicyVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go index 4ae01b4264787..b7f8a218e4d33 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go @@ -37,7 +37,7 @@ type NodeCollector struct { // NewNodeCollector creates a new collector for the Kubernetes Node resource. func NewNodeCollector(metadataAsTags utils.MetadataAsTags) *NodeCollector { - resourceType := collectors.GetResourceType(collectors.K8sNodeName, collectors.K8sNodeVersion) + resourceType := getResourceType(nodeName, nodeVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -48,9 +48,9 @@ func NewNodeCollector(metadataAsTags utils.MetadataAsTags) *NodeCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sNodeName, + Name: nodeName, NodeType: orchestrator.K8sNode, - Version: collectors.K8sNodeVersion, + Version: nodeVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go index 1b429e9c5f43b..5da11a6755be5 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go @@ -38,7 +38,7 @@ type PersistentVolumeCollector struct { // NewPersistentVolumeCollector creates a new collector for the Kubernetes // PersistentVolume resource. func NewPersistentVolumeCollector(metadataAsTags utils.MetadataAsTags) *PersistentVolumeCollector { - resourceType := collectors.GetResourceType(collectors.K8sPersistentVolumeName, collectors.K8sPersistentVolumeVersion) + resourceType := getResourceType(persistentVolumeName, persistentVolumeVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewPersistentVolumeCollector(metadataAsTags utils.MetadataAsTags) *Persiste IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sPersistentVolumeName, + Name: persistentVolumeName, NodeType: orchestrator.K8sPersistentVolume, - Version: collectors.K8sPersistentVolumeVersion, + Version: persistentVolumeVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go index 33b008a14b80f..71a586bb5401e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go @@ -38,7 +38,7 @@ type PersistentVolumeClaimCollector struct { // NewPersistentVolumeClaimCollector creates a new collector for the Kubernetes // PersistentVolumeClaim resource. func NewPersistentVolumeClaimCollector(metadataAsTags utils.MetadataAsTags) *PersistentVolumeClaimCollector { - resourceType := collectors.GetResourceType(collectors.K8sPersistentVolumeClaimName, collectors.K8sPersistentVolumeClaimVersion) + resourceType := getResourceType(persistentVolumeClaimName, persistentVolumeClaimVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewPersistentVolumeClaimCollector(metadataAsTags utils.MetadataAsTags) *Per IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sPersistentVolumeClaimName, + Name: persistentVolumeClaimName, NodeType: orchestrator.K8sPersistentVolumeClaim, - Version: collectors.K8sPersistentVolumeClaimVersion, + Version: persistentVolumeClaimVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go index e9b8a490c236c..db98a0d23bdf3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go @@ -42,7 +42,7 @@ type UnassignedPodCollector struct { // NewUnassignedPodCollector creates a new collector for the Kubernetes Pod // resource that is not assigned to any node. func NewUnassignedPodCollector(cfg config.Component, store workloadmeta.Component, tagger tagger.Component, metadataAsTags utils.MetadataAsTags) *UnassignedPodCollector { - resourceType := collectors.GetResourceType(collectors.K8sPodName, collectors.K8sPodVersion) + resourceType := getResourceType(podName, podVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -53,9 +53,9 @@ func NewUnassignedPodCollector(cfg config.Component, store workloadmeta.Componen IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sPodName, + Name: podName, NodeType: orchestrator.K8sPod, - Version: collectors.K8sPodVersion, + Version: podVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go index cb4b65bd63cf5..05befb7a0b699 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/poddisruptionbudget.go @@ -38,7 +38,7 @@ type PodDisruptionBudgetCollector struct { // NewPodDisruptionBudgetCollectorVersion creates a new collector for the Kubernetes Pod Disruption Budget // resource. func NewPodDisruptionBudgetCollectorVersion(metadataAsTags utils.MetadataAsTags) *PodDisruptionBudgetCollector { - resourceType := collectors.GetResourceType(collectors.K8sPodDisruptionBudgetName, collectors.K8sPodDisruptionBudgetVersion) + resourceType := getResourceType(podDisruptionBudgetName, podDisruptionBudgetVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -51,9 +51,9 @@ func NewPodDisruptionBudgetCollectorVersion(metadataAsTags utils.MetadataAsTags) IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sPodDisruptionBudgetName, + Name: podDisruptionBudgetName, NodeType: orchestrator.K8sPodDisruptionBudget, - Version: collectors.K8sPodDisruptionBudgetVersion, + Version: podDisruptionBudgetVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go index 03ebb9d2322bc..8300d218b83b7 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go @@ -38,7 +38,7 @@ type ReplicaSetCollector struct { // NewReplicaSetCollector creates a new collector for the Kubernetes ReplicaSet // resource. func NewReplicaSetCollector(metadataAsTags utils.MetadataAsTags) *ReplicaSetCollector { - resourceType := collectors.GetResourceType(collectors.K8sReplicaSetName, collectors.K8sReplicaSetVersion) + resourceType := getResourceType(replicaSetName, replicaSetVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewReplicaSetCollector(metadataAsTags utils.MetadataAsTags) *ReplicaSetColl IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sReplicaSetName, + Name: replicaSetName, NodeType: orchestrator.K8sReplicaSet, - Version: collectors.K8sReplicaSetVersion, + Version: replicaSetVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go index 4f4d60b057099..c923c313eb3c0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go @@ -37,7 +37,7 @@ type RoleCollector struct { // NewRoleCollector creates a new collector for the Kubernetes Role resource. func NewRoleCollector(metadataAsTags utils.MetadataAsTags) *RoleCollector { - resourceType := collectors.GetResourceType(collectors.K8sRoleName, collectors.K8sRoleVersion) + resourceType := getResourceType(roleName, roleVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -48,9 +48,9 @@ func NewRoleCollector(metadataAsTags utils.MetadataAsTags) *RoleCollector { IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sRoleName, + Name: roleName, NodeType: orchestrator.K8sRole, - Version: collectors.K8sRoleVersion, + Version: roleVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go index 56c868f762fe1..490ff62e458b0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go @@ -38,7 +38,7 @@ type RoleBindingCollector struct { // NewRoleBindingCollector creates a new collector for the Kubernetes // RoleBinding resource. func NewRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *RoleBindingCollector { - resourceType := collectors.GetResourceType(collectors.K8sRoleBindingName, collectors.K8sRoleBindingVersion) + resourceType := getResourceType(roleBindingName, roleBindingVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewRoleBindingCollector(metadataAsTags utils.MetadataAsTags) *RoleBindingCo IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sRoleBindingName, + Name: roleBindingName, NodeType: orchestrator.K8sRoleBinding, - Version: collectors.K8sRoleBindingVersion, + Version: roleBindingVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go index fc1d66f315efd..a6b6f36451a4f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go @@ -38,7 +38,7 @@ type ServiceCollector struct { // NewServiceCollector creates a new collector for the Kubernetes Service // resource. func NewServiceCollector(metadataAsTags utils.MetadataAsTags) *ServiceCollector { - resourceType := collectors.GetResourceType(collectors.K8sServiceName, collectors.K8sServiceVersion) + resourceType := getResourceType(serviceName, serviceVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewServiceCollector(metadataAsTags utils.MetadataAsTags) *ServiceCollector IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sServiceName, + Name: serviceName, NodeType: orchestrator.K8sService, - Version: collectors.K8sServiceVersion, + Version: serviceVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go index 6b10e1f91f965..606c47b9abe84 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go @@ -38,7 +38,7 @@ type ServiceAccountCollector struct { // NewServiceAccountCollector creates a new collector for the Kubernetes // ServiceAccount resource. func NewServiceAccountCollector(metadataAsTags utils.MetadataAsTags) *ServiceAccountCollector { - resourceType := collectors.GetResourceType(collectors.K8sServiceAccountName, collectors.K8sServiceAccountVersion) + resourceType := getResourceType(serviceAccountName, serviceAccountVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewServiceAccountCollector(metadataAsTags utils.MetadataAsTags) *ServiceAcc IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sServiceAccountName, + Name: serviceAccountName, NodeType: orchestrator.K8sServiceAccount, - Version: collectors.K8sServiceAccountVersion, + Version: serviceAccountVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go index ade033877db8a..e3bd48ffa774d 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go @@ -38,7 +38,7 @@ type StatefulSetCollector struct { // NewStatefulSetCollector creates a new collector for the Kubernetes // StatefulSet resource. func NewStatefulSetCollector(metadataAsTags utils.MetadataAsTags) *StatefulSetCollector { - resourceType := collectors.GetResourceType(collectors.K8sStatefulSetName, collectors.K8sStatefulSetVersion) + resourceType := getResourceType(statefulSetName, statefulSetVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewStatefulSetCollector(metadataAsTags utils.MetadataAsTags) *StatefulSetCo IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sStatefulSetName, + Name: statefulSetName, NodeType: orchestrator.K8sStatefulSet, - Version: collectors.K8sStatefulSetVersion, + Version: statefulSetVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go index f39b1937bd0f8..ec65ca6e8ca02 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/storageclass.go @@ -38,7 +38,7 @@ type StorageClassCollector struct { // NewStorageClassCollector creates a new collector for the Kubernetes // StorageClass resource. func NewStorageClassCollector(metadataAsTags utils.MetadataAsTags) *StorageClassCollector { - resourceType := collectors.GetResourceType(collectors.K8sStorageClassName, collectors.K8sStorageClassVersion) + resourceType := getResourceType(storageClassName, storageClassVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -49,9 +49,9 @@ func NewStorageClassCollector(metadataAsTags utils.MetadataAsTags) *StorageClass IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sStorageClassName, + Name: storageClassName, NodeType: orchestrator.K8sStorageClass, - Version: collectors.K8sStorageClassVersion, + Version: storageClassVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/types.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/types.go new file mode 100644 index 0000000000000..8d2029121da2d --- /dev/null +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/types.go @@ -0,0 +1,104 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +//go:build kubeapiserver && orchestrator + +package k8s + +import ( + "fmt" + "strings" +) + +const ( + clusterName = "clusters" + + clusterRoleName = "clusterroles" + clusterRoleVersion = "rbac.authorization.k8s.io/v1" + + clusterRoleBindingName = "clusterrolebindings" + clusterRoleBindingVersion = "rbac.authorization.k8s.io/v1" + + crdName = "customresourcedefinitions" + crdVersion = "apiextensions.k8s.io/v1" + + cronJobName = "cronjobs" + cronJobVersionV1 = "batch/v1" + cronJobVersionV1Beta1 = "batch/v1beta1" + + daemonSetName = "daemonsets" + daemonSetVersion = "apps/v1" + + deploymentName = "deployments" + deploymentVersion = "apps/v1" + + hpaName = "horizontalpodautoscalers" + hpaVersion = "autoscaling/v2" + + ingressName = "ingresses" + ingressVersion = "networking.k8s.io/v1" + + jobName = "jobs" + jobVersion = "batch/v1" + + limitRangeName = "limitranges" + limitRangeVersion = "v1" + + namespaceName = "namespaces" + namespaceVersion = "v1" + + networkPolicyName = "networkpolicies" + networkPolicyVersion = "networking.k8s.io/v1" + + nodeName = "nodes" + nodeVersion = "v1" + + persistentVolumeName = "persistentvolumes" + persistentVolumeVersion = "v1" + + persistentVolumeClaimName = "persistentvolumeclaims" + persistentVolumeClaimVersion = "v1" + + podName = "pods" + podVersion = "v1" + + podDisruptionBudgetName = "poddisruptionbudgets" + podDisruptionBudgetVersion = "policy/v1" + + replicaSetName = "replicasets" + replicaSetVersion = "apps/v1" + + roleName = "roles" + roleVersion = "rbac.authorization.k8s.io/v1" + + roleBindingName = "rolebindings" + roleBindingVersion = "rbac.authorization.k8s.io/v1" + + serviceName = "services" + serviceVersion = "v1" + + serviceAccountName = "serviceaccounts" + serviceAccountVersion = "v1" + + statefulSetName = "statefulsets" + statefulSetVersion = "apps/v1" + + storageClassName = "storageclasses" + storageClassVersion = "storage.k8s.io/v1" + + vpaName = "verticalpodautoscalers" + vpaVersion = "autoscaling.k8s.io/v1" +) + +// getResourceType returns a string in the format "name.apiGroup" if an API group is present in the version. +// Otherwise, it returns the name. +func getResourceType(name string, version string) string { + apiVersionParts := strings.Split(version, "/") + if len(apiVersionParts) == 2 { + apiGroup := apiVersionParts[0] + return fmt.Sprintf("%s.%s", name, apiGroup) + } + return name +} diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go index bb20d48a07487..61a46a22d559e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go @@ -39,7 +39,7 @@ type VerticalPodAutoscalerCollector struct { // NewVerticalPodAutoscalerCollector creates a new collector for the Kubernetes // VerticalPodAutoscaler resource. func NewVerticalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *VerticalPodAutoscalerCollector { - resourceType := collectors.GetResourceType(collectors.K8sVPAName, collectors.K8sVPAVersion) + resourceType := getResourceType(vpaName, vpaVersion) labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] @@ -50,9 +50,9 @@ func NewVerticalPodAutoscalerCollector(metadataAsTags utils.MetadataAsTags) *Ver IsMetadataProducer: true, IsManifestProducer: true, SupportsManifestBuffering: true, - Name: collectors.K8sVPAName, + Name: vpaName, NodeType: orchestrator.K8sVerticalPodAutoscaler, - Version: collectors.K8sVPAVersion, + Version: vpaVersion, LabelsAsTags: labelsAsTags, AnnotationsAsTags: annotationsAsTags, }, diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go deleted file mode 100644 index 70dd31c952ff6..0000000000000 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/types.go +++ /dev/null @@ -1,155 +0,0 @@ -// Unless explicitly stated otherwise all files in this repository are licensed -// under the Apache License Version 2.0. -// This product includes software developed at Datadog (https://www.datadoghq.com/). -// Copyright 2016-present Datadog, Inc. - -package collectors - -import ( - "fmt" - "strings" -) - -const ( - // ECSTaskName represents the name for ECS tasks. - ECSTaskName = "ecstasks" - - // K8sClusterRoleName represents the name for Kubernetes ClusterRoles. - K8sClusterRoleName = "clusterroles" - // K8sClusterRoleVersion represents the API version for Kubernetes ClusterRoles. - K8sClusterRoleVersion = "rbac.authorization.k8s.io/v1" - - // K8sClusterRoleBindingName represents the name for Kubernetes ClusterRoleBindings. - K8sClusterRoleBindingName = "clusterrolebindings" - // K8sClusterRoleBindingVersion represents the API version for Kubernetes ClusterRoleBindings. - K8sClusterRoleBindingVersion = "rbac.authorization.k8s.io/v1" - - // K8sCRDName represents the name for Kubernetes CustomResourceDefinitions. - K8sCRDName = "customresourcedefinitions" - // K8sCRDVersion represents the API version for Kubernetes CustomResourceDefinitions. - K8sCRDVersion = "apiextensions.k8s.io/v1" - - // K8sCronJobName represents the name for Kubernetes CronJobs. - K8sCronJobName = "cronjobs" - // K8sCronJobVersionV1 represents the v1 API version for Kubernetes CronJobs. - K8sCronJobVersionV1 = "batch/v1" - // K8sCronJobVersionV1Beta1 represents the v1beta1 API version for Kubernetes CronJobs. - K8sCronJobVersionV1Beta1 = "batch/v1beta1" - - // K8sDaemonSetName represents the name for Kubernetes DaemonSets. - K8sDaemonSetName = "daemonsets" - // K8sDaemonSetVersion represents the API version for Kubernetes DaemonSets. - K8sDaemonSetVersion = "apps/v1" - - // K8sDeploymentName represents the name for Kubernetes Deployments. - K8sDeploymentName = "deployments" - // K8sDeploymentVersion represents the API version for Kubernetes Deployments. - K8sDeploymentVersion = "apps/v1" - - // K8sHPAName represents the name for Kubernetes HorizontalPodAutoscalers. - K8sHPAName = "horizontalpodautoscalers" - // K8sHPAVersion represents the API version for Kubernetes HorizontalPodAutoscalers. - K8sHPAVersion = "autoscaling/v2" - - // K8sIngressName represents the name for Kubernetes Ingresses. - K8sIngressName = "ingresses" - // K8sIngressVersion represents the API version for Kubernetes Ingresses. - K8sIngressVersion = "networking.k8s.io/v1" - - // K8sJobName represents the name for Kubernetes Jobs. - K8sJobName = "jobs" - // K8sJobVersion represents the API version for Kubernetes Jobs. - K8sJobVersion = "batch/v1" - - // K8sLimitRangeName represents the name for Kubernetes LimitRanges. - K8sLimitRangeName = "limitranges" - // K8sLimitRangeVersion represents the API version for Kubernetes LimitRanges. - K8sLimitRangeVersion = "v1" - - // K8sNamespaceName represents the name for Kubernetes Namespaces. - K8sNamespaceName = "namespaces" - // K8sNamespaceVersion represents the API version for Kubernetes Namespaces. - K8sNamespaceVersion = "v1" - - // K8sNetworkPolicyName represents the name for Kubernetes NetworkPolicies. - K8sNetworkPolicyName = "networkpolicies" - // K8sNetworkPolicyVersion represents the API version for Kubernetes NetworkPolicies. - K8sNetworkPolicyVersion = "networking.k8s.io/v1" - - // K8sNodeName represents the name for Kubernetes Nodes. - K8sNodeName = "nodes" - // K8sNodeVersion represents the API version for Kubernetes Nodes. - K8sNodeVersion = "v1" - - // K8sPersistentVolumeName represents the name for Kubernetes PersistentVolumes. - K8sPersistentVolumeName = "persistentvolumes" - // K8sPersistentVolumeVersion represents the API version for Kubernetes PersistentVolumes. - K8sPersistentVolumeVersion = "v1" - - // K8sPersistentVolumeClaimName represents the name for Kubernetes PersistentVolumeClaims. - K8sPersistentVolumeClaimName = "persistentvolumeclaims" - // K8sPersistentVolumeClaimVersion represents the API version for Kubernetes PersistentVolumeClaims. - K8sPersistentVolumeClaimVersion = "v1" - - // K8sPodName represents the name for Kubernetes Pods. - K8sPodName = "pods" - // K8sPodVersion represents the API version for Kubernetes Pods. - K8sPodVersion = "v1" - - // K8sPodDisruptionBudgetName represents the name for Kubernetes PodDisruptionBudgets. - K8sPodDisruptionBudgetName = "poddisruptionbudgets" - // K8sPodDisruptionBudgetVersion represents the API version for Kubernetes PodDisruptionBudgets. - K8sPodDisruptionBudgetVersion = "policy/v1" - - // K8sReplicaSetName represents the name for Kubernetes ReplicaSets. - K8sReplicaSetName = "replicasets" - // K8sReplicaSetVersion represents the API version for Kubernetes ReplicaSets. - K8sReplicaSetVersion = "apps/v1" - - // K8sRoleName represents the name for Kubernetes Roles. - K8sRoleName = "roles" - // K8sRoleVersion represents the API version for Kubernetes Roles. - K8sRoleVersion = "rbac.authorization.k8s.io/v1" - - // K8sRoleBindingName represents the name for Kubernetes RoleBindings. - K8sRoleBindingName = "rolebindings" - // K8sRoleBindingVersion represents the API version for Kubernetes RoleBindings. - K8sRoleBindingVersion = "rbac.authorization.k8s.io/v1" - - // K8sServiceName represents the name for Kubernetes Services. - K8sServiceName = "services" - // K8sServiceVersion represents the API version for Kubernetes Services. - K8sServiceVersion = "v1" - - // K8sServiceAccountName represents the name for Kubernetes ServiceAccounts. - K8sServiceAccountName = "serviceaccounts" - // K8sServiceAccountVersion represents the API version for Kubernetes ServiceAccounts. - K8sServiceAccountVersion = "v1" - - // K8sStatefulSetName represents the name for Kubernetes StatefulSets. - K8sStatefulSetName = "statefulsets" - // K8sStatefulSetVersion represents the API version for Kubernetes StatefulSets. - K8sStatefulSetVersion = "apps/v1" - - // K8sStorageClassName represents the name for Kubernetes StorageClasses. - K8sStorageClassName = "storageclasses" - // K8sStorageClassVersion represents the API version for Kubernetes StorageClasses. - K8sStorageClassVersion = "storage.k8s.io/v1" - - // K8sVPAName represents the name for Kubernetes VerticalPodAutoscalers. - K8sVPAName = "verticalpodautoscalers" - // K8sVPAVersion represents the API version for Kubernetes VerticalPodAutoscalers. - K8sVPAVersion = "autoscaling.k8s.io/v1" -) - -// GetResourceType returns a string in the format "name.apiGroup" if an API group is present in the version. -// Otherwise, it returns the name. -func GetResourceType(name string, version string) string { - var apiGroup string - apiVersionParts := strings.Split(version, "/") - if len(apiVersionParts) == 2 { - apiGroup = apiVersionParts[0] - return fmt.Sprintf("%s.%s", name, apiGroup) - } - return name -} From 26642e9c837bbb1cc95858581918e0e290f2b7ee Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Fri, 10 Jan 2025 14:42:30 -0500 Subject: [PATCH 5/6] shortened release note name --- ...-resource-metadata-tags-to-orchestrator-ad98dafedded7e55.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename releasenotes-dca/notes/{add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml => add-resource-metadata-tags-to-orchestrator-ad98dafedded7e55.yaml} (100%) diff --git a/releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml b/releasenotes-dca/notes/add-resource-metadata-tags-to-orchestrator-ad98dafedded7e55.yaml similarity index 100% rename from releasenotes-dca/notes/add-resource-labels-and-annotations-as-tags-to-orchestrator-ad98dafedded7e55.yaml rename to releasenotes-dca/notes/add-resource-metadata-tags-to-orchestrator-ad98dafedded7e55.yaml From ff8c96d4a5ec740ea9f39b03ad7d59296ce3e84d Mon Sep 17 00:00:00 2001 From: Ethan Wood-Thomas Date: Tue, 21 Jan 2025 10:22:20 -0500 Subject: [PATCH 6/6] removed testing log --- .../cluster/orchestrator/collectors/k8s/clusterrole.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go index 45f4fa662e1b5..8584a7b220a9c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go @@ -13,8 +13,6 @@ import ( k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" "github.com/DataDog/datadog-agent/pkg/config/utils" "github.com/DataDog/datadog-agent/pkg/orchestrator" - "github.com/DataDog/datadog-agent/pkg/util/log" - "k8s.io/apimachinery/pkg/labels" rbacv1Informers "k8s.io/client-go/informers/rbac/v1" rbacv1Listers "k8s.io/client-go/listers/rbac/v1" @@ -43,8 +41,6 @@ func NewClusterRoleCollector(metadataAsTags utils.MetadataAsTags) *ClusterRoleCo labelsAsTags := metadataAsTags.GetResourcesLabelsAsTags()[resourceType] annotationsAsTags := metadataAsTags.GetResourcesAnnotationsAsTags()[resourceType] - log.Infof("ethan_test_log resourceType: %v labelsAsTags: %v annotationsAsTags: %v", resourceType, labelsAsTags, annotationsAsTags) - return &ClusterRoleCollector{ metadata: &collectors.CollectorMetadata{ IsDefaultVersion: true,