2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-23 00:02:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientInterface holds the methods for clients of Kubenetes, an interface to allow mock testing
|
|
|
|
type ClientInterface interface {
|
2014-06-23 00:02:48 +00:00
|
|
|
ListPods(selector labels.Selector) (api.PodList, error)
|
2014-06-09 05:38:45 +00:00
|
|
|
GetPod(name string) (api.Pod, error)
|
|
|
|
DeletePod(name string) error
|
|
|
|
CreatePod(api.Pod) (api.Pod, error)
|
|
|
|
UpdatePod(api.Pod) (api.Pod, error)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
GetReplicationController(name string) (api.ReplicationController, error)
|
|
|
|
CreateReplicationController(api.ReplicationController) (api.ReplicationController, error)
|
|
|
|
UpdateReplicationController(api.ReplicationController) (api.ReplicationController, error)
|
|
|
|
DeleteReplicationController(string) error
|
|
|
|
|
|
|
|
GetService(name string) (api.Service, error)
|
|
|
|
CreateService(api.Service) (api.Service, error)
|
|
|
|
UpdateService(api.Service) (api.Service, error)
|
|
|
|
DeleteService(string) error
|
|
|
|
}
|
|
|
|
|
2014-06-23 01:14:32 +00:00
|
|
|
// StatusErr might get returned from an api call if your request is still being processed
|
|
|
|
// and hence the expected return data is not available yet.
|
|
|
|
type StatusErr struct {
|
|
|
|
Status api.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StatusErr) Error() string {
|
2014-06-26 23:10:38 +00:00
|
|
|
return fmt.Sprintf("Status: %v (%#v)", s.Status.Status, s.Status)
|
2014-06-23 01:14:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
// AuthInfo is used to store authorization information
|
|
|
|
type AuthInfo struct {
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client is the actual implementation of a Kubernetes client.
|
|
|
|
// Host is the http://... base for the URL
|
|
|
|
type Client struct {
|
2014-06-22 21:18:01 +00:00
|
|
|
host string
|
|
|
|
auth *AuthInfo
|
2014-06-06 23:40:48 +00:00
|
|
|
httpClient *http.Client
|
|
|
|
}
|
|
|
|
|
2014-06-22 21:18:01 +00:00
|
|
|
// Create a new client object.
|
|
|
|
func New(host string, auth *AuthInfo) *Client {
|
|
|
|
return &Client{
|
|
|
|
auth: auth,
|
|
|
|
host: host,
|
|
|
|
httpClient: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute a request, adds authentication (if auth != nil), and HTTPS cert ignoring.
|
|
|
|
func (c *Client) doRequest(request *http.Request) ([]byte, error) {
|
|
|
|
if c.auth != nil {
|
|
|
|
request.SetBasicAuth(c.auth.User, c.auth.Password)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
response, err := c.httpClient.Do(request)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-06-24 04:55:11 +00:00
|
|
|
return nil, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
|
|
|
}
|
2014-06-26 23:10:38 +00:00
|
|
|
|
2014-07-02 20:51:27 +00:00
|
|
|
// Did the server give us a status response?
|
|
|
|
isStatusResponse := false
|
2014-06-26 23:10:38 +00:00
|
|
|
var status api.Status
|
|
|
|
if err := api.DecodeInto(body, &status); err == nil && status.Status != "" {
|
2014-07-02 20:51:27 +00:00
|
|
|
isStatusResponse = true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case response.StatusCode == http.StatusConflict:
|
|
|
|
// Return error given by server, if there was one.
|
|
|
|
if isStatusResponse {
|
|
|
|
return nil, &StatusErr{status}
|
2014-06-23 01:14:32 +00:00
|
|
|
}
|
2014-07-02 20:51:27 +00:00
|
|
|
fallthrough
|
|
|
|
case response.StatusCode < http.StatusOK || response.StatusCode > http.StatusPartialContent:
|
|
|
|
return nil, fmt.Errorf("request [%#v] failed (%d) %s: %s", request, response.StatusCode, response.Status, string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the server gave us a status back, look at what it was.
|
|
|
|
if isStatusResponse && status.Status != api.StatusSuccess {
|
2014-06-26 23:10:38 +00:00
|
|
|
// "Working" requests need to be handled specially.
|
|
|
|
// "Failed" requests are clearly just an error and it makes sense to return them as such.
|
|
|
|
return nil, &StatusErr{status}
|
2014-06-23 01:14:32 +00:00
|
|
|
}
|
2014-06-22 21:18:01 +00:00
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Underlying base implementation of performing a request.
|
|
|
|
// method is the HTTP method (e.g. "GET")
|
|
|
|
// path is the path on the host to hit
|
|
|
|
// requestBody is the body of the request. Can be nil.
|
|
|
|
// target the interface to marshal the JSON response into. Can be nil.
|
|
|
|
func (c *Client) rawRequest(method, path string, requestBody io.Reader, target interface{}) ([]byte, error) {
|
|
|
|
request, err := http.NewRequest(method, c.makeURL(path), requestBody)
|
|
|
|
if err != nil {
|
2014-06-24 04:55:11 +00:00
|
|
|
return nil, err
|
2014-06-22 21:18:01 +00:00
|
|
|
}
|
|
|
|
body, err := c.doRequest(request)
|
|
|
|
if err != nil {
|
|
|
|
return body, err
|
2014-06-13 23:26:38 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
if target != nil {
|
2014-06-22 21:18:01 +00:00
|
|
|
err = api.DecodeInto(body, target)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("Failed to parse: %s\n", string(body))
|
2014-06-06 23:40:48 +00:00
|
|
|
// FIXME: no need to return err here?
|
|
|
|
}
|
|
|
|
return body, err
|
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) makeURL(path string) string {
|
2014-06-22 21:18:01 +00:00
|
|
|
return client.host + "/api/v1beta1/" + path
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 23:47:41 +00:00
|
|
|
// ListPods takes a selector, and returns the list of pods that match that selector
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) ListPods(selector labels.Selector) (result api.PodList, err error) {
|
|
|
|
err = client.Get().Path("pods").Selector(selector).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
// GetPod takes the name of the pod, and returns the corresponding Pod object, and an error if it occurs
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) GetPod(name string) (result api.Pod, err error) {
|
|
|
|
err = client.Get().Path("pods").Path(name).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
// DeletePod takes the name of the pod, and returns an error if one occurs
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) DeletePod(name string) error {
|
|
|
|
return client.Delete().Path("pods").Path(name).Do().Error()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
// CreatePod takes the representation of a pod. Returns the server's representation of the pod, and an error, if it occurs
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) CreatePod(pod api.Pod) (result api.Pod, err error) {
|
|
|
|
err = client.Post().Path("pods").Body(pod).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 05:38:45 +00:00
|
|
|
// UpdatePod takes the representation of a pod to update. Returns the server's representation of the pod, and an error, if it occurs
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) UpdatePod(pod api.Pod) (result api.Pod, err error) {
|
|
|
|
err = client.Put().Path("pods").Path(pod.ID).Body(pod).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReplicationController returns information about a particular replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) GetReplicationController(name string) (result api.ReplicationController, err error) {
|
|
|
|
err = client.Get().Path("replicationControllers").Path(name).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateReplicationController creates a new replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) CreateReplicationController(controller api.ReplicationController) (result api.ReplicationController, err error) {
|
|
|
|
err = client.Post().Path("replicationControllers").Body(controller).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateReplicationController updates an existing replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) UpdateReplicationController(controller api.ReplicationController) (result api.ReplicationController, err error) {
|
|
|
|
err = client.Put().Path("replicationControllers").Path(controller.ID).Body(controller).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) DeleteReplicationController(name string) error {
|
|
|
|
return client.Delete().Path("replicationControllers").Path(name).Do().Error()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReplicationController returns information about a particular replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) GetService(name string) (result api.Service, err error) {
|
|
|
|
err = client.Get().Path("services").Path(name).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateReplicationController creates a new replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) CreateService(svc api.Service) (result api.Service, err error) {
|
|
|
|
err = client.Post().Path("services").Body(svc).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateReplicationController updates an existing replication controller
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) UpdateService(svc api.Service) (result api.Service, err error) {
|
|
|
|
err = client.Put().Path("services").Path(svc.ID).Body(svc).Do().Into(&result)
|
|
|
|
return
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 00:02:48 +00:00
|
|
|
func (client *Client) DeleteService(name string) error {
|
|
|
|
return client.Delete().Path("services").Path(name).Do().Error()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|