@@ -6,11 +6,70 @@ import (
6
6
"net/url"
7
7
"path"
8
8
"reflect"
9
+ "strconv"
9
10
"strings"
11
+ "time"
10
12
11
13
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
12
14
)
13
15
16
+ // Option represents optional call parameters, such as label selectors.
17
+ type Option interface {
18
+ updateURL (base string , v url.Values ) string
19
+ }
20
+
21
+ type queryParam struct {
22
+ paramName string
23
+ paramValue string
24
+ }
25
+
26
+ func (o queryParam ) updateURL (base string , v url.Values ) string {
27
+ v .Set (o .paramName , o .paramValue )
28
+ return base
29
+ }
30
+
31
+ // QueryParam can be used to manually set a URL query parameter by name.
32
+ func QueryParam (name , value string ) Option {
33
+ return queryParam {
34
+ paramName : name ,
35
+ paramValue : value ,
36
+ }
37
+ }
38
+
39
+ // ResourceVersion causes watch operations to only show changes since
40
+ // a particular version of a resource.
41
+ func ResourceVersion (resourceVersion string ) Option {
42
+ return queryParam {"resourceVersion" , resourceVersion }
43
+ }
44
+
45
+ // Timeout declares the timeout for list and watch operations. Timeout
46
+ // is only accurate to the second.
47
+ func Timeout (d time.Duration ) Option {
48
+ return queryParam {
49
+ "timeoutSeconds" ,
50
+ strconv .FormatInt (int64 (d / time .Second ), 10 ),
51
+ }
52
+ }
53
+
54
+ // Subresource is a way to interact with a part of an API object without needing
55
+ // permissions on the entire resource. For example, a node isn't able to modify
56
+ // a pod object, but can update the "pods/status" subresource.
57
+ //
58
+ // Common subresources are "status" and "scale".
59
+ //
60
+ // See https://kubernetes.io/docs/reference/api-concepts/
61
+ func Subresource (name string ) Option {
62
+ return subresource {name }
63
+ }
64
+
65
+ type subresource struct {
66
+ name string
67
+ }
68
+
69
+ func (s subresource ) updateURL (base string , v url.Values ) string {
70
+ return base + "/" + s .name
71
+ }
72
+
14
73
type resourceType struct {
15
74
apiGroup string
16
75
apiVersion string
@@ -74,8 +133,10 @@ func urlFor(endpoint, apiGroup, apiVersion, namespace, resource, name string, op
74
133
75
134
v := url.Values {}
76
135
for _ , option := range options {
77
- key , val := option .queryParam ()
78
- v .Set (key , val )
136
+ e = option .updateURL (e , v )
137
+ }
138
+ if len (v ) == 0 {
139
+ return e
79
140
}
80
141
return e + "?" + v .Encode ()
81
142
}
0 commit comments