2019-03-14 14:56:06 +00:00
|
|
|
package crd
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-30 16:56:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-02-21 22:08:05 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-04-17 08:56:36 +00:00
|
|
|
traefikclientset "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/generated/clientset/versioned"
|
|
|
|
traefikinformers "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/generated/informers/externalversions"
|
|
|
|
traefikv1alpha1 "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/traefikio/v1alpha1"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/provider/kubernetes/k8s"
|
2024-01-11 16:06:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/version"
|
2019-02-21 22:08:05 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-04-17 08:56:36 +00:00
|
|
|
kerror "k8s.io/apimachinery/pkg/api/errors"
|
2019-02-21 22:08:05 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2023-04-17 08:56:36 +00:00
|
|
|
kinformers "k8s.io/client-go/informers"
|
|
|
|
kclientset "k8s.io/client-go/kubernetes"
|
2019-02-21 22:08:05 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2019-03-11 13:54:05 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2019-02-21 22:08:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const resyncPeriod = 10 * time.Minute
|
|
|
|
|
|
|
|
// Client is a client for the Provider master.
|
|
|
|
// WatchAll starts the watch of the Provider resources and updates the stores.
|
|
|
|
// The stores can then be accessed via the Get* functions.
|
|
|
|
type Client interface {
|
2019-06-17 09:48:05 +00:00
|
|
|
WatchAll(namespaces []string, stopCh <-chan struct{}) (<-chan interface{}, error)
|
2023-04-17 08:56:36 +00:00
|
|
|
GetIngressRoutes() []*traefikv1alpha1.IngressRoute
|
|
|
|
GetIngressRouteTCPs() []*traefikv1alpha1.IngressRouteTCP
|
|
|
|
GetIngressRouteUDPs() []*traefikv1alpha1.IngressRouteUDP
|
|
|
|
GetMiddlewares() []*traefikv1alpha1.Middleware
|
|
|
|
GetMiddlewareTCPs() []*traefikv1alpha1.MiddlewareTCP
|
|
|
|
GetTraefikService(namespace, name string) (*traefikv1alpha1.TraefikService, bool, error)
|
|
|
|
GetTraefikServices() []*traefikv1alpha1.TraefikService
|
|
|
|
GetTLSOptions() []*traefikv1alpha1.TLSOption
|
|
|
|
GetServersTransports() []*traefikv1alpha1.ServersTransport
|
|
|
|
GetServersTransportTCPs() []*traefikv1alpha1.ServersTransportTCP
|
|
|
|
GetTLSStores() []*traefikv1alpha1.TLSStore
|
2019-02-21 22:08:05 +00:00
|
|
|
GetService(namespace, name string) (*corev1.Service, bool, error)
|
|
|
|
GetSecret(namespace, name string) (*corev1.Secret, bool, error)
|
|
|
|
GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error)
|
2024-02-27 09:54:04 +00:00
|
|
|
GetNodes() ([]*corev1.Node, bool, error)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
// TODO: add tests for the clientWrapper (and its methods) itself.
|
|
|
|
type clientWrapper struct {
|
2023-04-17 08:56:36 +00:00
|
|
|
csCrd traefikclientset.Interface
|
|
|
|
csKube kclientset.Interface
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2024-02-27 09:54:04 +00:00
|
|
|
factoryClusterScope kinformers.SharedInformerFactory
|
|
|
|
factoriesCrd map[string]traefikinformers.SharedInformerFactory
|
|
|
|
factoriesKube map[string]kinformers.SharedInformerFactory
|
|
|
|
factoriesSecret map[string]kinformers.SharedInformerFactory
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2020-11-19 23:18:04 +00:00
|
|
|
labelSelector string
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
isNamespaceAll bool
|
2019-06-17 09:48:05 +00:00
|
|
|
watchedNamespaces []string
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createClientFromConfig(c *rest.Config) (*clientWrapper, error) {
|
2020-10-30 16:56:03 +00:00
|
|
|
c.UserAgent = fmt.Sprintf(
|
|
|
|
"%s/%s (%s/%s) kubernetes/crd",
|
|
|
|
filepath.Base(os.Args[0]),
|
|
|
|
version.Version,
|
|
|
|
runtime.GOOS,
|
|
|
|
runtime.GOARCH,
|
|
|
|
)
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
csCrd, err := traefikclientset.NewForConfig(c)
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
csKube, err := kclientset.NewForConfig(c)
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClientImpl(csKube, csCrd), nil
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func newClientImpl(csKube kclientset.Interface, csCrd traefikclientset.Interface) *clientWrapper {
|
2019-03-14 14:56:06 +00:00
|
|
|
return &clientWrapper{
|
2020-11-19 13:32:03 +00:00
|
|
|
csCrd: csCrd,
|
|
|
|
csKube: csKube,
|
2023-04-17 08:56:36 +00:00
|
|
|
factoriesCrd: make(map[string]traefikinformers.SharedInformerFactory),
|
|
|
|
factoriesKube: make(map[string]kinformers.SharedInformerFactory),
|
|
|
|
factoriesSecret: make(map[string]kinformers.SharedInformerFactory),
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInClusterClient returns a new Provider client that is expected to run
|
|
|
|
// inside the cluster.
|
2019-03-14 14:56:06 +00:00
|
|
|
func newInClusterClient(endpoint string) (*clientWrapper, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to create in-cluster configuration: %w", err)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint != "" {
|
|
|
|
config.Host = endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return createClientFromConfig(config)
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
func newExternalClusterClientFromFile(file string) (*clientWrapper, error) {
|
2019-03-11 13:54:05 +00:00
|
|
|
configFromFlags, err := clientcmd.BuildConfigFromFlags("", file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return createClientFromConfig(configFromFlags)
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
// newExternalClusterClient returns a new Provider client that may run outside
|
|
|
|
// of the cluster.
|
|
|
|
// The endpoint parameter must not be empty.
|
2024-01-11 16:06:06 +00:00
|
|
|
func newExternalClusterClient(endpoint, caFilePath string, token types.FileOrContent) (*clientWrapper, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("endpoint missing for external cluster client")
|
|
|
|
}
|
|
|
|
|
2024-01-11 16:06:06 +00:00
|
|
|
tokenData, err := token.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read token: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
config := &rest.Config{
|
|
|
|
Host: endpoint,
|
2024-01-11 16:06:06 +00:00
|
|
|
BearerToken: string(tokenData),
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if caFilePath != "" {
|
2021-03-04 19:08:03 +00:00
|
|
|
caData, err := os.ReadFile(caFilePath)
|
2019-02-21 22:08:05 +00:00
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("failed to read CA file %s: %w", caFilePath, err)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.TLSClientConfig = rest.TLSClientConfig{CAData: caData}
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
return createClientFromConfig(config)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WatchAll starts namespace-specific controllers for all relevant kinds.
|
2019-06-17 09:48:05 +00:00
|
|
|
func (c *clientWrapper) WatchAll(namespaces []string, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
eventCh := make(chan interface{}, 1)
|
2021-04-29 14:20:03 +00:00
|
|
|
eventHandler := &k8s.ResourceEventHandler{Ev: eventCh}
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
if len(namespaces) == 0 {
|
2019-06-17 09:48:05 +00:00
|
|
|
namespaces = []string{metav1.NamespaceAll}
|
2019-02-21 22:08:05 +00:00
|
|
|
c.isNamespaceAll = true
|
|
|
|
}
|
2020-11-19 23:18:04 +00:00
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
c.watchedNamespaces = namespaces
|
|
|
|
|
2020-11-19 13:32:03 +00:00
|
|
|
notOwnedByHelm := func(opts *metav1.ListOptions) {
|
|
|
|
opts.LabelSelector = "owner!=helm"
|
|
|
|
}
|
|
|
|
|
2020-11-19 23:18:04 +00:00
|
|
|
matchesLabelSelector := func(opts *metav1.ListOptions) {
|
|
|
|
opts.LabelSelector = c.labelSelector
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
for _, ns := range namespaces {
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryCrd := traefikinformers.NewSharedInformerFactoryWithOptions(c.csCrd, resyncPeriod, traefikinformers.WithNamespace(ns), traefikinformers.WithTweakListOptions(matchesLabelSelector))
|
2023-03-27 10:14:05 +00:00
|
|
|
_, err := factoryCrd.Traefik().V1alpha1().IngressRoutes().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().Middlewares().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().MiddlewareTCPs().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().IngressRouteTCPs().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().IngressRouteUDPs().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().TLSOptions().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().ServersTransports().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 09:34:00 +00:00
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().ServersTransportTCPs().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 09:34:00 +00:00
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().TLSStores().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-17 09:34:00 +00:00
|
|
|
_, err = factoryCrd.Traefik().V1alpha1().TraefikServices().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryKube := kinformers.NewSharedInformerFactoryWithOptions(c.csKube, resyncPeriod, kinformers.WithNamespace(ns))
|
2023-03-27 10:14:05 +00:00
|
|
|
_, err = factoryKube.Core().V1().Services().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryKube.Core().V1().Endpoints().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-19 13:32:03 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factorySecret := kinformers.NewSharedInformerFactoryWithOptions(c.csKube, resyncPeriod, kinformers.WithNamespace(ns), kinformers.WithTweakListOptions(notOwnedByHelm))
|
2023-03-27 10:14:05 +00:00
|
|
|
_, err = factorySecret.Core().V1().Secrets().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
c.factoriesCrd[ns] = factoryCrd
|
|
|
|
c.factoriesKube[ns] = factoryKube
|
2020-11-19 13:32:03 +00:00
|
|
|
c.factoriesSecret[ns] = factorySecret
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:54:04 +00:00
|
|
|
c.factoryClusterScope = kinformers.NewSharedInformerFactory(c.csKube, resyncPeriod)
|
|
|
|
_, err := c.factoryClusterScope.Core().V1().Nodes().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
for _, ns := range namespaces {
|
2019-03-14 14:56:06 +00:00
|
|
|
c.factoriesCrd[ns].Start(stopCh)
|
|
|
|
c.factoriesKube[ns].Start(stopCh)
|
2020-11-19 13:32:03 +00:00
|
|
|
c.factoriesSecret[ns].Start(stopCh)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
2024-02-27 09:54:04 +00:00
|
|
|
c.factoryClusterScope.Start(stopCh)
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
for _, ns := range namespaces {
|
2019-03-14 14:56:06 +00:00
|
|
|
for t, ok := range c.factoriesCrd[ns].WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", t.String(), ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for t, ok := range c.factoriesKube[ns].WaitForCacheSync(stopCh) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", t.String(), ns)
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 13:32:03 +00:00
|
|
|
|
|
|
|
for t, ok := range c.factoriesSecret[ns].WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", t.String(), ns)
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:54:04 +00:00
|
|
|
for t, ok := range c.factoryClusterScope.WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s", t.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
return eventCh, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetIngressRoutes() []*traefikv1alpha1.IngressRoute {
|
|
|
|
var result []*traefikv1alpha1.IngressRoute
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
ings, err := factory.Traefik().V1alpha1().IngressRoutes().Lister().List(labels.Everything())
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list ingress routes in namespace %s", ns)
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
result = append(result, ings...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetIngressRouteTCPs() []*traefikv1alpha1.IngressRouteTCP {
|
|
|
|
var result []*traefikv1alpha1.IngressRouteTCP
|
2019-06-11 13:12:04 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
ings, err := factory.Traefik().V1alpha1().IngressRouteTCPs().Lister().List(labels.Everything())
|
2019-06-11 13:12:04 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list tcp ingress routes in namespace %s", ns)
|
2019-06-11 13:12:04 +00:00
|
|
|
}
|
|
|
|
result = append(result, ings...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2019-06-11 13:12:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetIngressRouteUDPs() []*traefikv1alpha1.IngressRouteUDP {
|
|
|
|
var result []*traefikv1alpha1.IngressRouteUDP
|
2020-02-26 11:28:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
ings, err := factory.Traefik().V1alpha1().IngressRouteUDPs().Lister().List(labels.Everything())
|
2020-02-26 11:28:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list udp ingress routes in namespace %s", ns)
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
result = append(result, ings...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2020-02-26 11:28:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetMiddlewares() []*traefikv1alpha1.Middleware {
|
|
|
|
var result []*traefikv1alpha1.Middleware
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
middlewares, err := factory.Traefik().V1alpha1().Middlewares().Lister().List(labels.Everything())
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list middlewares in namespace %s", ns)
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
2019-10-07 11:12:05 +00:00
|
|
|
result = append(result, middlewares...)
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetMiddlewareTCPs() []*traefikv1alpha1.MiddlewareTCP {
|
|
|
|
var result []*traefikv1alpha1.MiddlewareTCP
|
2021-06-11 13:30:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
|
|
|
middlewares, err := factory.Traefik().V1alpha1().MiddlewareTCPs().Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list TCP middlewares in namespace %s", ns)
|
2021-06-11 13:30:05 +00:00
|
|
|
}
|
|
|
|
result = append(result, middlewares...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2021-06-11 13:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 18:28:04 +00:00
|
|
|
// GetTraefikService returns the named service from the given namespace.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTraefikService(namespace, name string) (*traefikv1alpha1.TraefikService, bool, error) {
|
2019-11-14 18:28:04 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get service %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
service, err := c.factoriesCrd[c.lookupNamespace(namespace)].Traefik().V1alpha1().TraefikServices().Lister().TraefikServices(namespace).Get(name)
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
|
|
|
|
return service, exist, err
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTraefikServices() []*traefikv1alpha1.TraefikService {
|
|
|
|
var result []*traefikv1alpha1.TraefikService
|
2019-11-14 18:28:04 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2023-03-20 14:38:08 +00:00
|
|
|
traefikServices, err := factory.Traefik().V1alpha1().TraefikServices().Lister().List(labels.Everything())
|
2019-11-14 18:28:04 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list Traefik services in namespace %s", ns)
|
2019-11-14 18:28:04 +00:00
|
|
|
}
|
2023-03-20 14:38:08 +00:00
|
|
|
result = append(result, traefikServices...)
|
2019-11-14 18:28:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2019-11-14 18:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
// GetServersTransports returns all ServersTransport.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetServersTransports() []*traefikv1alpha1.ServersTransport {
|
|
|
|
var result []*traefikv1alpha1.ServersTransport
|
2020-09-11 13:40:03 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-20 10:30:07 +00:00
|
|
|
serversTransports, err := factory.Traefik().V1alpha1().ServersTransports().Lister().List(labels.Everything())
|
2020-09-11 13:40:03 +00:00
|
|
|
if err != nil {
|
2023-03-21 11:00:46 +00:00
|
|
|
log.Error().Err(err).Str("namespace", ns).Msg("Failed to list servers transport in namespace")
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
result = append(result, serversTransports...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
// GetServersTransportTCPs returns all ServersTransportTCP.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetServersTransportTCPs() []*traefikv1alpha1.ServersTransportTCP {
|
|
|
|
var result []*traefikv1alpha1.ServersTransportTCP
|
2022-12-09 08:58:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
|
|
|
serversTransports, err := factory.Traefik().V1alpha1().ServersTransportTCPs().Lister().List(labels.Everything())
|
|
|
|
if err != nil {
|
2023-03-21 11:00:46 +00:00
|
|
|
log.Error().Err(err).Str("namespace", ns).Msg("Failed to list servers transport TCP in namespace")
|
2022-12-09 08:58:05 +00:00
|
|
|
}
|
|
|
|
result = append(result, serversTransports...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2022-12-09 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:14:06 +00:00
|
|
|
// GetTLSOptions returns all TLS options.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTLSOptions() []*traefikv1alpha1.TLSOption {
|
|
|
|
var result []*traefikv1alpha1.TLSOption
|
2019-06-21 15:18:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
options, err := factory.Traefik().V1alpha1().TLSOptions().Lister().List(labels.Everything())
|
2019-06-21 15:18:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list tls options in namespace %s", ns)
|
2019-06-21 15:18:05 +00:00
|
|
|
}
|
|
|
|
result = append(result, options...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2019-06-21 15:18:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 16:14:06 +00:00
|
|
|
// GetTLSStores returns all TLS stores.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTLSStores() []*traefikv1alpha1.TLSStore {
|
|
|
|
var result []*traefikv1alpha1.TLSStore
|
2020-02-24 16:14:06 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesCrd {
|
2020-11-19 23:18:04 +00:00
|
|
|
stores, err := factory.Traefik().V1alpha1().TLSStores().Lister().List(labels.Everything())
|
2020-02-24 16:14:06 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list tls stores in namespace %s", ns)
|
2020-02-24 16:14:06 +00:00
|
|
|
}
|
|
|
|
result = append(result, stores...)
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:40:06 +00:00
|
|
|
return result
|
2020-02-24 16:14:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
// GetService returns the named service from the given namespace.
|
2019-03-14 14:56:06 +00:00
|
|
|
func (c *clientWrapper) GetService(namespace, name string) (*corev1.Service, bool, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get service %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
service, err := c.factoriesKube[c.lookupNamespace(namespace)].Core().V1().Services().Lister().Services(namespace).Get(name)
|
2019-02-21 22:08:05 +00:00
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
return service, exist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEndpoints returns the named endpoints from the given namespace.
|
2019-03-14 14:56:06 +00:00
|
|
|
func (c *clientWrapper) GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get endpoints %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
endpoint, err := c.factoriesKube[c.lookupNamespace(namespace)].Core().V1().Endpoints().Lister().Endpoints(namespace).Get(name)
|
2019-02-21 22:08:05 +00:00
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
return endpoint, exist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSecret returns the named secret from the given namespace.
|
2019-03-14 14:56:06 +00:00
|
|
|
func (c *clientWrapper) GetSecret(namespace, name string) (*corev1.Secret, bool, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get secret %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:32:03 +00:00
|
|
|
secret, err := c.factoriesSecret[c.lookupNamespace(namespace)].Core().V1().Secrets().Lister().Secrets(namespace).Get(name)
|
2019-02-21 22:08:05 +00:00
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
return secret, exist, err
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:54:04 +00:00
|
|
|
func (c *clientWrapper) GetNodes() ([]*corev1.Node, bool, error) {
|
|
|
|
nodes, err := c.factoryClusterScope.Core().V1().Nodes().Lister().List(labels.Everything())
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
return nodes, exist, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +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.
|
2019-03-14 14:56:06 +00:00
|
|
|
func (c *clientWrapper) lookupNamespace(ns string) string {
|
2019-02-21 22:08:05 +00:00
|
|
|
if c.isNamespaceAll {
|
|
|
|
return metav1.NamespaceAll
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWatchedNamespace checks to ensure that the namespace is being watched before we request
|
|
|
|
// it to ensure we don't panic by requesting an out-of-watch object.
|
2019-03-14 14:56:06 +00:00
|
|
|
func (c *clientWrapper) isWatchedNamespace(ns string) bool {
|
2019-02-21 22:08:05 +00:00
|
|
|
if c.isNamespaceAll {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, watchedNamespace := range c.watchedNamespaces {
|
|
|
|
if watchedNamespace == ns {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-03-20 14:38:08 +00:00
|
|
|
|
|
|
|
// translateNotFoundError will translate a "not found" error to a boolean return
|
|
|
|
// value which indicates if the resource exists and a nil error.
|
|
|
|
func translateNotFoundError(err error) (bool, error) {
|
2023-04-17 08:56:36 +00:00
|
|
|
if kerror.IsNotFound(err) {
|
2023-03-20 14:38:08 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return err == nil, err
|
|
|
|
}
|