2019-03-14 14:56:06 +00:00
|
|
|
package ingress
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-14 16:36:06 +00:00
|
|
|
"context"
|
2019-02-21 22:08:05 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-30 16:56:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-02-21 22:08:05 +00:00
|
|
|
"time"
|
|
|
|
|
2020-07-21 13:32:04 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
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
|
|
|
traefikversion "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
|
|
|
netv1 "k8s.io/api/networking/v1"
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-05-14 16:36:06 +00:00
|
|
|
const (
|
|
|
|
resyncPeriod = 10 * time.Minute
|
|
|
|
defaultTimeout = 5 * time.Second
|
|
|
|
)
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
GetIngresses() []*netv1.Ingress
|
|
|
|
GetIngressClasses() ([]*netv1.IngressClass, error)
|
2019-02-21 22:08:05 +00:00
|
|
|
GetService(namespace, name string) (*corev1.Service, bool, error)
|
|
|
|
GetSecret(namespace, name string) (*corev1.Secret, bool, error)
|
2024-02-27 09:54:04 +00:00
|
|
|
GetNodes() ([]*corev1.Node, bool, error)
|
2019-02-21 22:08:05 +00:00
|
|
|
GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error)
|
2023-04-03 08:06:06 +00:00
|
|
|
UpdateIngressStatus(ing *netv1.Ingress, ingStatus []netv1.IngressLoadBalancerIngress) error
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
type clientWrapper struct {
|
2023-04-17 08:56:36 +00:00
|
|
|
clientset kclientset.Interface
|
2024-02-27 09:54:04 +00:00
|
|
|
factoryClusterScope kinformers.SharedInformerFactory
|
2023-04-17 08:56:36 +00:00
|
|
|
factoriesKube map[string]kinformers.SharedInformerFactory
|
|
|
|
factoriesSecret map[string]kinformers.SharedInformerFactory
|
|
|
|
factoriesIngress map[string]kinformers.SharedInformerFactory
|
|
|
|
clusterFactory kinformers.SharedInformerFactory
|
2022-12-22 15:30:05 +00:00
|
|
|
ingressLabelSelector string
|
|
|
|
isNamespaceAll bool
|
|
|
|
disableIngressClassInformer bool
|
|
|
|
watchedNamespaces []string
|
|
|
|
serverVersion *version.Version
|
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}
|
|
|
|
}
|
|
|
|
return createClientFromConfig(config)
|
|
|
|
}
|
|
|
|
|
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/ingress",
|
|
|
|
filepath.Base(os.Args[0]),
|
|
|
|
traefikversion.Version,
|
|
|
|
runtime.GOOS,
|
|
|
|
runtime.GOARCH,
|
|
|
|
)
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
clientset, err := kclientset.NewForConfig(c)
|
2019-02-21 22:08:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClientImpl(clientset), nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func newClientImpl(clientset kclientset.Interface) *clientWrapper {
|
2019-03-14 14:56:06 +00:00
|
|
|
return &clientWrapper{
|
2020-11-19 23:18:04 +00:00
|
|
|
clientset: clientset,
|
2023-04-17 08:56:36 +00:00
|
|
|
factoriesSecret: make(map[string]kinformers.SharedInformerFactory),
|
|
|
|
factoriesIngress: make(map[string]kinformers.SharedInformerFactory),
|
|
|
|
factoriesKube: make(map[string]kinformers.SharedInformerFactory),
|
2019-03-14 14:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-20 11:02:10 +00:00
|
|
|
// Get and store the serverVersion for future use.
|
|
|
|
serverVersionInfo, err := c.clientset.Discovery().ServerVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not retrieve server version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverVersion, err := version.NewVersion(serverVersionInfo.GitVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse server version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.serverVersion = serverVersion
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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.ingressLabelSelector
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
for _, ns := range namespaces {
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryIngress := kinformers.NewSharedInformerFactoryWithOptions(c.clientset, resyncPeriod, kinformers.WithNamespace(ns), kinformers.WithTweakListOptions(matchesLabelSelector))
|
2021-03-15 10:16:04 +00:00
|
|
|
|
2023-06-20 08:26:05 +00:00
|
|
|
_, err = factoryIngress.Networking().V1().Ingresses().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-15 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 23:18:04 +00:00
|
|
|
c.factoriesIngress[ns] = factoryIngress
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryKube := kinformers.NewSharedInformerFactoryWithOptions(c.clientset, 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 23:18:04 +00:00
|
|
|
c.factoriesKube[ns] = factoryKube
|
2020-11-19 13:32:03 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factorySecret := kinformers.NewSharedInformerFactoryWithOptions(c.clientset, 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
|
|
|
|
}
|
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.clientset, 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 {
|
2020-11-19 23:18:04 +00:00
|
|
|
c.factoriesIngress[ns].Start(stopCh)
|
2020-11-19 13:32:03 +00:00
|
|
|
c.factoriesKube[ns].Start(stopCh)
|
|
|
|
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 {
|
2020-11-19 23:18:04 +00:00
|
|
|
for typ, ok := range c.factoriesIngress[ns].WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", typ, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:32:03 +00:00
|
|
|
for typ, ok := range c.factoriesKube[ns].WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", typ, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for typ, ok := range c.factoriesSecret[ns].WaitForCacheSync(stopCh) {
|
2019-02-21 22:08:05 +00:00
|
|
|
if !ok {
|
2020-07-15 17:18:03 +00:00
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s in namespace %q", typ, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:26:05 +00:00
|
|
|
if !c.disableIngressClassInformer {
|
2023-04-17 08:56:36 +00:00
|
|
|
c.clusterFactory = kinformers.NewSharedInformerFactoryWithOptions(c.clientset, resyncPeriod)
|
2021-05-17 14:50:09 +00:00
|
|
|
|
2023-06-20 08:26:05 +00:00
|
|
|
_, err = c.clusterFactory.Networking().V1().IngressClasses().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-17 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 17:18:03 +00:00
|
|
|
c.clusterFactory.Start(stopCh)
|
|
|
|
|
|
|
|
for typ, ok := range c.clusterFactory.WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s", typ)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventCh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIngresses returns all Ingresses for observed namespaces in the cluster.
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetIngresses() []*netv1.Ingress {
|
|
|
|
var results []*netv1.Ingress
|
2020-01-16 09:14:06 +00:00
|
|
|
|
2021-03-15 10:16:04 +00:00
|
|
|
for ns, factory := range c.factoriesIngress {
|
2023-06-20 08:26:05 +00:00
|
|
|
// networking
|
|
|
|
listNew, err := factory.Networking().V1().Ingresses().Lister().List(labels.Everything())
|
2020-01-16 09:14:06 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list ingresses in namespace %s", ns)
|
2021-03-15 10:16:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:26:05 +00:00
|
|
|
results = append(results, listNew...)
|
2021-03-15 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 08:26:05 +00:00
|
|
|
return results
|
2021-03-15 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
// UpdateIngressStatus updates an Ingress with a provided status.
|
2023-04-03 08:06:06 +00:00
|
|
|
func (c *clientWrapper) UpdateIngressStatus(src *netv1.Ingress, ingStatus []netv1.IngressLoadBalancerIngress) error {
|
2020-01-16 09:14:06 +00:00
|
|
|
if !c.isWatchedNamespace(src.Namespace) {
|
|
|
|
return fmt.Errorf("failed to get ingress %s/%s: namespace is not within watched namespaces", src.Namespace, src.Name)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 10:16:04 +00:00
|
|
|
ing, err := c.factoriesIngress[c.lookupNamespace(src.Namespace)].Networking().V1().Ingresses().Lister().Ingresses(src.Namespace).Get(src.Name)
|
2019-02-21 22:08:05 +00:00
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return fmt.Errorf("failed to get ingress %s/%s: %w", src.Namespace, src.Name, err)
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.With().Str("namespace", ing.Namespace).Str("ingress", ing.Name).Logger()
|
2020-09-15 11:48:32 +00:00
|
|
|
|
|
|
|
if isLoadBalancerIngressEquals(ing.Status.LoadBalancer.Ingress, ingStatus) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Skipping ingress status update")
|
2020-09-15 11:48:32 +00:00
|
|
|
return nil
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
2020-01-16 09:14:06 +00:00
|
|
|
|
|
|
|
ingCopy := ing.DeepCopy()
|
2023-04-03 08:06:06 +00:00
|
|
|
ingCopy.Status = netv1.IngressStatus{LoadBalancer: netv1.IngressLoadBalancerStatus{Ingress: ingStatus}}
|
2020-01-16 09:14:06 +00:00
|
|
|
|
2020-05-14 16:36:06 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-03-15 10:16:04 +00:00
|
|
|
_, err = c.clientset.NetworkingV1().Ingresses(ingCopy.Namespace).UpdateStatus(ctx, ingCopy, metav1.UpdateOptions{})
|
2020-01-16 09:14:06 +00:00
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return fmt.Errorf("failed to update ingress status %s/%s: %w", src.Namespace, src.Name, err)
|
2020-01-16 09:14:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Info().Msg("Updated ingress status")
|
2020-01-16 09:14:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:48:32 +00:00
|
|
|
// isLoadBalancerIngressEquals returns true if the given slices are equal, false otherwise.
|
2023-04-03 08:06:06 +00:00
|
|
|
func isLoadBalancerIngressEquals(aSlice, bSlice []netv1.IngressLoadBalancerIngress) bool {
|
2020-09-15 11:48:32 +00:00
|
|
|
if len(aSlice) != len(bSlice) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
aMap := make(map[string]struct{})
|
|
|
|
for _, aIngress := range aSlice {
|
|
|
|
aMap[aIngress.Hostname+aIngress.IP] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bIngress := range bSlice {
|
|
|
|
if _, exists := aMap[bIngress.Hostname+bIngress.IP]; !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:32:03 +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)
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:32:03 +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
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetIngressClasses() ([]*netv1.IngressClass, error) {
|
2020-07-15 17:18:03 +00:00
|
|
|
if c.clusterFactory == nil {
|
2021-01-28 14:08:04 +00:00
|
|
|
return nil, errors.New("cluster factory not loaded")
|
2020-07-15 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
var ics []*netv1.IngressClass
|
2021-05-17 14:50:09 +00:00
|
|
|
ingressClasses, err := c.clusterFactory.Networking().V1().IngressClasses().Lister().List(labels.Everything())
|
2020-07-15 17:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ic := range ingressClasses {
|
|
|
|
if ic.Spec.Controller == traefikDefaultIngressClassController {
|
2021-01-28 14:08:04 +00:00
|
|
|
ics = append(ics, ic)
|
2020-07-15 17:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:08:04 +00:00
|
|
|
return ics, nil
|
2020-07-15 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2019-02-21 22:08:05 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2020-07-21 13:32:04 +00:00
|
|
|
|
2021-03-02 20:34:03 +00:00
|
|
|
// filterIngressClassByName return a slice containing ingressclasses with the correct name.
|
2023-04-17 08:56:36 +00:00
|
|
|
func filterIngressClassByName(ingressClassName string, ics []*netv1.IngressClass) []*netv1.IngressClass {
|
|
|
|
var ingressClasses []*netv1.IngressClass
|
2021-03-02 20:34:03 +00:00
|
|
|
|
|
|
|
for _, ic := range ics {
|
|
|
|
if ic.Name == ingressClassName {
|
|
|
|
ingressClasses = append(ingressClasses, ic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ingressClasses
|
|
|
|
}
|