2020-12-15 15:40:05 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-04 19:08:03 +00:00
|
|
|
"os"
|
2024-07-29 13:48:05 +00:00
|
|
|
"reflect"
|
2024-02-19 14:44:03 +00:00
|
|
|
"slices"
|
2020-12-15 15:40:05 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-11 16:06:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2020-12-15 15:40:05 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2024-06-21 12:56:03 +00:00
|
|
|
discoveryv1 "k8s.io/api/discovery/v1"
|
2023-04-17 08:56:36 +00:00
|
|
|
kerror "k8s.io/apimachinery/pkg/api/errors"
|
2020-12-15 15:40:05 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2024-06-21 12:56:03 +00:00
|
|
|
"k8s.io/apimachinery/pkg/selection"
|
2024-05-01 04:38:03 +00:00
|
|
|
ktypes "k8s.io/apimachinery/pkg/types"
|
2023-04-17 08:56:36 +00:00
|
|
|
kinformers "k8s.io/client-go/informers"
|
|
|
|
kclientset "k8s.io/client-go/kubernetes"
|
2020-12-15 15:40:05 +00:00
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2024-07-11 09:26:03 +00:00
|
|
|
"k8s.io/client-go/util/retry"
|
2024-01-09 09:28:05 +00:00
|
|
|
gatev1 "sigs.k8s.io/gateway-api/apis/v1"
|
2023-04-17 08:56:36 +00:00
|
|
|
gatev1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
|
2024-09-17 08:50:04 +00:00
|
|
|
gatev1alpha3 "sigs.k8s.io/gateway-api/apis/v1alpha3"
|
2024-01-30 15:44:05 +00:00
|
|
|
gatev1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
|
2024-01-09 09:28:05 +00:00
|
|
|
gateclientset "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
|
|
|
|
gateinformers "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"
|
2020-12-15 15:40:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const resyncPeriod = 10 * time.Minute
|
|
|
|
|
|
|
|
type resourceEventHandler struct {
|
|
|
|
ev chan<- interface{}
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (reh *resourceEventHandler) OnAdd(obj interface{}, _ bool) {
|
2020-12-15 15:40:05 +00:00
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (reh *resourceEventHandler) OnUpdate(_, newObj interface{}) {
|
2024-01-30 15:44:05 +00:00
|
|
|
eventHandlerFunc(reh.ev, newObj)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnDelete(obj interface{}) {
|
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2023-04-17 08:56:36 +00:00
|
|
|
csGateway gateclientset.Interface
|
|
|
|
csKube kclientset.Interface
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryNamespace kinformers.SharedInformerFactory
|
|
|
|
factoryGatewayClass gateinformers.SharedInformerFactory
|
|
|
|
factoriesGateway map[string]gateinformers.SharedInformerFactory
|
|
|
|
factoriesKube map[string]kinformers.SharedInformerFactory
|
|
|
|
factoriesSecret map[string]kinformers.SharedInformerFactory
|
2020-12-15 15:40:05 +00:00
|
|
|
|
|
|
|
isNamespaceAll bool
|
|
|
|
watchedNamespaces []string
|
|
|
|
|
2024-04-02 15:32:04 +00:00
|
|
|
labelSelector string
|
|
|
|
experimentalChannel bool
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createClientFromConfig(c *rest.Config) (*clientWrapper, error) {
|
2023-04-17 08:56:36 +00:00
|
|
|
csGateway, err := gateclientset.NewForConfig(c)
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
csKube, err := kclientset.NewForConfig(c)
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newClientImpl(csKube, csGateway), nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func newClientImpl(csKube kclientset.Interface, csGateway gateclientset.Interface) *clientWrapper {
|
2020-12-15 15:40:05 +00:00
|
|
|
return &clientWrapper{
|
|
|
|
csGateway: csGateway,
|
|
|
|
csKube: csKube,
|
2023-04-17 08:56:36 +00:00
|
|
|
factoriesGateway: make(map[string]gateinformers.SharedInformerFactory),
|
|
|
|
factoriesKube: make(map[string]kinformers.SharedInformerFactory),
|
|
|
|
factoriesSecret: make(map[string]kinformers.SharedInformerFactory),
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInClusterClient returns a new Provider client that is expected to run
|
|
|
|
// inside the cluster.
|
|
|
|
func newInClusterClient(endpoint string) (*clientWrapper, error) {
|
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create in-cluster configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint != "" {
|
|
|
|
config.Host = endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return createClientFromConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newExternalClusterClientFromFile(file string) (*clientWrapper, error) {
|
|
|
|
configFromFlags, err := clientcmd.BuildConfigFromFlags("", file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return createClientFromConfig(configFromFlags)
|
|
|
|
}
|
|
|
|
|
2023-04-03 08:06:06 +00:00
|
|
|
// newExternalClusterClient returns a new Provider client that may run outside of the cluster.
|
2020-12-15 15:40:05 +00:00
|
|
|
// 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) {
|
2020-12-15 15:40: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)
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:40:05 +00:00
|
|
|
config := &rest.Config{
|
|
|
|
Host: endpoint,
|
2024-01-11 16:06:06 +00:00
|
|
|
BearerToken: string(tokenData),
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if caFilePath != "" {
|
2021-03-04 19:08:03 +00:00
|
|
|
caData, err := os.ReadFile(caFilePath)
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read CA file %s: %w", caFilePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.TLSClientConfig = rest.TLSClientConfig{CAData: caData}
|
|
|
|
}
|
|
|
|
|
|
|
|
return createClientFromConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchAll starts namespace-specific controllers for all relevant kinds.
|
|
|
|
func (c *clientWrapper) WatchAll(namespaces []string, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
|
|
|
eventCh := make(chan interface{}, 1)
|
|
|
|
eventHandler := &resourceEventHandler{ev: eventCh}
|
|
|
|
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
namespaces = []string{metav1.NamespaceAll}
|
|
|
|
c.isNamespaceAll = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c.watchedNamespaces = namespaces
|
|
|
|
|
|
|
|
notOwnedByHelm := func(opts *metav1.ListOptions) {
|
|
|
|
opts.LabelSelector = "owner!=helm"
|
|
|
|
}
|
|
|
|
|
|
|
|
labelSelectorOptions := func(options *metav1.ListOptions) {
|
|
|
|
options.LabelSelector = c.labelSelector
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
c.factoryNamespace = kinformers.NewSharedInformerFactory(c.csKube, resyncPeriod)
|
2023-03-27 10:14:05 +00:00
|
|
|
_, err := c.factoryNamespace.Core().V1().Namespaces().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-04 13:46:08 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
c.factoryGatewayClass = gateinformers.NewSharedInformerFactoryWithOptions(c.csGateway, resyncPeriod, gateinformers.WithTweakListOptions(labelSelectorOptions))
|
2024-01-09 09:28:05 +00:00
|
|
|
_, err = c.factoryGatewayClass.Gateway().V1().GatewayClasses().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-09 10:34:06 +00:00
|
|
|
|
2020-12-15 15:40:05 +00:00
|
|
|
for _, ns := range namespaces {
|
2024-09-17 08:50:04 +00:00
|
|
|
factoryKube := kinformers.NewSharedInformerFactoryWithOptions(c.csKube, resyncPeriod, kinformers.WithNamespace(ns))
|
|
|
|
_, err = factoryKube.Core().V1().Services().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryKube.Discovery().V1().EndpointSlices().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
factoryGateway := gateinformers.NewSharedInformerFactoryWithOptions(c.csGateway, resyncPeriod, gateinformers.WithNamespace(ns))
|
2024-01-09 09:28:05 +00:00
|
|
|
_, err = factoryGateway.Gateway().V1().Gateways().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-09 09:28:05 +00:00
|
|
|
_, err = factoryGateway.Gateway().V1().HTTPRoutes().Informer().AddEventHandler(eventHandler)
|
2023-03-27 10:14:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-30 08:36:06 +00:00
|
|
|
_, err = factoryGateway.Gateway().V1().GRPCRoutes().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-30 15:44:05 +00:00
|
|
|
_, err = factoryGateway.Gateway().V1beta1().ReferenceGrants().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-04-02 15:32:04 +00:00
|
|
|
if c.experimentalChannel {
|
|
|
|
_, err = factoryGateway.Gateway().V1alpha2().TCPRoutes().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryGateway.Gateway().V1alpha2().TLSRoutes().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-17 08:50:04 +00:00
|
|
|
_, err = factoryGateway.Gateway().V1alpha3().BackendTLSPolicies().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = factoryKube.Core().V1().ConfigMaps().Informer().AddEventHandler(eventHandler)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-27 10:14:05 +00:00
|
|
|
}
|
2020-12-15 15:40:05 +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
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
|
|
|
c.factoriesGateway[ns] = factoryGateway
|
|
|
|
c.factoriesKube[ns] = factoryKube
|
|
|
|
c.factoriesSecret[ns] = factorySecret
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
c.factoryNamespace.Start(stopCh)
|
2020-12-15 15:40:05 +00:00
|
|
|
c.factoryGatewayClass.Start(stopCh)
|
|
|
|
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
c.factoriesGateway[ns].Start(stopCh)
|
|
|
|
c.factoriesKube[ns].Start(stopCh)
|
|
|
|
c.factoriesSecret[ns].Start(stopCh)
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
for t, ok := range c.factoryNamespace.WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s", t.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:40:05 +00:00
|
|
|
for t, ok := range c.factoryGatewayClass.WaitForCacheSync(stopCh) {
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("timed out waiting for controller caches to sync %s", t.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
for t, ok := range c.factoriesGateway[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) {
|
|
|
|
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.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventCh, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListNamespaces(selector labels.Selector) ([]string, error) {
|
2021-10-04 13:46:08 +00:00
|
|
|
ns, err := c.factoryNamespace.Core().V1().Namespaces().Lister().List(selector)
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
var namespaces []string
|
|
|
|
for _, namespace := range ns {
|
|
|
|
if !c.isWatchedNamespace(namespace.Name) {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Warn().Msgf("Namespace %q is not within %q watched namespaces", selector, namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
namespaces = append(namespaces, namespace.Name)
|
|
|
|
}
|
|
|
|
return namespaces, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListHTTPRoutes() ([]*gatev1.HTTPRoute, error) {
|
2024-01-09 09:28:05 +00:00
|
|
|
var httpRoutes []*gatev1.HTTPRoute
|
2024-05-28 12:30:04 +00:00
|
|
|
for _, namespace := range c.watchedNamespaces {
|
2024-01-09 09:28:05 +00:00
|
|
|
routes, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1().HTTPRoutes().Lister().HTTPRoutes(namespace).List(labels.Everything())
|
2021-10-04 13:46:08 +00:00
|
|
|
if err != nil {
|
2024-05-28 12:30:04 +00:00
|
|
|
return nil, fmt.Errorf("listing HTTP routes in namespace %s", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
httpRoutes = append(httpRoutes, routes...)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return httpRoutes, nil
|
|
|
|
}
|
|
|
|
|
2024-08-30 08:36:06 +00:00
|
|
|
func (c *clientWrapper) ListGRPCRoutes() ([]*gatev1.GRPCRoute, error) {
|
|
|
|
var grpcRoutes []*gatev1.GRPCRoute
|
|
|
|
for _, namespace := range c.watchedNamespaces {
|
|
|
|
routes, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1().GRPCRoutes().Lister().GRPCRoutes(namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("listing GRPC routes in namespace %s", namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcRoutes = append(grpcRoutes, routes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpcRoutes, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListTCPRoutes() ([]*gatev1alpha2.TCPRoute, error) {
|
2023-04-17 08:56:36 +00:00
|
|
|
var tcpRoutes []*gatev1alpha2.TCPRoute
|
2024-05-28 12:30:04 +00:00
|
|
|
for _, namespace := range c.watchedNamespaces {
|
2021-11-09 10:34:06 +00:00
|
|
|
routes, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1alpha2().TCPRoutes().Lister().TCPRoutes(namespace).List(labels.Everything())
|
2021-10-04 13:46:08 +00:00
|
|
|
if err != nil {
|
2024-05-28 12:30:04 +00:00
|
|
|
return nil, fmt.Errorf("listing TCP routes in namespace %s", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
tcpRoutes = append(tcpRoutes, routes...)
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2021-05-20 09:50:12 +00:00
|
|
|
return tcpRoutes, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListTLSRoutes() ([]*gatev1alpha2.TLSRoute, error) {
|
2023-04-17 08:56:36 +00:00
|
|
|
var tlsRoutes []*gatev1alpha2.TLSRoute
|
2024-05-28 12:30:04 +00:00
|
|
|
for _, namespace := range c.watchedNamespaces {
|
2021-11-09 10:34:06 +00:00
|
|
|
routes, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1alpha2().TLSRoutes().Lister().TLSRoutes(namespace).List(labels.Everything())
|
2021-10-04 13:46:08 +00:00
|
|
|
if err != nil {
|
2024-05-28 12:30:04 +00:00
|
|
|
return nil, fmt.Errorf("listing TLS routes in namespace %s", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
tlsRoutes = append(tlsRoutes, routes...)
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2021-05-20 09:50:12 +00:00
|
|
|
return tlsRoutes, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListReferenceGrants(namespace string) ([]*gatev1beta1.ReferenceGrant, error) {
|
2024-01-30 15:44:05 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, fmt.Errorf("failed to get ReferenceGrants: namespace %s is not within watched namespaces", namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
referenceGrants, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1beta1().ReferenceGrants().Lister().ReferenceGrants(namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return referenceGrants, nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListGateways() []*gatev1.Gateway {
|
2024-01-09 09:28:05 +00:00
|
|
|
var result []*gatev1.Gateway
|
2020-12-15 15:40:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesGateway {
|
2024-05-01 04:38:03 +00:00
|
|
|
gateways, err := factory.Gateway().V1().Gateways().Lister().Gateways(ns).List(labels.Everything())
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Failed to list Gateways in namespace %s", ns)
|
2020-12-15 15:40:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, gateways...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) ListGatewayClasses() ([]*gatev1.GatewayClass, error) {
|
2024-01-09 09:28:05 +00:00
|
|
|
return c.factoryGatewayClass.Gateway().V1().GatewayClasses().Lister().List(labels.Everything())
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
func (c *clientWrapper) UpdateGatewayClassStatus(ctx context.Context, name string, status gatev1.GatewayClassStatus) error {
|
2024-07-11 09:26:03 +00:00
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentGatewayClass, err := c.factoryGatewayClass.Gateway().V1().GatewayClasses().Lister().Get(name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
if conditionsEqual(currentGatewayClass.Status.Conditions, status.Conditions) {
|
|
|
|
return nil
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
currentGatewayClass = currentGatewayClass.DeepCopy()
|
|
|
|
currentGatewayClass.Status = status
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
if _, err = c.csGateway.GatewayV1().GatewayClasses().UpdateStatus(ctx, currentGatewayClass, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
2024-09-17 08:50:04 +00:00
|
|
|
return fmt.Errorf("failed to update GatewayClass %s status: %w", name, err)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
func (c *clientWrapper) UpdateGatewayStatus(ctx context.Context, gateway ktypes.NamespacedName, status gatev1.GatewayStatus) error {
|
2020-12-15 15:40:05 +00:00
|
|
|
if !c.isWatchedNamespace(gateway.Namespace) {
|
|
|
|
return fmt.Errorf("cannot update Gateway status %s/%s: namespace is not within watched namespaces", gateway.Namespace, gateway.Name)
|
|
|
|
}
|
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentGateway, err := c.factoriesGateway[c.lookupNamespace(gateway.Namespace)].Gateway().V1().Gateways().Lister().Gateways(gateway.Namespace).Get(gateway.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
if gatewayStatusEqual(currentGateway.Status, status) {
|
2024-07-11 09:26:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
currentGateway = currentGateway.DeepCopy()
|
|
|
|
currentGateway.Status = status
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
if _, err = c.csGateway.GatewayV1().Gateways(gateway.Namespace).UpdateStatus(ctx, currentGateway, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
2024-09-17 08:50:04 +00:00
|
|
|
return fmt.Errorf("failed to update Gateway %s/%s status: %w", gateway.Namespace, gateway.Name, err)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) UpdateHTTPRouteStatus(ctx context.Context, route ktypes.NamespacedName, status gatev1.HTTPRouteStatus) error {
|
|
|
|
if !c.isWatchedNamespace(route.Namespace) {
|
|
|
|
return fmt.Errorf("updating HTTPRoute status %s/%s: namespace is not within watched namespaces", route.Namespace, route.Name)
|
2024-05-01 04:38:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentRoute, err := c.factoriesGateway[c.lookupNamespace(route.Namespace)].Gateway().V1().HTTPRoutes().Lister().HTTPRoutes(route.Namespace).Get(route.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2024-05-01 04:38:03 +00:00
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
parentStatuses := make([]gatev1.RouteParentStatus, len(status.Parents))
|
|
|
|
copy(parentStatuses, status.Parents)
|
|
|
|
|
|
|
|
// keep statuses added by other gateway controllers.
|
|
|
|
// TODO: we should also keep statuses for gateways managed by other Traefik instances.
|
|
|
|
for _, parentStatus := range currentRoute.Status.Parents {
|
|
|
|
if parentStatus.ControllerName != controllerName {
|
|
|
|
parentStatuses = append(parentStatuses, parentStatus)
|
2024-07-11 09:26:03 +00:00
|
|
|
}
|
2024-05-01 04:38:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
// do not update status when nothing has changed.
|
|
|
|
if routeParentStatusesEqual(currentRoute.Status.Parents, parentStatuses) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
currentRoute = currentRoute.DeepCopy()
|
|
|
|
currentRoute.Status = gatev1.HTTPRouteStatus{
|
|
|
|
RouteStatus: gatev1.RouteStatus{
|
|
|
|
Parents: parentStatuses,
|
|
|
|
},
|
|
|
|
}
|
2024-05-01 04:38:03 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
if _, err = c.csGateway.GatewayV1().HTTPRoutes(route.Namespace).UpdateStatus(ctx, currentRoute, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-09-17 08:50:04 +00:00
|
|
|
return fmt.Errorf("failed to update HTTPRoute %s/%s status: %w", route.Namespace, route.Name, err)
|
2024-05-01 04:38:03 +00:00
|
|
|
}
|
2024-07-11 09:26:03 +00:00
|
|
|
|
2024-05-01 04:38:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-30 08:36:06 +00:00
|
|
|
func (c *clientWrapper) UpdateGRPCRouteStatus(ctx context.Context, route ktypes.NamespacedName, status gatev1.GRPCRouteStatus) error {
|
|
|
|
if !c.isWatchedNamespace(route.Namespace) {
|
|
|
|
return fmt.Errorf("updating GRPCRoute status %s/%s: namespace is not within watched namespaces", route.Namespace, route.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentRoute, err := c.factoriesGateway[c.lookupNamespace(route.Namespace)].Gateway().V1().GRPCRoutes().Lister().GRPCRoutes(route.Namespace).Get(route.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parentStatuses := make([]gatev1.RouteParentStatus, len(status.Parents))
|
|
|
|
copy(parentStatuses, status.Parents)
|
|
|
|
|
|
|
|
// keep statuses added by other gateway controllers.
|
|
|
|
// TODO: we should also keep statuses for gateways managed by other Traefik instances.
|
|
|
|
for _, parentStatus := range currentRoute.Status.Parents {
|
|
|
|
if parentStatus.ControllerName != controllerName {
|
|
|
|
parentStatuses = append(parentStatuses, parentStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not update status when nothing has changed.
|
|
|
|
if routeParentStatusesEqual(currentRoute.Status.Parents, parentStatuses) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currentRoute = currentRoute.DeepCopy()
|
|
|
|
currentRoute.Status = gatev1.GRPCRouteStatus{
|
|
|
|
RouteStatus: gatev1.RouteStatus{
|
|
|
|
Parents: parentStatuses,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = c.csGateway.GatewayV1().GRPCRoutes(route.Namespace).UpdateStatus(ctx, currentRoute, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update GRPCRoute %q status: %w", route.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) UpdateTCPRouteStatus(ctx context.Context, route ktypes.NamespacedName, status gatev1alpha2.TCPRouteStatus) error {
|
|
|
|
if !c.isWatchedNamespace(route.Namespace) {
|
|
|
|
return fmt.Errorf("updating TCPRoute status %s/%s: namespace is not within watched namespaces", route.Namespace, route.Name)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentRoute, err := c.factoriesGateway[c.lookupNamespace(route.Namespace)].Gateway().V1alpha2().TCPRoutes().Lister().TCPRoutes(route.Namespace).Get(route.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
parentStatuses := make([]gatev1.RouteParentStatus, len(status.Parents))
|
|
|
|
copy(parentStatuses, status.Parents)
|
|
|
|
|
|
|
|
// keep statuses added by other gateway controllers.
|
|
|
|
// TODO: we should also keep statuses for gateways managed by other Traefik instances.
|
|
|
|
for _, parentStatus := range currentRoute.Status.Parents {
|
|
|
|
if parentStatus.ControllerName != controllerName {
|
|
|
|
parentStatuses = append(parentStatuses, parentStatus)
|
2024-07-11 09:26:03 +00:00
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
// do not update status when nothing has changed.
|
|
|
|
if routeParentStatusesEqual(currentRoute.Status.Parents, parentStatuses) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
currentRoute = currentRoute.DeepCopy()
|
|
|
|
currentRoute.Status = gatev1alpha2.TCPRouteStatus{
|
|
|
|
RouteStatus: gatev1.RouteStatus{
|
|
|
|
Parents: parentStatuses,
|
|
|
|
},
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
if _, err = c.csGateway.GatewayV1alpha2().TCPRoutes(route.Namespace).UpdateStatus(ctx, currentRoute, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-09-17 08:50:04 +00:00
|
|
|
return fmt.Errorf("failed to update TCPRoute %s/%s status: %w", route.Namespace, route.Name, err)
|
2024-05-28 12:30:04 +00:00
|
|
|
}
|
2024-07-11 09:26:03 +00:00
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
return nil
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) UpdateTLSRouteStatus(ctx context.Context, route ktypes.NamespacedName, status gatev1alpha2.TLSRouteStatus) error {
|
|
|
|
if !c.isWatchedNamespace(route.Namespace) {
|
|
|
|
return fmt.Errorf("updating TLSRoute status %s/%s: namespace is not within watched namespaces", route.Namespace, route.Name)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentRoute, err := c.factoriesGateway[c.lookupNamespace(route.Namespace)].Gateway().V1alpha2().TLSRoutes().Lister().TLSRoutes(route.Namespace).Get(route.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
parentStatuses := make([]gatev1.RouteParentStatus, len(status.Parents))
|
|
|
|
copy(parentStatuses, status.Parents)
|
|
|
|
|
|
|
|
// keep statuses added by other gateway controllers.
|
|
|
|
// TODO: we should also keep statuses for gateways managed by other Traefik instances.
|
|
|
|
for _, parentStatus := range currentRoute.Status.Parents {
|
|
|
|
if parentStatus.ControllerName != controllerName {
|
|
|
|
parentStatuses = append(parentStatuses, parentStatus)
|
2024-07-11 09:26:03 +00:00
|
|
|
}
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
// do not update status when nothing has changed.
|
|
|
|
if routeParentStatusesEqual(currentRoute.Status.Parents, parentStatuses) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
currentRoute = currentRoute.DeepCopy()
|
|
|
|
currentRoute.Status = gatev1alpha2.TLSRouteStatus{
|
|
|
|
RouteStatus: gatev1.RouteStatus{
|
|
|
|
Parents: parentStatuses,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = c.csGateway.GatewayV1alpha2().TLSRoutes(route.Namespace).UpdateStatus(ctx, currentRoute, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
|
2024-07-11 09:26:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-09-17 08:50:04 +00:00
|
|
|
return fmt.Errorf("failed to update TLSRoute %s/%s status: %w", route.Namespace, route.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) UpdateBackendTLSPolicyStatus(ctx context.Context, policy ktypes.NamespacedName, status gatev1alpha2.PolicyStatus) error {
|
|
|
|
if !c.isWatchedNamespace(policy.Namespace) {
|
|
|
|
return fmt.Errorf("updating BackendTLSPolicy status %s/%s: namespace is not within watched namespaces", policy.Namespace, policy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
|
|
|
currentPolicy, err := c.factoriesGateway[c.lookupNamespace(policy.Namespace)].Gateway().V1alpha3().BackendTLSPolicies().Lister().BackendTLSPolicies(policy.Namespace).Get(policy.Name)
|
|
|
|
if err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ancestorStatuses := make([]gatev1alpha2.PolicyAncestorStatus, len(status.Ancestors))
|
|
|
|
copy(ancestorStatuses, status.Ancestors)
|
|
|
|
|
|
|
|
// keep statuses added by other gateway controllers,
|
|
|
|
// and statuses for Traefik gateway controller but not for the same Gateway as the one in parameter (AncestorRef).
|
|
|
|
for _, ancestorStatus := range currentPolicy.Status.Ancestors {
|
|
|
|
if ancestorStatus.ControllerName != controllerName {
|
|
|
|
ancestorStatuses = append(ancestorStatuses, ancestorStatus)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if slices.ContainsFunc(status.Ancestors, func(status gatev1alpha2.PolicyAncestorStatus) bool {
|
|
|
|
return reflect.DeepEqual(ancestorStatus.AncestorRef, status.AncestorRef)
|
|
|
|
}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ancestorStatuses = append(ancestorStatuses, ancestorStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ancestorStatuses) > 16 {
|
|
|
|
return fmt.Errorf("failed to update BackendTLSPolicy %s/%s status: PolicyAncestor statuses count exceeds 16", policy.Namespace, policy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not update status when nothing has changed.
|
|
|
|
if policyAncestorStatusesEqual(currentPolicy.Status.Ancestors, ancestorStatuses) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPolicy = currentPolicy.DeepCopy()
|
|
|
|
currentPolicy.Status = gatev1alpha2.PolicyStatus{
|
|
|
|
Ancestors: ancestorStatuses,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = c.csGateway.GatewayV1alpha3().BackendTLSPolicies(policy.Namespace).UpdateStatus(ctx, currentPolicy, metav1.UpdateOptions{}); err != nil {
|
|
|
|
// We have to return err itself here (not wrapped inside another error)
|
|
|
|
// so that RetryOnConflict can identify it correctly.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update BackendTLSPolicy %s/%s status: %w", policy.Namespace, policy.Name, err)
|
2024-05-28 12:30:04 +00:00
|
|
|
}
|
2024-07-11 09:26:03 +00:00
|
|
|
|
2024-05-28 12:30:04 +00:00
|
|
|
return nil
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetService returns the named service from the given namespace.
|
|
|
|
func (c *clientWrapper) GetService(namespace, name string) (*corev1.Service, bool, error) {
|
|
|
|
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.factoriesKube[c.lookupNamespace(namespace)].Core().V1().Services().Lister().Services(namespace).Get(name)
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
|
|
|
|
return service, exist, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
// ListEndpointSlicesForService returns the EndpointSlices for the given service name in the given namespace.
|
|
|
|
func (c *clientWrapper) ListEndpointSlicesForService(namespace, serviceName string) ([]*discoveryv1.EndpointSlice, error) {
|
2020-12-15 15:40:05 +00:00
|
|
|
if !c.isWatchedNamespace(namespace) {
|
2024-06-21 12:56:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to get endpointslices for service %s/%s: namespace is not within watched namespaces", namespace, serviceName)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
serviceLabelRequirement, err := labels.NewRequirement(discoveryv1.LabelServiceName, selection.Equals, []string{serviceName})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create service label selector requirement: %w", err)
|
|
|
|
}
|
|
|
|
serviceSelector := labels.NewSelector()
|
|
|
|
serviceSelector = serviceSelector.Add(*serviceLabelRequirement)
|
2020-12-15 15:40:05 +00:00
|
|
|
|
2024-06-21 12:56:03 +00:00
|
|
|
return c.factoriesKube[c.lookupNamespace(namespace)].Discovery().V1().EndpointSlices().Lister().EndpointSlices(namespace).List(serviceSelector)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 08:50:04 +00:00
|
|
|
// ListBackendTLSPoliciesForService returns the BackendTLSPolicy for the given service name in the given namespace.
|
|
|
|
func (c *clientWrapper) ListBackendTLSPoliciesForService(namespace, serviceName string) ([]*gatev1alpha3.BackendTLSPolicy, error) {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, fmt.Errorf("failed to get BackendTLSPolicies for service %s/%s: namespace is not within watched namespaces", namespace, serviceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := c.factoriesGateway[c.lookupNamespace(namespace)].Gateway().V1alpha3().BackendTLSPolicies().Lister().BackendTLSPolicies(namespace).List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to list BackendTLSPolicies in namespace %s", namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
var servicePolicies []*gatev1alpha3.BackendTLSPolicy
|
|
|
|
for _, policy := range policies {
|
|
|
|
for _, ref := range policy.Spec.TargetRefs {
|
|
|
|
// The policy does not target the service.
|
|
|
|
if ref.Group != groupCore || ref.Kind != kindService || string(ref.Name) != serviceName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
servicePolicies = append(servicePolicies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return servicePolicies, nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:40:05 +00:00
|
|
|
// GetSecret returns the named secret from the given namespace.
|
|
|
|
func (c *clientWrapper) GetSecret(namespace, name string) (*corev1.Secret, bool, error) {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get secret %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := c.factoriesSecret[c.lookupNamespace(namespace)].Core().V1().Secrets().Lister().Secrets(namespace).Get(name)
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
|
|
|
|
return secret, exist, err
|
|
|
|
}
|
|
|
|
|
2024-09-17 08:50:04 +00:00
|
|
|
// GetConfigMap returns the named configMap from the given namespace.
|
|
|
|
func (c *clientWrapper) GetConfigMap(namespace, name string) (*corev1.ConfigMap, bool, error) {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get configMap %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
configMap, err := c.factoriesKube[c.lookupNamespace(namespace)].Core().V1().ConfigMaps().Lister().ConfigMaps(namespace).Get(name)
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
|
|
|
|
return configMap, exist, err
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:40: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.
|
2024-05-28 12:30:04 +00:00
|
|
|
func (c *clientWrapper) lookupNamespace(namespace string) string {
|
2020-12-15 15:40:05 +00:00
|
|
|
if c.isNamespaceAll {
|
|
|
|
return metav1.NamespaceAll
|
|
|
|
}
|
2024-05-28 12:30:04 +00:00
|
|
|
return namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *clientWrapper) isWatchedNamespace(namespace string) bool {
|
|
|
|
if c.isNamespaceAll {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return slices.Contains(c.watchedNamespaces, namespace)
|
2020-12-15 15:40:05 +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 signaling anyway so dropping a few is ok.
|
|
|
|
func eventHandlerFunc(events chan<- interface{}, obj interface{}) {
|
|
|
|
select {
|
|
|
|
case events <- obj:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-12-15 15:40:05 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
func gatewayStatusEqual(statusA, statusB gatev1.GatewayStatus) bool {
|
2024-08-05 10:22:04 +00:00
|
|
|
return reflect.DeepEqual(statusA.Addresses, statusB.Addresses) &&
|
|
|
|
listenersStatusEqual(statusA.Listeners, statusB.Listeners) &&
|
|
|
|
conditionsEqual(statusA.Conditions, statusB.Conditions)
|
2024-05-28 12:30:04 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 08:50:04 +00:00
|
|
|
func policyAncestorStatusesEqual(policyAncestorStatusesA, policyAncestorStatusesB []gatev1alpha2.PolicyAncestorStatus) bool {
|
|
|
|
if len(policyAncestorStatusesA) != len(policyAncestorStatusesB) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sA := range policyAncestorStatusesA {
|
|
|
|
if !slices.ContainsFunc(policyAncestorStatusesB, func(sB gatev1alpha2.PolicyAncestorStatus) bool {
|
|
|
|
return policyAncestorStatusEqual(sB, sA)
|
|
|
|
}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sB := range policyAncestorStatusesB {
|
|
|
|
if !slices.ContainsFunc(policyAncestorStatusesA, func(sA gatev1alpha2.PolicyAncestorStatus) bool {
|
|
|
|
return policyAncestorStatusEqual(sA, sB)
|
|
|
|
}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func policyAncestorStatusEqual(sA, sB gatev1alpha2.PolicyAncestorStatus) bool {
|
|
|
|
return sA.ControllerName == sB.ControllerName &&
|
|
|
|
reflect.DeepEqual(sA.AncestorRef, sB.AncestorRef) &&
|
|
|
|
conditionsEqual(sA.Conditions, sB.Conditions)
|
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
func routeParentStatusesEqual(routeParentStatusesA, routeParentStatusesB []gatev1alpha2.RouteParentStatus) bool {
|
|
|
|
if len(routeParentStatusesA) != len(routeParentStatusesB) {
|
2024-05-28 12:30:04 +00:00
|
|
|
return false
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
2024-02-19 14:44:03 +00:00
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
for _, sA := range routeParentStatusesA {
|
|
|
|
if !slices.ContainsFunc(routeParentStatusesB, func(sB gatev1alpha2.RouteParentStatus) bool {
|
|
|
|
return routeParentStatusEqual(sB, sA)
|
|
|
|
}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sB := range routeParentStatusesB {
|
|
|
|
if !slices.ContainsFunc(routeParentStatusesA, func(sA gatev1alpha2.RouteParentStatus) bool {
|
|
|
|
return routeParentStatusEqual(sA, sB)
|
|
|
|
}) {
|
|
|
|
return false
|
2024-05-28 12:30:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 13:48:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func routeParentStatusEqual(sA, sB gatev1alpha2.RouteParentStatus) bool {
|
2024-08-05 10:22:04 +00:00
|
|
|
return sA.ControllerName == sB.ControllerName &&
|
|
|
|
reflect.DeepEqual(sA.ParentRef, sB.ParentRef) &&
|
|
|
|
conditionsEqual(sA.Conditions, sB.Conditions)
|
|
|
|
}
|
2024-07-29 13:48:05 +00:00
|
|
|
|
2024-08-05 10:22:04 +00:00
|
|
|
func listenersStatusEqual(listenerA, listenerB []gatev1.ListenerStatus) bool {
|
|
|
|
return slices.EqualFunc(listenerA, listenerB, func(lA gatev1.ListenerStatus, lB gatev1.ListenerStatus) bool {
|
|
|
|
return lA.Name == lB.Name &&
|
|
|
|
lA.AttachedRoutes == lB.AttachedRoutes &&
|
|
|
|
conditionsEqual(lA.Conditions, lB.Conditions)
|
|
|
|
})
|
2024-07-29 13:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func conditionsEqual(conditionsA, conditionsB []metav1.Condition) bool {
|
|
|
|
return slices.EqualFunc(conditionsA, conditionsB, func(cA metav1.Condition, cB metav1.Condition) bool {
|
|
|
|
return cA.Type == cB.Type &&
|
|
|
|
cA.Reason == cB.Reason &&
|
|
|
|
cA.Status == cB.Status &&
|
|
|
|
cA.Message == cB.Message &&
|
|
|
|
cA.ObservedGeneration == cB.ObservedGeneration
|
|
|
|
})
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|