Skip to content

Commit

Permalink
feat: allow dynamic tags based on regex capture groups
Browse files Browse the repository at this point in the history
Addresses: #11

Tag rules are matched based on a regex that is matched against resource
labels. This commit allows for expanding capture groups in the regex for
us in a given tag rule's absent/present tags list.

Tested working:
https://gist.github.com/tjhop/7e088c26b88b47138f5ae4281a2147af
  • Loading branch information
tjhop committed Nov 11, 2022
1 parent 54056f8 commit 45eba5a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,26 @@ func getNewTags(objectLabel string, tags []string, rules []TagRule) ([]string, b

if validRegex.MatchString(objectLabel) {
found = true
var newTags []string
var newTags, expandedAbsent, expandedPresent []string

for _, absent := range rule.Tags.Absent {
expandedAbsent = append(expandedAbsent, validRegex.ReplaceAllString(objectLabel, absent))
}
for _, present := range rule.Tags.Present {
expandedPresent = append(expandedPresent, validRegex.ReplaceAllString(objectLabel, present))
}

// check `absent` tags to remove unwanted tags
for _, tag := range tags {
if !slices.Contains(rule.Tags.Absent, tag) {
if !slices.Contains(expandedAbsent, tag) {
// if this tag is not on the `absent` list,
// we can persist it through to the new tag set
newTags = append(newTags, tag)
}
}

// check `present` tags to ensure specific tags exist
for _, tag := range rule.Tags.Present {
for _, tag := range expandedPresent {
// compare filter against `newTags`, as
// newTags == (the linode's tags - absent tags)
if !slices.Contains(newTags, tag) {
Expand Down

0 comments on commit 45eba5a

Please sign in to comment.