2017-04-17 10:50:02 +00:00
|
|
|
package kubernetes
|
2016-02-08 20:57:32 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-07 12:09:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-11-13 21:11:58 +00:00
|
|
|
"time"
|
|
|
|
|
2017-04-07 10:49:53 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/pkg/api"
|
|
|
|
"k8s.io/client-go/pkg/api/v1"
|
|
|
|
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
|
|
|
"k8s.io/client-go/pkg/fields"
|
|
|
|
"k8s.io/client-go/pkg/labels"
|
|
|
|
"k8s.io/client-go/pkg/runtime"
|
|
|
|
"k8s.io/client-go/pkg/watch"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2016-02-08 20:57:32 +00:00
|
|
|
)
|
|
|
|
|
2016-11-13 21:11:58 +00:00
|
|
|
const resyncPeriod = time.Minute * 5
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Client is a client for the Provider master.
|
|
|
|
// WatchAll starts the watch of the Provider ressources and updates the stores.
|
2016-11-30 18:25:22 +00:00
|
|
|
// The stores can then be accessed via the Get* functions.
|
2016-04-20 11:26:51 +00:00
|
|
|
type Client interface {
|
2016-11-11 22:50:20 +00:00
|
|
|
GetIngresses(namespaces Namespaces) []*v1beta1.Ingress
|
|
|
|
GetService(namespace, name string) (*v1.Service, bool, error)
|
2017-04-23 14:17:20 +00:00
|
|
|
GetSecret(namespace, name string) (*v1.Secret, bool, error)
|
2016-11-11 22:50:20 +00:00
|
|
|
GetEndpoints(namespace, name string) (*v1.Endpoints, bool, error)
|
2016-12-03 20:20:39 +00:00
|
|
|
WatchAll(labelSelector string, stopCh <-chan struct{}) (<-chan interface{}, error)
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientImpl struct {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingController *cache.Controller
|
|
|
|
svcController *cache.Controller
|
|
|
|
epController *cache.Controller
|
2017-04-23 14:17:20 +00:00
|
|
|
secController *cache.Controller
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
ingStore cache.Store
|
|
|
|
svcStore cache.Store
|
|
|
|
epStore cache.Store
|
2017-04-23 14:17:20 +00:00
|
|
|
secStore cache.Store
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
clientset *kubernetes.Clientset
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// NewInClusterClient returns a new Provider client that is expected to run
|
2017-03-07 12:09:11 +00:00
|
|
|
// inside the cluster.
|
|
|
|
func NewInClusterClient(endpoint string) (Client, error) {
|
2016-11-11 22:50:20 +00:00
|
|
|
config, err := rest.InClusterConfig()
|
2016-02-08 20:57:32 +00:00
|
|
|
if err != nil {
|
2017-03-07 12:09:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to create in-cluster configuration: %s", err)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2017-03-07 12:09:11 +00:00
|
|
|
|
|
|
|
if endpoint != "" {
|
|
|
|
config.Host = endpoint
|
2016-11-11 22:50:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 12:09:11 +00:00
|
|
|
return createClientFromConfig(config)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// NewExternalClusterClient returns a new Provider client that may run outside
|
2017-03-07 12:09:11 +00:00
|
|
|
// of the cluster.
|
|
|
|
// The endpoint parameter must not be empty.
|
|
|
|
func NewExternalClusterClient(endpoint, token, caFilePath string) (Client, error) {
|
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("endpoint missing for external cluster client")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &rest.Config{
|
|
|
|
Host: endpoint,
|
|
|
|
BearerToken: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
if caFilePath != "" {
|
|
|
|
caData, err := ioutil.ReadFile(caFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read CA file %s: %s", caFilePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.TLSClientConfig = rest.TLSClientConfig{CAData: caData}
|
2016-07-12 05:25:01 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2017-03-07 12:09:11 +00:00
|
|
|
return createClientFromConfig(config)
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2017-03-07 12:09:11 +00:00
|
|
|
func createClientFromConfig(c *rest.Config) (Client, error) {
|
|
|
|
clientset, err := kubernetes.NewForConfig(c)
|
2016-07-12 05:25:01 +00:00
|
|
|
if err != nil {
|
2016-11-11 22:50:20 +00:00
|
|
|
return nil, err
|
2016-07-12 05:25:01 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
return &clientImpl{
|
|
|
|
clientset: clientset,
|
|
|
|
}, nil
|
2016-07-12 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 00:23:55 +00:00
|
|
|
// GetIngresses returns all ingresses in the cluster
|
2016-11-11 22:50:20 +00:00
|
|
|
func (c *clientImpl) GetIngresses(namespaces Namespaces) []*v1beta1.Ingress {
|
|
|
|
ingList := c.ingStore.List()
|
|
|
|
result := make([]*v1beta1.Ingress, 0, len(ingList))
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
for _, obj := range ingList {
|
|
|
|
ingress := obj.(*v1beta1.Ingress)
|
|
|
|
if HasNamespace(ingress, namespaces) {
|
|
|
|
result = append(result, ingress)
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
return result
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// WatchIngresses starts the watch of Provider Ingresses resources and updates the corresponding store
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c *clientImpl) WatchIngresses(labelSelector labels.Selector, watchCh chan<- interface{}, stopCh <-chan struct{}) {
|
2016-11-11 22:50:20 +00:00
|
|
|
source := NewListWatchFromClient(
|
2017-04-07 10:49:53 +00:00
|
|
|
c.clientset.ExtensionsV1beta1().RESTClient(),
|
2016-11-11 22:50:20 +00:00
|
|
|
"ingresses",
|
|
|
|
api.NamespaceAll,
|
|
|
|
fields.Everything(),
|
|
|
|
labelSelector)
|
|
|
|
|
|
|
|
c.ingStore, c.ingController = cache.NewInformer(
|
|
|
|
source,
|
|
|
|
&v1beta1.Ingress{},
|
2016-11-13 21:11:58 +00:00
|
|
|
resyncPeriod,
|
2016-11-11 22:50:20 +00:00
|
|
|
newResourceEventHandlerFuncs(watchCh))
|
|
|
|
go c.ingController.Run(stopCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 19:35:19 +00:00
|
|
|
// eventHandlerFunc will pass the obj on to the events channel or drop it
|
|
|
|
// This is so passing the events along won't block in the case of high volume
|
|
|
|
// The events are only used for signalling anyway so dropping a few is ok
|
2016-12-03 20:20:39 +00:00
|
|
|
func eventHandlerFunc(events chan<- interface{}, obj interface{}) {
|
2016-12-02 19:35:19 +00:00
|
|
|
select {
|
|
|
|
case events <- obj:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2016-04-25 14:56:06 +00:00
|
|
|
|
2016-12-03 20:20:39 +00:00
|
|
|
func newResourceEventHandlerFuncs(events chan<- interface{}) cache.ResourceEventHandlerFuncs {
|
2016-11-11 22:50:20 +00:00
|
|
|
return cache.ResourceEventHandlerFuncs{
|
2016-12-02 19:35:19 +00:00
|
|
|
AddFunc: func(obj interface{}) { eventHandlerFunc(events, obj) },
|
|
|
|
UpdateFunc: func(old, new interface{}) { eventHandlerFunc(events, new) },
|
|
|
|
DeleteFunc: func(obj interface{}) { eventHandlerFunc(events, obj) },
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetService returns the named service from the named namespace
|
|
|
|
func (c *clientImpl) GetService(namespace, name string) (*v1.Service, bool, error) {
|
|
|
|
var service *v1.Service
|
|
|
|
item, exists, err := c.svcStore.GetByKey(namespace + "/" + name)
|
|
|
|
if item != nil {
|
|
|
|
service = item.(*v1.Service)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
return service, exists, err
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 14:17:20 +00:00
|
|
|
func (c *clientImpl) GetSecret(namespace, name string) (*v1.Secret, bool, error) {
|
|
|
|
var secret *v1.Secret
|
|
|
|
item, exists, err := c.secStore.GetByKey(namespace + "/" + name)
|
|
|
|
if err == nil && item != nil {
|
|
|
|
secret = item.(*v1.Secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret, exists, err
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// WatchServices starts the watch of Provider Service resources and updates the corresponding store
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c *clientImpl) WatchServices(watchCh chan<- interface{}, stopCh <-chan struct{}) {
|
2016-11-11 22:50:20 +00:00
|
|
|
source := cache.NewListWatchFromClient(
|
2017-04-07 10:49:53 +00:00
|
|
|
c.clientset.CoreV1().RESTClient(),
|
2016-11-11 22:50:20 +00:00
|
|
|
"services",
|
|
|
|
api.NamespaceAll,
|
|
|
|
fields.Everything())
|
|
|
|
|
|
|
|
c.svcStore, c.svcController = cache.NewInformer(
|
|
|
|
source,
|
|
|
|
&v1.Service{},
|
2016-11-13 21:11:58 +00:00
|
|
|
resyncPeriod,
|
2016-11-11 22:50:20 +00:00
|
|
|
newResourceEventHandlerFuncs(watchCh))
|
|
|
|
go c.svcController.Run(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
|
2016-11-11 22:50:20 +00:00
|
|
|
func (c *clientImpl) GetEndpoints(namespace, name string) (*v1.Endpoints, bool, error) {
|
|
|
|
var endpoint *v1.Endpoints
|
|
|
|
item, exists, err := c.epStore.GetByKey(namespace + "/" + name)
|
2016-04-25 14:56:06 +00:00
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
if item != nil {
|
|
|
|
endpoint = item.(*v1.Endpoints)
|
2016-05-20 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
return endpoint, exists, err
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// WatchEndpoints starts the watch of Provider Endpoints resources and updates the corresponding store
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c *clientImpl) WatchEndpoints(watchCh chan<- interface{}, stopCh <-chan struct{}) {
|
2016-11-11 22:50:20 +00:00
|
|
|
source := cache.NewListWatchFromClient(
|
2017-04-07 10:49:53 +00:00
|
|
|
c.clientset.CoreV1().RESTClient(),
|
2016-11-11 22:50:20 +00:00
|
|
|
"endpoints",
|
|
|
|
api.NamespaceAll,
|
|
|
|
fields.Everything())
|
|
|
|
|
|
|
|
c.epStore, c.epController = cache.NewInformer(
|
|
|
|
source,
|
|
|
|
&v1.Endpoints{},
|
2016-11-13 21:11:58 +00:00
|
|
|
resyncPeriod,
|
2016-11-11 22:50:20 +00:00
|
|
|
newResourceEventHandlerFuncs(watchCh))
|
|
|
|
go c.epController.Run(stopCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 14:17:20 +00:00
|
|
|
func (c *clientImpl) WatchSecrets(watchCh chan<- interface{}, stopCh <-chan struct{}) {
|
|
|
|
source := cache.NewListWatchFromClient(
|
|
|
|
c.clientset.CoreV1().RESTClient(),
|
|
|
|
"secrets",
|
|
|
|
api.NamespaceAll,
|
|
|
|
fields.Everything())
|
|
|
|
|
|
|
|
c.secStore, c.secController = cache.NewInformer(
|
|
|
|
source,
|
2017-06-02 01:50:50 +00:00
|
|
|
&v1.Secret{},
|
2017-04-23 14:17:20 +00:00
|
|
|
resyncPeriod,
|
|
|
|
newResourceEventHandlerFuncs(watchCh))
|
|
|
|
go c.secController.Run(stopCh)
|
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
// WatchAll returns events in the cluster and updates the stores via informer
|
|
|
|
// Filters ingresses by labelSelector
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c *clientImpl) WatchAll(labelSelector string, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
|
|
|
watchCh := make(chan interface{}, 1)
|
|
|
|
eventCh := make(chan interface{}, 1)
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
kubeLabelSelector, err := labels.Parse(labelSelector)
|
2016-04-25 14:56:06 +00:00
|
|
|
if err != nil {
|
2016-11-11 22:50:20 +00:00
|
|
|
return nil, err
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2016-12-03 20:20:39 +00:00
|
|
|
c.WatchIngresses(kubeLabelSelector, eventCh, stopCh)
|
|
|
|
c.WatchServices(eventCh, stopCh)
|
|
|
|
c.WatchEndpoints(eventCh, stopCh)
|
2017-04-23 14:17:20 +00:00
|
|
|
c.WatchSecrets(eventCh, stopCh)
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2016-04-25 14:56:06 +00:00
|
|
|
go func() {
|
|
|
|
defer close(watchCh)
|
2016-12-03 20:20:39 +00:00
|
|
|
defer close(eventCh)
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2016-04-25 14:56:06 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
2016-05-19 18:09:01 +00:00
|
|
|
return
|
2016-12-03 20:20:39 +00:00
|
|
|
case event := <-eventCh:
|
2016-11-11 22:50:20 +00:00
|
|
|
c.fireEvent(event, watchCh)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
return watchCh, nil
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
// fireEvent checks if all controllers have synced before firing
|
|
|
|
// Used after startup or a reconnect
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c *clientImpl) fireEvent(event interface{}, eventCh chan interface{}) {
|
2017-07-05 19:30:18 +00:00
|
|
|
if !c.ingController.HasSynced() || !c.svcController.HasSynced() || !c.epController.HasSynced() {
|
2016-12-03 20:20:39 +00:00
|
|
|
return
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-12-03 20:20:39 +00:00
|
|
|
eventHandlerFunc(eventCh, event)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
// HasNamespace checks if the ingress is in one of the namespaces
|
|
|
|
func HasNamespace(ingress *v1beta1.Ingress, namespaces Namespaces) bool {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return true
|
2016-05-19 08:52:17 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
for _, n := range namespaces {
|
|
|
|
if ingress.ObjectMeta.Namespace == n {
|
|
|
|
return true
|
|
|
|
}
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
return false
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, field selector and label selector.
|
|
|
|
// Extends cache.NewListWatchFromClient to support labelSelector
|
|
|
|
func NewListWatchFromClient(c cache.Getter, resource string, namespace string, fieldSelector fields.Selector, labelSelector labels.Selector) *cache.ListWatch {
|
|
|
|
listFunc := func(options api.ListOptions) (runtime.Object, error) {
|
|
|
|
return c.Get().
|
|
|
|
Namespace(namespace).
|
|
|
|
Resource(resource).
|
|
|
|
VersionedParams(&options, api.ParameterCodec).
|
|
|
|
FieldsSelectorParam(fieldSelector).
|
|
|
|
LabelsSelectorParam(labelSelector).
|
|
|
|
Do().
|
|
|
|
Get()
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
watchFunc := func(options api.ListOptions) (watch.Interface, error) {
|
|
|
|
return c.Get().
|
|
|
|
Prefix("watch").
|
|
|
|
Namespace(namespace).
|
|
|
|
Resource(resource).
|
|
|
|
VersionedParams(&options, api.ParameterCodec).
|
|
|
|
FieldsSelectorParam(fieldSelector).
|
|
|
|
LabelsSelectorParam(labelSelector).
|
|
|
|
Watch()
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|