2016-02-08 20:57:32 +00:00
|
|
|
package k8s
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-07-12 05:25:01 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-05-19 18:09:01 +00:00
|
|
|
"github.com/parnurzeal/gorequest"
|
2016-02-08 20:57:32 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// APIEndpoint defines the base path for kubernetes API resources.
|
|
|
|
APIEndpoint = "/api/v1"
|
|
|
|
extentionsEndpoint = "/apis/extensions/v1beta1"
|
|
|
|
defaultIngress = "/ingresses"
|
2016-05-25 23:53:51 +00:00
|
|
|
namespaces = "/namespaces/"
|
2016-02-08 20:57:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a client for the Kubernetes master.
|
2016-04-20 11:26:51 +00:00
|
|
|
type Client interface {
|
2016-07-12 05:25:01 +00:00
|
|
|
GetIngresses(labelSelector string, predicate func(Ingress) bool) ([]Ingress, error)
|
2016-05-25 23:53:51 +00:00
|
|
|
GetService(name, namespace string) (Service, error)
|
2016-05-20 16:34:57 +00:00
|
|
|
GetEndpoints(name, namespace string) (Endpoints, error)
|
2016-07-12 05:25:01 +00:00
|
|
|
WatchAll(labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error)
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientImpl struct {
|
2016-02-08 20:57:32 +00:00
|
|
|
endpointURL string
|
|
|
|
tls *tls.Config
|
|
|
|
token string
|
|
|
|
caCert []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Kubernetes client.
|
|
|
|
// The provided host is an url (scheme://hostname[:port]) of a
|
|
|
|
// Kubernetes master without any path.
|
|
|
|
// The provided client is an authorized http.Client used to perform requests to the Kubernetes API master.
|
2016-04-20 11:26:51 +00:00
|
|
|
func NewClient(baseURL string, caCert []byte, token string) (Client, error) {
|
2016-02-08 20:57:32 +00:00
|
|
|
validURL, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse URL %q: %v", baseURL, err)
|
|
|
|
}
|
2016-04-20 11:26:51 +00:00
|
|
|
return &clientImpl{
|
2016-02-08 20:57:32 +00:00
|
|
|
endpointURL: strings.TrimSuffix(validURL.String(), "/"),
|
|
|
|
token: token,
|
|
|
|
caCert: caCert,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
func makeQueryString(baseParams map[string]string, labelSelector string) (string, error) {
|
|
|
|
if labelSelector != "" {
|
|
|
|
baseParams["labelSelector"] = labelSelector
|
|
|
|
}
|
|
|
|
queryData, err := json.Marshal(baseParams)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(queryData), nil
|
|
|
|
}
|
|
|
|
|
2016-04-28 00:23:55 +00:00
|
|
|
// GetIngresses returns all ingresses in the cluster
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) GetIngresses(labelSelector string, predicate func(Ingress) bool) ([]Ingress, error) {
|
2016-02-08 20:57:32 +00:00
|
|
|
getURL := c.endpointURL + extentionsEndpoint + defaultIngress
|
2016-07-12 05:25:01 +00:00
|
|
|
queryParams := map[string]string{}
|
|
|
|
queryData, err := makeQueryString(queryParams, labelSelector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Had problems constructing query string %s : %v", queryParams, err)
|
|
|
|
}
|
|
|
|
body, err := c.do(c.request(getURL, queryData))
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return nil, fmt.Errorf("failed to create ingresses request: GET %q : %v", getURL, err)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ingressList IngressList
|
|
|
|
if err := json.Unmarshal(body, &ingressList); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode list of ingress resources: %v", err)
|
|
|
|
}
|
|
|
|
ingresses := ingressList.Items[:0]
|
|
|
|
for _, ingress := range ingressList.Items {
|
|
|
|
if predicate(ingress) {
|
|
|
|
ingresses = append(ingresses, ingress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ingresses, nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 14:56:06 +00:00
|
|
|
// WatchIngresses returns all ingresses in the cluster
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) WatchIngresses(labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
2016-04-25 14:56:06 +00:00
|
|
|
getURL := c.endpointURL + extentionsEndpoint + defaultIngress
|
2016-07-12 05:25:01 +00:00
|
|
|
return c.watch(getURL, labelSelector, stopCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 23:53:51 +00:00
|
|
|
// GetService returns the named service from the named namespace
|
|
|
|
func (c *clientImpl) GetService(name, namespace string) (Service, error) {
|
|
|
|
getURL := c.endpointURL + APIEndpoint + namespaces + namespace + "/services/" + name
|
2016-04-25 14:56:06 +00:00
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
body, err := c.do(c.request(getURL, ""))
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-05-25 23:53:51 +00:00
|
|
|
return Service{}, fmt.Errorf("failed to create services request: GET %q : %v", getURL, err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 23:53:51 +00:00
|
|
|
var service Service
|
|
|
|
if err := json.Unmarshal(body, &service); err != nil {
|
|
|
|
return Service{}, fmt.Errorf("failed to decode service resource: %v", err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-05-25 23:53:51 +00:00
|
|
|
return service, nil
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WatchServices returns all services in the cluster
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) WatchServices(labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
2016-04-25 14:56:06 +00:00
|
|
|
getURL := c.endpointURL + APIEndpoint + "/services"
|
2016-07-12 05:25:01 +00:00
|
|
|
return c.watch(getURL, labelSelector, stopCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:34:57 +00:00
|
|
|
// GetEndpoints returns the named Endpoints
|
|
|
|
// Endpoints have the same name as the coresponding service
|
|
|
|
func (c *clientImpl) GetEndpoints(name, namespace string) (Endpoints, error) {
|
2016-05-25 23:53:51 +00:00
|
|
|
getURL := c.endpointURL + APIEndpoint + namespaces + namespace + "/endpoints/" + name
|
2016-04-25 14:56:06 +00:00
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
body, err := c.do(c.request(getURL, ""))
|
2016-05-20 16:34:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return Endpoints{}, fmt.Errorf("failed to create endpoints request: GET %q : %v", getURL, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var endpoints Endpoints
|
|
|
|
if err := json.Unmarshal(body, &endpoints); err != nil {
|
|
|
|
return Endpoints{}, fmt.Errorf("failed to decode endpoints resources: %v", err)
|
|
|
|
}
|
|
|
|
return endpoints, nil
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:34:57 +00:00
|
|
|
// WatchEndpoints returns endpoints in the cluster
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) WatchEndpoints(labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
2016-05-20 16:34:57 +00:00
|
|
|
getURL := c.endpointURL + APIEndpoint + "/endpoints"
|
2016-07-12 05:25:01 +00:00
|
|
|
return c.watch(getURL, labelSelector, stopCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WatchAll returns events in the cluster
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) WatchAll(labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
2016-05-19 18:09:01 +00:00
|
|
|
watchCh := make(chan interface{}, 10)
|
|
|
|
errCh := make(chan error, 10)
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2016-04-25 14:56:06 +00:00
|
|
|
stopIngresses := make(chan bool)
|
2016-07-12 05:25:01 +00:00
|
|
|
chanIngresses, chanIngressesErr, err := c.WatchIngresses(labelSelector, stopIngresses)
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to create watch: %v", err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
stopServices := make(chan bool)
|
2016-07-12 05:25:01 +00:00
|
|
|
chanServices, chanServicesErr, err := c.WatchServices(labelSelector, stopServices)
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to create watch: %v", err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-05-20 16:34:57 +00:00
|
|
|
stopEndpoints := make(chan bool)
|
2016-07-12 05:25:01 +00:00
|
|
|
chanEndpoints, chanEndpointsErr, err := c.WatchEndpoints(labelSelector, stopEndpoints)
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to create watch: %v", err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer close(watchCh)
|
|
|
|
defer close(errCh)
|
|
|
|
defer close(stopIngresses)
|
|
|
|
defer close(stopServices)
|
2016-05-20 16:34:57 +00:00
|
|
|
defer close(stopEndpoints)
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2016-04-25 14:56:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
stopIngresses <- true
|
|
|
|
stopServices <- true
|
2016-05-20 16:34:57 +00:00
|
|
|
stopEndpoints <- true
|
2016-05-19 18:09:01 +00:00
|
|
|
return
|
2016-04-25 14:56:06 +00:00
|
|
|
case err := <-chanIngressesErr:
|
|
|
|
errCh <- err
|
|
|
|
case err := <-chanServicesErr:
|
|
|
|
errCh <- err
|
2016-05-20 16:34:57 +00:00
|
|
|
case err := <-chanEndpointsErr:
|
2016-04-25 14:56:06 +00:00
|
|
|
errCh <- err
|
|
|
|
case event := <-chanIngresses:
|
|
|
|
watchCh <- event
|
|
|
|
case event := <-chanServices:
|
|
|
|
watchCh <- event
|
2016-05-20 16:34:57 +00:00
|
|
|
case event := <-chanEndpoints:
|
2016-04-25 14:56:06 +00:00
|
|
|
watchCh <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return watchCh, errCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientImpl) do(request *gorequest.SuperAgent) ([]byte, error) {
|
|
|
|
res, body, errs := request.EndBytes()
|
|
|
|
if errs != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create request: GET %q : %v", request.Url, errs)
|
|
|
|
}
|
2016-05-19 18:09:01 +00:00
|
|
|
defer res.Body.Close()
|
2016-04-25 14:56:06 +00:00
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("http error %d GET %q: %q", res.StatusCode, request.Url, string(body))
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) request(reqURL string, queryContent interface{}) *gorequest.SuperAgent {
|
2016-02-08 20:57:32 +00:00
|
|
|
// Make request to Kubernetes API
|
2016-07-12 05:25:01 +00:00
|
|
|
parsedURL, parseErr := url.Parse(reqURL)
|
|
|
|
if parseErr != nil {
|
|
|
|
log.Errorf("Had issues parsing url %s. Trying anyway.", reqURL)
|
2016-05-19 08:52:17 +00:00
|
|
|
}
|
2016-07-12 05:25:01 +00:00
|
|
|
request := gorequest.New().Get(reqURL)
|
|
|
|
request.Transport.DisableKeepAlives = true
|
2016-05-19 08:52:17 +00:00
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
if parsedURL.Scheme == "https" {
|
2016-02-08 20:57:32 +00:00
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AppendCertsFromPEM(c.caCert)
|
|
|
|
c.tls = &tls.Config{RootCAs: pool}
|
2016-07-12 05:25:01 +00:00
|
|
|
request.TLSClientConfig(c.tls)
|
|
|
|
}
|
|
|
|
if len(c.token) > 0 {
|
|
|
|
request.Header["Authorization"] = "Bearer " + c.token
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2016-07-12 05:25:01 +00:00
|
|
|
request.Query(queryContent)
|
|
|
|
return request
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenericObject generic object
|
|
|
|
type GenericObject struct {
|
|
|
|
TypeMeta `json:",inline"`
|
|
|
|
ListMeta `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-07-12 05:25:01 +00:00
|
|
|
func (c *clientImpl) watch(url string, labelSelector string, stopCh <-chan bool) (chan interface{}, chan error, error) {
|
2016-05-19 18:09:01 +00:00
|
|
|
watchCh := make(chan interface{}, 10)
|
|
|
|
errCh := make(chan error, 10)
|
2016-04-25 14:56:06 +00:00
|
|
|
|
|
|
|
// get version
|
2016-07-12 05:25:01 +00:00
|
|
|
body, err := c.do(c.request(url, ""))
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to do version request: GET %q : %v", url, err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var generic GenericObject
|
|
|
|
if err := json.Unmarshal(body, &generic); err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to decode version %v", err)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
resourceVersion := generic.ResourceVersion
|
2016-07-12 05:25:01 +00:00
|
|
|
queryParams := map[string]string{"watch": "", "resourceVersion": resourceVersion}
|
|
|
|
queryData, err := makeQueryString(queryParams, labelSelector)
|
|
|
|
if err != nil {
|
|
|
|
return watchCh, errCh, fmt.Errorf("Unable to construct query args")
|
|
|
|
}
|
|
|
|
request := c.request(url, queryData)
|
2016-04-26 20:13:45 +00:00
|
|
|
req, err := request.MakeRequest()
|
2016-02-08 20:57:32 +00:00
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to make watch request: GET %q : %v", url, err)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2016-04-26 20:13:45 +00:00
|
|
|
request.Client.Transport = request.Transport
|
2016-05-19 18:09:01 +00:00
|
|
|
|
2016-02-08 20:57:32 +00:00
|
|
|
res, err := request.Client.Do(req)
|
|
|
|
if err != nil {
|
2016-04-26 20:13:45 +00:00
|
|
|
return watchCh, errCh, fmt.Errorf("failed to do watch request: GET %q: %v", url, err)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2016-05-19 18:09:01 +00:00
|
|
|
finishCh := make(chan bool)
|
|
|
|
defer close(finishCh)
|
2016-02-08 20:57:32 +00:00
|
|
|
defer close(watchCh)
|
|
|
|
defer close(errCh)
|
2016-05-19 18:09:01 +00:00
|
|
|
go func() {
|
|
|
|
defer res.Body.Close()
|
|
|
|
for {
|
|
|
|
var eventList interface{}
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&eventList); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "net/http: request canceled") {
|
|
|
|
errCh <- fmt.Errorf("failed to decode watch event: GET %q : %v", url, err)
|
|
|
|
}
|
|
|
|
finishCh <- true
|
|
|
|
return
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2016-05-19 18:09:01 +00:00
|
|
|
watchCh <- eventList
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2016-05-19 18:09:01 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
go func() {
|
|
|
|
request.Transport.CancelRequest(req)
|
|
|
|
}()
|
|
|
|
<-finishCh
|
|
|
|
return
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return watchCh, errCh, nil
|
|
|
|
}
|