Skip to content

Commit 3a2ba65

Browse files
authored
feat: add noprefix labels (#44) (#46)
Signed-off-by: Patrik Cyvoct <[email protected]>
1 parent f40f4d3 commit 3a2ba65

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

docs/tags.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ In this case, the Kuebrnetes nodes will have the label `k8s.scaleway.com/foo=bar
1111

1212
Once the tag is removed from the instance, it will also be removed as a label on the node.
1313

14+
### Non prefixed labels
15+
16+
It is possible to add labels nop prefixed with `k8s.scaleway.com`. The downside, is that when you will delete the associated tag, the label won't get removed.
17+
In order to have non prefixed labels, you should prefix the tag with `noprefix=`.
18+
19+
For intance the tag `noprefix=foo=bar` will yield the `foo=bar` label on the Kubernetes nodes.
20+
1421
## Taints
1522

1623
In order for a tag to be synced to a taint, it needs to be of the form `taint=foo:bar:Effect`, where `Effect` is one of `NoExecute`, `NoSchedule` or `PreferNoSchedule`.

scaleway/sync.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var (
5959
labelsPrefix = "k8s.scaleway.com/"
6060
taintsPrefix = "k8s.scaleway.com/"
6161
labelTaintPrefix = "taint="
62+
labelNoPrefix = "noprefix="
6263

6364
// K8S labels
6465
labelNodeRoleExcludeBalancer = "node.kubernetes.io/exclude-from-external-load-balancers"
@@ -333,10 +334,17 @@ func (s *syncController) SyncLBTags() {
333334
}
334335

335336
func tagLabelParser(tag string) (key string, value string) {
337+
prefix := labelsPrefix
338+
339+
if strings.HasPrefix(tag, labelNoPrefix) {
340+
prefix = ""
341+
tag = strings.TrimPrefix(tag, labelNoPrefix)
342+
}
343+
336344
split := splitRegexp.Split(tag, -1)
337345

338-
key = labelsPrefix + labelsRegexp.FindString(split[0])
339-
if key == labelsPrefix || len(key) > 63 {
346+
key = prefix + labelsRegexp.FindString(split[0])
347+
if key == prefix || len(key) > 63 {
340348
return "", ""
341349
}
342350

scaleway/sync_test.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,32 @@ func TestTagTaintParser(t *testing.T) {
5252

5353
func TestTagLabelParser(t *testing.T) {
5454
tagsTest := map[string][2]string{
55-
"": {"", ""},
56-
"word": {"k8s.scaleway.com/word", ""},
57-
"underscore_word": {"k8s.scaleway.com/underscore_word", ""},
58-
"dash-word": {"k8s.scaleway.com/dash-word", ""},
59-
"equal1=value": {"k8s.scaleway.com/equal1", "value"},
60-
"equal2 = value": {"k8s.scaleway.com/equal2", "value"},
61-
"equal3 =value": {"k8s.scaleway.com/equal3", "value"},
62-
"equal4= value": {"k8s.scaleway.com/equal4", "value"},
63-
"equal5==value": {"k8s.scaleway.com/equal5", "value"},
64-
"equal6= =value": {"k8s.scaleway.com/equal6", "value"},
65-
"colon1:value": {"k8s.scaleway.com/colon1", "value"},
66-
"colon2 : value": {"k8s.scaleway.com/colon2", "value"},
67-
"colon3 :value": {"k8s.scaleway.com/colon3", "value"},
68-
"colon4: value": {"k8s.scaleway.com/colon4", "value"},
69-
"colon5::value": {"k8s.scaleway.com/colon5", "value"},
70-
"colon6: :value": {"k8s.scaleway.com/colon6", "value"},
71-
"space word": {"k8s.scaleway.com/space", "word"},
72-
"two space word": {"k8s.scaleway.com/two", "space"},
73-
"key=accentué": {"", ""},
74-
"accentué=value": {"", ""},
75-
"word=invalid/dash": {"", ""},
76-
"dash/word": {"", ""},
77-
"dash/equal=value": {"", ""},
55+
"": {"", ""},
56+
"word": {"k8s.scaleway.com/word", ""},
57+
"noprefix=word": {"word", ""},
58+
"underscore_word": {"k8s.scaleway.com/underscore_word", ""},
59+
"dash-word": {"k8s.scaleway.com/dash-word", ""},
60+
"equal1=value": {"k8s.scaleway.com/equal1", "value"},
61+
"noprefix=equal1=value": {"equal1", "value"},
62+
"equal2 = value": {"k8s.scaleway.com/equal2", "value"},
63+
"equal3 =value": {"k8s.scaleway.com/equal3", "value"},
64+
"equal4= value": {"k8s.scaleway.com/equal4", "value"},
65+
"equal5==value": {"k8s.scaleway.com/equal5", "value"},
66+
"equal6= =value": {"k8s.scaleway.com/equal6", "value"},
67+
"colon1:value": {"k8s.scaleway.com/colon1", "value"},
68+
"noprefix=colon1:value": {"colon1", "value"},
69+
"colon2 : value": {"k8s.scaleway.com/colon2", "value"},
70+
"colon3 :value": {"k8s.scaleway.com/colon3", "value"},
71+
"colon4: value": {"k8s.scaleway.com/colon4", "value"},
72+
"colon5::value": {"k8s.scaleway.com/colon5", "value"},
73+
"colon6: :value": {"k8s.scaleway.com/colon6", "value"},
74+
"space word": {"k8s.scaleway.com/space", "word"},
75+
"two space word": {"k8s.scaleway.com/two", "space"},
76+
"key=accentué": {"", ""},
77+
"accentué=value": {"", ""},
78+
"word=invalid/dash": {"", ""},
79+
"dash/word": {"", ""},
80+
"dash/equal=value": {"", ""},
7881
}
7982

8083
for tag, expected := range tagsTest {

0 commit comments

Comments
 (0)