From e9f189ec43fbdcb677f0d8e10dcf64a0a5f52fe6 Mon Sep 17 00:00:00 2001 From: Kangyi LI Date: Mon, 18 Mar 2024 12:27:21 +0100 Subject: [PATCH] update code structure --- .../cluster/orchestrator/collector_bundle.go | 20 ++-- .../orchestrator/collectors/collector.go | 72 +++-------- .../collectors/inventory/inventory.go | 8 +- .../orchestrator/collectors/k8s/cluster.go | 10 +- .../collectors/k8s/clusterrole.go | 2 +- .../collectors/k8s/clusterrolebinding.go | 2 +- .../cluster/orchestrator/collectors/k8s/cr.go | 8 +- .../orchestrator/collectors/k8s/crd.go | 8 +- .../orchestrator/collectors/k8s/cronjob_v1.go | 2 +- .../collectors/k8s/cronjob_v1beta1.go | 2 +- .../orchestrator/collectors/k8s/daemonset.go | 2 +- .../orchestrator/collectors/k8s/deployment.go | 2 +- .../collectors/k8s/horizontalpodautoscaler.go | 2 +- .../orchestrator/collectors/k8s/ingress.go | 2 +- .../orchestrator/collectors/k8s/job.go | 2 +- .../orchestrator/collectors/k8s/namespace.go | 2 +- .../collectors/k8s/networkpolicy.go | 12 +- .../orchestrator/collectors/k8s/node.go | 2 +- .../collectors/k8s/persistentvolume.go | 2 +- .../collectors/k8s/persistentvolumeclaim.go | 2 +- .../collectors/k8s/pod_unassigned.go | 2 +- .../orchestrator/collectors/k8s/replicaset.go | 2 +- .../orchestrator/collectors/k8s/role.go | 2 +- .../collectors/k8s/rolebinding.go | 2 +- .../orchestrator/collectors/k8s/service.go | 2 +- .../collectors/k8s/serviceaccount.go | 2 +- .../collectors/k8s/statefulset.go | 2 +- .../collectors/k8s/verticalpodautoscaler.go | 2 +- .../orchestrator/collectors/k8scollector.go | 76 ++++++++++++ .../cluster/orchestrator/collectors/stub.go | 11 ++ .../discovery/collector_discovery.go | 6 +- .../discovery/collector_discovery_crd.go | 14 +-- .../cluster/orchestrator/manifest_buffer.go | 20 ++-- .../cluster/orchestrator/orchestrator_test.go | 12 +- .../orchestrator/processors/chunking.go | 1 - .../processors/{k8s => common}/base.go | 21 ++-- .../orchestrator/processors/k8s/cluster.go | 19 +-- .../processors/k8s/clusterrole.go | 26 ++-- .../processors/k8s/clusterrolebinding.go | 26 ++-- .../cluster/orchestrator/processors/k8s/cr.go | 30 ++--- .../orchestrator/processors/k8s/crd.go | 27 +++-- .../orchestrator/processors/k8s/cronjob_v1.go | 33 +++--- .../processors/k8s/cronjob_v1beta1.go | 33 +++--- .../orchestrator/processors/k8s/daemonset.go | 33 +++--- .../orchestrator/processors/k8s/deployment.go | 33 +++--- .../processors/k8s/horizontalpodautoscaler.go | 31 ++--- .../orchestrator/processors/k8s/ingress.go | 28 +++-- .../orchestrator/processors/k8s/job.go | 33 +++--- .../orchestrator/processors/k8s/namespace.go | 29 +++-- .../processors/k8s/networkpolicy.go | 35 +++--- .../orchestrator/processors/k8s/node.go | 28 +++-- .../processors/k8s/persistentvolume.go | 26 ++-- .../processors/k8s/persistentvolumeclaim.go | 26 ++-- .../orchestrator/processors/k8s/pod.go | 40 ++++--- .../orchestrator/processors/k8s/replicaset.go | 33 +++--- .../orchestrator/processors/k8s/role.go | 26 ++-- .../processors/k8s/rolebinding.go | 26 ++-- .../orchestrator/processors/k8s/service.go | 26 ++-- .../processors/k8s/serviceaccount.go | 26 ++-- .../processors/k8s/statefulset.go | 33 +++--- .../processors/k8s/verticalpodautoscaler.go | 31 ++--- .../orchestrator/processors/processor.go | 112 +++++++++++++----- .../orchestrator/processors/processor_test.go | 4 +- .../transformers/k8s/clusterrole.go | 1 - .../orchestrator/transformers/k8s/common.go | 1 + .../corechecks/orchestrator/pod/pod.go | 12 +- 66 files changed, 683 insertions(+), 525 deletions(-) create mode 100644 pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go create mode 100644 pkg/collector/corechecks/cluster/orchestrator/collectors/stub.go rename pkg/collector/corechecks/cluster/orchestrator/processors/{k8s => common}/base.go (59%) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collector_bundle.go b/pkg/collector/corechecks/cluster/orchestrator/collector_bundle.go index 9e171936ed29bb..77c6bbde557829 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collector_bundle.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collector_bundle.go @@ -41,7 +41,7 @@ var ( // to easily run them all. type CollectorBundle struct { check *OrchestratorCheck - collectors []collectors.Collector + collectors []collectors.K8sCollector discoverCollectors bool extraSyncTimeout time.Duration inventory *inventory.CollectorInventory @@ -68,11 +68,13 @@ func NewCollectorBundle(chk *OrchestratorCheck) *CollectorBundle { check: chk, inventory: inventory.NewCollectorInventory(), runCfg: &collectors.CollectorRunConfig{ - APIClient: chk.apiClient, - ClusterID: chk.clusterID, - Config: chk.orchestratorConfig, - MsgGroupRef: chk.groupID, - OrchestratorInformerFactory: chk.orchestratorInformerFactory, + K8sCollectorRunConfig: collectors.K8sCollectorRunConfig{ + APIClient: chk.apiClient, + OrchestratorInformerFactory: chk.orchestratorInformerFactory, + }, + ClusterID: chk.clusterID, + Config: chk.orchestratorConfig, + MsgGroupRef: chk.groupID, }, stopCh: chk.stopCh, manifestBuffer: NewManifestBuffer(chk), @@ -135,7 +137,7 @@ func (cb *CollectorBundle) skipImportingDefaultCollectors() bool { // - / (e.g. "batch/v1/cronjobs") func (cb *CollectorBundle) addCollectorFromConfig(collectorName string, isCRD bool) { var ( - collector collectors.Collector + collector collectors.K8sCollector err error ) @@ -229,11 +231,11 @@ func (cb *CollectorBundle) importCollectorsFromDiscovery() bool { collectors, err := discovery.NewAPIServerDiscoveryProvider().Discover(cb.inventory) if err != nil { - _ = cb.check.Warnf("Collector discovery failed: %s", err) + _ = cb.check.Warnf("Kubernetes collector discovery failed: %s", err) return false } if len(collectors) == 0 { - _ = cb.check.Warn("Collector discovery returned no collector") + _ = cb.check.Warn("Kubernetes collector discovery returned no collector") return false } diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go index 0183ea8330e326..c16a63d7d64d16 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/collector.go @@ -3,7 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2016-present Datadog, Inc. -//go:build kubeapiserver && orchestrator +//go:build orchestrator //nolint:revive // TODO(CAPP) Fix revive linter package collectors @@ -12,12 +12,8 @@ import ( "fmt" "go.uber.org/atomic" - "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions" - vpai "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/informers/externalversions" - "k8s.io/client-go/dynamic/dynamicinformer" - "k8s.io/client-go/informers" - "k8s.io/client-go/tools/cache" + "github.com/DataDog/datadog-agent/comp/core/workloadmeta" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/orchestrator/config" pkgorchestratormodel "github.com/DataDog/datadog-agent/pkg/orchestrator/model" @@ -27,9 +23,6 @@ import ( // Collector is an interface that represents the collection process for a // resource type. type Collector interface { - // Informer returns the shared informer for that resource. - Informer() cache.SharedInformer - // Init is where the collector initialization happens. It is used to create // informers and listers. Init(*CollectorRunConfig) @@ -42,30 +35,6 @@ type Collector interface { Run(*CollectorRunConfig) (*CollectorRunResult, error) } -// CollectorVersions represents the list of collector implementations that are -// supported, each one being tied to a specific kubernetes group and version. -type CollectorVersions struct { - Collectors []Collector -} - -// NewCollectorVersions is used to build the collector version list. -func NewCollectorVersions(versions ...Collector) CollectorVersions { - return CollectorVersions{ - versions, - } -} - -// CollectorForVersion retrieves the collector implementing a given version. If -// no collector is known for that version, returns (nil, false). -func (cv *CollectorVersions) CollectorForVersion(version string) (Collector, bool) { - for _, collector := range cv.Collectors { - if collector.Metadata().Version == version { - return collector, true - } - } - return nil, false -} - // CollectorMetadata contains information about a collector. type CollectorMetadata struct { IsDefaultVersion bool @@ -88,23 +57,25 @@ func (cm CollectorMetadata) FullName() string { return cm.Name } -// OrchestratorInformerFactory contains all informer factories used by the orchestration check -type OrchestratorInformerFactory struct { - InformerFactory informers.SharedInformerFactory - UnassignedPodInformerFactory informers.SharedInformerFactory - DynamicInformerFactory dynamicinformer.DynamicSharedInformerFactory - CRDInformerFactory externalversions.SharedInformerFactory - VPAInformerFactory vpai.SharedInformerFactory +// K8sCollectorRunConfig is the configuration used to initialize or run the kubernetes collector. +type K8sCollectorRunConfig struct { + APIClient *apiserver.APIClient + OrchestratorInformerFactory *OrchestratorInformerFactory +} + +// ECSCollectorRunConfig is the configuration used to initialize or run the ECS collector. +type ECSCollectorRunConfig struct { + WorkloadmetaStore workloadmeta.Component } // CollectorRunConfig is the configuration used to initialize or run the // collector. type CollectorRunConfig struct { - APIClient *apiserver.APIClient - ClusterID string - Config *config.OrchestratorConfig - MsgGroupRef *atomic.Int32 - OrchestratorInformerFactory *OrchestratorInformerFactory + K8sCollectorRunConfig + ECSCollectorRunConfig + ClusterID string + Config *config.OrchestratorConfig + MsgGroupRef *atomic.Int32 } // CollectorRunResult contains information about what the collector has done. @@ -116,14 +87,3 @@ type CollectorRunResult struct { ResourcesListed int ResourcesProcessed int } - -func NewProcessorContext(rcfg *CollectorRunConfig, metadata *CollectorMetadata) *processors.ProcessorContext { - return &processors.ProcessorContext{ - APIClient: rcfg.APIClient, - ApiGroupVersionTag: fmt.Sprintf("kube_api_version:%s", metadata.Version), - Cfg: rcfg.Config, - ClusterID: rcfg.ClusterID, - MsgGroupID: rcfg.MsgGroupRef.Inc(), - NodeType: metadata.NodeType, - } -} diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go index 85acef298671c6..233c6d1ec34de9 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/inventory/inventory.go @@ -54,7 +54,7 @@ func NewCollectorInventory() *CollectorInventory { // CollectorForDefaultVersion retrieves a collector given its name. It returns an error if the // name is not known. -func (ci *CollectorInventory) CollectorForDefaultVersion(collectorName string) (collectors.Collector, error) { +func (ci *CollectorInventory) CollectorForDefaultVersion(collectorName string) (collectors.K8sCollector, error) { for _, cv := range ci.collectors { for _, c := range cv.Collectors { if c.Metadata().Name == collectorName && c.Metadata().IsDefaultVersion { @@ -67,7 +67,7 @@ func (ci *CollectorInventory) CollectorForDefaultVersion(collectorName string) ( // CollectorForVersion gets a collector given its name and version. It returns // an error if the collector name or version is not known. -func (ci *CollectorInventory) CollectorForVersion(collectorName, collectorVersion string) (collectors.Collector, error) { +func (ci *CollectorInventory) CollectorForVersion(collectorName, collectorVersion string) (collectors.K8sCollector, error) { for _, cv := range ci.collectors { for _, c := range cv.Collectors { if c.Metadata().Name == collectorName && c.Metadata().Version == collectorVersion { @@ -79,8 +79,8 @@ func (ci *CollectorInventory) CollectorForVersion(collectorName, collectorVersio } // StableCollectors get a list of all stable collectors in the inventory. -func (ci *CollectorInventory) StableCollectors() []collectors.Collector { - var stableCollectors []collectors.Collector +func (ci *CollectorInventory) StableCollectors() []collectors.K8sCollector { + var stableCollectors []collectors.K8sCollector for _, cv := range ci.collectors { for _, c := range cv.Collectors { if c.Metadata().IsStable && c.Metadata().IsDefaultVersion { diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go index ae3a2892de2d21..916ab29d9eb739 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cluster.go @@ -9,14 +9,14 @@ package k8s import ( - "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" - k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" - "github.com/DataDog/datadog-agent/pkg/orchestrator" - "k8s.io/apimachinery/pkg/labels" corev1Informers "k8s.io/client-go/informers/core/v1" corev1Listers "k8s.io/client-go/listers/core/v1" "k8s.io/client-go/tools/cache" + + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/collectors" + k8sProcessors "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/orchestrator" ) // NewClusterCollectorVersions builds the group of collector versions. @@ -74,7 +74,7 @@ func (c *ClusterCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed, err := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go index db90e632567f3d..1cdc6518ddc4fd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrole.go @@ -75,7 +75,7 @@ func (c *ClusterRoleCollector) Run(rcfg *collectors.CollectorRunConfig) (*collec return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go index 3401d7044e4d40..715289b771dc8c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/clusterrolebinding.go @@ -75,7 +75,7 @@ func (c *ClusterRoleBindingCollector) Run(rcfg *collectors.CollectorRunConfig) ( return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cr.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cr.go index 9e7f5c22d6defe..003968213b756a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cr.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cr.go @@ -93,13 +93,7 @@ func (c *CRCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors.Coll return nil, collectors.NewListingError(fmt.Errorf("crd collector %s/%s has reached to the limit %d, skipping it", c.metadata.Version, c.metadata.Name, defaultMaximumCRDQuota)) } - ctx := &processors.ProcessorContext{ - APIClient: rcfg.APIClient, - Cfg: rcfg.Config, - ClusterID: rcfg.ClusterID, - MsgGroupID: rcfg.MsgGroupRef.Inc(), - NodeType: c.metadata.NodeType, - } + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go index 9fe457c00fd0e7..b2f5f5463ec582 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/crd.go @@ -81,13 +81,7 @@ func (c *CRDCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors.Col return nil, collectors.NewListingError(err) } - ctx := &processors.ProcessorContext{ - APIClient: rcfg.APIClient, - Cfg: rcfg.Config, - ClusterID: rcfg.ClusterID, - MsgGroupID: rcfg.MsgGroupRef.Inc(), - NodeType: c.metadata.NodeType, - } + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) 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 38c0e1e40563ca..72551c168ee3dd 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1.go @@ -67,7 +67,7 @@ func (c *CronJobV1Collector) Run(rcfg *collectors.CollectorRunConfig) (*collecto return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) 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 8809dbca5df590..000fa354b5b81c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/cronjob_v1beta1.go @@ -67,7 +67,7 @@ func (c *CronJobV1Beta1Collector) Run(rcfg *collectors.CollectorRunConfig) (*col return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go index 81adefed6527a5..bc01177cbb564f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/daemonset.go @@ -75,7 +75,7 @@ func (c *DaemonSetCollector) Run(rcfg *collectors.CollectorRunConfig) (*collecto return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go index ce7c728e8b264d..668603d8a7e4ce 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/deployment.go @@ -75,7 +75,7 @@ func (c *DeploymentCollector) Run(rcfg *collectors.CollectorRunConfig) (*collect return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go index e9ad98beadf980..2f6565ca7647d0 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/horizontalpodautoscaler.go @@ -76,7 +76,7 @@ func (c *HorizontalPodAutoscalerCollector) Run(rcfg *collectors.CollectorRunConf return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go index 10b9be393f383f..197dea30727eda 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/ingress.go @@ -75,7 +75,7 @@ func (c *IngressCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go index cf3776fdd1c453..6a66eed9348771 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/job.go @@ -74,7 +74,7 @@ func (c *JobCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors.Col return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go index 482926b335664b..e635d10e762d60 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/namespace.go @@ -75,7 +75,7 @@ func (c *NamespaceCollector) Run(rcfg *collectors.CollectorRunConfig) (*collecto return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go index b66985c775ca26..b6dbd63b056d67 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/networkpolicy.go @@ -8,15 +8,15 @@ package k8s 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/orchestrator" - "k8s.io/apimachinery/pkg/labels" networkingv1Informers "k8s.io/client-go/informers/networking/v1" networkingv1Listers "k8s.io/client-go/listers/networking/v1" "k8s.io/client-go/tools/cache" + + "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/orchestrator" ) // NewNetworkPolicyCollectorVersions builds the group of collector versions. @@ -75,7 +75,7 @@ func (c *NetworkPolicyCollector) Run(rcfg *collectors.CollectorRunConfig) (*coll return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go index a8d6160e0ecd66..991f52ae253faa 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/node.go @@ -74,7 +74,7 @@ func (c *NodeCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors.Co return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go index 3b1bfe4f564ce3..7c78f6af2efdc8 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolume.go @@ -75,7 +75,7 @@ func (c *PersistentVolumeCollector) Run(rcfg *collectors.CollectorRunConfig) (*c return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go index a13b582c0797c0..e5bf23dadade3c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/persistentvolumeclaim.go @@ -75,7 +75,7 @@ func (c *PersistentVolumeClaimCollector) Run(rcfg *collectors.CollectorRunConfig return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) 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 e9492393164587..a965c32d5efa3a 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/pod_unassigned.go @@ -76,7 +76,7 @@ func (c *UnassignedPodCollector) Run(rcfg *collectors.CollectorRunConfig) (*coll return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go index 113cba74ca5c72..7fa0e593c75648 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/replicaset.go @@ -75,7 +75,7 @@ func (c *ReplicaSetCollector) Run(rcfg *collectors.CollectorRunConfig) (*collect return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go index 3630eea43999d8..5d0999f5bd59d8 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/role.go @@ -74,7 +74,7 @@ func (c *RoleCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors.Co return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go index 82c6ca4ef22cf0..6a499a0faa151e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/rolebinding.go @@ -75,7 +75,7 @@ func (c *RoleBindingCollector) Run(rcfg *collectors.CollectorRunConfig) (*collec return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go index 64f4f8d7fffbbd..434a5243736a0d 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/service.go @@ -75,7 +75,7 @@ func (c *ServiceCollector) Run(rcfg *collectors.CollectorRunConfig) (*collectors return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go index bf834cebc878e9..1bb1d5cd92cf09 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/serviceaccount.go @@ -75,7 +75,7 @@ func (c *ServiceAccountCollector) Run(rcfg *collectors.CollectorRunConfig) (*col return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go index 7f25cbbdca00e9..2c1d839882847b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/statefulset.go @@ -75,7 +75,7 @@ func (c *StatefulSetCollector) Run(rcfg *collectors.CollectorRunConfig) (*collec return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go index 8b883b887795ff..e7008e9e04b8c6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8s/verticalpodautoscaler.go @@ -76,7 +76,7 @@ func (c *VerticalPodAutoscalerCollector) Run(rcfg *collectors.CollectorRunConfig return nil, collectors.NewListingError(err) } - ctx := collectors.NewProcessorContext(rcfg, c.metadata) + ctx := collectors.NewK8sProcessorContext(rcfg, c.metadata) processResult, processed := c.processor.Process(ctx, list) diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go new file mode 100644 index 00000000000000..b7c3051a393777 --- /dev/null +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/k8scollector.go @@ -0,0 +1,76 @@ +// 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 collectors + +import ( + "fmt" + + "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions" + vpai "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/client/informers/externalversions" + "k8s.io/client-go/dynamic/dynamicinformer" + "k8s.io/client-go/informers" + "k8s.io/client-go/tools/cache" + + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" +) + +// K8sCollector is an interface that represents the collection process for a k8s resource type. +type K8sCollector interface { + Collector + + // Informer returns the shared informer for that resource. + Informer() cache.SharedInformer +} + +// CollectorVersions represents the list of collector implementations that are +// supported, each one being tied to a specific kubernetes group and version. +type CollectorVersions struct { + Collectors []K8sCollector +} + +// NewCollectorVersions is used to build the collector version list. +func NewCollectorVersions(versions ...K8sCollector) CollectorVersions { + return CollectorVersions{ + versions, + } +} + +// CollectorForVersion retrieves the collector implementing a given version. If +// no collector is known for that version, returns (nil, false). +func (cv *CollectorVersions) CollectorForVersion(version string) (K8sCollector, bool) { + for _, collector := range cv.Collectors { + if collector.Metadata().Version == version { + return collector, true + } + } + return nil, false +} + +// OrchestratorInformerFactory contains all informer factories used by the orchestration check +type OrchestratorInformerFactory struct { + InformerFactory informers.SharedInformerFactory + UnassignedPodInformerFactory informers.SharedInformerFactory + DynamicInformerFactory dynamicinformer.DynamicSharedInformerFactory + CRDInformerFactory externalversions.SharedInformerFactory + VPAInformerFactory vpai.SharedInformerFactory +} + +// NewK8sProcessorContext creates a new processor context for k8s resources. +func NewK8sProcessorContext(rcfg *CollectorRunConfig, metadata *CollectorMetadata) *processors.K8sProcessorContext { + return &processors.K8sProcessorContext{ + BaseProcessorContext: processors.BaseProcessorContext{ + Cfg: rcfg.Config, + MsgGroupID: rcfg.MsgGroupRef.Inc(), + NodeType: metadata.NodeType, + ManifestProducer: true, + ClusterID: rcfg.ClusterID, + }, + APIClient: rcfg.APIClient, + ApiGroupVersionTag: fmt.Sprintf("kube_api_version:%s", metadata.Version), + } +} diff --git a/pkg/collector/corechecks/cluster/orchestrator/collectors/stub.go b/pkg/collector/corechecks/cluster/orchestrator/collectors/stub.go new file mode 100644 index 00000000000000..09a7b320d1b877 --- /dev/null +++ b/pkg/collector/corechecks/cluster/orchestrator/collectors/stub.go @@ -0,0 +1,11 @@ +// 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 collectors + +// OrchestratorInformerFactory contains all informer factories used by the orchestration check +type OrchestratorInformerFactory struct{} diff --git a/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery.go b/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery.go index c9d1322623cdab..5ae2030c457d7c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery.go +++ b/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery.go @@ -30,7 +30,7 @@ const ( // APIServerDiscoveryProvider is a discovery provider that uses the Kubernetes // API Server as its data source. type APIServerDiscoveryProvider struct { - result []collectors.Collector + result []collectors.K8sCollector seen map[string]struct{} } @@ -43,7 +43,7 @@ func NewAPIServerDiscoveryProvider() *APIServerDiscoveryProvider { } // Discover returns collectors to enable based on information exposed by the API server. -func (p *APIServerDiscoveryProvider) Discover(inventory *inventory.CollectorInventory) ([]collectors.Collector, error) { +func (p *APIServerDiscoveryProvider) Discover(inventory *inventory.CollectorInventory) ([]collectors.K8sCollector, error) { groups, resources, err := GetServerGroupsAndResources() if err != nil { return nil, err @@ -90,7 +90,7 @@ func GetServerGroupsAndResources() ([]*v1.APIGroup, []*v1.APIResourceList, error return groups, resources, nil } -func (p *APIServerDiscoveryProvider) addCollector(collector collectors.Collector) { +func (p *APIServerDiscoveryProvider) addCollector(collector collectors.K8sCollector) { // Make sure resource collectors are added at most once if _, found := p.seen[collector.Metadata().Name]; found { return diff --git a/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery_crd.go b/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery_crd.go index 638097fb9b824e..15ae536d49c4e4 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery_crd.go +++ b/pkg/collector/corechecks/cluster/orchestrator/discovery/collector_discovery_crd.go @@ -73,7 +73,7 @@ func (d *DiscoveryCollector) fillCache() error { } //nolint:revive // TODO(CAPP) Fix revive linter -func (d *DiscoveryCollector) VerifyForCRDInventory(resource string, groupVersion string) (collectors.Collector, error) { +func (d *DiscoveryCollector) VerifyForCRDInventory(resource string, groupVersion string) (collectors.K8sCollector, error) { collector, err := d.DiscoverCRDResource(resource, groupVersion) if err != nil { return nil, err @@ -82,7 +82,7 @@ func (d *DiscoveryCollector) VerifyForCRDInventory(resource string, groupVersion } //nolint:revive // TODO(CAPP) Fix revive linter -func (d *DiscoveryCollector) VerifyForInventory(resource string, groupVersion string, collectorInventory *inventory.CollectorInventory) (collectors.Collector, error) { +func (d *DiscoveryCollector) VerifyForInventory(resource string, groupVersion string, collectorInventory *inventory.CollectorInventory) (collectors.K8sCollector, error) { collector, err := d.DiscoverRegularResource(resource, groupVersion, collectorInventory) if err != nil { return nil, err @@ -91,7 +91,7 @@ func (d *DiscoveryCollector) VerifyForInventory(resource string, groupVersion st } //nolint:revive // TODO(CAPP) Fix revive linter -func (d *DiscoveryCollector) DiscoverCRDResource(resource string, groupVersion string) (collectors.Collector, error) { +func (d *DiscoveryCollector) DiscoverCRDResource(resource string, groupVersion string) (collectors.K8sCollector, error) { collector, err := k8sCollectors.NewCRCollectorVersion(resource, groupVersion) if err != nil { return nil, err @@ -101,8 +101,8 @@ func (d *DiscoveryCollector) DiscoverCRDResource(resource string, groupVersion s } //nolint:revive // TODO(CAPP) Fix revive linter -func (d *DiscoveryCollector) DiscoverRegularResource(resource string, groupVersion string, collectorInventory *inventory.CollectorInventory) (collectors.Collector, error) { - var collector collectors.Collector +func (d *DiscoveryCollector) DiscoverRegularResource(resource string, groupVersion string, collectorInventory *inventory.CollectorInventory) (collectors.K8sCollector, error) { + var collector collectors.K8sCollector var err error if groupVersion == "" { @@ -120,7 +120,7 @@ func (d *DiscoveryCollector) DiscoverRegularResource(resource string, groupVersi return d.isSupportCollector(collector) } -func (d *DiscoveryCollector) isSupportCollector(collector collectors.Collector) (collectors.Collector, error) { +func (d *DiscoveryCollector) isSupportCollector(collector collectors.K8sCollector) (collectors.K8sCollector, error) { if _, ok := d.cache.collectorForVersion[collectorVersion{ version: collector.Metadata().Version, name: collector.Metadata().Name, @@ -130,7 +130,7 @@ func (d *DiscoveryCollector) isSupportCollector(collector collectors.Collector) return nil, fmt.Errorf("failed to discover resource %s", collector.Metadata().Name) } -func (d *DiscoveryCollector) isSupportClusterCollector(collector collectors.Collector, collectorInventory *inventory.CollectorInventory) (collectors.Collector, error) { +func (d *DiscoveryCollector) isSupportClusterCollector(collector collectors.K8sCollector, collectorInventory *inventory.CollectorInventory) (collectors.K8sCollector, error) { nodeCollector, err := collectorInventory.CollectorForDefaultVersion("nodes") if err != nil { return nil, fmt.Errorf("failed to discover cluster resource %w", err) diff --git a/pkg/collector/corechecks/cluster/orchestrator/manifest_buffer.go b/pkg/collector/corechecks/cluster/orchestrator/manifest_buffer.go index 602517283ced6c..7f4c25ff3a8394 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/manifest_buffer.go +++ b/pkg/collector/corechecks/cluster/orchestrator/manifest_buffer.go @@ -17,7 +17,7 @@ import ( model "github.com/DataDog/agent-payload/v5/process" "github.com/DataDog/datadog-agent/pkg/aggregator/sender" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" - "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/k8s" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/common" "github.com/DataDog/datadog-agent/pkg/orchestrator" "github.com/DataDog/datadog-agent/pkg/orchestrator/config" pkgorchestratormodel "github.com/DataDog/datadog-agent/pkg/orchestrator/model" @@ -88,16 +88,18 @@ func NewManifestBuffer(chk *OrchestratorCheck) *ManifestBuffer { // flushManifest flushes manifests by chunking them first then sending them to the sender func (cb *ManifestBuffer) flushManifest(sender sender.Sender) { manifests := cb.bufferedManifests - ctx := &processors.ProcessorContext{ - ClusterID: cb.Cfg.ClusterID, - MsgGroupID: cb.Cfg.MsgGroupRef.Inc(), - Cfg: &config.OrchestratorConfig{ - KubeClusterName: cb.Cfg.KubeClusterName, - MaxPerMessage: cb.Cfg.MaxPerMessage, - MaxWeightPerMessageBytes: cb.Cfg.MaxWeightPerMessageBytes, + ctx := &processors.K8sProcessorContext{ + BaseProcessorContext: processors.BaseProcessorContext{ + MsgGroupID: cb.Cfg.MsgGroupRef.Inc(), + Cfg: &config.OrchestratorConfig{ + KubeClusterName: cb.Cfg.KubeClusterName, + MaxPerMessage: cb.Cfg.MaxPerMessage, + MaxWeightPerMessageBytes: cb.Cfg.MaxWeightPerMessageBytes, + }, + ClusterID: cb.Cfg.ClusterID, }, } - manifestMessages := processors.ChunkManifest(ctx, k8s.BaseHandlers{}.BuildManifestMessageBody, manifests) + manifestMessages := processors.ChunkManifest(ctx, common.BaseHandlers{}.BuildManifestMessageBody, manifests) sender.OrchestratorManifest(manifestMessages, cb.Cfg.ClusterID) setManifestStats(manifests) cb.bufferedManifests = cb.bufferedManifests[:0] diff --git a/pkg/collector/corechecks/cluster/orchestrator/orchestrator_test.go b/pkg/collector/corechecks/cluster/orchestrator/orchestrator_test.go index 52d7cc64419165..70c388a23828d2 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/orchestrator_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/orchestrator_test.go @@ -33,11 +33,13 @@ func newCollectorBundle(chk *OrchestratorCheck) *CollectorBundle { check: chk, inventory: inventory.NewCollectorInventory(), runCfg: &collectors.CollectorRunConfig{ - APIClient: chk.apiClient, - ClusterID: chk.clusterID, - Config: chk.orchestratorConfig, - MsgGroupRef: chk.groupID, - OrchestratorInformerFactory: chk.orchestratorInformerFactory, + K8sCollectorRunConfig: collectors.K8sCollectorRunConfig{ + APIClient: chk.apiClient, + OrchestratorInformerFactory: chk.orchestratorInformerFactory, + }, + ClusterID: chk.clusterID, + Config: chk.orchestratorConfig, + MsgGroupRef: chk.groupID, }, stopCh: chk.stopCh, manifestBuffer: NewManifestBuffer(chk), diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/chunking.go b/pkg/collector/corechecks/cluster/orchestrator/processors/chunking.go index 6e5e9b8785b280..5c9e1694640a2e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/chunking.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/chunking.go @@ -10,7 +10,6 @@ package processors import ( model "github.com/DataDog/agent-payload/v5/process" - "github.com/DataDog/datadog-agent/pkg/process/util" ) diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/base.go b/pkg/collector/corechecks/cluster/orchestrator/processors/common/base.go similarity index 59% rename from pkg/collector/corechecks/cluster/orchestrator/processors/k8s/base.go rename to pkg/collector/corechecks/cluster/orchestrator/processors/common/base.go index b69f5f06f498b5..f27314c53950ad 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/base.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/common/base.go @@ -5,8 +5,8 @@ //go:build orchestrator -//nolint:revive // TODO(CAPP) Fix revive linter -package k8s +// Package common provides basic handlers used by orchestrator processor +package common import ( model "github.com/DataDog/agent-payload/v5/process" @@ -17,25 +17,26 @@ import ( type BaseHandlers struct{} //nolint:revive // TODO(CAPP) Fix revive linter -func (BaseHandlers) BeforeCacheCheck(ctx *processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { +func (BaseHandlers) BeforeCacheCheck(ctx processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { return } //nolint:revive // TODO(CAPP) Fix revive linter -func (BaseHandlers) BeforeMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { +func (BaseHandlers) BeforeMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { return } //nolint:revive // TODO(CAPP) Fix revive linter -func (BaseHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) {} +func (BaseHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) {} //nolint:revive // TODO(CAPP) Fix revive linter -func (BaseHandlers) BuildManifestMessageBody(ctx *processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { +func (BaseHandlers) BuildManifestMessageBody(ctx processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { return ExtractModelManifests(ctx, resourceManifests, groupSize) } // ExtractModelManifests creates the model manifest from the given manifests -func ExtractModelManifests(ctx *processors.ProcessorContext, resourceManifests []interface{}, groupSize int) *model.CollectorManifest { +func ExtractModelManifests(ctx processors.ProcessorContext, resourceManifests []interface{}, groupSize int) *model.CollectorManifest { + pctx := ctx.(*processors.K8sProcessorContext) manifests := make([]*model.Manifest, 0, len(resourceManifests)) for _, m := range resourceManifests { @@ -43,10 +44,10 @@ func ExtractModelManifests(ctx *processors.ProcessorContext, resourceManifests [ } cm := &model.CollectorManifest{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, Manifests: manifests, - GroupId: ctx.MsgGroupID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), } return cm diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cluster.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cluster.go index efd65db08ed6a7..197fffc865f8c1 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cluster.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cluster.go @@ -5,6 +5,7 @@ //go:build orchestrator +// Package k8s defines handlers for processing kubernetes resources package k8s import ( @@ -43,7 +44,7 @@ func NewClusterProcessor() *ClusterProcessor { } // Process is used to process a list of node resources forming a cluster. -func (p *ClusterProcessor) Process(ctx *processors.ProcessorContext, list interface{}) (processResult processors.ProcessResult, processed int, err error) { +func (p *ClusterProcessor) Process(ctx processors.ProcessorContext, list interface{}) (processResult processors.ProcessResult, processed int, err error) { processed = -1 defer processors.RecoverOnPanic() @@ -58,7 +59,7 @@ func (p *ClusterProcessor) Process(ctx *processors.ProcessorContext, list interf podAllocatable uint32 podCapacity uint32 ) - + pctx := ctx.(*processors.K8sProcessorContext) resourceList := p.nodeHandlers.ResourceList(ctx, list) nodeCount := int32(len(resourceList)) @@ -92,12 +93,12 @@ func (p *ClusterProcessor) Process(ctx *processors.ProcessorContext, list interf PodCapacity: podCapacity, } - kubeSystemCreationTimestamp, err := getKubeSystemCreationTimeStamp(ctx.APIClient.Cl.CoreV1()) + kubeSystemCreationTimestamp, err := getKubeSystemCreationTimeStamp(pctx.APIClient.Cl.CoreV1()) if err != nil { return processResult, 0, fmt.Errorf("error getting server kube system creation timestamp: %s", err.Error()) } - apiVersion, err := ctx.APIClient.Cl.Discovery().ServerVersion() + apiVersion, err := pctx.APIClient.Cl.Discovery().ServerVersion() if err != nil { return processResult, 0, fmt.Errorf("error getting server apiVersion: %s", err.Error()) } @@ -112,7 +113,7 @@ func (p *ClusterProcessor) Process(ctx *processors.ProcessorContext, list interf return processResult, 0, fmt.Errorf("failed to compute resource version: %s", err.Error()) } - if orchestrator.SkipKubernetesResource(types.UID(ctx.ClusterID), clusterModel.ResourceVersion, orchestrator.K8sCluster) { + if orchestrator.SkipKubernetesResource(types.UID(pctx.ClusterID), clusterModel.ResourceVersion, orchestrator.K8sCluster) { stats := orchestrator.CheckStats{ CacheHits: 1, CacheMiss: 0, @@ -124,11 +125,11 @@ func (p *ClusterProcessor) Process(ctx *processors.ProcessorContext, list interf messages := []model.MessageBody{ &model.CollectorCluster{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, Cluster: clusterModel, - Tags: ctx.Cfg.ExtraTags, + Tags: pctx.Cfg.ExtraTags, }, } processResult = processors.ProcessResult{ diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go index 3fea2bfcf3cdeb..bc8bf4ee919095 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // ClusterRoleHandlers implements the Handlers interface for Kubernetes ClusterRoles. type ClusterRoleHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *ClusterRoleHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.ClusterRole) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *ClusterRoleHandlers) AfterMarshalling(ctx *processors.ProcessorContext, // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *ClusterRoleHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *ClusterRoleHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.ClusterRole, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,18 +44,18 @@ func (h *ClusterRoleHandlers) BuildMessageBody(ctx *processors.ProcessorContext, } return &model.CollectorClusterRole{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), ClusterRoles: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag)} + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag)} } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *ClusterRoleHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.ClusterRole) return k8sTransformers.ExtractClusterRole(r) } @@ -62,7 +64,7 @@ func (h *ClusterRoleHandlers) ExtractResource(ctx *processors.ProcessorContext, // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *ClusterRoleHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*rbacv1.ClusterRole) resources = make([]interface{}, 0, len(resourceList)) @@ -76,14 +78,14 @@ func (h *ClusterRoleHandlers) ResourceList(ctx *processors.ProcessorContext, lis // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *ClusterRoleHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*rbacv1.ClusterRole).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *ClusterRoleHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*rbacv1.ClusterRole).ResourceVersion } @@ -91,7 +93,7 @@ func (h *ClusterRoleHandlers) ResourceVersion(ctx *processors.ProcessorContext, // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ClusterRoleHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*rbacv1.ClusterRole) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go index e6b9736f93b981..e3652a9e614f3e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/clusterrolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // ClusterRoleBindingHandlers implements the Handlers interface for Kubernetes ClusteRoleBindings. type ClusterRoleBindingHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *ClusterRoleBindingHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.ClusterRoleBinding) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *ClusterRoleBindingHandlers) AfterMarshalling(ctx *processors.ProcessorC // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *ClusterRoleBindingHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *ClusterRoleBindingHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.ClusterRoleBinding, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *ClusterRoleBindingHandlers) BuildMessageBody(ctx *processors.ProcessorC } return &model.CollectorClusterRoleBinding{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), ClusterRoleBindings: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *ClusterRoleBindingHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.ClusterRoleBinding) return k8sTransformers.ExtractClusterRoleBinding(r) } @@ -63,7 +65,7 @@ func (h *ClusterRoleBindingHandlers) ExtractResource(ctx *processors.ProcessorCo // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *ClusterRoleBindingHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*rbacv1.ClusterRoleBinding) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *ClusterRoleBindingHandlers) ResourceList(ctx *processors.ProcessorConte // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *ClusterRoleBindingHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*rbacv1.ClusterRoleBinding).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *ClusterRoleBindingHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*rbacv1.ClusterRoleBinding).ResourceVersion } @@ -92,7 +94,7 @@ func (h *ClusterRoleBindingHandlers) ResourceVersion(ctx *processors.ProcessorCo // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ClusterRoleBindingHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ClusterRoleBindingHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*rbacv1.ClusterRoleBinding) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cr.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cr.go index 68d0b806981be2..5bd916592eb2b9 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cr.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cr.go @@ -8,24 +8,25 @@ package k8s import ( - model "github.com/DataDog/agent-payload/v5/process" - "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + model "github.com/DataDog/agent-payload/v5/process" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" - "k8s.io/apimachinery/pkg/types" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/common" + "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" ) // CRHandlers implements the Handlers interface for Kubernetes CronJobs. type CRHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (cr *CRHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { return } @@ -33,23 +34,24 @@ func (cr *CRHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resourc // extracted resources. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (cr *CRHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { return nil } //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) BuildManifestMessageBody(ctx *processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { - cm := ExtractModelManifests(ctx, resourceManifests, groupSize) +func (cr *CRHandlers) BuildManifestMessageBody(ctx processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) + cm := common.ExtractModelManifests(ctx, resourceManifests, groupSize) return &model.CollectorManifestCR{ Manifest: cm, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (cr *CRHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { return nil } @@ -57,7 +59,7 @@ func (cr *CRHandlers) ExtractResource(ctx *processors.ProcessorContext, resource // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (cr *CRHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]runtime.Object) resources = make([]interface{}, 0, len(resourceList)) @@ -71,14 +73,14 @@ func (cr *CRHandlers) ResourceList(ctx *processors.ProcessorContext, list interf // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (cr *CRHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*unstructured.Unstructured).GetUID() } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (cr *CRHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*unstructured.Unstructured).GetResourceVersion() } @@ -86,7 +88,7 @@ func (cr *CRHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (cr *CRHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (cr *CRHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*unstructured.Unstructured) annotations := r.GetAnnotations() redact.RemoveLastAppliedConfigurationAnnotation(annotations) diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/crd.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/crd.go index 73188d4ccdb790..3f36a60e0ec8e6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/crd.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/crd.go @@ -8,10 +8,12 @@ package k8s import ( - model "github.com/DataDog/agent-payload/v5/process" v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/runtime" + model "github.com/DataDog/agent-payload/v5/process" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/common" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" @@ -20,22 +22,23 @@ import ( // CRDHandlers implements the Handlers interface for Kubernetes CronJobs. type CRDHandlers struct { - BaseHandlers + common.BaseHandlers } // BuildManifestMessageBody builds the manifest payload body -func (crd *CRDHandlers) BuildManifestMessageBody(ctx *processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { - cm := ExtractModelManifests(ctx, resourceManifests, groupSize) +func (crd *CRDHandlers) BuildManifestMessageBody(ctx processors.ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) + cm := common.ExtractModelManifests(ctx, resourceManifests, groupSize) return &model.CollectorManifestCRD{ Manifest: cm, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (crd *CRDHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { return } @@ -43,14 +46,14 @@ func (crd *CRDHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resou // extracted resources. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (crd *CRDHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { return nil } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (crd *CRDHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { return } @@ -58,7 +61,7 @@ func (crd *CRDHandlers) ExtractResource(ctx *processors.ProcessorContext, resour // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (crd *CRDHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]runtime.Object) resources = make([]interface{}, 0, len(resourceList)) @@ -72,14 +75,14 @@ func (crd *CRDHandlers) ResourceList(ctx *processors.ProcessorContext, list inte // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (crd *CRDHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*v1.CustomResourceDefinition).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (crd *CRDHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*v1.CustomResourceDefinition).ResourceVersion } @@ -87,7 +90,7 @@ func (crd *CRDHandlers) ResourceVersion(ctx *processors.ProcessorContext, resour // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (crd *CRDHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (crd *CRDHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*v1.CustomResourceDefinition) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } 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 3d5e447a4e8c10..c7e1884361902c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1.go @@ -10,6 +10,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/processors/common" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" @@ -19,13 +20,13 @@ import ( // CronJobV1Handlers implements the Handlers interface for Kubernetes CronJobs. type CronJobV1Handlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *CronJobV1Handlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.CronJob) m.Yaml = yaml return @@ -33,7 +34,8 @@ func (h *CronJobV1Handlers) AfterMarshalling(ctx *processors.ProcessorContext, r // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *CronJobV1Handlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *CronJobV1Handlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.CronJob, 0, len(resourceModels)) for _, m := range resourceModels { @@ -41,19 +43,19 @@ func (h *CronJobV1Handlers) BuildMessageBody(ctx *processors.ProcessorContext, r } return &model.CollectorCronJob{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), CronJobs: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *CronJobV1Handlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1.CronJob) return k8sTransformers.ExtractCronJobV1(r) } @@ -62,7 +64,7 @@ func (h *CronJobV1Handlers) ExtractResource(ctx *processors.ProcessorContext, re // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *CronJobV1Handlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*batchv1.CronJob) resources = make([]interface{}, 0, len(resourceList)) @@ -76,14 +78,14 @@ func (h *CronJobV1Handlers) ResourceList(ctx *processors.ProcessorContext, list // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *CronJobV1Handlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*batchv1.CronJob).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *CronJobV1Handlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*batchv1.CronJob).ResourceVersion } @@ -91,16 +93,17 @@ func (h *CronJobV1Handlers) ResourceVersion(ctx *processors.ProcessorContext, re // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Handlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *CronJobV1Handlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*batchv1.CronJob) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *CronJobV1Handlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *CronJobV1Handlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*batchv1.CronJob) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.JobTemplate.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.JobTemplate.Spec.Template, pctx.Cfg.Scrubber) } } 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 9af47b6296614c..7a577c5ac72566 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/cronjob_v1beta1.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // CronJobV1Beta1Handlers implements the Handlers interface for Kubernetes CronJobs. type CronJobV1Beta1Handlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *CronJobV1Beta1Handlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.CronJob) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *CronJobV1Beta1Handlers) AfterMarshalling(ctx *processors.ProcessorConte // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *CronJobV1Beta1Handlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *CronJobV1Beta1Handlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.CronJob, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *CronJobV1Beta1Handlers) BuildMessageBody(ctx *processors.ProcessorConte } return &model.CollectorCronJob{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), CronJobs: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *CronJobV1Beta1Handlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1beta1.CronJob) return k8sTransformers.ExtractCronJobV1Beta1(r) } @@ -63,7 +65,7 @@ func (h *CronJobV1Beta1Handlers) ExtractResource(ctx *processors.ProcessorContex // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *CronJobV1Beta1Handlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*batchv1beta1.CronJob) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *CronJobV1Beta1Handlers) ResourceList(ctx *processors.ProcessorContext, // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *CronJobV1Beta1Handlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*batchv1beta1.CronJob).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *CronJobV1Beta1Handlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*batchv1beta1.CronJob).ResourceVersion } @@ -92,16 +94,17 @@ func (h *CronJobV1Beta1Handlers) ResourceVersion(ctx *processors.ProcessorContex // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *CronJobV1Beta1Handlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *CronJobV1Beta1Handlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*batchv1beta1.CronJob) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *CronJobV1Beta1Handlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *CronJobV1Beta1Handlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*batchv1beta1.CronJob) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.JobTemplate.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.JobTemplate.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go index 7c9c91cc677910..89636226157dfa 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/daemonset.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // DaemonSetHandlers implements the Handlers interface for Kubernetes DaemonSets. type DaemonSetHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *DaemonSetHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.DaemonSet) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *DaemonSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, r // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *DaemonSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *DaemonSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.DaemonSet, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *DaemonSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, r } return &model.CollectorDaemonSet{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), DaemonSets: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *DaemonSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.DaemonSet) return k8sTransformers.ExtractDaemonSet(r) } @@ -63,7 +65,7 @@ func (h *DaemonSetHandlers) ExtractResource(ctx *processors.ProcessorContext, re // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *DaemonSetHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*appsv1.DaemonSet) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *DaemonSetHandlers) ResourceList(ctx *processors.ProcessorContext, list // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *DaemonSetHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*appsv1.DaemonSet).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *DaemonSetHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*appsv1.DaemonSet).ResourceVersion } @@ -92,16 +94,17 @@ func (h *DaemonSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, re // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DaemonSetHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *DaemonSetHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*appsv1.DaemonSet) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *DaemonSetHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *DaemonSetHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*appsv1.DaemonSet) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go index 9c608d08a2eca7..cc0d06084db186 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/deployment.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // DeploymentHandlers implements the Handlers interface for Kubernetes Deployments. type DeploymentHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *DeploymentHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Deployment) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *DeploymentHandlers) AfterMarshalling(ctx *processors.ProcessorContext, // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *DeploymentHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *DeploymentHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Deployment, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *DeploymentHandlers) BuildMessageBody(ctx *processors.ProcessorContext, } return &model.CollectorDeployment{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Deployments: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *DeploymentHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.Deployment) return k8sTransformers.ExtractDeployment(r) } @@ -63,7 +65,7 @@ func (h *DeploymentHandlers) ExtractResource(ctx *processors.ProcessorContext, r // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *DeploymentHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*appsv1.Deployment) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *DeploymentHandlers) ResourceList(ctx *processors.ProcessorContext, list // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *DeploymentHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*appsv1.Deployment).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *DeploymentHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*appsv1.Deployment).ResourceVersion } @@ -92,16 +94,17 @@ func (h *DeploymentHandlers) ResourceVersion(ctx *processors.ProcessorContext, r // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *DeploymentHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *DeploymentHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*appsv1.Deployment) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *DeploymentHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *DeploymentHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*appsv1.Deployment) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go index c4e39af34320d8..2867553295a20c 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/horizontalpodautoscaler.go @@ -8,23 +8,25 @@ package k8s import ( + v2 "k8s.io/api/autoscaling/v2" + "k8s.io/apimachinery/pkg/types" + 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/processors/common" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" - v2 "k8s.io/api/autoscaling/v2" - "k8s.io/apimachinery/pkg/types" ) // HorizontalPodAutoscalerHandlers implements the Handlers interface for Kuberenetes HPAs type HorizontalPodAutoscalerHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *HorizontalPodAutoscalerHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.HorizontalPodAutoscaler) m.Yaml = yaml return @@ -32,7 +34,8 @@ func (h *HorizontalPodAutoscalerHandlers) AfterMarshalling(ctx *processors.Proce // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *HorizontalPodAutoscalerHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *HorizontalPodAutoscalerHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.HorizontalPodAutoscaler, 0, len(resourceModels)) for _, m := range resourceModels { @@ -40,19 +43,19 @@ func (h *HorizontalPodAutoscalerHandlers) BuildMessageBody(ctx *processors.Proce } return &model.CollectorHorizontalPodAutoscaler{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), HorizontalPodAutoscalers: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (horizontalPodAutoscalerModel interface{}) { +func (h *HorizontalPodAutoscalerHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (horizontalPodAutoscalerModel interface{}) { r := resource.(*v2.HorizontalPodAutoscaler) return k8sTransformers.ExtractHorizontalPodAutoscaler(r) } @@ -61,7 +64,7 @@ func (h *HorizontalPodAutoscalerHandlers) ExtractResource(ctx *processors.Proces // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *HorizontalPodAutoscalerHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*v2.HorizontalPodAutoscaler) resources = make([]interface{}, 0, len(resourceList)) @@ -75,14 +78,14 @@ func (h *HorizontalPodAutoscalerHandlers) ResourceList(ctx *processors.Processor // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *HorizontalPodAutoscalerHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*v2.HorizontalPodAutoscaler).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *HorizontalPodAutoscalerHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*v2.HorizontalPodAutoscaler).ResourceVersion } @@ -90,7 +93,7 @@ func (h *HorizontalPodAutoscalerHandlers) ResourceVersion(ctx *processors.Proces // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *HorizontalPodAutoscalerHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *HorizontalPodAutoscalerHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*v2.HorizontalPodAutoscaler) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go index 0b3e0a819ea128..db04f6cd354348 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/ingress.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // IngressHandlers implements the Handlers interface for Kubernetes Ingresss. type IngressHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *IngressHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Ingress) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *IngressHandlers) AfterMarshalling(ctx *processors.ProcessorContext, res // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *IngressHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *IngressHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Ingress, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *IngressHandlers) BuildMessageBody(ctx *processors.ProcessorContext, res } return &model.CollectorIngress{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Ingresses: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *IngressHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*netv1.Ingress) return k8sTransformers.ExtractIngress(r) } @@ -63,7 +65,7 @@ func (h *IngressHandlers) ExtractResource(ctx *processors.ProcessorContext, reso // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *IngressHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*netv1.Ingress) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *IngressHandlers) ResourceList(ctx *processors.ProcessorContext, list in // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *IngressHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*netv1.Ingress).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *IngressHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*netv1.Ingress).ResourceVersion } @@ -92,7 +94,7 @@ func (h *IngressHandlers) ResourceVersion(ctx *processors.ProcessorContext, reso // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *IngressHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*netv1.Ingress) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } @@ -101,5 +103,5 @@ func (h *IngressHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext // it is marshalled to generate a manifest. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *IngressHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *IngressHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go index 5ad581e1535dc3..e4e853727435bb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/job.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // JobHandlers implements the Handlers interface for Kubernetes Jobs. type JobHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *JobHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Job) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *JobHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resourc // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *JobHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *JobHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Job, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *JobHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourc } return &model.CollectorJob{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Jobs: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *JobHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*batchv1.Job) return k8sTransformers.ExtractJob(r) } @@ -63,7 +65,7 @@ func (h *JobHandlers) ExtractResource(ctx *processors.ProcessorContext, resource // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *JobHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*batchv1.Job) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *JobHandlers) ResourceList(ctx *processors.ProcessorContext, list interf // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *JobHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*batchv1.Job).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *JobHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*batchv1.Job).ResourceVersion } @@ -92,16 +94,17 @@ func (h *JobHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *JobHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *JobHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*batchv1.Job) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *JobHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *JobHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*batchv1.Job) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go index 18e0b68b45f5da..954de086113c7e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/namespace.go @@ -8,9 +8,11 @@ package k8s import ( - model "github.com/DataDog/agent-payload/v5/process" 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/common" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" @@ -20,13 +22,13 @@ import ( // NamespaceHandlers implements the Handlers interface for Kubernetes Namespace. type NamespaceHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *NamespaceHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Namespace) m.Yaml = yaml return @@ -34,7 +36,8 @@ func (h *NamespaceHandlers) AfterMarshalling(ctx *processors.ProcessorContext, r // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *NamespaceHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *NamespaceHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Namespace, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +45,19 @@ func (h *NamespaceHandlers) BuildMessageBody(ctx *processors.ProcessorContext, r } return &model.CollectorNamespace{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Namespaces: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (namespaceModel interface{}) { +func (h *NamespaceHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (namespaceModel interface{}) { r := resource.(*corev1.Namespace) return k8sTransformers.ExtractNamespace(r) } @@ -63,7 +66,7 @@ func (h *NamespaceHandlers) ExtractResource(ctx *processors.ProcessorContext, re // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *NamespaceHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.Namespace) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +80,14 @@ func (h *NamespaceHandlers) ResourceList(ctx *processors.ProcessorContext, list // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *NamespaceHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.Namespace).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *NamespaceHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.Namespace).ResourceVersion } @@ -92,7 +95,7 @@ func (h *NamespaceHandlers) ResourceVersion(ctx *processors.ProcessorContext, re // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NamespaceHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *NamespaceHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.Namespace) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go index 57ea9f69255ffa..bd04c29048162b 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/networkpolicy.go @@ -8,25 +8,25 @@ package k8s import ( - model "github.com/DataDog/agent-payload/v5/process" + netv1 "k8s.io/api/networking/v1" + "k8s.io/apimachinery/pkg/types" + 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/processors/common" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" - - netv1 "k8s.io/api/networking/v1" - "k8s.io/apimachinery/pkg/types" ) // NetworkPolicyHandlers implements the Handlers interface for Kubernetes NetworkPolicy. type NetworkPolicyHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *NetworkPolicyHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.NetworkPolicy) m.Yaml = yaml return @@ -34,7 +34,8 @@ func (h *NetworkPolicyHandlers) AfterMarshalling(ctx *processors.ProcessorContex // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *NetworkPolicyHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *NetworkPolicyHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.NetworkPolicy, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +43,19 @@ func (h *NetworkPolicyHandlers) BuildMessageBody(ctx *processors.ProcessorContex } return &model.CollectorNetworkPolicy{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), NetworkPolicies: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *NetworkPolicyHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*netv1.NetworkPolicy) return k8sTransformers.ExtractNetworkPolicy(r) } @@ -63,7 +64,7 @@ func (h *NetworkPolicyHandlers) ExtractResource(ctx *processors.ProcessorContext // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *NetworkPolicyHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*netv1.NetworkPolicy) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +78,14 @@ func (h *NetworkPolicyHandlers) ResourceList(ctx *processors.ProcessorContext, l // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *NetworkPolicyHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*netv1.NetworkPolicy).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *NetworkPolicyHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*netv1.NetworkPolicy).ResourceVersion } @@ -92,7 +93,7 @@ func (h *NetworkPolicyHandlers) ResourceVersion(ctx *processors.ProcessorContext // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *NetworkPolicyHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*netv1.NetworkPolicy) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } @@ -101,5 +102,5 @@ func (h *NetworkPolicyHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorC // it is marshalled to generate a manifest. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NetworkPolicyHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *NetworkPolicyHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go index 9e9ad63710e9d3..bf441ae78acc8f 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/node.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // NodeHandlers implements the Handlers interface for Kubernetes Nodes. type NodeHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *NodeHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Node) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *NodeHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resour // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *NodeHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *NodeHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Node, 0, len(resourceModels)) for _, r := range resourceModels { @@ -42,19 +44,19 @@ func (h *NodeHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resour } return &model.CollectorNode{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Nodes: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *NodeHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Node) return k8sTransformers.ExtractNode(r) } @@ -63,7 +65,7 @@ func (h *NodeHandlers) ExtractResource(ctx *processors.ProcessorContext, resourc // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *NodeHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.Node) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *NodeHandlers) ResourceList(ctx *processors.ProcessorContext, list inter // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *NodeHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.Node).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *NodeHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.Node).ResourceVersion } @@ -92,7 +94,7 @@ func (h *NodeHandlers) ResourceVersion(ctx *processors.ProcessorContext, resourc // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *NodeHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.Node) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } @@ -101,5 +103,5 @@ func (h *NodeHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, r // it is marshalled to generate a manifest. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *NodeHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *NodeHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go index 3a1bec1728912d..db4219057a5029 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolume.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // PersistentVolumeHandlers implements the Handlers interface for Kubernetes PersistentVolumes. type PersistentVolumeHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *PersistentVolumeHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.PersistentVolume) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *PersistentVolumeHandlers) AfterMarshalling(ctx *processors.ProcessorCon // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *PersistentVolumeHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *PersistentVolumeHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.PersistentVolume, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *PersistentVolumeHandlers) BuildMessageBody(ctx *processors.ProcessorCon } return &model.CollectorPersistentVolume{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), PersistentVolumes: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *PersistentVolumeHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.PersistentVolume) return k8sTransformers.ExtractPersistentVolume(r) } @@ -63,7 +65,7 @@ func (h *PersistentVolumeHandlers) ExtractResource(ctx *processors.ProcessorCont // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *PersistentVolumeHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.PersistentVolume) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *PersistentVolumeHandlers) ResourceList(ctx *processors.ProcessorContext // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *PersistentVolumeHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.PersistentVolume).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *PersistentVolumeHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.PersistentVolume).ResourceVersion } @@ -92,7 +94,7 @@ func (h *PersistentVolumeHandlers) ResourceVersion(ctx *processors.ProcessorCont // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *PersistentVolumeHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.PersistentVolume) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go index a3521c1ded1c55..40f95b02c867a7 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/persistentvolumeclaim.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // PersistentVolumeClaimHandlers implements the Handlers interface for Kubernetes PersistentVolumeClaims. type PersistentVolumeClaimHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *PersistentVolumeClaimHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.PersistentVolumeClaim) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *PersistentVolumeClaimHandlers) AfterMarshalling(ctx *processors.Process // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *PersistentVolumeClaimHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *PersistentVolumeClaimHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.PersistentVolumeClaim, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *PersistentVolumeClaimHandlers) BuildMessageBody(ctx *processors.Process } return &model.CollectorPersistentVolumeClaim{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), PersistentVolumeClaims: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *PersistentVolumeClaimHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.PersistentVolumeClaim) return k8sTransformers.ExtractPersistentVolumeClaim(r) } @@ -63,7 +65,7 @@ func (h *PersistentVolumeClaimHandlers) ExtractResource(ctx *processors.Processo // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *PersistentVolumeClaimHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.PersistentVolumeClaim) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *PersistentVolumeClaimHandlers) ResourceList(ctx *processors.ProcessorCo // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *PersistentVolumeClaimHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.PersistentVolumeClaim).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *PersistentVolumeClaimHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.PersistentVolumeClaim).ResourceVersion } @@ -92,7 +94,7 @@ func (h *PersistentVolumeClaimHandlers) ResourceVersion(ctx *processors.Processo // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PersistentVolumeClaimHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *PersistentVolumeClaimHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.PersistentVolumeClaim) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go index 3dc25b4c527117..cb4ee4e5d9b6b3 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/pod.go @@ -16,6 +16,7 @@ import ( "github.com/DataDog/datadog-agent/comp/core/tagger/collectors" kubetypes "github.com/DataDog/datadog-agent/internal/third_party/kubernetes/pkg/kubelet/types" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/common" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" @@ -29,27 +30,28 @@ import ( // PodHandlers implements the Handlers interface for Kubernetes Pods. type PodHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *PodHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Pod) m.Yaml = yaml return } // BeforeCacheCheck is a handler called before cache lookup. -func (h *PodHandlers) BeforeCacheCheck(ctx *processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { +func (h *PodHandlers) BeforeCacheCheck(ctx processors.ProcessorContext, resource, resourceModel interface{}) (skip bool) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*corev1.Pod) m := resourceModel.(*model.Pod) // static pods "uid" are actually not unique across nodes. // we differ from the k8 uuid format in purpose to differentiate those static pods. if kubetypes.IsStaticPod(r) { - newUID := k8sTransformers.GenerateUniqueK8sStaticPodHash(ctx.HostName, r.Name, r.Namespace, ctx.Cfg.KubeClusterName) + newUID := k8sTransformers.GenerateUniqueK8sStaticPodHash(pctx.HostName, r.Name, r.Namespace, pctx.Cfg.KubeClusterName) // modify it in the original pod for the YAML and in our model r.UID = types.UID(newUID) m.Metadata.Uid = newUID @@ -91,7 +93,8 @@ func (h *PodHandlers) BeforeCacheCheck(ctx *processors.ProcessorContext, resourc // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *PodHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *PodHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Pod, 0, len(resourceModels)) for _, m := range resourceModels { @@ -99,20 +102,20 @@ func (h *PodHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourc } return &model.CollectorPod{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), - HostName: ctx.HostName, + HostName: pctx.HostName, Pods: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *PodHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Pod) return k8sTransformers.ExtractPod(r) } @@ -121,7 +124,7 @@ func (h *PodHandlers) ExtractResource(ctx *processors.ProcessorContext, resource // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *PodHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.Pod) resources = make([]interface{}, 0, len(resourceList)) @@ -135,14 +138,14 @@ func (h *PodHandlers) ResourceList(ctx *processors.ProcessorContext, list interf // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *PodHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.Pod).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *PodHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resourceModel.(*model.Pod).Metadata.ResourceVersion } @@ -150,7 +153,7 @@ func (h *PodHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *PodHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.Pod) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } @@ -159,9 +162,10 @@ func (h *PodHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, re // it is marshalled to generate a manifest. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *PodHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *PodHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*corev1.Pod) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPod(r, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPod(r, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go index aef7525a44d70a..2ca9b98ae77b0e 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/replicaset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // ReplicaSetHandlers implements the Handlers interface for Kubernetes ReplicaSets. type ReplicaSetHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *ReplicaSetHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.ReplicaSet) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *ReplicaSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *ReplicaSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *ReplicaSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.ReplicaSet, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *ReplicaSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, } return &model.CollectorReplicaSet{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), ReplicaSets: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *ReplicaSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.ReplicaSet) return k8sTransformers.ExtractReplicaSet(r) } @@ -63,7 +65,7 @@ func (h *ReplicaSetHandlers) ExtractResource(ctx *processors.ProcessorContext, r // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *ReplicaSetHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*appsv1.ReplicaSet) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *ReplicaSetHandlers) ResourceList(ctx *processors.ProcessorContext, list // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *ReplicaSetHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*appsv1.ReplicaSet).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *ReplicaSetHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*appsv1.ReplicaSet).ResourceVersion } @@ -92,16 +94,17 @@ func (h *ReplicaSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, r // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ReplicaSetHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ReplicaSetHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*appsv1.ReplicaSet) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *ReplicaSetHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ReplicaSetHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*appsv1.ReplicaSet) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go index 98ee528ffaadb5..e015011ddc4472 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/role.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // RoleHandlers implements the Handlers interface for Kubernetes Roles. type RoleHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *RoleHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Role) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *RoleHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resour // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *RoleHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *RoleHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Role, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *RoleHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resour } return &model.CollectorRole{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Roles: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *RoleHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.Role) return k8sTransformers.ExtractRole(r) } @@ -63,7 +65,7 @@ func (h *RoleHandlers) ExtractResource(ctx *processors.ProcessorContext, resourc // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *RoleHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*rbacv1.Role) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *RoleHandlers) ResourceList(ctx *processors.ProcessorContext, list inter // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *RoleHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*rbacv1.Role).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *RoleHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*rbacv1.Role).ResourceVersion } @@ -92,7 +94,7 @@ func (h *RoleHandlers) ResourceVersion(ctx *processors.ProcessorContext, resourc // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *RoleHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*rbacv1.Role) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go index 9d7377d53e36d9..cefac2d97d3bcf 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/rolebinding.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // RoleBindingHandlers implements the Handlers interface for Kubernetes RoleBindings. type RoleBindingHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *RoleBindingHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.RoleBinding) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *RoleBindingHandlers) AfterMarshalling(ctx *processors.ProcessorContext, // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *RoleBindingHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *RoleBindingHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.RoleBinding, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,18 +44,18 @@ func (h *RoleBindingHandlers) BuildMessageBody(ctx *processors.ProcessorContext, } return &model.CollectorRoleBinding{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), RoleBindings: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag)} + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag)} } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *RoleBindingHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*rbacv1.RoleBinding) return k8sTransformers.ExtractRoleBinding(r) } @@ -62,7 +64,7 @@ func (h *RoleBindingHandlers) ExtractResource(ctx *processors.ProcessorContext, // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *RoleBindingHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*rbacv1.RoleBinding) resources = make([]interface{}, 0, len(resourceList)) @@ -76,14 +78,14 @@ func (h *RoleBindingHandlers) ResourceList(ctx *processors.ProcessorContext, lis // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *RoleBindingHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*rbacv1.RoleBinding).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *RoleBindingHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*rbacv1.RoleBinding).ResourceVersion } @@ -91,7 +93,7 @@ func (h *RoleBindingHandlers) ResourceVersion(ctx *processors.ProcessorContext, // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *RoleBindingHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *RoleBindingHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*rbacv1.RoleBinding) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go index 85f88f5cac663d..2196faa93475ed 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/service.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // ServiceHandlers implements the Handlers interface for Kubernetes Services. type ServiceHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *ServiceHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.Service) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *ServiceHandlers) AfterMarshalling(ctx *processors.ProcessorContext, res // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *ServiceHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *ServiceHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.Service, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *ServiceHandlers) BuildMessageBody(ctx *processors.ProcessorContext, res } return &model.CollectorService{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), Services: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *ServiceHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.Service) return k8sTransformers.ExtractService(r) } @@ -63,7 +65,7 @@ func (h *ServiceHandlers) ExtractResource(ctx *processors.ProcessorContext, reso // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *ServiceHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.Service) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *ServiceHandlers) ResourceList(ctx *processors.ProcessorContext, list in // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *ServiceHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.Service).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *ServiceHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.Service).ResourceVersion } @@ -92,7 +94,7 @@ func (h *ServiceHandlers) ResourceVersion(ctx *processors.ProcessorContext, reso // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ServiceHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.Service) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go index c3602ae1dc0926..5f1d2927de7edb 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/serviceaccount.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // ServiceAccountHandlers implements the Handlers interface for Kubernetes ServiceAccounts. type ServiceAccountHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *ServiceAccountHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.ServiceAccount) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *ServiceAccountHandlers) AfterMarshalling(ctx *processors.ProcessorConte // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *ServiceAccountHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *ServiceAccountHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.ServiceAccount, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,19 +44,19 @@ func (h *ServiceAccountHandlers) BuildMessageBody(ctx *processors.ProcessorConte } return &model.CollectorServiceAccount{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), ServiceAccounts: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *ServiceAccountHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*corev1.ServiceAccount) return k8sTransformers.ExtractServiceAccount(r) } @@ -63,7 +65,7 @@ func (h *ServiceAccountHandlers) ExtractResource(ctx *processors.ProcessorContex // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *ServiceAccountHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*corev1.ServiceAccount) resources = make([]interface{}, 0, len(resourceList)) @@ -77,14 +79,14 @@ func (h *ServiceAccountHandlers) ResourceList(ctx *processors.ProcessorContext, // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *ServiceAccountHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*corev1.ServiceAccount).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *ServiceAccountHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*corev1.ServiceAccount).ResourceVersion } @@ -92,7 +94,7 @@ func (h *ServiceAccountHandlers) ResourceVersion(ctx *processors.ProcessorContex // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *ServiceAccountHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *ServiceAccountHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*corev1.ServiceAccount) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go index 55d94bea846700..49cc4846d98351 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/statefulset.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/common" "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" @@ -20,13 +21,13 @@ import ( // StatefulSetHandlers implements the Handlers interface for Kubernetes StatefulSets. type StatefulSetHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *StatefulSetHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.StatefulSet) m.Yaml = yaml return @@ -34,7 +35,8 @@ func (h *StatefulSetHandlers) AfterMarshalling(ctx *processors.ProcessorContext, // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *StatefulSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *StatefulSetHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.StatefulSet, 0, len(resourceModels)) for _, m := range resourceModels { @@ -42,18 +44,18 @@ func (h *StatefulSetHandlers) BuildMessageBody(ctx *processors.ProcessorContext, } return &model.CollectorStatefulSet{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), StatefulSets: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag)} + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag)} } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { +func (h *StatefulSetHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (resourceModel interface{}) { r := resource.(*appsv1.StatefulSet) return k8sTransformers.ExtractStatefulSet(r) } @@ -62,7 +64,7 @@ func (h *StatefulSetHandlers) ExtractResource(ctx *processors.ProcessorContext, // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *StatefulSetHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*appsv1.StatefulSet) resources = make([]interface{}, 0, len(resourceList)) @@ -76,14 +78,14 @@ func (h *StatefulSetHandlers) ResourceList(ctx *processors.ProcessorContext, lis // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *StatefulSetHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*appsv1.StatefulSet).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *StatefulSetHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*appsv1.StatefulSet).ResourceVersion } @@ -91,16 +93,17 @@ func (h *StatefulSetHandlers) ResourceVersion(ctx *processors.ProcessorContext, // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *StatefulSetHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *StatefulSetHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*appsv1.StatefulSet) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } // ScrubBeforeMarshalling is a handler called to redact the raw resource before // it is marshalled to generate a manifest. -func (h *StatefulSetHandlers) ScrubBeforeMarshalling(ctx *processors.ProcessorContext, resource interface{}) { +func (h *StatefulSetHandlers) ScrubBeforeMarshalling(ctx processors.ProcessorContext, resource interface{}) { + pctx := ctx.(*processors.K8sProcessorContext) r := resource.(*appsv1.StatefulSet) - if ctx.Cfg.IsScrubbingEnabled { - redact.ScrubPodTemplateSpec(&r.Spec.Template, ctx.Cfg.Scrubber) + if pctx.Cfg.IsScrubbingEnabled { + redact.ScrubPodTemplateSpec(&r.Spec.Template, pctx.Cfg.Scrubber) } } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go index a9142eab780e92..349ff307cce944 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/k8s/verticalpodautoscaler.go @@ -8,23 +8,25 @@ package k8s import ( + "k8s.io/apimachinery/pkg/types" + 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" + "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/processors/common" k8sTransformers "github.com/DataDog/datadog-agent/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s" "github.com/DataDog/datadog-agent/pkg/orchestrator/redact" - "k8s.io/apimachinery/pkg/types" - v1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" ) // VerticalPodAutoscalerHandlers implements the Handlers interface for Kuberenetes VPAs type VerticalPodAutoscalerHandlers struct { - BaseHandlers + common.BaseHandlers } // AfterMarshalling is a handler called after resource marshalling. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) AfterMarshalling(ctx *processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { +func (h *VerticalPodAutoscalerHandlers) AfterMarshalling(ctx processors.ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) { m := resourceModel.(*model.VerticalPodAutoscaler) m.Yaml = yaml return @@ -32,7 +34,8 @@ func (h *VerticalPodAutoscalerHandlers) AfterMarshalling(ctx *processors.Process // BuildMessageBody is a handler called to build a message body out of a list of // extracted resources. -func (h *VerticalPodAutoscalerHandlers) BuildMessageBody(ctx *processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { +func (h *VerticalPodAutoscalerHandlers) BuildMessageBody(ctx processors.ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody { + pctx := ctx.(*processors.K8sProcessorContext) models := make([]*model.VerticalPodAutoscaler, 0, len(resourceModels)) for _, m := range resourceModels { @@ -40,19 +43,19 @@ func (h *VerticalPodAutoscalerHandlers) BuildMessageBody(ctx *processors.Process } return &model.CollectorVerticalPodAutoscaler{ - ClusterName: ctx.Cfg.KubeClusterName, - ClusterId: ctx.ClusterID, - GroupId: ctx.MsgGroupID, + ClusterName: pctx.Cfg.KubeClusterName, + ClusterId: pctx.ClusterID, + GroupId: pctx.MsgGroupID, GroupSize: int32(groupSize), VerticalPodAutoscalers: models, - Tags: append(ctx.Cfg.ExtraTags, ctx.ApiGroupVersionTag), + Tags: append(pctx.Cfg.ExtraTags, pctx.ApiGroupVersionTag), } } // ExtractResource is a handler called to extract the resource model out of a raw resource. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) ExtractResource(ctx *processors.ProcessorContext, resource interface{}) (verticalPodAutoscalerModel interface{}) { +func (h *VerticalPodAutoscalerHandlers) ExtractResource(ctx processors.ProcessorContext, resource interface{}) (verticalPodAutoscalerModel interface{}) { r := resource.(*v1.VerticalPodAutoscaler) return k8sTransformers.ExtractVerticalPodAutoscaler(r) } @@ -61,7 +64,7 @@ func (h *VerticalPodAutoscalerHandlers) ExtractResource(ctx *processors.Processo // interface to a list of generic interfaces. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) ResourceList(ctx *processors.ProcessorContext, list interface{}) (resources []interface{}) { +func (h *VerticalPodAutoscalerHandlers) ResourceList(ctx processors.ProcessorContext, list interface{}) (resources []interface{}) { resourceList := list.([]*v1.VerticalPodAutoscaler) resources = make([]interface{}, 0, len(resourceList)) @@ -75,14 +78,14 @@ func (h *VerticalPodAutoscalerHandlers) ResourceList(ctx *processors.ProcessorCo // ResourceUID is a handler called to retrieve the resource UID. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) ResourceUID(ctx *processors.ProcessorContext, resource interface{}) types.UID { +func (h *VerticalPodAutoscalerHandlers) ResourceUID(ctx processors.ProcessorContext, resource interface{}) types.UID { return resource.(*v1.VerticalPodAutoscaler).UID } // ResourceVersion is a handler called to retrieve the resource version. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) ResourceVersion(ctx *processors.ProcessorContext, resource, resourceModel interface{}) string { +func (h *VerticalPodAutoscalerHandlers) ResourceVersion(ctx processors.ProcessorContext, resource, resourceModel interface{}) string { return resource.(*v1.VerticalPodAutoscaler).ResourceVersion } @@ -90,7 +93,7 @@ func (h *VerticalPodAutoscalerHandlers) ResourceVersion(ctx *processors.Processo // it is extracted as an internal resource model. // //nolint:revive // TODO(CAPP) Fix revive linter -func (h *VerticalPodAutoscalerHandlers) ScrubBeforeExtraction(ctx *processors.ProcessorContext, resource interface{}) { +func (h *VerticalPodAutoscalerHandlers) ScrubBeforeExtraction(ctx processors.ProcessorContext, resource interface{}) { r := resource.(*v1.VerticalPodAutoscaler) redact.RemoveLastAppliedConfigurationAnnotation(r.Annotations) } diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go b/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go index 01bc203e71dbfd..c2147f3a9bddc6 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/processor.go @@ -8,10 +8,10 @@ package processors import ( - model "github.com/DataDog/agent-payload/v5/process" jsoniter "github.com/json-iterator/go" "k8s.io/apimachinery/pkg/types" + model "github.com/DataDog/agent-payload/v5/process" "github.com/DataDog/datadog-agent/pkg/orchestrator" "github.com/DataDog/datadog-agent/pkg/orchestrator/config" pkgorchestratormodel "github.com/DataDog/datadog-agent/pkg/orchestrator/model" @@ -24,18 +24,65 @@ import ( // the standard library does. var json = jsoniter.ConfigCompatibleWithStandardLibrary -// ProcessorContext holds resource processing attributes -type ProcessorContext struct { - APIClient *apiserver.APIClient - Cfg *config.OrchestratorConfig - ClusterID string - HostName string - MsgGroupID int32 - NodeType pkgorchestratormodel.NodeType +// ProcessorContext implements context for processor +type ProcessorContext interface { + GetOrchestratorConfig() *config.OrchestratorConfig + GetNodeType() pkgorchestratormodel.NodeType + GetMsgGroupID() int32 + IsManifestProducer() bool +} + +// BaseProcessorContext is the base context for all processors +type BaseProcessorContext struct { + Cfg *config.OrchestratorConfig + NodeType pkgorchestratormodel.NodeType + MsgGroupID int32 + ClusterID string + ManifestProducer bool +} + +// GetOrchestratorConfig returns the orchestrator config +func (c *BaseProcessorContext) GetOrchestratorConfig() *config.OrchestratorConfig { + return c.Cfg +} + +// GetNodeType returns the node type +func (c *BaseProcessorContext) GetNodeType() pkgorchestratormodel.NodeType { + return c.NodeType +} + +// GetMsgGroupID returns the message group ID +func (c *BaseProcessorContext) GetMsgGroupID() int32 { + return c.MsgGroupID +} + +// GetClusterID returns the cluster ID +func (c *BaseProcessorContext) GetClusterID() string { + return c.ClusterID +} + +// IsManifestProducer returns true if the collector is a manifest producer +func (c *BaseProcessorContext) IsManifestProducer() bool { + return c.ManifestProducer +} + +// K8sProcessorContext holds k8s resource processing attributes +type K8sProcessorContext struct { + BaseProcessorContext + APIClient *apiserver.APIClient + HostName string //nolint:revive // TODO(CAPP) Fix revive linter ApiGroupVersionTag string } +// ECSProcessorContext holds ECS resource processing attributes +type ECSProcessorContext struct { + BaseProcessorContext + AWSAccountID int + ClusterName string + Region string +} + // Handlers is the interface that is to be implemented for every resource type // and provides a way to plug in code at different levels of the Processor // logic. @@ -43,48 +90,48 @@ type Handlers interface { // AfterMarshalling runs before the Processor marshals the resource to // generate a manifest. If skip is true then the resource processing loop // moves on to the next resource. - AfterMarshalling(ctx *ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) + AfterMarshalling(ctx ProcessorContext, resource, resourceModel interface{}, yaml []byte) (skip bool) // BeforeCacheCheck runs before the Processor does a cache lookup for the // resource. If skip is true then the resource processing loop moves on to // the next resource. - BeforeCacheCheck(ctx *ProcessorContext, resource, resourceModel interface{}) (skip bool) + BeforeCacheCheck(ctx ProcessorContext, resource, resourceModel interface{}) (skip bool) // BeforeMarshalling runs before the Processor marshals the resource to // generate a manifest. If skip is true then the resource processing loop // moves on to the next resource. - BeforeMarshalling(ctx *ProcessorContext, resource, resourceModel interface{}) (skip bool) + BeforeMarshalling(ctx ProcessorContext, resource, resourceModel interface{}) (skip bool) // BuildMessageBody is used to build a message containing a chunk of // resource models of a certain size. If skip is true then the resource // processing loop moves on to the next resource. - BuildMessageBody(ctx *ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody + BuildMessageBody(ctx ProcessorContext, resourceModels []interface{}, groupSize int) model.MessageBody // BuildManifestMessageBody is used to build a message containing a chunk of // resource manifests of a certain size. - BuildManifestMessageBody(ctx *ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody + BuildManifestMessageBody(ctx ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody // ExtractResource is used to build a resource model from the raw // resource representation. - ExtractResource(ctx *ProcessorContext, resource interface{}) (resourceModel interface{}) + ExtractResource(ctx ProcessorContext, resource interface{}) (resourceModel interface{}) // ResourceList is used to convert a list of raw resources to a generic list // that can be used throughout a Processor. - ResourceList(ctx *ProcessorContext, list interface{}) (resources []interface{}) + ResourceList(ctx ProcessorContext, list interface{}) (resources []interface{}) // ResourceUID returns the resource UID. - ResourceUID(ctx *ProcessorContext, resource interface{}) types.UID + ResourceUID(ctx ProcessorContext, resource interface{}) types.UID // ResourceVersion returns the resource Version. - ResourceVersion(ctx *ProcessorContext, resource, resourceModel interface{}) string + ResourceVersion(ctx ProcessorContext, resource, resourceModel interface{}) string // ScrubBeforeExtraction replaces sensitive information in the resource // before resource extraction. - ScrubBeforeExtraction(ctx *ProcessorContext, resource interface{}) + ScrubBeforeExtraction(ctx ProcessorContext, resource interface{}) // ScrubBeforeMarshalling replaces sensitive information in the resource // before resource marshalling. - ScrubBeforeMarshalling(ctx *ProcessorContext, resource interface{}) + ScrubBeforeMarshalling(ctx ProcessorContext, resource interface{}) } // Processor is a generic resource processing component. It relies on a set of @@ -110,8 +157,13 @@ func NewProcessor(h Handlers) *Processor { } } +// Handlers returns handlers used by the processor. +func (p *Processor) Handlers() Handlers { + return p.h +} + // Process is used to process a list of resources of a certain type. -func (p *Processor) Process(ctx *ProcessorContext, list interface{}) (processResult ProcessResult, processed int) { +func (p *Processor) Process(ctx ProcessorContext, list interface{}) (processResult ProcessResult, processed int) { // This default allows detection of panic recoveries. processed = -1 @@ -138,7 +190,7 @@ func (p *Processor) Process(ctx *ProcessorContext, list interface{}) (processRes resourceUID := p.h.ResourceUID(ctx, resource) resourceVersion := p.h.ResourceVersion(ctx, resource, resourceMetadataModel) - if orchestrator.SkipKubernetesResource(resourceUID, resourceVersion, ctx.NodeType) { + if orchestrator.SkipKubernetesResource(resourceUID, resourceVersion, ctx.GetNodeType()) { continue } @@ -158,7 +210,7 @@ func (p *Processor) Process(ctx *ProcessorContext, list interface{}) (processRes } // Stop sending yaml if manifest collecion is enabled - if !ctx.Cfg.IsManifestCollectionEnabled { + if !ctx.GetOrchestratorConfig().IsManifestCollectionEnabled { // Execute code after marshalling. if skip := p.h.AfterMarshalling(ctx, resource, resourceMetadataModel, yaml); skip { continue @@ -169,7 +221,7 @@ func (p *Processor) Process(ctx *ProcessorContext, list interface{}) (processRes // Add resource manifest resourceManifestModels = append(resourceManifestModels, &model.Manifest{ - Type: int32(ctx.NodeType), + Type: int32(ctx.GetNodeType()), Uid: string(resourceUID), ResourceVersion: resourceVersion, Content: yaml, @@ -180,17 +232,19 @@ func (p *Processor) Process(ctx *ProcessorContext, list interface{}) (processRes processResult = ProcessResult{ MetadataMessages: ChunkMetadata(ctx, p, resourceMetadataModels, resourceManifestModels), - ManifestMessages: ChunkManifest(ctx, p.h.BuildManifestMessageBody, resourceManifestModels), + } + if ctx.IsManifestProducer() { + processResult.ManifestMessages = ChunkManifest(ctx, p.h.BuildManifestMessageBody, resourceManifestModels) } return processResult, len(resourceMetadataModels) } // ChunkManifest is to chunk Manifest payloads -func ChunkManifest(ctx *ProcessorContext, buildManifestBody func(ctx *ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody, resourceManifestModels []interface{}) []model.MessageBody { +func ChunkManifest(ctx ProcessorContext, buildManifestBody func(ctx ProcessorContext, resourceManifests []interface{}, groupSize int) model.MessageBody, resourceManifestModels []interface{}) []model.MessageBody { // Chunking resources based on the serialized size of their manifest and maximum messages number // Chunk manifest messages and use itself as weight indicator - chunks := chunkOrchestratorPayloadsBySizeAndWeight(resourceManifestModels, resourceManifestModels, ctx.Cfg.MaxPerMessage, ctx.Cfg.MaxWeightPerMessageBytes) + chunks := chunkOrchestratorPayloadsBySizeAndWeight(resourceManifestModels, resourceManifestModels, ctx.GetOrchestratorConfig().MaxPerMessage, ctx.GetOrchestratorConfig().MaxWeightPerMessageBytes) chunkCount := len(chunks) manifestMessages := make([]model.MessageBody, 0, chunkCount) @@ -203,10 +257,10 @@ func ChunkManifest(ctx *ProcessorContext, buildManifestBody func(ctx *ProcessorC } // ChunkMetadata is to chunk Metadata payloads -func ChunkMetadata(ctx *ProcessorContext, p *Processor, resourceMetadataModels, resourceManifestModels []interface{}) []model.MessageBody { +func ChunkMetadata(ctx ProcessorContext, p *Processor, resourceMetadataModels, resourceManifestModels []interface{}) []model.MessageBody { // Chunking resources based on the serialized size of their manifest and maximum messages number // Chunk metadata messages and use resourceManifestModels as weight indicator - chunks := chunkOrchestratorPayloadsBySizeAndWeight(resourceMetadataModels, resourceManifestModels, ctx.Cfg.MaxPerMessage, ctx.Cfg.MaxWeightPerMessageBytes) + chunks := chunkOrchestratorPayloadsBySizeAndWeight(resourceMetadataModels, resourceManifestModels, ctx.GetOrchestratorConfig().MaxPerMessage, ctx.GetOrchestratorConfig().MaxWeightPerMessageBytes) chunkCount := len(chunks) metadataMessages := make([]model.MessageBody, 0, chunkCount) diff --git a/pkg/collector/corechecks/cluster/orchestrator/processors/processor_test.go b/pkg/collector/corechecks/cluster/orchestrator/processors/processor_test.go index 4955a6c26175ac..9afc4a1eddfa48 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/processors/processor_test.go +++ b/pkg/collector/corechecks/cluster/orchestrator/processors/processor_test.go @@ -11,10 +11,10 @@ import ( "testing" "github.com/stretchr/testify/assert" - - model "github.com/DataDog/agent-payload/v5/process" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + model "github.com/DataDog/agent-payload/v5/process" ) type Item struct { diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go index 4e506452ad1a4e..56402e81226646 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/clusterrole.go @@ -5,7 +5,6 @@ //go:build orchestrator -//nolint:revive // TODO(CAPP) Fix revive linter package k8s import ( diff --git a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/common.go b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/common.go index f1ef9de63185ea..a59ea4481c3d90 100644 --- a/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/common.go +++ b/pkg/collector/corechecks/cluster/orchestrator/transformers/k8s/common.go @@ -5,6 +5,7 @@ //go:build orchestrator +// Package k8s provides methods for converting kubernetes resources to protobuf model. package k8s import ( diff --git a/pkg/collector/corechecks/orchestrator/pod/pod.go b/pkg/collector/corechecks/orchestrator/pod/pod.go index 51b5f2bb8aa22a..9f84f8ad415ad8 100644 --- a/pkg/collector/corechecks/orchestrator/pod/pod.go +++ b/pkg/collector/corechecks/orchestrator/pod/pod.go @@ -130,12 +130,14 @@ func (c *Check) Run() error { } groupID := nextGroupID() - ctx := &processors.ProcessorContext{ - ClusterID: c.clusterID, - Cfg: c.config, + ctx := &processors.K8sProcessorContext{ + BaseProcessorContext: processors.BaseProcessorContext{ + Cfg: c.config, + MsgGroupID: groupID, + NodeType: orchestrator.K8sPod, + ClusterID: c.clusterID, + }, HostName: c.hostName, - MsgGroupID: groupID, - NodeType: orchestrator.K8sPod, ApiGroupVersionTag: "kube_api_version:v1", }