2019-03-14 14:56:06 +00:00
|
|
|
package crd
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-14 14:56:06 +00:00
|
|
|
"fmt"
|
2021-03-04 19:08:03 +00:00
|
|
|
"os"
|
2019-06-11 13:12:04 +00:00
|
|
|
"path/filepath"
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
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"
|
2019-02-21 22:08:05 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2023-04-17 08:56:36 +00:00
|
|
|
kscheme "k8s.io/client-go/kubernetes/scheme"
|
2019-02-21 22:08:05 +00:00
|
|
|
)
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
var _ Client = (*clientMock)(nil)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// required by k8s.MustParseYaml
|
2023-04-17 08:56:36 +00:00
|
|
|
err := traefikv1alpha1.AddToScheme(kscheme.Scheme)
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
type clientMock struct {
|
|
|
|
services []*corev1.Service
|
|
|
|
secrets []*corev1.Secret
|
|
|
|
endpoints []*corev1.Endpoints
|
|
|
|
|
2020-01-16 09:14:06 +00:00
|
|
|
apiServiceError error
|
|
|
|
apiSecretError error
|
|
|
|
apiEndpointsError error
|
2019-03-14 14:56:06 +00:00
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
ingressRoutes []*traefikv1alpha1.IngressRoute
|
|
|
|
ingressRouteTCPs []*traefikv1alpha1.IngressRouteTCP
|
|
|
|
ingressRouteUDPs []*traefikv1alpha1.IngressRouteUDP
|
|
|
|
middlewares []*traefikv1alpha1.Middleware
|
|
|
|
middlewareTCPs []*traefikv1alpha1.MiddlewareTCP
|
|
|
|
tlsOptions []*traefikv1alpha1.TLSOption
|
|
|
|
tlsStores []*traefikv1alpha1.TLSStore
|
|
|
|
traefikServices []*traefikv1alpha1.TraefikService
|
|
|
|
serversTransports []*traefikv1alpha1.ServersTransport
|
|
|
|
serversTransportTCPs []*traefikv1alpha1.ServersTransportTCP
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
watchChan chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientMock(paths ...string) clientMock {
|
|
|
|
var c clientMock
|
|
|
|
|
|
|
|
for _, path := range paths {
|
2021-03-04 19:08:03 +00:00
|
|
|
yamlContent, err := os.ReadFile(filepath.FromSlash("./fixtures/" + path))
|
2019-03-14 14:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k8sObjects := k8s.MustParseYaml(yamlContent)
|
|
|
|
for _, obj := range k8sObjects {
|
|
|
|
switch o := obj.(type) {
|
|
|
|
case *corev1.Service:
|
|
|
|
c.services = append(c.services, o)
|
|
|
|
case *corev1.Endpoints:
|
|
|
|
c.endpoints = append(c.endpoints, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.IngressRoute:
|
2019-03-14 14:56:06 +00:00
|
|
|
c.ingressRoutes = append(c.ingressRoutes, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.IngressRouteTCP:
|
2019-06-11 13:12:04 +00:00
|
|
|
c.ingressRouteTCPs = append(c.ingressRouteTCPs, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.IngressRouteUDP:
|
2020-02-26 11:28:05 +00:00
|
|
|
c.ingressRouteUDPs = append(c.ingressRouteUDPs, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.Middleware:
|
2019-03-14 14:56:06 +00:00
|
|
|
c.middlewares = append(c.middlewares, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.MiddlewareTCP:
|
2021-06-11 13:30:05 +00:00
|
|
|
c.middlewareTCPs = append(c.middlewareTCPs, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.TraefikService:
|
2019-11-14 18:28:04 +00:00
|
|
|
c.traefikServices = append(c.traefikServices, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.TLSOption:
|
2019-06-21 15:18:05 +00:00
|
|
|
c.tlsOptions = append(c.tlsOptions, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.ServersTransport:
|
2022-12-09 08:58:05 +00:00
|
|
|
c.serversTransports = append(c.serversTransports, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.ServersTransportTCP:
|
2022-12-09 08:58:05 +00:00
|
|
|
c.serversTransportTCPs = append(c.serversTransportTCPs, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *traefikv1alpha1.TLSStore:
|
2020-02-24 16:14:06 +00:00
|
|
|
c.tlsStores = append(c.tlsStores, o)
|
2019-03-14 14:56:06 +00:00
|
|
|
case *corev1.Secret:
|
|
|
|
c.secrets = append(c.secrets, o)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown runtime object %+v %T", o, o))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetIngressRoutes() []*traefikv1alpha1.IngressRoute {
|
2019-03-14 14:56:06 +00:00
|
|
|
return c.ingressRoutes
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetIngressRouteTCPs() []*traefikv1alpha1.IngressRouteTCP {
|
2019-06-11 13:12:04 +00:00
|
|
|
return c.ingressRouteTCPs
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetIngressRouteUDPs() []*traefikv1alpha1.IngressRouteUDP {
|
2020-02-26 11:28:05 +00:00
|
|
|
return c.ingressRouteUDPs
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetMiddlewares() []*traefikv1alpha1.Middleware {
|
2019-03-14 14:56:06 +00:00
|
|
|
return c.middlewares
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetMiddlewareTCPs() []*traefikv1alpha1.MiddlewareTCP {
|
2021-06-11 13:30:05 +00:00
|
|
|
return c.middlewareTCPs
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetTraefikService(namespace, name string) (*traefikv1alpha1.TraefikService, bool, error) {
|
2019-11-14 18:28:04 +00:00
|
|
|
for _, svc := range c.traefikServices {
|
|
|
|
if svc.Namespace == namespace && svc.Name == name {
|
|
|
|
return svc, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetTraefikServices() []*traefikv1alpha1.TraefikService {
|
2019-11-14 18:28:04 +00:00
|
|
|
return c.traefikServices
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetTLSOptions() []*traefikv1alpha1.TLSOption {
|
2019-06-21 15:18:05 +00:00
|
|
|
return c.tlsOptions
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetTLSStores() []*traefikv1alpha1.TLSStore {
|
2020-02-24 16:14:06 +00:00
|
|
|
return c.tlsStores
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetServersTransports() []*traefikv1alpha1.ServersTransport {
|
2022-12-09 08:58:05 +00:00
|
|
|
return c.serversTransports
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetServersTransportTCPs() []*traefikv1alpha1.ServersTransportTCP {
|
2022-12-09 08:58:05 +00:00
|
|
|
return c.serversTransportTCPs
|
2020-09-11 13:40:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetTLSOption(namespace, name string) (*traefikv1alpha1.TLSOption, bool, error) {
|
2019-06-21 15:18:05 +00:00
|
|
|
for _, option := range c.tlsOptions {
|
|
|
|
if option.Namespace == namespace && option.Name == name {
|
|
|
|
return option, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
func (c clientMock) GetService(namespace, name string) (*corev1.Service, bool, error) {
|
|
|
|
if c.apiServiceError != nil {
|
|
|
|
return nil, false, c.apiServiceError
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range c.services {
|
|
|
|
if service.Namespace == namespace && service.Name == name {
|
|
|
|
return service, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false, c.apiServiceError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c clientMock) GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error) {
|
|
|
|
if c.apiEndpointsError != nil {
|
|
|
|
return nil, false, c.apiEndpointsError
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoints := range c.endpoints {
|
|
|
|
if endpoints.Namespace == namespace && endpoints.Name == name {
|
|
|
|
return endpoints, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &corev1.Endpoints{}, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c clientMock) GetSecret(namespace, name string) (*corev1.Secret, bool, error) {
|
|
|
|
if c.apiSecretError != nil {
|
|
|
|
return nil, false, c.apiSecretError
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, secret := range c.secrets {
|
|
|
|
if secret.Namespace == namespace && secret.Name == name {
|
|
|
|
return secret, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
func (c clientMock) WatchAll(namespaces []string, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
2019-02-21 22:08:05 +00:00
|
|
|
return c.watchChan, nil
|
|
|
|
}
|