-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #75 from thetechnick/hsts
Added hsts settings, Refactored bool/integer parsing
- Loading branch information
Showing
7 changed files
with
294 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package nginx | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"k8s.io/kubernetes/pkg/api/meta" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
) | ||
|
||
var ( | ||
// ErrorKeyNotFound is returned if the key was not found in the map | ||
ErrorKeyNotFound = errors.New("Key not found in map") | ||
) | ||
|
||
// There seems to be no composite interface in the kubernetes api package, | ||
// so we have to declare our own. | ||
type apiObject interface { | ||
meta.Object | ||
runtime.Object | ||
} | ||
|
||
// GetMapKeyAsBool searches the map for the given key and parses the key as bool | ||
func GetMapKeyAsBool(m map[string]string, key string, context apiObject) (bool, error) { | ||
if str, exists := m[key]; exists { | ||
b, err := strconv.ParseBool(str) | ||
if err != nil { | ||
return false, fmt.Errorf("%s %v/%v '%s' contains invalid bool: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err) | ||
} | ||
return b, nil | ||
} | ||
return false, ErrorKeyNotFound | ||
} | ||
|
||
// GetMapKeyAsInt tries to find and parse a key in a map as int64 | ||
func GetMapKeyAsInt(m map[string]string, key string, context apiObject) (int64, error) { | ||
if str, exists := m[key]; exists { | ||
i, err := strconv.ParseInt(str, 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("%s %v/%v '%s' contains invalid integer: %v, ignoring", context.GetObjectKind().GroupVersionKind().Kind, context.GetNamespace(), context.GetName(), key, err) | ||
} | ||
return i, nil | ||
} | ||
return 0, ErrorKeyNotFound | ||
} |
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,149 @@ | ||
package nginx | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/api/unversioned" | ||
"k8s.io/kubernetes/pkg/apis/extensions" | ||
) | ||
|
||
var configMap = api.ConfigMap{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "test", | ||
Namespace: "default", | ||
}, | ||
TypeMeta: unversioned.TypeMeta{ | ||
Kind: "ConfigMap", | ||
APIVersion: "v1", | ||
}, | ||
} | ||
var ingress = extensions.Ingress{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "test", | ||
Namespace: "kube-system", | ||
}, | ||
TypeMeta: unversioned.TypeMeta{ | ||
Kind: "Ingress", | ||
APIVersion: "extensions/v1beta1", | ||
}, | ||
} | ||
|
||
// | ||
// GetMapKeyAsBool | ||
// | ||
func TestGetMapKeyAsBool(t *testing.T) { | ||
configMap := configMap | ||
configMap.Data = map[string]string{ | ||
"key": "True", | ||
} | ||
|
||
b, err := GetMapKeyAsBool(configMap.Data, "key", &configMap) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if b != true { | ||
t.Errorf("Result should be true") | ||
} | ||
} | ||
|
||
func TestGetMapKeyAsBoolNotFound(t *testing.T) { | ||
configMap := configMap | ||
configMap.Data = map[string]string{} | ||
|
||
_, err := GetMapKeyAsBool(configMap.Data, "key", &configMap) | ||
if err != ErrorKeyNotFound { | ||
t.Errorf("ErrorKeyNotFound was expected, got: %v", err) | ||
} | ||
} | ||
|
||
func TestGetMapKeyAsBoolErrorMessage(t *testing.T) { | ||
cfgm := configMap | ||
cfgm.Data = map[string]string{ | ||
"key": "string", | ||
} | ||
|
||
// Test with configmap | ||
_, err := GetMapKeyAsBool(cfgm.Data, "key", &cfgm) | ||
if err == nil { | ||
t.Error("An error was expected") | ||
} | ||
expected := `ConfigMap default/test 'key' contains invalid bool: strconv.ParseBool: parsing "string": invalid syntax, ignoring` | ||
if err.Error() != expected { | ||
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) | ||
} | ||
|
||
// Test with ingress object | ||
ingress := ingress | ||
ingress.Annotations = map[string]string{ | ||
"key": "other_string", | ||
} | ||
_, err = GetMapKeyAsBool(ingress.Annotations, "key", &ingress) | ||
if err == nil { | ||
t.Error("An error was expected") | ||
} | ||
expected = `Ingress kube-system/test 'key' contains invalid bool: strconv.ParseBool: parsing "other_string": invalid syntax, ignoring` | ||
if err.Error() != expected { | ||
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) | ||
} | ||
} | ||
|
||
// | ||
// GetMapKeyAsInt | ||
// | ||
func TestGetMapKeyAsInt(t *testing.T) { | ||
configMap := configMap | ||
configMap.Data = map[string]string{ | ||
"key": "123456789", | ||
} | ||
|
||
i, err := GetMapKeyAsInt(configMap.Data, "key", &configMap) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
var expected int64 = 123456789 | ||
if i != expected { | ||
t.Errorf("Unexpected return value:\nGot: %v\nExpected: %v", i, expected) | ||
} | ||
} | ||
|
||
func TestGetMapKeyAsIntNotFound(t *testing.T) { | ||
configMap := configMap | ||
configMap.Data = map[string]string{} | ||
|
||
_, err := GetMapKeyAsInt(configMap.Data, "key", &configMap) | ||
if err != ErrorKeyNotFound { | ||
t.Errorf("ErrorKeyNotFound was expected, got: %v", err) | ||
} | ||
} | ||
|
||
func TestGetMapKeyAsIntErrorMessage(t *testing.T) { | ||
cfgm := configMap | ||
cfgm.Data = map[string]string{ | ||
"key": "string", | ||
} | ||
|
||
// Test with configmap | ||
_, err := GetMapKeyAsInt(cfgm.Data, "key", &cfgm) | ||
if err == nil { | ||
t.Error("An error was expected") | ||
} | ||
expected := `ConfigMap default/test 'key' contains invalid integer: strconv.ParseInt: parsing "string": invalid syntax, ignoring` | ||
if err.Error() != expected { | ||
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) | ||
} | ||
|
||
// Test with ingress object | ||
ingress := ingress | ||
ingress.Annotations = map[string]string{ | ||
"key": "other_string", | ||
} | ||
_, err = GetMapKeyAsInt(ingress.Annotations, "key", &ingress) | ||
if err == nil { | ||
t.Error("An error was expected") | ||
} | ||
expected = `Ingress kube-system/test 'key' contains invalid integer: strconv.ParseInt: parsing "other_string": invalid syntax, ignoring` | ||
if err.Error() != expected { | ||
t.Errorf("The error message does not match expectations:\nGot: %v\nExpected: %v", err, expected) | ||
} | ||
} |
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
Oops, something went wrong.