forked from kubernetes/kubernetes
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
321 lines (293 loc) · 8.13 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
"github.com/golang/glog"
)
// specialParams lists parameters that are handled specially and which users of Request
// are therefore not allowed to set manually.
var specialParams = util.NewStringSet("sync", "timeout")
// Request allows for building up a request to a server in a chained fashion.
// Any errors are stored until the end of your call, so you only have to
// check once.
type Request struct {
c *RESTClient
err error
verb string
path string
body io.Reader
params map[string]string
selector labels.Selector
timeout time.Duration
sync bool
pollPeriod time.Duration
}
// Path appends an item to the request path. You must call Path at least once.
func (r *Request) Path(item string) *Request {
if r.err != nil {
return r
}
r.path = path.Join(r.path, item)
return r
}
// Sync sets sync/async call status by setting the "sync" parameter to "true"/"false".
func (r *Request) Sync(sync bool) *Request {
if r.err != nil {
return r
}
r.sync = sync
return r
}
// AbsPath overwrites an existing path with the path parameter.
func (r *Request) AbsPath(path string) *Request {
if r.err != nil {
return r
}
r.path = path
return r
}
// ParseSelectorParam parses the given string as a resource label selector.
// This is a convenience function so you don't have to first check that it's a
// validly formatted selector.
func (r *Request) ParseSelectorParam(paramName, item string) *Request {
if r.err != nil {
return r
}
sel, err := labels.ParseSelector(item)
if err != nil {
r.err = err
return r
}
return r.setParam(paramName, sel.String())
}
// SelectorParam adds the given selector as a query parameter with the name paramName.
func (r *Request) SelectorParam(paramName string, s labels.Selector) *Request {
if r.err != nil {
return r
}
return r.setParam(paramName, s.String())
}
// UintParam creates a query parameter with the given value.
func (r *Request) UintParam(paramName string, u uint64) *Request {
if r.err != nil {
return r
}
return r.setParam(paramName, strconv.FormatUint(u, 10))
}
// Param creates a query parameter with the given string value.
func (r *Request) Param(paramName, s string) *Request {
if r.err != nil {
return r
}
return r.setParam(paramName, s)
}
func (r *Request) setParam(paramName, value string) *Request {
if specialParams.Has(paramName) {
r.err = fmt.Errorf("must set %v through the corresponding function, not directly.", paramName)
return r
}
r.params[paramName] = value
return r
}
// Timeout makes the request use the given duration as a timeout. Sets the "timeout"
// parameter. Ignored if sync=false.
func (r *Request) Timeout(d time.Duration) *Request {
if r.err != nil {
return r
}
r.timeout = d
return r
}
// Body makes the request use obj as the body. Optional.
// If obj is a string, try to read a file of that name.
// If obj is a []byte, send it directly.
// If obj is an io.Reader, use it directly.
// If obj is a runtime.Object, marshal it correctly.
// Otherwise, set an error.
func (r *Request) Body(obj interface{}) *Request {
if r.err != nil {
return r
}
switch t := obj.(type) {
case string:
data, err := ioutil.ReadFile(t)
if err != nil {
r.err = err
return r
}
r.body = bytes.NewBuffer(data)
case []byte:
r.body = bytes.NewBuffer(t)
case io.Reader:
r.body = t
case runtime.Object:
data, err := r.c.Codec.Encode(t)
if err != nil {
r.err = err
return r
}
r.body = bytes.NewBuffer(data)
default:
r.err = fmt.Errorf("Unknown type used for body: %#v", obj)
}
return r
}
// PollPeriod sets the poll period.
// If the server sends back a "working" status message, then repeatedly poll the server
// to see if the operation has completed yet, waiting 'd' between each poll.
// If you want to handle the "working" status yourself (it'll be delivered as StatusErr),
// set d to 0 to turn off this behavior.
func (r *Request) PollPeriod(d time.Duration) *Request {
if r.err != nil {
return r
}
r.pollPeriod = d
return r
}
func (r *Request) finalURL() string {
finalURL := *r.c.baseURL
finalURL.Path = r.path
query := url.Values{}
for key, value := range r.params {
query.Add(key, value)
}
// sync and timeout are handled specially here, to allow setting them
// in any order.
if r.sync {
query.Add("sync", "true")
if r.timeout != 0 {
query.Add("timeout", r.timeout.String())
}
}
finalURL.RawQuery = query.Encode()
return finalURL.String()
}
// Watch attempts to begin watching the requested location.
// Returns a watch.Interface, or an error.
func (r *Request) Watch() (watch.Interface, error) {
if r.err != nil {
return nil, r.err
}
req, err := http.NewRequest(r.verb, r.finalURL(), r.body)
if err != nil {
return nil, err
}
client := r.c.Client
if client == nil {
client = http.DefaultClient
}
response, err := client.Do(req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Got status: %v", response.StatusCode)
}
return watch.NewStreamWatcher(watchjson.NewDecoder(response.Body, r.c.Codec)), nil
}
// Stream formats and executes the request, and offers streaming of the response.
// Returns io.ReadCloser which could be used for streaming of the response, or an error
func (r *Request) Stream() (io.ReadCloser, error) {
if r.err != nil {
return nil, r.err
}
req, err := http.NewRequest(r.verb, r.finalURL(), nil)
if err != nil {
return nil, err
}
client := r.c.Client
if client == nil {
client = http.DefaultClient
}
response, err := client.Do(req)
if err != nil {
return nil, err
}
return response.Body, nil
}
// Do formats and executes the request. Returns the API object received, or an error.
func (r *Request) Do() Result {
for {
if r.err != nil {
return Result{err: r.err}
}
req, err := http.NewRequest(r.verb, r.finalURL(), r.body)
if err != nil {
return Result{err: err}
}
respBody, err := r.c.doRequest(req)
if err != nil {
if s, ok := err.(APIStatus); ok {
status := s.Status()
if status.Status == api.StatusWorking && r.pollPeriod != 0 {
if status.Details != nil {
id := status.Details.ID
if len(id) > 0 {
glog.Infof("Waiting for completion of /operations/%s", id)
time.Sleep(r.pollPeriod)
// Make a poll request
pollOp := r.c.PollFor(id).PollPeriod(r.pollPeriod)
// Could also say "return r.Do()" but this way doesn't grow the callstack.
r = pollOp
continue
}
}
}
}
}
return Result{respBody, err, r.c.Codec}
}
}
// Result contains the result of calling Request.Do().
type Result struct {
body []byte
err error
codec runtime.Codec
}
// Raw returns the raw result.
func (r Result) Raw() ([]byte, error) {
return r.body, r.err
}
// Get returns the result as an object.
func (r Result) Get() (runtime.Object, error) {
if r.err != nil {
return nil, r.err
}
return r.codec.Decode(r.body)
}
// Into stores the result into obj, if possible.
func (r Result) Into(obj runtime.Object) error {
if r.err != nil {
return r.err
}
return r.codec.DecodeInto(r.body, obj)
}
// Error returns the error executing the request, nil if no error occurred.
func (r Result) Error() error {
return r.err
}