Skip to content

Commit d56d41b

Browse files
committed
Add duration as a configurable type for localkube
1 parent 9e8ef22 commit d56d41b

File tree

2 files changed

+80
-38
lines changed

2 files changed

+80
-38
lines changed

pkg/util/config.go

+76-38
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"reflect"
2323
"strconv"
2424
"strings"
25+
"time"
2526

2627
utilnet "k8s.io/apimachinery/pkg/util/net"
2728
)
@@ -47,54 +48,91 @@ func findNestedElement(s string, c interface{}) (reflect.Value, error) {
4748

4849
// setElement sets the supplied element to the value in the supplied string. The string will be coerced to the correct type.
4950
func setElement(e reflect.Value, v string) error {
50-
switch e.Kind() {
51-
case reflect.Int, reflect.Int32, reflect.Int64:
52-
i, err := strconv.Atoi(v)
51+
switch e.Interface().(type) {
52+
case int, int32, int64:
53+
return convertInt(e, v)
54+
case string:
55+
return convertString(e, v)
56+
case float32, float64:
57+
return convertFloat(e, v)
58+
case bool:
59+
return convertBool(e, v)
60+
case net.IP:
61+
ip := net.ParseIP(v)
62+
if ip == nil {
63+
return fmt.Errorf("Error converting input %s to an IP.", v)
64+
}
65+
e.Set(reflect.ValueOf(ip))
66+
case net.IPNet:
67+
_, cidr, err := net.ParseCIDR(v)
5368
if err != nil {
54-
return fmt.Errorf("Error converting input %s to an integer: %s", v, err)
69+
return fmt.Errorf("Error converting input %s to a CIDR: %s", v, err)
5570
}
56-
e.SetInt(int64(i))
57-
case reflect.String:
58-
e.SetString(v)
59-
case reflect.Float32, reflect.Float64:
60-
f, err := strconv.ParseFloat(v, 64)
71+
e.Set(reflect.ValueOf(*cidr))
72+
case utilnet.PortRange:
73+
pr, err := utilnet.ParsePortRange(v)
6174
if err != nil {
62-
return fmt.Errorf("Error converting input %s to a float: %s", v, err)
75+
return fmt.Errorf("Error converting input %s to PortRange: %s", v, err)
6376
}
64-
e.SetFloat(f)
65-
case reflect.Bool:
66-
b, err := strconv.ParseBool(v)
77+
e.Set(reflect.ValueOf(*pr))
78+
case time.Duration:
79+
dur, err := time.ParseDuration(v)
6780
if err != nil {
68-
return fmt.Errorf("Error converting input %s to a bool: %s", v, err)
81+
return fmt.Errorf("Error converting input %s to Duration: %s", v, err)
6982
}
70-
e.SetBool(b)
83+
e.Set(reflect.ValueOf(dur))
84+
case []string:
85+
vals := strings.Split(v, ",")
86+
e.Set(reflect.ValueOf(vals))
7187
default:
72-
switch t := e.Interface().(type) {
73-
case net.IP:
74-
ip := net.ParseIP(v)
75-
if ip == nil {
76-
return fmt.Errorf("Error converting input %s to an IP.", v)
77-
}
78-
e.Set(reflect.ValueOf(ip))
79-
case net.IPNet:
80-
_, cidr, err := net.ParseCIDR(v)
81-
if err != nil {
82-
return fmt.Errorf("Error converting input %s to a CIDR: %s", v, err)
83-
}
84-
e.Set(reflect.ValueOf(*cidr))
85-
case utilnet.PortRange:
86-
pr, err := utilnet.ParsePortRange(v)
87-
if err != nil {
88-
return fmt.Errorf("Error converting input %s to PortRange: %s", v, err)
89-
}
90-
e.Set(reflect.ValueOf(*pr))
91-
case []string:
92-
vals := strings.Split(v, ",")
93-
e.Set(reflect.ValueOf(vals))
88+
// Last ditch attempt to convert anything based on its underlying kind.
89+
// This covers any types that are aliased to a native type
90+
switch e.Kind() {
91+
case reflect.Int, reflect.Int32, reflect.Int64:
92+
return convertInt(e, v)
93+
case reflect.String:
94+
return convertString(e, v)
95+
case reflect.Float32, reflect.Float64:
96+
return convertFloat(e, v)
97+
case reflect.Bool:
98+
return convertBool(e, v)
9499
default:
95-
return fmt.Errorf("Unable to set type %T.", t)
100+
return fmt.Errorf("Unable to set type %T.", e.Kind())
96101
}
97102
}
103+
104+
return nil
105+
}
106+
107+
func convertInt(e reflect.Value, v string) error {
108+
i, err := strconv.Atoi(v)
109+
if err != nil {
110+
return fmt.Errorf("Error converting input %s to an integer: %s", v, err)
111+
}
112+
e.SetInt(int64(i))
113+
return nil
114+
}
115+
116+
func convertString(e reflect.Value, v string) error {
117+
e.SetString(v)
118+
return nil
119+
}
120+
121+
func convertFloat(e reflect.Value, v string) error {
122+
f, err := strconv.ParseFloat(v, 64)
123+
if err != nil {
124+
return fmt.Errorf("Error converting input %s to a float: %s", v, err)
125+
}
126+
e.SetFloat(f)
127+
return nil
128+
}
129+
130+
func convertBool(e reflect.Value, v string) error {
131+
b, err := strconv.ParseBool(v)
132+
if err != nil {
133+
return fmt.Errorf("Error converting input %s to a bool: %s", v, err)
134+
}
135+
e.SetBool(b)
98136
return nil
99137
}
100138

pkg/util/config_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"reflect"
2323
"testing"
24+
"time"
2425

2526
utilnet "k8s.io/apimachinery/pkg/util/net"
2627
)
@@ -58,6 +59,7 @@ type subConfig3 struct {
5859
S []string
5960
T aliasedString
6061
U net.IPNet
62+
V time.Duration
6163
}
6264

6365
func buildConfig() testConfig {
@@ -78,6 +80,7 @@ func buildConfig() testConfig {
7880
Q: net.ParseIP("12.34.56.78"),
7981
R: utilnet.PortRange{Base: 2, Size: 4},
8082
U: *cidr,
83+
V: 5 * time.Second,
8184
},
8285
},
8386
E: &subConfig2{
@@ -180,6 +183,7 @@ func TestSetElement(t *testing.T) {
180183
{"D.I.S", "a,b", func(t testConfig) bool { return reflect.DeepEqual(t.D.I.S, []string{"a", "b"}) }},
181184
{"D.I.T", "foo", func(t testConfig) bool { return t.D.I.T == "foo" }},
182185
{"D.I.U", "11.22.0.0/16", func(t testConfig) bool { return t.D.I.U.String() == "11.22.0.0/16" }},
186+
{"D.I.V", "5s", func(t testConfig) bool { return t.D.I.V == 5*time.Second }},
183187
} {
184188
a := buildConfig()
185189
if err := FindAndSet(tc.path, &a, tc.newval); err != nil {

0 commit comments

Comments
 (0)