Skip to content

Commit

Permalink
Allow setting/overriding the default tags (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtougeron authored Mar 24, 2022
1 parent 3ad0211 commit d95217b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 88 deletions.
20 changes: 14 additions & 6 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ func buildTags(pvc *corev1.PersistentVolumeClaim) map[string]string {
// Set the default tags
for k, v := range defaultTags {
if !isValidTagName(k) {
log.Warnln(k, "is a restricted tag. Skipping...")
promInvalidTagsTotal.Inc()
continue
if !allowAllTags {
log.Warnln(k, "is a restricted tag. Skipping...")
promInvalidTagsTotal.Inc()
continue
} else {
log.Warnln(k, "is a restricted tag but still allowing it to be set...")
}
}
tags[k] = v
}
Expand All @@ -200,9 +204,13 @@ func buildTags(pvc *corev1.PersistentVolumeClaim) map[string]string {

for k, v := range customTags {
if !isValidTagName(k) {
log.Warnln(k, "is a restricted tag. Skipping...")
promInvalidTagsTotal.Inc()
continue
if !allowAllTags {
log.Warnln(k, "is a restricted tag. Skipping...")
promInvalidTagsTotal.Inc()
continue
} else {
log.Warnln(k, "is a restricted tag but still allowing it to be set...")
}
}
tags[k] = v
}
Expand Down
198 changes: 116 additions & 82 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,124 +146,157 @@ func Test_buildTags(t *testing.T) {
pvc.SetName("my-pvc")

tests := []struct {
name string
defaultTags map[string]string
annotations map[string]string
want map[string]string
tagFormat string
name string
defaultTags map[string]string
allowAllTags bool
annotations map[string]string
want map[string]string
tagFormat string
}{
{
name: "ignore annotation set",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
want: map[string]string{},
name: "ignore annotation set",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
want: map[string]string{},
},
{
name: "ignore annotation set with default tags",
defaultTags: map[string]string{"foo": "bar"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
want: map[string]string{},
},
{
name: "ignore annotation set with tags annotation set",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/ignore": "exists", "aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{},
},
{
name: "ignore annotation set with default tags",
defaultTags: map[string]string{"foo": "bar"},
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
want: map[string]string{},
name: "tags annotation not set with default tags",
defaultTags: map[string]string{"foo": "bar", "something": "else"},
allowAllTags: false,
annotations: map[string]string{},
want: map[string]string{"foo": "bar", "something": "else"},
},
{
name: "ignore annotation set with tags annotation set",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/ignore": "exists", "aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{},
name: "tags annotation not set with no default tags",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{},
want: map[string]string{},
},
{
name: "tags annotation not set with default tags",
defaultTags: map[string]string{"foo": "bar", "something": "else"},
annotations: map[string]string{},
want: map[string]string{"foo": "bar", "something": "else"},
name: "tags annotation set empty with no default tags",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": ""},
want: map[string]string{},
},
{
name: "tags annotation not set with no default tags",
defaultTags: map[string]string{},
annotations: map[string]string{},
want: map[string]string{},
name: "tags annotation set with no default tags",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{"foo": "bar"},
},
{
name: "tags annotation set empty with no default tags",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/tags": ""},
want: map[string]string{},
name: "tags annotation set with default tags",
defaultTags: map[string]string{"foo": "bar"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
want: map[string]string{"foo": "bar", "something": "else"},
},
{
name: "tags annotation set with no default tags",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{"foo": "bar"},
name: "tags annotation set with default tags with override",
defaultTags: map[string]string{"foo": "foo"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"something\": \"else\"}"},
want: map[string]string{"foo": "bar", "something": "else"},
},
{
name: "tags annotation set with default tags",
defaultTags: map[string]string{"foo": "bar"},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
want: map[string]string{"foo": "bar", "something": "else"},
name: "tags annotation invalid json with no default tags",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
want: map[string]string{},
},
{
name: "tags annotation set with default tags with override",
defaultTags: map[string]string{"foo": "foo"},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"something\": \"else\"}"},
want: map[string]string{"foo": "bar", "something": "else"},
name: "tags annotation invalid json with default tags",
defaultTags: map[string]string{"foo": "bar"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
want: map[string]string{"foo": "bar"},
},
{
name: "tags annotation invalid json with no default tags",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
want: map[string]string{},
name: "tags annotation set with invalid name with no default tags",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
want: map[string]string{"foo": "bar"},
},
{
name: "tags annotation invalid json with default tags",
defaultTags: map[string]string{"foo": "bar"},
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
want: map[string]string{"foo": "bar"},
name: "tags annotation set with invalid name but allowAllTags with no default tags",
defaultTags: map[string]string{},
allowAllTags: true,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
want: map[string]string{"foo": "bar", "kubernetes.io/foo": "bar"},
},
{
name: "tags annotation set with invalid name with no default tags",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
want: map[string]string{"foo": "bar"},
name: "tags annotation set with invalid name but allowAllTags with no default tags",
defaultTags: map[string]string{},
allowAllTags: true,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"Name\": \"bar\"}"},
want: map[string]string{"foo": "bar", "Name": "bar"},
},
{
name: "tags annotation set with invalid default tags",
defaultTags: map[string]string{"kubernetes.io/foo": "bar"},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
want: map[string]string{"something": "else"},
name: "tags annotation set with invalid default tags",
defaultTags: map[string]string{"kubernetes.io/foo": "bar"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
want: map[string]string{"something": "else"},
},
{
name: "tags annotation not set with default tags - csv",
defaultTags: map[string]string{"foo": "bar", "something": "else"},
annotations: map[string]string{},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
name: "tags annotation not set with default tags - csv",
defaultTags: map[string]string{"foo": "bar", "something": "else"},
allowAllTags: false,
annotations: map[string]string{},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
},
{
name: "tags annotation not set with no default tags - csv",
defaultTags: map[string]string{},
annotations: map[string]string{},
want: map[string]string{},
tagFormat: "csv",
name: "tags annotation not set with no default tags - csv",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{},
want: map[string]string{},
tagFormat: "csv",
},
{
name: "tags annotation set with default tags - csv",
defaultTags: map[string]string{"foo": "bar"},
annotations: map[string]string{"aws-ebs-tagger/tags": "something=else"},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
name: "tags annotation set with default tags - csv",
defaultTags: map[string]string{"foo": "bar"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "something=else"},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
},
{
name: "tags annotation set with default tags with override - csv",
defaultTags: map[string]string{"foo": "foo"},
annotations: map[string]string{"aws-ebs-tagger/tags": "foo=bar,something=else"},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
name: "tags annotation set with default tags with override - csv",
defaultTags: map[string]string{"foo": "foo"},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "foo=bar,something=else"},
want: map[string]string{"foo": "bar", "something": "else"},
tagFormat: "csv",
},
{
name: "tags annotation set with invalid tags - csv",
defaultTags: map[string]string{},
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{},
tagFormat: "csv",
name: "tags annotation set with invalid tags - csv",
defaultTags: map[string]string{},
allowAllTags: false,
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
want: map[string]string{},
tagFormat: "csv",
},

// foo=bar,something=else
Expand All @@ -272,6 +305,7 @@ func Test_buildTags(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
pvc.SetAnnotations(tt.annotations)
defaultTags = tt.defaultTags
allowAllTags = tt.allowAllTags
if tt.tagFormat != "" {
tagFormat = tt.tagFormat
} else {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
annotationPrefix string = "aws-ebs-tagger"
watchNamespace string
tagFormat string = "json"
allowAllTags bool

promActionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "k8s_aws_ebs_tagger_actions_total",
Expand Down Expand Up @@ -114,6 +115,7 @@ func main() {
flag.StringVar(&watchNamespace, "watch-namespace", os.Getenv("WATCH_NAMESPACE"), "A specific namespace to watch (default is all namespaces)")
flag.StringVar(&statusPort, "status-port", "8000", "The healthz port")
flag.StringVar(&metricsPort, "metrics-port", "8001", "The prometheus metrics port")
flag.BoolVar(&allowAllTags, "allow-all-tags", false, "Whether or not to allow any tag, even Kubernetes assigned ones, to be set")
flag.Parse()

if leaseLockName == "" {
Expand Down

0 comments on commit d95217b

Please sign in to comment.