Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package kubernetes contains a minimal client for the Kubernetes API. |
| 6 | package kubernetes |
| 7 | |
| 8 | import ( |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 9 | "bufio" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 10 | "bytes" |
Brad Fitzpatrick | a6dd626 | 2018-03-06 22:22:17 +0000 | [diff] [blame] | 11 | "context" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 12 | "encoding/json" |
| 13 | "fmt" |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 14 | "io" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 15 | "io/ioutil" |
Brad Fitzpatrick | c6ced0a | 2016-05-04 20:57:37 +0000 | [diff] [blame] | 16 | "log" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 17 | "net/http" |
| 18 | "net/url" |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 19 | "os" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | "time" |
| 22 | |
Evan Brown | c51d4e0 | 2015-09-08 15:56:15 -0700 | [diff] [blame] | 23 | "golang.org/x/build/kubernetes/api" |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 24 | "golang.org/x/net/context/ctxhttp" |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 27 | // Client is a client for the Kubernetes master. |
| 28 | type Client struct { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 29 | httpClient *http.Client |
| 30 | |
| 31 | // endPointURL is the Kubernetes master URL ending in |
| 32 | // "/api/v1". |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 33 | endpointURL string |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 34 | |
| 35 | namespace string // always in URL path-escaped form (for now) |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // NewClient returns a new Kubernetes client. |
| 39 | // The provided host is an url (scheme://hostname[:port]) of a |
| 40 | // Kubernetes master without any path. |
| 41 | // The provided client is an authorized http.Client used to perform requests to the Kubernetes API master. |
Heschi Kreinick | a9d7de1 | 2021-09-02 15:09:37 -0400 | [diff] [blame] | 42 | func NewClient(baseURL, namespace string, client *http.Client) (*Client, error) { |
| 43 | if namespace == "" { |
| 44 | return nil, fmt.Errorf("must specify Kubernetes namespace") |
| 45 | } |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 46 | validURL, err := url.Parse(baseURL) |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("failed to parse URL %q: %v", baseURL, err) |
| 49 | } |
| 50 | return &Client{ |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 51 | endpointURL: strings.TrimSuffix(validURL.String(), "/") + "/api/v1", |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 52 | httpClient: client, |
Heschi Kreinick | a9d7de1 | 2021-09-02 15:09:37 -0400 | [diff] [blame] | 53 | namespace: namespace, |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 54 | }, nil |
| 55 | } |
| 56 | |
Brad Fitzpatrick | adc161a | 2017-01-31 20:31:07 +0000 | [diff] [blame] | 57 | // Close closes any idle HTTP connections still connected to the Kubernetes master. |
| 58 | func (c *Client) Close() error { |
| 59 | if tr, ok := c.httpClient.Transport.(*http.Transport); ok { |
| 60 | tr.CloseIdleConnections() |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 65 | // nsEndpoint returns the API endpoint root for this client. |
| 66 | // (This has nothing to do with Service Endpoints.) |
| 67 | func (c *Client) nsEndpoint() string { |
| 68 | return c.endpointURL + "/namespaces/" + c.namespace + "/" |
| 69 | } |
| 70 | |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 71 | // RunLongLivedPod creates a new pod resource in the default pod namespace with |
| 72 | // the given pod API specification. It assumes the pod runs a |
| 73 | // long-lived server (i.e. if the container exit quickly quickly, even |
| 74 | // with success, then that is an error). |
| 75 | // |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 76 | // It returns the pod status once it has entered the Running phase. |
Evan Brown | 34ff1d9 | 2016-02-16 22:39:27 -0800 | [diff] [blame] | 77 | // An error is returned if the pod can not be created, or if ctx.Done |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 78 | // is closed. |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 79 | func (c *Client) RunLongLivedPod(ctx context.Context, pod *api.Pod) (*api.PodStatus, error) { |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 80 | var podJSON bytes.Buffer |
| 81 | if err := json.NewEncoder(&podJSON).Encode(pod); err != nil { |
| 82 | return nil, fmt.Errorf("failed to encode pod in json: %v", err) |
| 83 | } |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 84 | postURL := c.nsEndpoint() + "pods" |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 85 | req, err := http.NewRequest("POST", postURL, &podJSON) |
| 86 | if err != nil { |
| 87 | return nil, fmt.Errorf("failed to create request: POST %q : %v", postURL, err) |
| 88 | } |
| 89 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 90 | if err != nil { |
| 91 | return nil, fmt.Errorf("failed to make request: POST %q: %v", postURL, err) |
| 92 | } |
| 93 | body, err := ioutil.ReadAll(res.Body) |
| 94 | res.Body.Close() |
| 95 | if err != nil { |
| 96 | return nil, fmt.Errorf("failed to read request body for POST %q: %v", postURL, err) |
| 97 | } |
| 98 | if res.StatusCode != http.StatusCreated { |
| 99 | return nil, fmt.Errorf("http error: %d POST %q: %q: %v", res.StatusCode, postURL, string(body), err) |
| 100 | } |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 101 | var podResult api.Pod |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 102 | if err := json.Unmarshal(body, &podResult); err != nil { |
| 103 | return nil, fmt.Errorf("failed to decode pod resources: %v", err) |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 104 | } |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 105 | |
Evan Brown | 34ff1d9 | 2016-02-16 22:39:27 -0800 | [diff] [blame] | 106 | for { |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 107 | // TODO(bradfitz,evanbrown): pass podResult.ObjectMeta.ResourceVersion to PodStatus? |
| 108 | ps, err := c.PodStatus(ctx, podResult.Name) |
Evan Brown | 34ff1d9 | 2016-02-16 22:39:27 -0800 | [diff] [blame] | 109 | if err != nil { |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 110 | return nil, err |
Evan Brown | 34ff1d9 | 2016-02-16 22:39:27 -0800 | [diff] [blame] | 111 | } |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 112 | switch ps.Phase { |
| 113 | case api.PodPending: |
| 114 | // The main phase we're waiting on |
| 115 | break |
| 116 | case api.PodRunning: |
| 117 | return ps, nil |
| 118 | case api.PodSucceeded, api.PodFailed: |
| 119 | return nil, fmt.Errorf("pod entered phase %q", ps.Phase) |
| 120 | default: |
| 121 | log.Printf("RunLongLivedPod poll loop: pod %q in unexpected phase %q; sleeping", podResult.Name, ps.Phase) |
| 122 | } |
| 123 | select { |
| 124 | case <-time.After(5 * time.Second): |
| 125 | case <-ctx.Done(): |
| 126 | // The pod did not leave the pending |
| 127 | // state. Try to clean it up. |
| 128 | go c.DeletePod(context.Background(), podResult.Name) |
| 129 | return nil, ctx.Err() |
| 130 | } |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 131 | } |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 134 | func (c *Client) do(ctx context.Context, method, urlStr string, dst interface{}) error { |
| 135 | req, err := http.NewRequest(method, urlStr, nil) |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 136 | if err != nil { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 137 | return err |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 138 | } |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 139 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 140 | if err != nil { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 141 | return err |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 142 | } |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 143 | defer res.Body.Close() |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 144 | if res.StatusCode != http.StatusOK { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 145 | body, _ := ioutil.ReadAll(res.Body) |
| 146 | return fmt.Errorf("%v %s: %v, %s", method, urlStr, res.Status, body) |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 147 | } |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 148 | if dst != nil { |
| 149 | var r io.Reader = res.Body |
| 150 | if false && strings.Contains(urlStr, "endpoints") { // for debugging |
| 151 | r = io.TeeReader(r, os.Stderr) |
| 152 | } |
| 153 | return json.NewDecoder(r).Decode(dst) |
| 154 | } |
| 155 | return nil |
| 156 | } |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 157 | |
Brad Fitzpatrick | 7449a82 | 2017-02-11 00:46:40 +0000 | [diff] [blame] | 158 | // GetServices returns all services in the cluster, regardless of status. |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 159 | func (c *Client) GetServices(ctx context.Context) ([]api.Service, error) { |
| 160 | var list api.ServiceList |
| 161 | if err := c.do(ctx, "GET", c.nsEndpoint()+"services", &list); err != nil { |
| 162 | return nil, err |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 163 | } |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 164 | return list.Items, nil |
| 165 | } |
| 166 | |
| 167 | // Endpoint represents a service endpoint address. |
| 168 | type Endpoint struct { |
| 169 | IP string |
| 170 | Port int |
| 171 | PortName string |
| 172 | Protocol string // "TCP" or "UDP"; never empty |
| 173 | } |
| 174 | |
| 175 | // GetServiceEndpoints returns the endpoints for the named service. |
Brad Fitzpatrick | 7449a82 | 2017-02-11 00:46:40 +0000 | [diff] [blame] | 176 | // If portName is non-empty, only endpoints matching that port name are returned. |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 177 | func (c *Client) GetServiceEndpoints(ctx context.Context, serviceName, portName string) ([]Endpoint, error) { |
| 178 | var res api.Endpoints |
| 179 | // TODO: path escape serviceName? |
| 180 | if err := c.do(ctx, "GET", c.nsEndpoint()+"endpoints/"+serviceName, &res); err != nil { |
| 181 | return nil, err |
| 182 | } |
| 183 | var ep []Endpoint |
| 184 | for _, ss := range res.Subsets { |
| 185 | for _, port := range ss.Ports { |
| 186 | if portName != "" && port.Name != portName { |
| 187 | continue |
| 188 | } |
| 189 | for _, addr := range ss.Addresses { |
| 190 | proto := string(port.Protocol) |
| 191 | if proto == "" { |
| 192 | proto = "TCP" |
| 193 | } |
| 194 | ep = append(ep, Endpoint{ |
| 195 | IP: addr.IP, |
| 196 | Port: port.Port, |
| 197 | PortName: port.Name, |
| 198 | Protocol: proto, |
| 199 | }) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return ep, nil |
| 204 | } |
| 205 | |
| 206 | // GetPods returns all pods in the cluster, regardless of status. |
| 207 | func (c *Client) GetPods(ctx context.Context) ([]api.Pod, error) { |
| 208 | var list api.PodList |
| 209 | if err := c.do(ctx, "GET", c.nsEndpoint()+"pods", &list); err != nil { |
| 210 | return nil, err |
| 211 | } |
| 212 | return list.Items, nil |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // PodDelete deletes the specified Kubernetes pod. |
| 216 | func (c *Client) DeletePod(ctx context.Context, podName string) error { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 217 | url := c.nsEndpoint() + "pods/" + podName |
Brad Fitzpatrick | 71265ac | 2016-11-23 05:58:31 +0000 | [diff] [blame] | 218 | req, err := http.NewRequest("DELETE", url, strings.NewReader(`{"gracePeriodSeconds":0}`)) |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 219 | if err != nil { |
| 220 | return fmt.Errorf("failed to create request: DELETE %q : %v", url, err) |
| 221 | } |
| 222 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 223 | if err != nil { |
| 224 | return fmt.Errorf("failed to make request: DELETE %q: %v", url, err) |
| 225 | } |
| 226 | body, err := ioutil.ReadAll(res.Body) |
| 227 | res.Body.Close() |
| 228 | if err != nil { |
Kevin Burke | b7a944e | 2017-02-12 01:18:14 -0800 | [diff] [blame] | 229 | return fmt.Errorf("failed to read response body: DELETE %q: %v", url, err) |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 230 | } |
| 231 | if res.StatusCode != http.StatusOK { |
| 232 | return fmt.Errorf("http error: %d DELETE %q: %q: %v", res.StatusCode, url, string(body), err) |
| 233 | } |
| 234 | return nil |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 237 | // TODO(bradfitz): WatchPod is unreliable, so this is disabled. |
| 238 | // |
| 239 | // AwaitPodNotPending will return a pod's status in a |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 240 | // podStatusResult when the pod is no longer in the pending |
| 241 | // state. |
| 242 | // The podResourceVersion is required to prevent a pod's entire |
| 243 | // history from being retrieved when the watch is initiated. |
| 244 | // If there is an error polling for the pod's status, or if |
| 245 | // ctx.Done is closed, podStatusResult will contain an error. |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 246 | func (c *Client) _AwaitPodNotPending(ctx context.Context, podName, podResourceVersion string) (*api.Pod, error) { |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 247 | if podResourceVersion == "" { |
| 248 | return nil, fmt.Errorf("resourceVersion for pod %v must be provided", podName) |
| 249 | } |
| 250 | ctx, cancel := context.WithCancel(ctx) |
| 251 | defer cancel() |
| 252 | |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 253 | podStatusUpdates, err := c._WatchPod(ctx, podName, podResourceVersion) |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 254 | if err != nil { |
| 255 | return nil, err |
| 256 | } |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 257 | for { |
| 258 | select { |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 259 | case <-ctx.Done(): |
| 260 | return nil, ctx.Err() |
| 261 | case psr := <-podStatusUpdates: |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 262 | if psr.Err != nil { |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 263 | // If the context is done, prefer its error: |
| 264 | select { |
| 265 | case <-ctx.Done(): |
| 266 | return nil, ctx.Err() |
| 267 | default: |
| 268 | return nil, psr.Err |
| 269 | } |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 270 | } |
Evan Brown | 2e452e1 | 2015-11-20 09:43:56 -0800 | [diff] [blame] | 271 | if psr.Pod.Status.Phase != api.PodPending { |
| 272 | return psr.Pod, nil |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 278 | // PodStatusResult wraps an api.PodStatus and error. |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 279 | type PodStatusResult struct { |
Evan Brown | 2e452e1 | 2015-11-20 09:43:56 -0800 | [diff] [blame] | 280 | Pod *api.Pod |
| 281 | Type string |
| 282 | Err error |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | type watchPodStatus struct { |
| 286 | // The type of watch update contained in the message |
| 287 | Type string `json:"type"` |
| 288 | // Pod details |
| 289 | Object api.Pod `json:"object"` |
| 290 | } |
| 291 | |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 292 | // TODO(bradfitz): WatchPod is unreliable and sometimes hangs forever |
| 293 | // without closing and sometimes ends prematurely, so this API is |
| 294 | // disabled. |
| 295 | // |
Evan Brown | 2e452e1 | 2015-11-20 09:43:56 -0800 | [diff] [blame] | 296 | // WatchPod long-polls the Kubernetes watch API to be notified |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 297 | // of changes to the specified pod. Changes are sent on the returned |
| 298 | // PodStatusResult channel as they are received. |
| 299 | // The podResourceVersion is required to prevent a pod's entire |
| 300 | // history from being retrieved when the watch is initiated. |
| 301 | // The provided context must be canceled or timed out to stop the watch. |
| 302 | // If any error occurs communicating with the Kubernetes API, the |
| 303 | // error will be sent on the returned PodStatusResult channel and |
| 304 | // it will be closed. |
Brad Fitzpatrick | 4a3f4a8 | 2016-05-05 18:29:41 +0000 | [diff] [blame] | 305 | func (c *Client) _WatchPod(ctx context.Context, podName, podResourceVersion string) (<-chan PodStatusResult, error) { |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 306 | if podResourceVersion == "" { |
| 307 | return nil, fmt.Errorf("resourceVersion for pod %v must be provided", podName) |
| 308 | } |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 309 | statusChan := make(chan PodStatusResult, 1) |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 310 | |
| 311 | go func() { |
| 312 | defer close(statusChan) |
Brad Fitzpatrick | c6ced0a | 2016-05-04 20:57:37 +0000 | [diff] [blame] | 313 | ctx, cancel := context.WithCancel(ctx) |
| 314 | defer cancel() |
| 315 | |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 316 | // Make request to Kubernetes API |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 317 | getURL := c.endpointURL + "/watch/namespaces/" + c.namespace + "/pods/" + podName |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 318 | req, err := http.NewRequest("GET", getURL, nil) |
| 319 | req.URL.Query().Add("resourceVersion", podResourceVersion) |
| 320 | if err != nil { |
| 321 | statusChan <- PodStatusResult{Err: fmt.Errorf("failed to create request: GET %q : %v", getURL, err)} |
| 322 | return |
| 323 | } |
| 324 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 325 | if err != nil { |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 326 | statusChan <- PodStatusResult{Err: err} |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 327 | return |
| 328 | } |
Evan Brown | 34ff1d9 | 2016-02-16 22:39:27 -0800 | [diff] [blame] | 329 | defer res.Body.Close() |
Brad Fitzpatrick | c6ced0a | 2016-05-04 20:57:37 +0000 | [diff] [blame] | 330 | if res.StatusCode != 200 { |
| 331 | statusChan <- PodStatusResult{Err: fmt.Errorf("WatchPod status %v", res.Status)} |
| 332 | return |
| 333 | } |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 334 | reader := bufio.NewReader(res.Body) |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 335 | |
| 336 | // bufio.Reader.ReadBytes is blocking, so we watch for |
| 337 | // context timeout or cancellation in a goroutine |
| 338 | // and close the response body when see see it. The |
| 339 | // response body is also closed via defer when the |
| 340 | // request is made, but closing twice is OK. |
| 341 | go func() { |
| 342 | <-ctx.Done() |
| 343 | res.Body.Close() |
| 344 | }() |
| 345 | |
Brad Fitzpatrick | c6ced0a | 2016-05-04 20:57:37 +0000 | [diff] [blame] | 346 | const backupPollDuration = 30 * time.Second |
| 347 | backupPoller := time.AfterFunc(backupPollDuration, func() { |
| 348 | log.Printf("kubernetes: backup poller in WatchPod checking on %q", podName) |
| 349 | st, err := c.PodStatus(ctx, podName) |
| 350 | log.Printf("kubernetes: backup poller in WatchPod PodStatus(%q) = %v, %v", podName, st, err) |
| 351 | if err != nil { |
| 352 | // Some error. |
| 353 | cancel() |
| 354 | } |
| 355 | }) |
| 356 | defer backupPoller.Stop() |
| 357 | |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 358 | for { |
| 359 | line, err := reader.ReadBytes('\n') |
Brad Fitzpatrick | c6ced0a | 2016-05-04 20:57:37 +0000 | [diff] [blame] | 360 | log.Printf("kubernetes WatchPod status line of %q: %q, %v", podName, line, err) |
| 361 | backupPoller.Reset(backupPollDuration) |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 362 | if err != nil { |
| 363 | statusChan <- PodStatusResult{Err: fmt.Errorf("error reading streaming response body: %v", err)} |
| 364 | return |
| 365 | } |
Brad Fitzpatrick | 90f7179 | 2016-04-08 19:03:58 +0000 | [diff] [blame] | 366 | var wps watchPodStatus |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 367 | if err := json.Unmarshal(line, &wps); err != nil { |
| 368 | statusChan <- PodStatusResult{Err: fmt.Errorf("failed to decode watch pod status: %v", err)} |
| 369 | return |
| 370 | } |
Evan Brown | 2e452e1 | 2015-11-20 09:43:56 -0800 | [diff] [blame] | 371 | statusChan <- PodStatusResult{Pod: &wps.Object, Type: wps.Type} |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 372 | } |
| 373 | }() |
| 374 | return statusChan, nil |
| 375 | } |
| 376 | |
| 377 | // Retrieve the status of a pod synchronously from the Kube |
| 378 | // API server. |
| 379 | func (c *Client) PodStatus(ctx context.Context, podName string) (*api.PodStatus, error) { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 380 | getURL := c.nsEndpoint() + "pods/" + podName // TODO: escape podName? |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 381 | |
| 382 | // Make request to Kubernetes API |
| 383 | req, err := http.NewRequest("GET", getURL, nil) |
| 384 | if err != nil { |
| 385 | return nil, fmt.Errorf("failed to create request: GET %q : %v", getURL, err) |
| 386 | } |
| 387 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 388 | if err != nil { |
| 389 | return nil, fmt.Errorf("failed to make request: GET %q: %v", getURL, err) |
| 390 | } |
| 391 | |
| 392 | body, err := ioutil.ReadAll(res.Body) |
| 393 | res.Body.Close() |
| 394 | if err != nil { |
| 395 | return nil, fmt.Errorf("failed to read request body for GET %q: %v", getURL, err) |
| 396 | } |
| 397 | if res.StatusCode != http.StatusOK { |
| 398 | return nil, fmt.Errorf("http error %d GET %q: %q: %v", res.StatusCode, getURL, string(body), err) |
| 399 | } |
| 400 | |
| 401 | var pod *api.Pod |
| 402 | if err := json.Unmarshal(body, &pod); err != nil { |
| 403 | return nil, fmt.Errorf("failed to decode pod resources: %v", err) |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 404 | } |
| 405 | return &pod.Status, nil |
| 406 | } |
| 407 | |
| 408 | // PodLog retrieves the container log for the first container |
| 409 | // in the pod. |
| 410 | func (c *Client) PodLog(ctx context.Context, podName string) (string, error) { |
| 411 | // TODO(evanbrown): support multiple containers |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 412 | url := c.nsEndpoint() + "pods/" + podName + "/log" // TODO: escape podName? |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 413 | req, err := http.NewRequest("GET", url, nil) |
| 414 | if err != nil { |
| 415 | return "", fmt.Errorf("failed to create request: GET %q : %v", url, err) |
Evan Brown | 956434c | 2015-10-02 15:38:22 -0700 | [diff] [blame] | 416 | } |
Brad Fitzpatrick | 304492c | 2016-07-19 02:30:34 +0000 | [diff] [blame] | 417 | res, err := ctxhttp.Do(ctx, c.httpClient, req) |
| 418 | if err != nil { |
| 419 | return "", fmt.Errorf("failed to make request: GET %q: %v", url, err) |
| 420 | } |
| 421 | body, err := ioutil.ReadAll(res.Body) |
| 422 | res.Body.Close() |
| 423 | if err != nil { |
| 424 | return "", fmt.Errorf("failed to read response body: GET %q: %v", url, err) |
| 425 | } |
| 426 | if res.StatusCode != http.StatusOK { |
| 427 | return "", fmt.Errorf("http error %d GET %q: %q: %v", res.StatusCode, url, string(body), err) |
| 428 | } |
| 429 | return string(body), nil |
Johan Euphrosine | 093044a | 2015-05-12 14:21:46 -0700 | [diff] [blame] | 430 | } |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 431 | |
| 432 | // PodNodes returns the list of nodes that comprise the Kubernetes cluster |
| 433 | func (c *Client) GetNodes(ctx context.Context) ([]api.Node, error) { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 434 | var list api.NodeList |
Brad Fitzpatrick | c5562d0 | 2017-02-17 21:58:52 +0000 | [diff] [blame] | 435 | if err := c.do(ctx, "GET", c.endpointURL+"/nodes", &list); err != nil { |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 436 | return nil, err |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 437 | } |
Brad Fitzpatrick | b30f506 | 2017-02-09 22:41:29 +0000 | [diff] [blame] | 438 | return list.Items, nil |
Evan Brown | 83f9748 | 2015-10-17 20:35:55 -0700 | [diff] [blame] | 439 | } |