From f72dadbe034ac36f4e041f68c7cb080effe4935f Mon Sep 17 00:00:00 2001 From: dlorenc Date: Tue, 22 Nov 2016 09:46:51 -0800 Subject: [PATCH] Add PortRange to the extra-config parser. --- pkg/util/config.go | 10 +++++++++- pkg/util/config_test.go | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/util/config.go b/pkg/util/config.go index 8af24914a8..9e46b2ac89 100644 --- a/pkg/util/config.go +++ b/pkg/util/config.go @@ -22,6 +22,8 @@ import ( "reflect" "strconv" "strings" + + utilnet "k8s.io/client-go/1.4/pkg/util/net" ) // findNestedElement uses reflection to find the element corresponding to the dot-separated string parameter. @@ -63,7 +65,7 @@ func setElement(e reflect.Value, v string) error { case bool: b, err := strconv.ParseBool(v) if err != nil { - return fmt.Errorf("Error converting input %s to a bool: %s", b, err) + return fmt.Errorf("Error converting input %s to a bool: %s", v, err) } e.SetBool(b) case net.IP: @@ -72,6 +74,12 @@ func setElement(e reflect.Value, v string) error { return fmt.Errorf("Error converting input %s to an IP.", v) } e.Set(reflect.ValueOf(ip)) + case utilnet.PortRange: + pr, err := utilnet.ParsePortRange(v) + if err != nil { + return fmt.Errorf("Error converting input %s to PortRange: %s", v, err) + } + e.Set(reflect.ValueOf(*pr)) default: return fmt.Errorf("Unable to set type %s.", t) } diff --git a/pkg/util/config_test.go b/pkg/util/config_test.go index 24e6aa5b78..0d81878760 100644 --- a/pkg/util/config_test.go +++ b/pkg/util/config_test.go @@ -20,6 +20,8 @@ import ( "math" "net" "testing" + + utilnet "k8s.io/client-go/1.4/pkg/util/net" ) type testConfig struct { @@ -49,6 +51,7 @@ type subConfig3 struct { O float32 P bool Q net.IP + R utilnet.PortRange } func buildConfig() testConfig { @@ -66,6 +69,7 @@ func buildConfig() testConfig { O: 3.3, P: false, Q: net.ParseIP("12.34.56.78"), + R: utilnet.PortRange{Base: 2, Size: 4}, }, }, E: &subConfig2{ @@ -164,6 +168,7 @@ func TestSetElement(t *testing.T) { {"D.I.P", "true", func(t testConfig) bool { return bool(t.D.I.P) == true }}, {"D.I.P", "false", func(t testConfig) bool { return bool(t.D.I.P) == false }}, {"D.I.Q", "11.22.33.44", func(t testConfig) bool { return t.D.I.Q.Equal(net.ParseIP("11.22.33.44")) }}, + {"D.I.R", "7-11", func(t testConfig) bool { return t.D.I.R.Base == 7 && t.D.I.R.Size == 5 }}, } { a := buildConfig() if err := FindAndSet(tc.path, &a, tc.newval); err != nil {