2016-04-19 17:23:08 +00:00
|
|
|
package provider
|
2016-04-20 11:26:51 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-26 20:26:25 +00:00
|
|
|
"encoding/json"
|
2017-02-06 23:04:30 +00:00
|
|
|
"fmt"
|
2016-04-20 11:26:51 +00:00
|
|
|
"reflect"
|
2017-02-06 23:04:30 +00:00
|
|
|
"strings"
|
2016-04-20 11:26:51 +00:00
|
|
|
"testing"
|
2016-11-11 22:50:20 +00:00
|
|
|
|
2016-12-30 08:21:13 +00:00
|
|
|
"github.com/containous/traefik/provider/k8s"
|
|
|
|
"github.com/containous/traefik/types"
|
2016-11-11 22:50:20 +00:00
|
|
|
"k8s.io/client-go/1.5/pkg/api/v1"
|
|
|
|
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
|
|
|
|
"k8s.io/client-go/1.5/pkg/util/intstr"
|
2016-04-20 11:26:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoadIngresses(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-20 16:34:57 +00:00
|
|
|
Namespace: "testing",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
|
|
|
Host: "foo",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
|
|
|
Path: "/bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-20 11:26:51 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(80),
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
2017-02-10 01:25:38 +00:00
|
|
|
{
|
|
|
|
Path: "/namedthing",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service4",
|
|
|
|
ServicePort: intstr.FromString("https"),
|
|
|
|
},
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: "bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-20 11:26:51 +00:00
|
|
|
ServiceName: "service3",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromString("https"),
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-20 11:26:51 +00:00
|
|
|
ServiceName: "service2",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(802),
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-20 16:34:57 +00:00
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-20 11:26:51 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
2016-05-20 16:34:57 +00:00
|
|
|
Port: 80,
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-20 16:34:57 +00:00
|
|
|
Name: "service2",
|
|
|
|
UID: "2",
|
|
|
|
Namespace: "testing",
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-20 11:26:51 +00:00
|
|
|
ClusterIP: "10.0.0.2",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
|
|
|
Port: 802,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-20 16:34:57 +00:00
|
|
|
Name: "service3",
|
|
|
|
UID: "3",
|
|
|
|
Namespace: "testing",
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-20 11:26:51 +00:00
|
|
|
ClusterIP: "10.0.0.3",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-20 11:26:51 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
2016-05-26 11:09:36 +00:00
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "https",
|
2016-04-26 20:26:25 +00:00
|
|
|
Port: 443,
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-10 01:25:38 +00:00
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service4",
|
|
|
|
UID: "4",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.4",
|
|
|
|
Type: "ExternalName",
|
|
|
|
ExternalName: "example.com",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "https",
|
|
|
|
Port: 443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
endpoints := []*v1.Endpoints{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-20 16:34:57 +00:00
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Subsets: []v1.EndpointSubset{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Addresses: []v1.EndpointAddress{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
|
|
|
IP: "10.10.0.1",
|
|
|
|
},
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.EndpointPort{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Addresses: []v1.EndpointAddress{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
|
|
|
IP: "10.21.0.1",
|
|
|
|
},
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.EndpointPort{
|
2016-05-20 16:34:57 +00:00
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-26 11:09:36 +00:00
|
|
|
Name: "service3",
|
|
|
|
UID: "3",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Subsets: []v1.EndpointSubset{
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Addresses: []v1.EndpointAddress{
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
|
|
|
IP: "10.15.0.1",
|
|
|
|
},
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.EndpointPort{
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "https",
|
|
|
|
Port: 8443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Addresses: []v1.EndpointAddress{
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
|
|
|
IP: "10.15.0.2",
|
|
|
|
},
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.EndpointPort{
|
2016-05-26 11:09:36 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 9080,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "https",
|
|
|
|
Port: 9443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-05-20 16:34:57 +00:00
|
|
|
}
|
2016-04-20 11:26:51 +00:00
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
2016-05-20 16:34:57 +00:00
|
|
|
endpoints: endpoints,
|
2016-04-20 11:26:51 +00:00
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
2016-05-20 16:34:57 +00:00
|
|
|
"http://10.10.0.1:8080": {
|
|
|
|
URL: "http://10.10.0.1:8080",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"http://10.21.0.1:8080": {
|
|
|
|
URL: "http://10.21.0.1:8080",
|
2016-04-20 11:26:51 +00:00
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
2017-02-10 01:25:38 +00:00
|
|
|
"foo/namedthing": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"https://example.com": {
|
|
|
|
URL: "https://example.com",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
"bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"2": {
|
|
|
|
URL: "http://10.0.0.2:802",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
2016-05-26 11:09:36 +00:00
|
|
|
"https://10.15.0.1:8443": {
|
|
|
|
URL: "https://10.15.0.1:8443",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"https://10.15.0.2:9443": {
|
|
|
|
URL: "https://10.15.0.2:9443",
|
2016-04-20 11:26:51 +00:00
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: true,
|
2016-08-02 23:48:53 +00:00
|
|
|
Priority: len("/bar"),
|
2016-04-20 11:26:51 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
2016-05-05 17:57:35 +00:00
|
|
|
Rule: "PathPrefix:/bar",
|
2016-04-20 11:26:51 +00:00
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-10 01:25:38 +00:00
|
|
|
"foo/namedthing": {
|
|
|
|
Backend: "foo/namedthing",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Priority: len("/namedthing"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/namedthing": {
|
|
|
|
Rule: "PathPrefix:/namedthing",
|
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-20 11:26:51 +00:00
|
|
|
"bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "bar",
|
|
|
|
PassHostHeader: true,
|
2016-04-20 11:26:51 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"bar": {
|
|
|
|
Rule: "Host:bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-04-26 20:26:25 +00:00
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 10:50:06 +00:00
|
|
|
func TestRuleType(t *testing.T) {
|
2017-02-06 23:04:30 +00:00
|
|
|
tests := []struct {
|
|
|
|
desc string
|
|
|
|
ingressRuleType string
|
|
|
|
frontendRuleType string
|
|
|
|
}{
|
2016-05-17 10:50:06 +00:00
|
|
|
{
|
2017-02-06 23:04:30 +00:00
|
|
|
desc: "implicit default",
|
|
|
|
ingressRuleType: "",
|
|
|
|
frontendRuleType: ruleTypePathPrefix,
|
2016-05-15 09:16:27 +00:00
|
|
|
},
|
2016-05-17 10:50:06 +00:00
|
|
|
{
|
2017-02-06 23:04:30 +00:00
|
|
|
desc: "unknown ingress / explicit default",
|
|
|
|
ingressRuleType: "unknown",
|
|
|
|
frontendRuleType: ruleTypePathPrefix,
|
2016-05-17 10:50:06 +00:00
|
|
|
},
|
|
|
|
{
|
2017-02-06 23:04:30 +00:00
|
|
|
desc: "explicit ingress",
|
|
|
|
ingressRuleType: ruleTypePath,
|
|
|
|
frontendRuleType: ruleTypePath,
|
2016-05-17 10:50:06 +00:00
|
|
|
},
|
2017-02-06 23:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ingress := &v1beta1.Ingress{
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "host",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/path",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
2016-05-17 10:50:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-06 23:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if test.ingressRuleType != "" {
|
|
|
|
ingress.ObjectMeta.Annotations = map[string]string{
|
|
|
|
annotationFrontendRuleType: test.ingressRuleType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
service := &v1.Service{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service",
|
|
|
|
UID: "1",
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.1",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
2016-05-17 10:50:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-02-06 23:04:30 +00:00
|
|
|
}
|
2016-05-15 09:16:27 +00:00
|
|
|
|
2017-02-06 23:04:30 +00:00
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: []*v1beta1.Ingress{ingress},
|
|
|
|
services: []*v1.Service{service},
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{DisablePassHostHeaders: true}
|
|
|
|
actualConfig, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error loading ingresses: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := actualConfig.Frontends
|
|
|
|
expected := map[string]*types.Frontend{
|
|
|
|
"host/path": {
|
|
|
|
Backend: "host/path",
|
|
|
|
Priority: len("/path"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/path": {
|
|
|
|
Rule: fmt.Sprintf("%s:/path", test.frontendRuleType),
|
|
|
|
},
|
|
|
|
"host": {
|
|
|
|
Rule: "Host:host",
|
|
|
|
},
|
|
|
|
},
|
2016-05-15 09:16:27 +00:00
|
|
|
},
|
2017-02-06 23:04:30 +00:00
|
|
|
}
|
2016-05-15 09:16:27 +00:00
|
|
|
|
2017-02-06 23:04:30 +00:00
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
})
|
2016-05-15 09:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 11:43:24 +00:00
|
|
|
func TestGetPassHostHeader(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-05-10 11:43:24 +00:00
|
|
|
{
|
|
|
|
Host: "foo",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-05-10 11:43:24 +00:00
|
|
|
{
|
|
|
|
Path: "/bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-05-10 11:43:24 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-05-10 11:43:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-05-10 11:43:24 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service1",
|
|
|
|
Namespace: "awesome",
|
|
|
|
UID: "1",
|
2016-05-10 11:43:24 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-10 11:43:24 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-10 11:43:24 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
2016-05-19 15:44:33 +00:00
|
|
|
provider := Kubernetes{DisablePassHostHeaders: true}
|
2016-05-10 11:43:24 +00:00
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:801",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-05-10 11:43:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
2016-08-02 23:48:53 +00:00
|
|
|
Backend: "foo/bar",
|
|
|
|
Priority: len("/bar"),
|
2016-05-10 11:43:24 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
2016-05-05 17:57:35 +00:00
|
|
|
Rule: "PathPrefix:/bar",
|
2016-05-10 11:43:24 +00:00
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 15:46:19 +00:00
|
|
|
func TestOnlyReferencesServicesFromOwnNamespace(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
|
|
|
Host: "foo",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-05-18 15:46:19 +00:00
|
|
|
ServiceName: "service",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(80),
|
2016-05-18 15:46:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-18 15:46:19 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service",
|
|
|
|
UID: "2",
|
|
|
|
Namespace: "not-awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-18 15:46:19 +00:00
|
|
|
ClusterIP: "10.0.0.2",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:80",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-05-18 15:46:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo": {
|
|
|
|
Backend: "foo",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 00:23:55 +00:00
|
|
|
func TestLoadNamespacedIngresses(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-04-28 00:23:55 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Host: "foo",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Path: "/bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: "bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service3",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(443),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service2",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(802),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-04-28 00:23:55 +00:00
|
|
|
Namespace: "not-awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Host: "baz",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Path: "/baz",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service1",
|
|
|
|
Namespace: "not-awesome",
|
|
|
|
UID: "1",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-18 15:46:19 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service2",
|
|
|
|
Namespace: "awesome",
|
|
|
|
UID: "2",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.2",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Port: 802,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service3",
|
|
|
|
Namespace: "awesome",
|
|
|
|
UID: "3",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.3",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{
|
|
|
|
Namespaces: []string{"awesome"},
|
|
|
|
}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:801",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"2": {
|
|
|
|
URL: "http://10.0.0.2:802",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"3": {
|
|
|
|
URL: "https://10.0.0.3:443",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: true,
|
2016-08-02 23:48:53 +00:00
|
|
|
Priority: len("/bar"),
|
2016-04-28 00:23:55 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
2016-05-05 17:57:35 +00:00
|
|
|
Rule: "PathPrefix:/bar",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "bar",
|
|
|
|
PassHostHeader: true,
|
2016-04-28 00:23:55 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"bar": {
|
|
|
|
Rule: "Host:bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadMultipleNamespacedIngresses(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-04-28 00:23:55 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Host: "foo",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Path: "/bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: "bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service3",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(443),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service2",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(802),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-04-28 00:23:55 +00:00
|
|
|
Namespace: "somewhat-awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Host: "awesome",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Path: "/quix",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-04-28 00:23:55 +00:00
|
|
|
Namespace: "not-awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Host: "baz",
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Path: "/baz",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-04-28 00:23:55 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Name: "service1",
|
|
|
|
Namespace: "awesome",
|
|
|
|
UID: "1",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "somewhat-awesome",
|
|
|
|
Name: "service1",
|
|
|
|
UID: "17",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-18 15:46:19 +00:00
|
|
|
ClusterIP: "10.0.0.4",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-18 15:46:19 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
Name: "service2",
|
|
|
|
UID: "2",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.2",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Port: 802,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-18 15:46:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
Name: "service3",
|
|
|
|
UID: "3",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-04-28 00:23:55 +00:00
|
|
|
ClusterIP: "10.0.0.3",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-04-28 00:23:55 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 443,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{
|
|
|
|
Namespaces: []string{"awesome", "somewhat-awesome"},
|
|
|
|
}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:801",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"2": {
|
|
|
|
URL: "http://10.0.0.2:802",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"3": {
|
|
|
|
URL: "https://10.0.0.3:443",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"awesome/quix": {
|
|
|
|
Servers: map[string]types.Server{
|
2016-05-18 15:46:19 +00:00
|
|
|
"17": {
|
|
|
|
URL: "http://10.0.0.4:801",
|
2016-04-28 00:23:55 +00:00
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: true,
|
2016-08-02 23:48:53 +00:00
|
|
|
Priority: len("/bar"),
|
2016-04-28 00:23:55 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
2016-05-05 17:57:35 +00:00
|
|
|
Rule: "PathPrefix:/bar",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "bar",
|
|
|
|
PassHostHeader: true,
|
2016-04-28 00:23:55 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"bar": {
|
|
|
|
Rule: "Host:bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"awesome/quix": {
|
2016-05-10 11:43:24 +00:00
|
|
|
Backend: "awesome/quix",
|
|
|
|
PassHostHeader: true,
|
2016-08-02 23:48:53 +00:00
|
|
|
Priority: len("/quix"),
|
2016-04-28 00:23:55 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/quix": {
|
2016-05-05 17:57:35 +00:00
|
|
|
Rule: "PathPrefix:/quix",
|
2016-04-28 00:23:55 +00:00
|
|
|
},
|
|
|
|
"awesome": {
|
|
|
|
Rule: "Host:awesome",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-05-25 12:16:19 +00:00
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHostlessIngress(t *testing.T) {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-25 12:16:19 +00:00
|
|
|
Namespace: "awesome",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
2016-05-25 12:16:19 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
2016-05-25 12:16:19 +00:00
|
|
|
{
|
|
|
|
Path: "/bar",
|
2016-11-11 22:50:20 +00:00
|
|
|
Backend: v1beta1.IngressBackend{
|
2016-05-25 12:16:19 +00:00
|
|
|
ServiceName: "service1",
|
2016-11-11 22:50:20 +00:00
|
|
|
ServicePort: intstr.FromInt(801),
|
2016-05-25 12:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
2016-11-11 22:50:20 +00:00
|
|
|
services := []*v1.Service{
|
2016-05-25 12:16:19 +00:00
|
|
|
{
|
2016-11-11 22:50:20 +00:00
|
|
|
ObjectMeta: v1.ObjectMeta{
|
2016-05-25 12:16:19 +00:00
|
|
|
Name: "service1",
|
|
|
|
Namespace: "awesome",
|
|
|
|
UID: "1",
|
|
|
|
},
|
2016-11-11 22:50:20 +00:00
|
|
|
Spec: v1.ServiceSpec{
|
2016-05-25 12:16:19 +00:00
|
|
|
ClusterIP: "10.0.0.1",
|
2016-11-11 22:50:20 +00:00
|
|
|
Ports: []v1.ServicePort{
|
2016-05-25 12:16:19 +00:00
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 801,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
2016-05-26 13:55:50 +00:00
|
|
|
provider := Kubernetes{DisablePassHostHeaders: true}
|
2016-05-25 12:16:19 +00:00
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"1": {
|
|
|
|
URL: "http://10.0.0.1:801",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
2016-05-25 12:16:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"/bar": {
|
2016-08-02 23:48:53 +00:00
|
|
|
Backend: "/bar",
|
|
|
|
Priority: len("/bar"),
|
2016-05-25 12:16:19 +00:00
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
|
|
|
Rule: "PathPrefix:/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-04-28 00:23:55 +00:00
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 16:47:48 +00:00
|
|
|
func TestServiceAnnotations(t *testing.T) {
|
2017-01-25 13:11:00 +00:00
|
|
|
ingresses := []*v1beta1.Ingress{{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/bar",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service1",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Host: "bar",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service2",
|
|
|
|
ServicePort: intstr.FromInt(802),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
services := []*v1.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
|
|
|
Annotations: map[string]string{
|
2017-02-03 16:47:48 +00:00
|
|
|
"traefik.backend.circuitbreaker": "NetworkErrorRatio() > 0.5",
|
2017-01-25 13:11:00 +00:00
|
|
|
"traefik.backend.loadbalancer.method": "drr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.1",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service2",
|
|
|
|
UID: "2",
|
|
|
|
Namespace: "testing",
|
|
|
|
Annotations: map[string]string{
|
2017-02-03 16:47:48 +00:00
|
|
|
"traefik.backend.circuitbreaker": "",
|
2017-01-25 13:11:00 +00:00
|
|
|
"traefik.backend.loadbalancer.sticky": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.2",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Port: 802,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
endpoints := []*v1.Endpoints{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Subsets: []v1.EndpointSubset{
|
|
|
|
{
|
|
|
|
Addresses: []v1.EndpointAddress{
|
|
|
|
{
|
|
|
|
IP: "10.10.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addresses: []v1.EndpointAddress{
|
|
|
|
{
|
|
|
|
IP: "10.21.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service2",
|
|
|
|
UID: "2",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Subsets: []v1.EndpointSubset{
|
|
|
|
{
|
|
|
|
Addresses: []v1.EndpointAddress{
|
|
|
|
{
|
|
|
|
IP: "10.15.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Addresses: []v1.EndpointAddress{
|
|
|
|
{
|
|
|
|
IP: "10.15.0.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ports: []v1.EndpointPort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
endpoints: endpoints,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"http://10.10.0.1:8080": {
|
|
|
|
URL: "http://10.10.0.1:8080",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"http://10.21.0.1:8080": {
|
|
|
|
URL: "http://10.21.0.1:8080",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
2017-02-03 16:47:48 +00:00
|
|
|
CircuitBreaker: &types.CircuitBreaker{
|
|
|
|
Expression: "NetworkErrorRatio() > 0.5",
|
|
|
|
},
|
2017-01-25 13:11:00 +00:00
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "drr",
|
|
|
|
Sticky: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"http://10.15.0.1:8080": {
|
|
|
|
URL: "http://10.15.0.1:8080",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
"http://10.15.0.2:8080": {
|
|
|
|
URL: "http://10.15.0.2:8080",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Method: "wrr",
|
|
|
|
Sticky: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Priority: len("/bar"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
|
|
|
Rule: "PathPrefix:/bar",
|
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Backend: "bar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"bar": {
|
|
|
|
Rule: "Host:bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 11:27:30 +00:00
|
|
|
func TestIngressAnnotations(t *testing.T) {
|
|
|
|
ingresses := []*v1beta1.Ingress{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Namespace: "testing",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"traefik.frontend.passHostHeader": "false",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/bar",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service1",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Namespace: "testing",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"traefik.frontend.passHostHeader": "true",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "other",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/stuff",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service1",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
services := []*v1.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.1",
|
|
|
|
Type: "ExternalName",
|
|
|
|
ExternalName: "example.com",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints := []*v1.Endpoints{}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
endpoints: endpoints,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"http://example.com": {
|
|
|
|
URL: "http://example.com",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"other/stuff": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"http://example.com": {
|
|
|
|
URL: "http://example.com",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: false,
|
|
|
|
Priority: len("/bar"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
|
|
|
Rule: "PathPrefix:/bar",
|
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"other/stuff": {
|
|
|
|
Backend: "other/stuff",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Priority: len("/stuff"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/stuff": {
|
|
|
|
Rule: "PathPrefix:/stuff",
|
|
|
|
},
|
|
|
|
"other": {
|
|
|
|
Rule: "Host:other",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 19:54:27 +00:00
|
|
|
func TestInvalidPassHostHeaderValue(t *testing.T) {
|
|
|
|
ingresses := []*v1beta1.Ingress{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Namespace: "testing",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"traefik.frontend.passHostHeader": "herpderp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1beta1.IngressSpec{
|
|
|
|
Rules: []v1beta1.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo",
|
|
|
|
IngressRuleValue: v1beta1.IngressRuleValue{
|
|
|
|
HTTP: &v1beta1.HTTPIngressRuleValue{
|
|
|
|
Paths: []v1beta1.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/bar",
|
|
|
|
Backend: v1beta1.IngressBackend{
|
|
|
|
ServiceName: "service1",
|
|
|
|
ServicePort: intstr.FromInt(80),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
services := []*v1.Service{
|
|
|
|
{
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "service1",
|
|
|
|
UID: "1",
|
|
|
|
Namespace: "testing",
|
|
|
|
},
|
|
|
|
Spec: v1.ServiceSpec{
|
|
|
|
ClusterIP: "10.0.0.1",
|
|
|
|
Type: "ExternalName",
|
|
|
|
ExternalName: "example.com",
|
|
|
|
Ports: []v1.ServicePort{
|
|
|
|
{
|
|
|
|
Name: "http",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints := []*v1.Endpoints{}
|
|
|
|
watchChan := make(chan interface{})
|
|
|
|
client := clientMock{
|
|
|
|
ingresses: ingresses,
|
|
|
|
services: services,
|
|
|
|
endpoints: endpoints,
|
|
|
|
watchChan: watchChan,
|
|
|
|
}
|
|
|
|
provider := Kubernetes{}
|
|
|
|
actual, err := provider.loadIngresses(client)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &types.Configuration{
|
|
|
|
Backends: map[string]*types.Backend{
|
|
|
|
"foo/bar": {
|
|
|
|
Servers: map[string]types.Server{
|
|
|
|
"http://example.com": {
|
|
|
|
URL: "http://example.com",
|
|
|
|
Weight: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CircuitBreaker: nil,
|
|
|
|
LoadBalancer: &types.LoadBalancer{
|
|
|
|
Sticky: false,
|
|
|
|
Method: "wrr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Frontends: map[string]*types.Frontend{
|
|
|
|
"foo/bar": {
|
|
|
|
Backend: "foo/bar",
|
|
|
|
PassHostHeader: true,
|
|
|
|
Priority: len("/bar"),
|
|
|
|
Routes: map[string]types.Route{
|
|
|
|
"/bar": {
|
|
|
|
Rule: "PathPrefix:/bar",
|
|
|
|
},
|
|
|
|
"foo": {
|
|
|
|
Rule: "Host:foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actualJSON, _ := json.Marshal(actual)
|
|
|
|
expectedJSON, _ := json.Marshal(expected)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("expected %+v, got %+v", string(expectedJSON), string(actualJSON))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:04:30 +00:00
|
|
|
func TestGetRuleTypeFromAnnotation(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
wantedUnknown bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: ruleTypePathPrefixStrip,
|
|
|
|
wantedUnknown: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: ruleTypePathStrip,
|
|
|
|
wantedUnknown: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: ruleTypePath,
|
|
|
|
wantedUnknown: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: ruleTypePathPrefix,
|
|
|
|
wantedUnknown: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
wantedUnknown: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "Unknown",
|
|
|
|
wantedUnknown: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
inputs := []string{test.in, strings.ToLower(test.in)}
|
|
|
|
if inputs[0] == inputs[1] {
|
|
|
|
// Lower-casing makes no difference -- truncating to single case.
|
|
|
|
inputs = inputs[:1]
|
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
|
|
|
t.Run(fmt.Sprintf("in='%s'", input), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
annotations := map[string]string{}
|
|
|
|
if test.in != "" {
|
|
|
|
annotations[annotationFrontendRuleType] = test.in
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRuleType, gotUnknown := getRuleTypeFromAnnotation(annotations)
|
|
|
|
|
|
|
|
if gotUnknown != test.wantedUnknown {
|
|
|
|
t.Errorf("got unknown '%t', wanted '%t'", gotUnknown, test.wantedUnknown)
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotRuleType != test.in {
|
|
|
|
t.Errorf("got rule type '%s', wanted '%s'", gotRuleType, test.in)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 11:26:51 +00:00
|
|
|
type clientMock struct {
|
2016-11-11 22:50:20 +00:00
|
|
|
ingresses []*v1beta1.Ingress
|
|
|
|
services []*v1.Service
|
|
|
|
endpoints []*v1.Endpoints
|
2016-04-20 11:26:51 +00:00
|
|
|
watchChan chan interface{}
|
|
|
|
}
|
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
func (c clientMock) GetIngresses(namespaces k8s.Namespaces) []*v1beta1.Ingress {
|
|
|
|
result := make([]*v1beta1.Ingress, 0, len(c.ingresses))
|
|
|
|
|
2016-04-28 00:23:55 +00:00
|
|
|
for _, ingress := range c.ingresses {
|
2016-11-11 22:50:20 +00:00
|
|
|
if k8s.HasNamespace(ingress, namespaces) {
|
|
|
|
result = append(result, ingress)
|
2016-04-28 00:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
return result
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
|
|
|
|
func (c clientMock) GetService(namespace, name string) (*v1.Service, bool, error) {
|
2016-05-18 15:46:19 +00:00
|
|
|
for _, service := range c.services {
|
2016-05-25 23:53:51 +00:00
|
|
|
if service.Namespace == namespace && service.Name == name {
|
2016-11-11 22:50:20 +00:00
|
|
|
return service, true, nil
|
2016-05-18 15:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
return &v1.Service{}, true, nil
|
2016-04-20 11:26:51 +00:00
|
|
|
}
|
2016-05-20 16:34:57 +00:00
|
|
|
|
2016-11-11 22:50:20 +00:00
|
|
|
func (c clientMock) GetEndpoints(namespace, name string) (*v1.Endpoints, bool, error) {
|
2016-05-20 16:34:57 +00:00
|
|
|
for _, endpoints := range c.endpoints {
|
|
|
|
if endpoints.Namespace == namespace && endpoints.Name == name {
|
2016-11-11 22:50:20 +00:00
|
|
|
return endpoints, true, nil
|
2016-05-20 16:34:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 22:50:20 +00:00
|
|
|
return &v1.Endpoints{}, true, nil
|
2016-05-20 16:34:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 20:20:39 +00:00
|
|
|
func (c clientMock) WatchAll(labelString string, stopCh <-chan struct{}) (<-chan interface{}, error) {
|
2016-11-11 22:50:20 +00:00
|
|
|
return c.watchChan, nil
|
2016-04-25 14:56:06 +00:00
|
|
|
}
|