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"
|
|
|
|
|
2018-03-22 09:14:04 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2018-02-14 08:56:04 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2018-03-22 09:14:04 +00:00
|
|
|
"k8s.io/client-go/informers"
|
2017-04-07 10:49:53 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2016-02-08 20:57:32 +00:00
|
|
|
)
|
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
const resyncPeriod = 10 * time.Minute
|
|
|
|
|
|
|
|
type resourceEventHandler struct {
|
|
|
|
ev chan<- interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnAdd(obj interface{}) {
|
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnUpdate(oldObj, newObj interface{}) {
|
|
|
|
eventHandlerFunc(reh.ev, newObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnDelete(obj interface{}) {
|
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Client is a client for the Provider master.
|
2017-08-26 10:12:44 +00:00
|
|
|
// WatchAll starts the watch of the Provider resources 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 {
|
2018-04-12 09:14:05 +00:00
|
|
|
WatchAll(namespaces Namespaces, stopCh <-chan struct{}) (<-chan interface{}, error)
|
2018-02-14 08:56:04 +00:00
|
|
|
GetIngresses() []*extensionsv1beta1.Ingress
|
|
|
|
GetService(namespace, name string) (*corev1.Service, bool, error)
|
|
|
|
GetSecret(namespace, name string) (*corev1.Secret, bool, error)
|
|
|
|
GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error)
|
2018-05-18 12:12:03 +00:00
|
|
|
UpdateIngressStatus(namespace, name, ip, hostname string) error
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientImpl struct {
|
2018-04-12 09:14:05 +00:00
|
|
|
clientset *kubernetes.Clientset
|
|
|
|
factories map[string]informers.SharedInformerFactory
|
|
|
|
ingressLabelSelector labels.Selector
|
|
|
|
isNamespaceAll bool
|
2017-10-10 14:26:03 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2018-04-12 09:14:05 +00:00
|
|
|
func newClientImpl(clientset *kubernetes.Clientset) *clientImpl {
|
2017-10-10 14:26:03 +00:00
|
|
|
return &clientImpl{
|
|
|
|
clientset: clientset,
|
2018-03-22 09:14:04 +00:00
|
|
|
factories: make(map[string]informers.SharedInformerFactory),
|
2017-10-10 14:26:03 +00:00
|
|
|
}
|
2016-02-08 20:57:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 09:14:05 +00:00
|
|
|
// newInClusterClient returns a new Provider client that is expected to run
|
2017-03-07 12:09:11 +00:00
|
|
|
// inside the cluster.
|
2018-04-12 09:14:05 +00:00
|
|
|
func newInClusterClient(endpoint string) (*clientImpl, 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
|
|
|
}
|
|
|
|
|
2018-04-12 09:14:05 +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.
|
2018-04-12 09:14:05 +00:00
|
|
|
func newExternalClusterClient(endpoint, token, caFilePath string) (*clientImpl, error) {
|
2017-03-07 12:09:11 +00:00
|
|
|
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
|
|
|
|
2018-04-12 09:14:05 +00:00
|
|
|
func createClientFromConfig(c *rest.Config) (*clientImpl, error) {
|
2017-03-07 12:09:11 +00:00
|
|
|
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
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
return newClientImpl(clientset), nil
|
2016-07-12 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
// WatchAll starts namespace-specific controllers for all relevant kinds.
|
2018-04-12 09:14:05 +00:00
|
|
|
func (c *clientImpl) WatchAll(namespaces Namespaces, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
2017-10-10 14:26:03 +00:00
|
|
|
eventCh := make(chan interface{}, 1)
|
2016-02-08 20:57:32 +00:00
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
if len(namespaces) == 0 {
|
2018-02-14 08:56:04 +00:00
|
|
|
namespaces = Namespaces{metav1.NamespaceAll}
|
2017-10-10 14:26:03 +00:00
|
|
|
c.isNamespaceAll = true
|
|
|
|
}
|
|
|
|
|
2018-04-12 09:14:05 +00:00
|
|
|
eventHandler := c.newResourceEventHandler(eventCh)
|
2017-10-10 14:26:03 +00:00
|
|
|
for _, ns := range namespaces {
|
2018-04-12 09:14:05 +00:00
|
|
|
factory := informers.NewFilteredSharedInformerFactory(c.clientset, resyncPeriod, ns, nil)
|
2018-03-22 09:14:04 +00:00
|
|
|
factory.Extensions().V1beta1().Ingresses().Informer().AddEventHandler(eventHandler)
|
|
|
|
factory.Core().V1().Services().Informer().AddEventHandler(eventHandler)
|
|
|
|
factory.Core().V1().Endpoints().Informer().AddEventHandler(eventHandler)
|
|
|
|
c.factories[ns] = factory
|
2017-10-10 14:26:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 09:14:04 +00:00
|
|
|
for _, ns := range namespaces {
|
|
|
|
c.factories[ns].Start(stopCh)
|
2017-10-10 14:26:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 09:14:04 +00:00
|
|
|
for _, ns := range namespaces {
|
|
|
|
for t, ok := range c.factories[ns].WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", t.String(), ns)
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 08:56:04 +00:00
|
|
|
}
|
2017-10-10 14:26:03 +00:00
|
|
|
|
2018-03-22 09:14:04 +00:00
|
|
|
// Do not wait for the Secrets store to get synced since we cannot rely on
|
|
|
|
// users having granted RBAC permissions for this object.
|
|
|
|
// https://github.com/containous/traefik/issues/1784 should improve the
|
|
|
|
// situation here in the future.
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
c.factories[ns].Core().V1().Secrets().Informer().AddEventHandler(eventHandler)
|
|
|
|
c.factories[ns].Start(stopCh)
|
|
|
|
}
|
2017-10-10 14:26:03 +00:00
|
|
|
|
2018-03-22 09:14:04 +00:00
|
|
|
return eventCh, nil
|
2016-12-02 19:35:19 +00:00
|
|
|
}
|
2016-04-25 14:56:06 +00:00
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
// GetIngresses returns all Ingresses for observed namespaces in the cluster.
|
2018-02-14 08:56:04 +00:00
|
|
|
func (c *clientImpl) GetIngresses() []*extensionsv1beta1.Ingress {
|
|
|
|
var result []*extensionsv1beta1.Ingress
|
2018-03-22 09:14:04 +00:00
|
|
|
for ns, factory := range c.factories {
|
2018-04-12 09:14:05 +00:00
|
|
|
ings, err := factory.Extensions().V1beta1().Ingresses().Lister().List(c.ingressLabelSelector)
|
2018-03-22 09:14:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to list ingresses in namespace %s: %s", ns, err)
|
|
|
|
}
|
|
|
|
for _, ing := range ings {
|
2017-10-10 14:26:03 +00:00
|
|
|
result = append(result, ing)
|
|
|
|
}
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2017-10-10 14:26:03 +00:00
|
|
|
return result
|
2016-11-11 22:50:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 12:12:03 +00:00
|
|
|
// UpdateIngressStatus updates an Ingress with a provided status.
|
|
|
|
func (c *clientImpl) UpdateIngressStatus(namespace, name, ip, hostname string) error {
|
|
|
|
keyName := namespace + "/" + name
|
|
|
|
|
|
|
|
item, exists, err := c.factories[c.lookupNamespace(namespace)].Extensions().V1beta1().Ingresses().Informer().GetStore().GetByKey(keyName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get ingress %s with error: %v", keyName, err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("failed to update ingress %s because it does not exist", keyName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ing := item.(*extensionsv1beta1.Ingress)
|
|
|
|
if ing.Status.LoadBalancer.Ingress[0].Hostname == hostname && ing.Status.LoadBalancer.Ingress[0].IP == ip {
|
|
|
|
// If status is already set, skip update
|
|
|
|
log.Debugf("Skipping status update on ingress %s/%s", ing.Namespace, ing.Name)
|
|
|
|
} else {
|
|
|
|
ingCopy := ing.DeepCopy()
|
|
|
|
ingCopy.Status = extensionsv1beta1.IngressStatus{LoadBalancer: corev1.LoadBalancerStatus{Ingress: []corev1.LoadBalancerIngress{{IP: ip, Hostname: hostname}}}}
|
|
|
|
|
|
|
|
_, err := c.clientset.ExtensionsV1beta1().Ingresses(ingCopy.Namespace).UpdateStatus(ingCopy)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update ingress status %s with error: %v", keyName, err)
|
|
|
|
}
|
|
|
|
log.Infof("Updated status on ingress %s", keyName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
// GetService returns the named service from the given namespace.
|
2018-02-14 08:56:04 +00:00
|
|
|
func (c *clientImpl) GetService(namespace, name string) (*corev1.Service, bool, error) {
|
|
|
|
var service *corev1.Service
|
2018-03-22 09:14:04 +00:00
|
|
|
item, exists, err := c.factories[c.lookupNamespace(namespace)].Core().V1().Services().Informer().GetStore().GetByKey(namespace + "/" + name)
|
2016-11-11 22:50:20 +00:00
|
|
|
if item != nil {
|
2018-02-14 08:56:04 +00:00
|
|
|
service = item.(*corev1.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-10-10 14:26:03 +00:00
|
|
|
// GetEndpoints returns the named endpoints from the given namespace.
|
2018-02-14 08:56:04 +00:00
|
|
|
func (c *clientImpl) GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error) {
|
|
|
|
var endpoint *corev1.Endpoints
|
2018-03-22 09:14:04 +00:00
|
|
|
item, exists, err := c.factories[c.lookupNamespace(namespace)].Core().V1().Endpoints().Informer().GetStore().GetByKey(namespace + "/" + name)
|
2016-11-11 22:50:20 +00:00
|
|
|
if item != nil {
|
2018-02-14 08:56:04 +00:00
|
|
|
endpoint = item.(*corev1.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-10-10 14:26:03 +00:00
|
|
|
// GetSecret returns the named secret from the given namespace.
|
2018-02-14 08:56:04 +00:00
|
|
|
func (c *clientImpl) GetSecret(namespace, name string) (*corev1.Secret, bool, error) {
|
|
|
|
var secret *corev1.Secret
|
2018-03-22 09:14:04 +00:00
|
|
|
item, exists, err := c.factories[c.lookupNamespace(namespace)].Core().V1().Secrets().Informer().GetStore().GetByKey(namespace + "/" + name)
|
2017-10-10 14:26:03 +00:00
|
|
|
if err == nil && item != nil {
|
2018-02-14 08:56:04 +00:00
|
|
|
secret = item.(*corev1.Secret)
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
2017-10-10 14:26:03 +00:00
|
|
|
return secret, exists, err
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 14:26:03 +00:00
|
|
|
// lookupNamespace returns the lookup namespace key for the given namespace.
|
|
|
|
// When listening on all namespaces, it returns the client-go identifier ("")
|
|
|
|
// for all-namespaces. Otherwise, it returns the given namespace.
|
|
|
|
// The distinction is necessary because we index all informers on the special
|
|
|
|
// identifier iff all-namespaces are requested but receive specific namespace
|
|
|
|
// identifiers from the Kubernetes API, so we have to bridge this gap.
|
|
|
|
func (c *clientImpl) lookupNamespace(ns string) string {
|
|
|
|
if c.isNamespaceAll {
|
2018-02-14 08:56:04 +00:00
|
|
|
return metav1.NamespaceAll
|
2016-05-19 08:52:17 +00:00
|
|
|
}
|
2017-10-10 14:26:03 +00:00
|
|
|
return ns
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 09:14:05 +00:00
|
|
|
func (c *clientImpl) newResourceEventHandler(events chan<- interface{}) cache.ResourceEventHandler {
|
|
|
|
return &cache.FilteringResourceEventHandler{
|
|
|
|
FilterFunc: func(obj interface{}) bool {
|
|
|
|
// Ignore Ingresses that do not match our custom label selector.
|
|
|
|
if ing, ok := obj.(*extensionsv1beta1.Ingress); ok {
|
|
|
|
lbls := labels.Set(ing.GetLabels())
|
|
|
|
return c.ingressLabelSelector.Matches(lbls)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
Handler: &resourceEventHandler{events},
|
|
|
|
}
|
2017-10-10 14:26:03 +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.
|
|
|
|
func eventHandlerFunc(events chan<- interface{}, obj interface{}) {
|
|
|
|
select {
|
|
|
|
case events <- obj:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|