-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract kubernetes resource parsers (pod, deployment and metadata) to…
… a separate package
- Loading branch information
Showing
20 changed files
with
943 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
// Package filters implements the Tagger Filters. | ||
package filters | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/DataDog/datadog-agent/comp/core/tagger/types" | ||
) | ||
|
||
// AllPrefixesSet returns a set of all possible entity id prefixes that can be used in the tagger | ||
func AllPrefixesSet() map[EntityIDPrefix]struct{} { | ||
return map[EntityIDPrefix]struct{}{ | ||
ContainerID: {}, | ||
ContainerImageMetadata: {}, | ||
ECSTask: {}, | ||
Host: {}, | ||
KubernetesDeployment: {}, | ||
KubernetesMetadata: {}, | ||
KubernetesPodUID: {}, | ||
Process: {}, | ||
} | ||
} | ||
|
||
// FilterBuilder builds a tagger subscriber filter based on include/exclude rules | ||
type FilterBuilder struct { | ||
prefixesToInclude map[types.EntityIDPrefix]struct{} | ||
|
||
prefixesToExclude map[types.EntityIDPrefix]struct{} | ||
} | ||
|
||
// NewFilterBuilder returns a new empty filter builder | ||
func NewFilterBuilder() FilterBuilder { | ||
return FilterBuilder{} | ||
} | ||
|
||
// Include includes the specified prefixes in the filter | ||
func (fb FilterBuilder) Include(prefixes ...types.EntityIDPrefix) { | ||
for _, prefix := range prefixes { | ||
fb.prefixesToInclude[prefix] = struct{}{} | ||
} | ||
} | ||
|
||
// Exclude excludes the specified prefixes from the filter | ||
func (fb FilterBuilder) Exclude(prefixes ...types.EntityIDPrefix) { | ||
for _, prefix := range prefixes { | ||
fb.prefixesToExclude[prefix] = struct{}{} | ||
} | ||
} | ||
|
||
// Build builds a new Filter object based on the calls to Include and Exclude | ||
// If the builder only excludes prefixes, the created filter will match any prefix except for the excluded ones. | ||
// If the builder only includes prefixes, the created filter will match only the prefixes included in the builder. | ||
// If the builder includes prefixes and excludes prefixes, the created filter will match only prefixes that are included but a not excluded in the builder | ||
func (fb FilterBuilder) Build(card types.TagCardinality) Filter { | ||
prefixSet := make(map[types.EntityIDPrefix]struct{}) | ||
|
||
// only exclude | ||
for prefix := range types.AllPrefixesSet() { | ||
if _, found := fb.prefixesToExclude[prefix]; !found { | ||
prefixSet[prefix] = struct{}{} | ||
} | ||
} | ||
|
||
// only include | ||
prefixSet = maps.Clone(fb.prefixesToInclude) | ||
|
||
// both include and exclude | ||
return *newFilter(prefixSet, card) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
// Package filters implements the Tagger Filters. | ||
package filters | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/DataDog/datadog-agent/comp/core/tagger/types" | ||
) | ||
|
||
// Filter represents a subscription filter for the tagger | ||
type Filter struct { | ||
prefixes map[types.EntityIDPrefix]struct{} | ||
cardinality types.TagCardinality | ||
} | ||
|
||
func newFilter(prefixes map[types.EntityIDPrefix]struct{}, cardinality types.TagCardinality) *Filter { | ||
return &Filter{ | ||
prefixes: prefixes, | ||
cardinality: cardinality, | ||
} | ||
} | ||
|
||
// GetPrefixes returns the prefix set of the filter | ||
// If the filter is nil, an empty set is returned | ||
func (f *Filter) GetPrefixes() map[types.EntityIDPrefix]struct{} { | ||
if f == nil { | ||
return nil | ||
} | ||
|
||
return maps.Clone(f.prefixes) | ||
} | ||
|
||
// GetCardinality returns the filter cardinality | ||
// If the filter is nil, High cardinality is returned | ||
func (f *Filter) GetCardinality() types.TagCardinality { | ||
if f == nil { | ||
return types.HighCardinality | ||
} | ||
|
||
return f.cardinality | ||
} | ||
|
||
// MatchesPrefix returns whether or not the filter matches the prefix passed as argument | ||
func (f *Filter) MatchesPrefix(prefix types.EntityIDPrefix) bool { | ||
// A nil filter should match everything | ||
if f == nil { | ||
return true | ||
} | ||
|
||
_, found := f.prefixes[prefix] | ||
|
||
return found | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.