diff --git a/kubernetes.go b/kubernetes.go index 817c4d8..40c6597 100644 --- a/kubernetes.go +++ b/kubernetes.go @@ -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 } @@ -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 } diff --git a/kubernetes_test.go b/kubernetes_test.go index 10b08b4..391a0a4 100644 --- a/kubernetes_test.go +++ b/kubernetes_test.go @@ -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 @@ -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 { diff --git a/main.go b/main.go index bea4fe1..404981c 100644 --- a/main.go +++ b/main.go @@ -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", @@ -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 == "" {