-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #814 from frodopwns/issue-805-filter-tags
filter bad characters from label keys when creating tags list
- Loading branch information
Showing
5 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package helpers | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"github.com/Azure/go-autorest/autorest/to" | ||
) | ||
|
||
func TestLabelsToTags(t *testing.T) { | ||
checks := []struct { | ||
Name string | ||
In map[string]string | ||
Out map[string]*string | ||
}{ | ||
{ | ||
Name: "normal labels", | ||
In: map[string]string{ | ||
"age": "null", | ||
"type": "null", | ||
}, | ||
Out: map[string]*string{ | ||
"age": to.StringPtr("null"), | ||
"type": to.StringPtr("null"), | ||
}, | ||
}, | ||
{ | ||
Name: "backslash labels", | ||
In: map[string]string{ | ||
"age/date": "null", | ||
"type\\kind": "null", | ||
"fun/\\zone": "null", | ||
}, | ||
Out: map[string]*string{ | ||
"age.date": to.StringPtr("null"), | ||
"type.kind": to.StringPtr("null"), | ||
"fun..zone": to.StringPtr("null"), | ||
}, | ||
}, | ||
{ | ||
Name: "greater than less than labels", | ||
In: map[string]string{ | ||
"age>date": "null", | ||
"type<kind": "null", | ||
"fun<>zone": "null", | ||
}, | ||
Out: map[string]*string{ | ||
"age.date": to.StringPtr("null"), | ||
"type.kind": to.StringPtr("null"), | ||
"fun..zone": to.StringPtr("null"), | ||
}, | ||
}, | ||
{ | ||
Name: "percent labels", | ||
In: map[string]string{ | ||
"age%date": "null", | ||
"%": "null", | ||
}, | ||
Out: map[string]*string{ | ||
"age.date": to.StringPtr("null"), | ||
".": to.StringPtr("null"), | ||
}, | ||
}, | ||
} | ||
for _, check := range checks { | ||
log.Println("Checking", check.Name) | ||
translated := LabelsToTags(check.In) | ||
for k, v := range translated { | ||
value := v | ||
if check.Out[k] == nil { | ||
t.Errorf("Expected 'null'\nGot nil\n%q", k) | ||
} | ||
if check.Out[k] != nil && *value != *check.Out[k] { | ||
t.Errorf("Expected %s\nGot %s", *check.Out[k], *value) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package helpers | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// LabelsToTags converts labels from a kube resource to the data structure expected by Azure for tags | ||
// this function will translate characters that are not allows by Azure to "."s | ||
func LabelsToTags(in map[string]string) map[string]*string { | ||
out := map[string]*string{} | ||
for k, v := range in { | ||
newK := k | ||
value := v | ||
if strings.ContainsAny(k, "<>%/?\\") { | ||
newK = ReplaceAny(k, []string{"<", ">", "%", "/", "\\\\", "?"}) | ||
} | ||
out[newK] = &value | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters