2019-03-14 14:56:06 +00:00
|
|
|
package ingress
|
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-03-14 14:56:06 +00:00
|
|
|
|
2020-07-21 13:32:04 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
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
|
|
|
netv1 "k8s.io/api/networking/v1"
|
|
|
|
netv1beta1 "k8s.io/api/networking/v1beta1"
|
2019-02-21 22:08:05 +00:00
|
|
|
)
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
var _ Client = (*clientMock)(nil)
|
|
|
|
|
2019-02-21 22:08:05 +00:00
|
|
|
type clientMock struct {
|
2023-04-17 08:56:36 +00:00
|
|
|
ingresses []*netv1.Ingress
|
2021-01-28 14:08:04 +00:00
|
|
|
services []*corev1.Service
|
|
|
|
secrets []*corev1.Secret
|
|
|
|
endpoints []*corev1.Endpoints
|
2023-04-17 08:56:36 +00:00
|
|
|
ingressClasses []*netv1.IngressClass
|
2020-07-15 17:18:03 +00:00
|
|
|
|
2020-07-21 13:32:04 +00:00
|
|
|
serverVersion *version.Version
|
2019-02-21 22:08:05 +00:00
|
|
|
|
|
|
|
apiServiceError error
|
|
|
|
apiSecretError error
|
|
|
|
apiEndpointsError error
|
|
|
|
apiIngressStatusError error
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
watchChan chan interface{}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:32:04 +00:00
|
|
|
func newClientMock(serverVersion string, paths ...string) clientMock {
|
|
|
|
c := clientMock{}
|
|
|
|
|
|
|
|
c.serverVersion = version.Must(version.NewVersion(serverVersion))
|
2019-03-14 14:56:06 +00:00
|
|
|
|
|
|
|
for _, path := range paths {
|
2021-03-04 19:08:03 +00:00
|
|
|
yamlContent, err := os.ReadFile(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.Secret:
|
|
|
|
c.secrets = append(c.secrets, o)
|
|
|
|
case *corev1.Endpoints:
|
|
|
|
c.endpoints = append(c.endpoints, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *netv1beta1.Ingress:
|
2021-03-15 10:16:04 +00:00
|
|
|
ing, err := toNetworkingV1(o)
|
2020-01-16 09:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-15 10:16:04 +00:00
|
|
|
addServiceFromV1Beta1(ing, *o)
|
2020-01-16 09:14:06 +00:00
|
|
|
c.ingresses = append(c.ingresses, ing)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *netv1.Ingress:
|
2021-03-15 10:16:04 +00:00
|
|
|
c.ingresses = append(c.ingresses, o)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *netv1beta1.IngressClass:
|
2021-05-17 14:50:09 +00:00
|
|
|
ic, err := toNetworkingV1IngressClass(o)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c.ingressClasses = append(c.ingressClasses, ic)
|
2023-04-17 08:56:36 +00:00
|
|
|
case *netv1.IngressClass:
|
2021-01-28 14:08:04 +00:00
|
|
|
c.ingressClasses = append(c.ingressClasses, o)
|
2019-03-14 14:56:06 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown runtime object %+v %T", o, o))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
2019-02-21 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetIngresses() []*netv1.Ingress {
|
2019-02-21 22:08:05 +00:00
|
|
|
return c.ingresses
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:06:07 +00:00
|
|
|
func (c clientMock) GetServerVersion() *version.Version {
|
|
|
|
return c.serverVersion
|
2020-07-15 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) GetIngressClasses() ([]*netv1.IngressClass, error) {
|
2021-01-28 14:08:04 +00:00
|
|
|
return c.ingressClasses, nil
|
2020-07-15 17:18:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:56:36 +00:00
|
|
|
func (c clientMock) UpdateIngressStatus(_ *netv1.Ingress, _ []corev1.LoadBalancerIngress) error {
|
2019-02-21 22:08:05 +00:00
|
|
|
return c.apiIngressStatusError
|
|
|
|
}
|