2020-12-15 15:40:05 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-04 19:08:03 +00:00
|
|
|
"os"
|
2020-12-15 15:40:05 +00:00
|
|
|
"time"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-12-15 15:40:05 +00:00
|
|
|
corev1 "k8s.io/api/core/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"
|
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-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-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-01-09 09:28:05 +00:00
|
|
|
func (reh *resourceEventHandler) OnAdd(obj interface{}, isInInitialList bool) {
|
2020-12-15 15:40:05 +00:00
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnUpdate(oldObj, newObj interface{}) {
|
|
|
|
switch oldObj.(type) {
|
2024-01-09 09:28:05 +00:00
|
|
|
case *gatev1.GatewayClass:
|
2020-12-15 15:40:05 +00:00
|
|
|
// Skip update for gateway classes. We only manage addition or deletion for this cluster-wide resource.
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
eventHandlerFunc(reh.ev, newObj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reh *resourceEventHandler) OnDelete(obj interface{}) {
|
|
|
|
eventHandlerFunc(reh.ev, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
WatchAll(namespaces []string, stopCh <-chan struct{}) (<-chan interface{}, error)
|
2024-01-09 09:28:05 +00:00
|
|
|
GetGatewayClasses() ([]*gatev1.GatewayClass, error)
|
|
|
|
UpdateGatewayStatus(gateway *gatev1.Gateway, gatewayStatus gatev1.GatewayStatus) error
|
|
|
|
UpdateGatewayClassStatus(gatewayClass *gatev1.GatewayClass, condition metav1.Condition) error
|
|
|
|
GetGateways() []*gatev1.Gateway
|
|
|
|
GetHTTPRoutes(namespaces []string) ([]*gatev1.HTTPRoute, error)
|
2023-04-17 08:56:36 +00:00
|
|
|
GetTCPRoutes(namespaces []string) ([]*gatev1alpha2.TCPRoute, error)
|
|
|
|
GetTLSRoutes(namespaces []string) ([]*gatev1alpha2.TLSRoute, error)
|
2020-12-15 15:40: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)
|
2021-10-04 13:46:08 +00:00
|
|
|
GetNamespaces(selector labels.Selector) ([]string, error)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
labelSelector string
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func newExternalClusterClient(endpoint, token, caFilePath string) (*clientWrapper, error) {
|
|
|
|
if endpoint == "" {
|
|
|
|
return nil, errors.New("endpoint missing for external cluster client")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &rest.Config{
|
|
|
|
Host: endpoint,
|
|
|
|
BearerToken: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// TODO manage Reference Policy
|
|
|
|
// https://gateway-api.sigs.k8s.io/v1alpha2/references/spec/#gateway.networking.k8s.io/v1alpha2.ReferencePolicy
|
2020-12-15 15:40:05 +00:00
|
|
|
|
|
|
|
for _, ns := range namespaces {
|
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
|
|
|
|
}
|
|
|
|
_, 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
|
|
|
|
}
|
2020-12-15 15:40:05 +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-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
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
func (c *clientWrapper) GetNamespaces(selector labels.Selector) ([]string, error) {
|
|
|
|
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-01-09 09:28:05 +00:00
|
|
|
func (c *clientWrapper) GetHTTPRoutes(namespaces []string) ([]*gatev1.HTTPRoute, error) {
|
|
|
|
var httpRoutes []*gatev1.HTTPRoute
|
2021-10-04 13:46:08 +00:00
|
|
|
for _, namespace := range namespaces {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Warn().Msgf("Failed to get HTTPRoutes: %q is not within watched namespaces", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) == 0 {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Msgf("No HTTPRoutes found in namespace %q", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
httpRoutes = append(httpRoutes, routes...)
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return httpRoutes, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTCPRoutes(namespaces []string) ([]*gatev1alpha2.TCPRoute, error) {
|
|
|
|
var tcpRoutes []*gatev1alpha2.TCPRoute
|
2021-10-04 13:46:08 +00:00
|
|
|
for _, namespace := range namespaces {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Warn().Msgf("Failed to get TCPRoutes: %q is not within watched namespaces", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
if len(routes) == 0 {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Msgf("No TCPRoutes found in namespace %q", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
tcpRoutes = append(tcpRoutes, routes...)
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
return tcpRoutes, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c *clientWrapper) GetTLSRoutes(namespaces []string) ([]*gatev1alpha2.TLSRoute, error) {
|
|
|
|
var tlsRoutes []*gatev1alpha2.TLSRoute
|
2021-10-04 13:46:08 +00:00
|
|
|
for _, namespace := range namespaces {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Warn().Msgf("Failed to get TLSRoutes: %q is not within watched namespaces", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
if len(routes) == 0 {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Debug().Msgf("No TLSRoutes found in namespace %q", namespace)
|
2021-10-04 13:46:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
|
2021-10-04 13:46:08 +00:00
|
|
|
tlsRoutes = append(tlsRoutes, routes...)
|
|
|
|
}
|
2021-05-20 09:50:12 +00:00
|
|
|
return tlsRoutes, nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
func (c *clientWrapper) GetGateways() []*gatev1.Gateway {
|
|
|
|
var result []*gatev1.Gateway
|
2020-12-15 15:40:05 +00:00
|
|
|
|
|
|
|
for ns, factory := range c.factoriesGateway {
|
2024-01-09 09:28:05 +00:00
|
|
|
gateways, err := factory.Gateway().V1().Gateways().Lister().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-01-09 09:28:05 +00:00
|
|
|
func (c *clientWrapper) GetGatewayClasses() ([]*gatev1.GatewayClass, error) {
|
|
|
|
return c.factoryGatewayClass.Gateway().V1().GatewayClasses().Lister().List(labels.Everything())
|
2020-12-15 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
func (c *clientWrapper) UpdateGatewayClassStatus(gatewayClass *gatev1.GatewayClass, condition metav1.Condition) error {
|
2020-12-15 15:40:05 +00:00
|
|
|
gc := gatewayClass.DeepCopy()
|
|
|
|
|
|
|
|
var newConditions []metav1.Condition
|
|
|
|
for _, cond := range gc.Status.Conditions {
|
|
|
|
// No update for identical condition.
|
|
|
|
if cond.Type == condition.Type && cond.Status == condition.Status {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep other condition types.
|
|
|
|
if cond.Type != condition.Type {
|
|
|
|
newConditions = append(newConditions, cond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the condition to update.
|
|
|
|
newConditions = append(newConditions, condition)
|
|
|
|
gc.Status.Conditions = newConditions
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
_, err := c.csGateway.GatewayV1().GatewayClasses().UpdateStatus(ctx, gc, metav1.UpdateOptions{})
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update GatewayClass %q status: %w", gatewayClass.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
func (c *clientWrapper) UpdateGatewayStatus(gateway *gatev1.Gateway, gatewayStatus 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusEquals(gateway.Status, gatewayStatus) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
g := gateway.DeepCopy()
|
|
|
|
g.Status = gatewayStatus
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
_, err := c.csGateway.GatewayV1().Gateways(gateway.Namespace).UpdateStatus(ctx, g, metav1.UpdateOptions{})
|
2020-12-15 15:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update Gateway %q status: %w", gateway.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 09:28:05 +00:00
|
|
|
func statusEquals(oldStatus, newStatus gatev1.GatewayStatus) bool {
|
2020-12-15 15:40:05 +00:00
|
|
|
if len(oldStatus.Listeners) != len(newStatus.Listeners) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !conditionsEquals(oldStatus.Conditions, newStatus.Conditions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerMatches := 0
|
|
|
|
for _, newListener := range newStatus.Listeners {
|
|
|
|
for _, oldListener := range oldStatus.Listeners {
|
2021-11-09 10:34:06 +00:00
|
|
|
if newListener.Name == oldListener.Name {
|
2020-12-15 15:40:05 +00:00
|
|
|
if !conditionsEquals(newListener.Conditions, oldListener.Conditions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerMatches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return listenerMatches == len(oldStatus.Listeners)
|
|
|
|
}
|
|
|
|
|
|
|
|
func conditionsEquals(conditionsA, conditionsB []metav1.Condition) bool {
|
|
|
|
if len(conditionsA) != len(conditionsB) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
conditionMatches := 0
|
|
|
|
for _, conditionA := range conditionsA {
|
|
|
|
for _, conditionB := range conditionsB {
|
|
|
|
if conditionA.Type == conditionB.Type {
|
|
|
|
if conditionA.Reason != conditionB.Reason || conditionA.Status != conditionB.Status || conditionA.Message != conditionB.Message {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
conditionMatches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conditionMatches == len(conditionsA)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEndpoints returns the named endpoints from the given namespace.
|
|
|
|
func (c *clientWrapper) GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error) {
|
|
|
|
if !c.isWatchedNamespace(namespace) {
|
|
|
|
return nil, false, fmt.Errorf("failed to get endpoints %s/%s: namespace is not within watched namespaces", namespace, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := c.factoriesKube[c.lookupNamespace(namespace)].Core().V1().Endpoints().Lister().Endpoints(namespace).Get(name)
|
|
|
|
exist, err := translateNotFoundError(err)
|
|
|
|
|
|
|
|
return endpoint, exist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupNamespace returns the lookup namespace key for the given namespace.
|
|
|
|
// When listening on all namespaces, it returns the client-go identifier ("")
|
|
|
|
// for all-namespaces. Otherwise, it returns the given namespace.
|
|
|
|
// The distinction is necessary because we index all informers on the special
|
|
|
|
// identifier iff all-namespaces are requested but receive specific namespace
|
|
|
|
// identifiers from the Kubernetes API, so we have to bridge this gap.
|
|
|
|
func (c *clientWrapper) lookupNamespace(ns string) string {
|
|
|
|
if c.isNamespaceAll {
|
|
|
|
return metav1.NamespaceAll
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(ns string) bool {
|
|
|
|
if c.isNamespaceAll {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, watchedNamespace := range c.watchedNamespaces {
|
|
|
|
if watchedNamespace == ns {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|