forked from kubernetes-sigs/apiserver-network-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
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 kubernetes-sigs#670 from kinvolk/imran/make-lease-…
…controller-configurable make lease label and lease namespace configurable
- Loading branch information
Showing
6 changed files
with
146 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
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
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,27 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ParseLabels takes a comma-separated string of key-value pairs and returns a map of labels. | ||
func ParseLabels(labelStr string) (map[string]string, error) { | ||
labels := make(map[string]string) | ||
|
||
if len(labelStr) == 0 { | ||
return labels, fmt.Errorf("empty string provided") | ||
} | ||
pairs := strings.Split(labelStr, ",") | ||
|
||
for _, pair := range pairs { | ||
keyValue := strings.Split(pair, "=") | ||
if len(keyValue) != 2 { | ||
return nil, fmt.Errorf("invalid label format: %s", pair) | ||
} | ||
key := strings.TrimSpace(keyValue[0]) | ||
value := strings.TrimSpace(keyValue[1]) | ||
labels[key] = value | ||
} | ||
return labels, nil | ||
} |
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,74 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseLabels(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
expectedOutput map[string]string | ||
shouldError bool | ||
}{ | ||
{ | ||
input: "app=myapp,env=prod,version=1.0", | ||
expectedOutput: map[string]string{ | ||
"app": "myapp", | ||
"env": "prod", | ||
"version": "1.0", | ||
}, | ||
shouldError: false, | ||
}, | ||
{ | ||
input: "app=myapp,env=prod,invalid", | ||
expectedOutput: nil, | ||
shouldError: true, | ||
}, | ||
{ | ||
input: "app=myapp", | ||
expectedOutput: map[string]string{ | ||
"app": "myapp", | ||
}, | ||
shouldError: false, | ||
}, | ||
{ | ||
input: "", | ||
expectedOutput: map[string]string{}, | ||
shouldError: true, | ||
}, | ||
{ | ||
input: " key = value , another = test ", | ||
expectedOutput: map[string]string{ | ||
"key": "value", | ||
"another": "test", | ||
}, | ||
shouldError: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
output, err := ParseLabels(tc.input) | ||
|
||
// Check for unexpected errors or missing errors | ||
if tc.shouldError && err == nil { | ||
t.Errorf("expected error for input %q but got none", tc.input) | ||
continue | ||
} | ||
if !tc.shouldError && err != nil { | ||
t.Errorf("did not expect error for input %q but got: %v", tc.input, err) | ||
continue | ||
} | ||
|
||
// Compare maps if there was no error | ||
if !tc.shouldError { | ||
if len(output) != len(tc.expectedOutput) { | ||
t.Errorf("for input %q, expected map length %d but got %d", tc.input, len(tc.expectedOutput), len(output)) | ||
} | ||
for key, expectedValue := range tc.expectedOutput { | ||
if output[key] != expectedValue { | ||
t.Errorf("for input %q, expected %q=%q but got %q=%q", tc.input, key, expectedValue, key, output[key]) | ||
} | ||
} | ||
} | ||
} | ||
} |