This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
controller_test.go
170 lines (156 loc) · 3.9 KB
/
controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package kubernetes
import (
"context"
"net"
"strconv"
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
clusterfake "github.com/openshift/cluster-api/pkg/client/clientset_generated/clientset/fake"
api "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
)
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func BenchmarkController(b *testing.B) {
client := fake.NewSimpleClientset()
clusterclient := clusterfake.NewSimpleClientset()
dco := dnsControlOpts{
zones: []string{"cluster.local."},
}
controller := newdnsController(client, clusterclient, dco)
cidr := "10.0.0.0/19"
// Add resources
generateEndpoints(cidr, client)
generateSvcs(cidr, "all", client)
m := new(dns.Msg)
m.SetQuestion("svc1.testns.svc.cluster.local.", dns.TypeA)
k := New([]string{"cluster.local."})
k.APIConn = controller
ctx := context.Background()
rw := &test.ResponseWriter{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
k.ServeDNS(ctx, rw, m)
}
}
func generateEndpoints(cidr string, client kubernetes.Interface) {
// https://groups.google.com/d/msg/golang-nuts/zlcYA4qk-94/TWRFHeXJCcYJ
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
count := 1
ep := &api.Endpoints{
Subsets: []api.EndpointSubset{{
Ports: []api.EndpointPort{
{
Port: 80,
Protocol: "tcp",
Name: "http",
},
},
}},
ObjectMeta: meta.ObjectMeta{
Namespace: "testns",
},
}
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ep.Subsets[0].Addresses = []api.EndpointAddress{
{
IP: ip.String(),
Hostname: "foo" + strconv.Itoa(count),
},
}
ep.ObjectMeta.Name = "svc" + strconv.Itoa(count)
_, err = client.Core().Endpoints("testns").Create(ep)
count += 1
}
}
func generateSvcs(cidr string, svcType string, client kubernetes.Interface) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
count := 1
switch svcType {
case "clusterip":
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
createClusterIPSvc(count, client, ip)
count += 1
}
case "headless":
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
createHeadlessSvc(count, client, ip)
count += 1
}
case "external":
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
createExternalSvc(count, client, ip)
count += 1
}
default:
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
if count%3 == 0 {
createClusterIPSvc(count, client, ip)
} else if count%3 == 1 {
createHeadlessSvc(count, client, ip)
} else if count%3 == 2 {
createExternalSvc(count, client, ip)
}
count += 1
}
}
}
func createClusterIPSvc(suffix int, client kubernetes.Interface, ip net.IP) {
client.Core().Services("testns").Create(&api.Service{
ObjectMeta: meta.ObjectMeta{
Name: "svc" + strconv.Itoa(suffix),
Namespace: "testns",
},
Spec: api.ServiceSpec{
ClusterIP: ip.String(),
Ports: []api.ServicePort{{
Name: "http",
Protocol: "tcp",
Port: 80,
}},
},
})
}
func createHeadlessSvc(suffix int, client kubernetes.Interface, ip net.IP) {
client.Core().Services("testns").Create(&api.Service{
ObjectMeta: meta.ObjectMeta{
Name: "hdls" + strconv.Itoa(suffix),
Namespace: "testns",
},
Spec: api.ServiceSpec{
ClusterIP: api.ClusterIPNone,
},
})
}
func createExternalSvc(suffix int, client kubernetes.Interface, ip net.IP) {
client.Core().Services("testns").Create(&api.Service{
ObjectMeta: meta.ObjectMeta{
Name: "external" + strconv.Itoa(suffix),
Namespace: "testns",
},
Spec: api.ServiceSpec{
ExternalName: "coredns" + strconv.Itoa(suffix) + ".io",
Ports: []api.ServicePort{{
Name: "http",
Protocol: "tcp",
Port: 80,
}},
Type: api.ServiceTypeExternalName,
},
})
}