Skip to content

Commit

Permalink
Adding different flag for include and exclude labels
Browse files Browse the repository at this point in the history
  • Loading branch information
gizas committed Oct 3, 2023
1 parent 1f83853 commit c665bc2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions kubernetes/metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
type Config struct {
KubeConfig string `config:"kube_config"`

UseRegex bool `config:"use_regex"`
UseRegexInclude bool `config:"use_regex_include"`
UseRegexExclude bool `config:"use_regex_exclude"`
IncludeLabels []string `config:"include_labels"`
ExcludeLabels []string `config:"exclude_labels"`
IncludeAnnotations []string `config:"include_annotations"`
Expand All @@ -46,7 +47,8 @@ type AddResourceMetadataConfig struct {
func (c *Config) InitDefaults() {
c.LabelsDedot = true
c.AnnotationsDedot = true
c.UseRegex = false
c.UseRegexInclude = false
c.UseRegexExclude = false
}

// Unmarshal unpacks a Config into the metagen Config
Expand Down
6 changes: 3 additions & 3 deletions kubernetes/metadata/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ func (r *Resource) GenerateK8s(kind string, obj kubernetes.Resource, options ...
if len(r.config.IncludeLabels) == 0 {
labelMap = GenerateMap(accessor.GetLabels(), r.config.LabelsDedot)
} else {
labelMap = generateMapSubset(accessor.GetLabels(), r.config.IncludeLabels, r.config.LabelsDedot, r.config.UseRegex)
labelMap = generateMapSubset(accessor.GetLabels(), r.config.IncludeLabels, r.config.LabelsDedot, r.config.UseRegexInclude)
}

var labelMaptoExclude mapstr.M
if len(r.config.ExcludeLabels) != 0 {
labelMaptoExclude = generateMapSubset(accessor.GetLabels(), r.config.ExcludeLabels, r.config.LabelsDedot, r.config.UseRegex)
labelMaptoExclude = generateMapSubset(accessor.GetLabels(), r.config.ExcludeLabels, r.config.LabelsDedot, r.config.UseRegexExclude)
}

// Exclude any labels that are present in the exclude_labels config
for label := range labelMaptoExclude {
_ = labelMap.Delete(label)
}

annotationsMap := generateMapSubset(accessor.GetAnnotations(), r.config.IncludeAnnotations, r.config.AnnotationsDedot, r.config.UseRegex)
annotationsMap := generateMapSubset(accessor.GetAnnotations(), r.config.IncludeAnnotations, r.config.AnnotationsDedot, r.config.UseRegexInclude)

meta := mapstr.M{
strings.ToLower(kind): mapstr.M{
Expand Down
10 changes: 10 additions & 0 deletions kubernetes/metadata/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,14 @@ func Test_generateMapSubset(t *testing.T) {
output6 := generateMapSubset(Labelsdedot, key6, true, true)
assert.Equal(t, expectedresult6, output6)

//Default code, UsegexInclude=false Validation
key7 := []string{
"app.kubernetes.io/name",
}
expectedresult7 := mapstr.M{
"app_kubernetes_io/name": "no",
}
output7 := generateMapSubset(Labelsdedot, key7, true, false)
assert.Equal(t, expectedresult7, output7)

}

0 comments on commit c665bc2

Please sign in to comment.