Skip to content

Commit 848e45c

Browse files
jbdoumenjoujuliens
authored andcommitted
Adds Kubernetes provider support
Co-authored-by: Julien Salleyron <[email protected]>
1 parent 2c0bf33 commit 848e45c

13 files changed

+3773
-3
lines changed

cmd/configuration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import (
1414
"github.com/containous/traefik/old/provider/ecs"
1515
"github.com/containous/traefik/old/provider/etcd"
1616
"github.com/containous/traefik/old/provider/eureka"
17-
"github.com/containous/traefik/old/provider/kubernetes"
1817
"github.com/containous/traefik/old/provider/mesos"
1918
"github.com/containous/traefik/old/provider/rancher"
2019
"github.com/containous/traefik/old/provider/zk"
2120
"github.com/containous/traefik/ping"
2221
"github.com/containous/traefik/provider/docker"
2322
"github.com/containous/traefik/provider/file"
23+
"github.com/containous/traefik/provider/kubernetes"
2424
"github.com/containous/traefik/provider/marathon"
2525
"github.com/containous/traefik/provider/rest"
2626
"github.com/containous/traefik/tracing/datadog"

cmd/traefik/traefik.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"github.com/containous/traefik/job"
2828
"github.com/containous/traefik/log"
2929
"github.com/containous/traefik/old/provider/ecs"
30-
"github.com/containous/traefik/old/provider/kubernetes"
3130
oldtypes "github.com/containous/traefik/old/types"
3231
"github.com/containous/traefik/provider/aggregator"
32+
"github.com/containous/traefik/provider/kubernetes"
3333
"github.com/containous/traefik/safe"
3434
"github.com/containous/traefik/server"
3535
"github.com/containous/traefik/server/router"

config/static/static_config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
"github.com/containous/traefik/old/provider/ecs"
1616
"github.com/containous/traefik/old/provider/etcd"
1717
"github.com/containous/traefik/old/provider/eureka"
18-
"github.com/containous/traefik/old/provider/kubernetes"
1918
"github.com/containous/traefik/old/provider/mesos"
2019
"github.com/containous/traefik/old/provider/rancher"
2120
"github.com/containous/traefik/old/provider/zk"
2221
"github.com/containous/traefik/ping"
2322
acmeprovider "github.com/containous/traefik/provider/acme"
2423
"github.com/containous/traefik/provider/docker"
2524
"github.com/containous/traefik/provider/file"
25+
"github.com/containous/traefik/provider/kubernetes"
2626
"github.com/containous/traefik/provider/marathon"
2727
"github.com/containous/traefik/provider/rest"
2828
"github.com/containous/traefik/tls"

provider/aggregator/aggregator.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func NewProviderAggregator(conf static.Providers) ProviderAggregator {
3535
p.quietAddProvider(conf.Rest)
3636
}
3737

38+
if conf.Kubernetes != nil {
39+
p.quietAddProvider(conf.Kubernetes)
40+
}
41+
3842
return p
3943
}
4044

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package kubernetes
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
corev1 "k8s.io/api/core/v1"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/types"
10+
)
11+
12+
func buildEndpoint(opts ...func(*corev1.Endpoints)) *corev1.Endpoints {
13+
e := &corev1.Endpoints{}
14+
for _, opt := range opts {
15+
opt(e)
16+
}
17+
return e
18+
}
19+
20+
func eNamespace(value string) func(*corev1.Endpoints) {
21+
return func(i *corev1.Endpoints) {
22+
i.Namespace = value
23+
}
24+
}
25+
26+
func eName(value string) func(*corev1.Endpoints) {
27+
return func(i *corev1.Endpoints) {
28+
i.Name = value
29+
}
30+
}
31+
32+
func eUID(value types.UID) func(*corev1.Endpoints) {
33+
return func(i *corev1.Endpoints) {
34+
i.UID = value
35+
}
36+
}
37+
38+
func subset(opts ...func(*corev1.EndpointSubset)) func(*corev1.Endpoints) {
39+
return func(e *corev1.Endpoints) {
40+
s := &corev1.EndpointSubset{}
41+
for _, opt := range opts {
42+
opt(s)
43+
}
44+
e.Subsets = append(e.Subsets, *s)
45+
}
46+
}
47+
48+
func eAddresses(opts ...func(*corev1.EndpointAddress)) func(*corev1.EndpointSubset) {
49+
return func(subset *corev1.EndpointSubset) {
50+
for _, opt := range opts {
51+
a := &corev1.EndpointAddress{}
52+
opt(a)
53+
subset.Addresses = append(subset.Addresses, *a)
54+
}
55+
}
56+
}
57+
58+
func eAddress(ip string) func(*corev1.EndpointAddress) {
59+
return func(address *corev1.EndpointAddress) {
60+
address.IP = ip
61+
}
62+
}
63+
64+
func eAddressWithTargetRef(targetRef, ip string) func(*corev1.EndpointAddress) {
65+
return func(address *corev1.EndpointAddress) {
66+
address.TargetRef = &corev1.ObjectReference{Name: targetRef}
67+
address.IP = ip
68+
}
69+
}
70+
71+
func ePorts(opts ...func(port *corev1.EndpointPort)) func(*corev1.EndpointSubset) {
72+
return func(spec *corev1.EndpointSubset) {
73+
for _, opt := range opts {
74+
p := &corev1.EndpointPort{}
75+
opt(p)
76+
spec.Ports = append(spec.Ports, *p)
77+
}
78+
}
79+
}
80+
81+
func ePort(port int32, name string) func(*corev1.EndpointPort) {
82+
return func(sp *corev1.EndpointPort) {
83+
sp.Port = port
84+
sp.Name = name
85+
}
86+
}
87+
88+
// Test
89+
90+
func TestBuildEndpoint(t *testing.T) {
91+
actual := buildEndpoint(
92+
eNamespace("testing"),
93+
eName("service3"),
94+
eUID("3"),
95+
subset(
96+
eAddresses(eAddress("10.15.0.1")),
97+
ePorts(
98+
ePort(8080, "http"),
99+
ePort(8443, "https"),
100+
),
101+
),
102+
subset(
103+
eAddresses(eAddress("10.15.0.2")),
104+
ePorts(
105+
ePort(9080, "http"),
106+
ePort(9443, "https"),
107+
),
108+
),
109+
)
110+
111+
assert.EqualValues(t, sampleEndpoint1(), actual)
112+
}
113+
114+
func sampleEndpoint1() *corev1.Endpoints {
115+
return &corev1.Endpoints{
116+
ObjectMeta: metav1.ObjectMeta{
117+
Name: "service3",
118+
UID: "3",
119+
Namespace: "testing",
120+
},
121+
Subsets: []corev1.EndpointSubset{
122+
{
123+
Addresses: []corev1.EndpointAddress{
124+
{
125+
IP: "10.15.0.1",
126+
},
127+
},
128+
Ports: []corev1.EndpointPort{
129+
{
130+
Name: "http",
131+
Port: 8080,
132+
},
133+
{
134+
Name: "https",
135+
Port: 8443,
136+
},
137+
},
138+
},
139+
{
140+
Addresses: []corev1.EndpointAddress{
141+
{
142+
IP: "10.15.0.2",
143+
},
144+
},
145+
Ports: []corev1.EndpointPort{
146+
{
147+
Name: "http",
148+
Port: 9080,
149+
},
150+
{
151+
Name: "https",
152+
Port: 9443,
153+
},
154+
},
155+
},
156+
},
157+
}
158+
}

0 commit comments

Comments
 (0)