Skip to content

Commit d95217b

Browse files
authored
Allow setting/overriding the default tags (#44)
1 parent 3ad0211 commit d95217b

File tree

3 files changed

+132
-88
lines changed

3 files changed

+132
-88
lines changed

kubernetes.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ func buildTags(pvc *corev1.PersistentVolumeClaim) map[string]string {
177177
// Set the default tags
178178
for k, v := range defaultTags {
179179
if !isValidTagName(k) {
180-
log.Warnln(k, "is a restricted tag. Skipping...")
181-
promInvalidTagsTotal.Inc()
182-
continue
180+
if !allowAllTags {
181+
log.Warnln(k, "is a restricted tag. Skipping...")
182+
promInvalidTagsTotal.Inc()
183+
continue
184+
} else {
185+
log.Warnln(k, "is a restricted tag but still allowing it to be set...")
186+
}
183187
}
184188
tags[k] = v
185189
}
@@ -200,9 +204,13 @@ func buildTags(pvc *corev1.PersistentVolumeClaim) map[string]string {
200204

201205
for k, v := range customTags {
202206
if !isValidTagName(k) {
203-
log.Warnln(k, "is a restricted tag. Skipping...")
204-
promInvalidTagsTotal.Inc()
205-
continue
207+
if !allowAllTags {
208+
log.Warnln(k, "is a restricted tag. Skipping...")
209+
promInvalidTagsTotal.Inc()
210+
continue
211+
} else {
212+
log.Warnln(k, "is a restricted tag but still allowing it to be set...")
213+
}
206214
}
207215
tags[k] = v
208216
}

kubernetes_test.go

+116-82
Original file line numberDiff line numberDiff line change
@@ -146,124 +146,157 @@ func Test_buildTags(t *testing.T) {
146146
pvc.SetName("my-pvc")
147147

148148
tests := []struct {
149-
name string
150-
defaultTags map[string]string
151-
annotations map[string]string
152-
want map[string]string
153-
tagFormat string
149+
name string
150+
defaultTags map[string]string
151+
allowAllTags bool
152+
annotations map[string]string
153+
want map[string]string
154+
tagFormat string
154155
}{
155156
{
156-
name: "ignore annotation set",
157-
defaultTags: map[string]string{},
158-
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
159-
want: map[string]string{},
157+
name: "ignore annotation set",
158+
defaultTags: map[string]string{},
159+
allowAllTags: false,
160+
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
161+
want: map[string]string{},
162+
},
163+
{
164+
name: "ignore annotation set with default tags",
165+
defaultTags: map[string]string{"foo": "bar"},
166+
allowAllTags: false,
167+
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
168+
want: map[string]string{},
169+
},
170+
{
171+
name: "ignore annotation set with tags annotation set",
172+
defaultTags: map[string]string{},
173+
allowAllTags: false,
174+
annotations: map[string]string{"aws-ebs-tagger/ignore": "exists", "aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
175+
want: map[string]string{},
160176
},
161177
{
162-
name: "ignore annotation set with default tags",
163-
defaultTags: map[string]string{"foo": "bar"},
164-
annotations: map[string]string{"aws-ebs-tagger/ignore": ""},
165-
want: map[string]string{},
178+
name: "tags annotation not set with default tags",
179+
defaultTags: map[string]string{"foo": "bar", "something": "else"},
180+
allowAllTags: false,
181+
annotations: map[string]string{},
182+
want: map[string]string{"foo": "bar", "something": "else"},
166183
},
167184
{
168-
name: "ignore annotation set with tags annotation set",
169-
defaultTags: map[string]string{},
170-
annotations: map[string]string{"aws-ebs-tagger/ignore": "exists", "aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
171-
want: map[string]string{},
185+
name: "tags annotation not set with no default tags",
186+
defaultTags: map[string]string{},
187+
allowAllTags: false,
188+
annotations: map[string]string{},
189+
want: map[string]string{},
172190
},
173191
{
174-
name: "tags annotation not set with default tags",
175-
defaultTags: map[string]string{"foo": "bar", "something": "else"},
176-
annotations: map[string]string{},
177-
want: map[string]string{"foo": "bar", "something": "else"},
192+
name: "tags annotation set empty with no default tags",
193+
defaultTags: map[string]string{},
194+
allowAllTags: false,
195+
annotations: map[string]string{"aws-ebs-tagger/tags": ""},
196+
want: map[string]string{},
178197
},
179198
{
180-
name: "tags annotation not set with no default tags",
181-
defaultTags: map[string]string{},
182-
annotations: map[string]string{},
183-
want: map[string]string{},
199+
name: "tags annotation set with no default tags",
200+
defaultTags: map[string]string{},
201+
allowAllTags: false,
202+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
203+
want: map[string]string{"foo": "bar"},
184204
},
185205
{
186-
name: "tags annotation set empty with no default tags",
187-
defaultTags: map[string]string{},
188-
annotations: map[string]string{"aws-ebs-tagger/tags": ""},
189-
want: map[string]string{},
206+
name: "tags annotation set with default tags",
207+
defaultTags: map[string]string{"foo": "bar"},
208+
allowAllTags: false,
209+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
210+
want: map[string]string{"foo": "bar", "something": "else"},
190211
},
191212
{
192-
name: "tags annotation set with no default tags",
193-
defaultTags: map[string]string{},
194-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
195-
want: map[string]string{"foo": "bar"},
213+
name: "tags annotation set with default tags with override",
214+
defaultTags: map[string]string{"foo": "foo"},
215+
allowAllTags: false,
216+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"something\": \"else\"}"},
217+
want: map[string]string{"foo": "bar", "something": "else"},
196218
},
197219
{
198-
name: "tags annotation set with default tags",
199-
defaultTags: map[string]string{"foo": "bar"},
200-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
201-
want: map[string]string{"foo": "bar", "something": "else"},
220+
name: "tags annotation invalid json with no default tags",
221+
defaultTags: map[string]string{},
222+
allowAllTags: false,
223+
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
224+
want: map[string]string{},
202225
},
203226
{
204-
name: "tags annotation set with default tags with override",
205-
defaultTags: map[string]string{"foo": "foo"},
206-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"something\": \"else\"}"},
207-
want: map[string]string{"foo": "bar", "something": "else"},
227+
name: "tags annotation invalid json with default tags",
228+
defaultTags: map[string]string{"foo": "bar"},
229+
allowAllTags: false,
230+
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
231+
want: map[string]string{"foo": "bar"},
208232
},
209233
{
210-
name: "tags annotation invalid json with no default tags",
211-
defaultTags: map[string]string{},
212-
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
213-
want: map[string]string{},
234+
name: "tags annotation set with invalid name with no default tags",
235+
defaultTags: map[string]string{},
236+
allowAllTags: false,
237+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
238+
want: map[string]string{"foo": "bar"},
214239
},
215240
{
216-
name: "tags annotation invalid json with default tags",
217-
defaultTags: map[string]string{"foo": "bar"},
218-
annotations: map[string]string{"aws-ebs-tagger/tags": "'asdas:\"asdasd\""},
219-
want: map[string]string{"foo": "bar"},
241+
name: "tags annotation set with invalid name but allowAllTags with no default tags",
242+
defaultTags: map[string]string{},
243+
allowAllTags: true,
244+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
245+
want: map[string]string{"foo": "bar", "kubernetes.io/foo": "bar"},
220246
},
221247
{
222-
name: "tags annotation set with invalid name with no default tags",
223-
defaultTags: map[string]string{},
224-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"kubernetes.io/foo\": \"bar\"}"},
225-
want: map[string]string{"foo": "bar"},
248+
name: "tags annotation set with invalid name but allowAllTags with no default tags",
249+
defaultTags: map[string]string{},
250+
allowAllTags: true,
251+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\", \"Name\": \"bar\"}"},
252+
want: map[string]string{"foo": "bar", "Name": "bar"},
226253
},
227254
{
228-
name: "tags annotation set with invalid default tags",
229-
defaultTags: map[string]string{"kubernetes.io/foo": "bar"},
230-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
231-
want: map[string]string{"something": "else"},
255+
name: "tags annotation set with invalid default tags",
256+
defaultTags: map[string]string{"kubernetes.io/foo": "bar"},
257+
allowAllTags: false,
258+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"something\": \"else\"}"},
259+
want: map[string]string{"something": "else"},
232260
},
233261
{
234-
name: "tags annotation not set with default tags - csv",
235-
defaultTags: map[string]string{"foo": "bar", "something": "else"},
236-
annotations: map[string]string{},
237-
want: map[string]string{"foo": "bar", "something": "else"},
238-
tagFormat: "csv",
262+
name: "tags annotation not set with default tags - csv",
263+
defaultTags: map[string]string{"foo": "bar", "something": "else"},
264+
allowAllTags: false,
265+
annotations: map[string]string{},
266+
want: map[string]string{"foo": "bar", "something": "else"},
267+
tagFormat: "csv",
239268
},
240269
{
241-
name: "tags annotation not set with no default tags - csv",
242-
defaultTags: map[string]string{},
243-
annotations: map[string]string{},
244-
want: map[string]string{},
245-
tagFormat: "csv",
270+
name: "tags annotation not set with no default tags - csv",
271+
defaultTags: map[string]string{},
272+
allowAllTags: false,
273+
annotations: map[string]string{},
274+
want: map[string]string{},
275+
tagFormat: "csv",
246276
},
247277
{
248-
name: "tags annotation set with default tags - csv",
249-
defaultTags: map[string]string{"foo": "bar"},
250-
annotations: map[string]string{"aws-ebs-tagger/tags": "something=else"},
251-
want: map[string]string{"foo": "bar", "something": "else"},
252-
tagFormat: "csv",
278+
name: "tags annotation set with default tags - csv",
279+
defaultTags: map[string]string{"foo": "bar"},
280+
allowAllTags: false,
281+
annotations: map[string]string{"aws-ebs-tagger/tags": "something=else"},
282+
want: map[string]string{"foo": "bar", "something": "else"},
283+
tagFormat: "csv",
253284
},
254285
{
255-
name: "tags annotation set with default tags with override - csv",
256-
defaultTags: map[string]string{"foo": "foo"},
257-
annotations: map[string]string{"aws-ebs-tagger/tags": "foo=bar,something=else"},
258-
want: map[string]string{"foo": "bar", "something": "else"},
259-
tagFormat: "csv",
286+
name: "tags annotation set with default tags with override - csv",
287+
defaultTags: map[string]string{"foo": "foo"},
288+
allowAllTags: false,
289+
annotations: map[string]string{"aws-ebs-tagger/tags": "foo=bar,something=else"},
290+
want: map[string]string{"foo": "bar", "something": "else"},
291+
tagFormat: "csv",
260292
},
261293
{
262-
name: "tags annotation set with invalid tags - csv",
263-
defaultTags: map[string]string{},
264-
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
265-
want: map[string]string{},
266-
tagFormat: "csv",
294+
name: "tags annotation set with invalid tags - csv",
295+
defaultTags: map[string]string{},
296+
allowAllTags: false,
297+
annotations: map[string]string{"aws-ebs-tagger/tags": "{\"foo\": \"bar\"}"},
298+
want: map[string]string{},
299+
tagFormat: "csv",
267300
},
268301

269302
// foo=bar,something=else
@@ -272,6 +305,7 @@ func Test_buildTags(t *testing.T) {
272305
t.Run(tt.name, func(t *testing.T) {
273306
pvc.SetAnnotations(tt.annotations)
274307
defaultTags = tt.defaultTags
308+
allowAllTags = tt.allowAllTags
275309
if tt.tagFormat != "" {
276310
tagFormat = tt.tagFormat
277311
} else {

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var (
5252
annotationPrefix string = "aws-ebs-tagger"
5353
watchNamespace string
5454
tagFormat string = "json"
55+
allowAllTags bool
5556

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

119121
if leaseLockName == "" {

0 commit comments

Comments
 (0)