Benchmark for k8s services (#2107)

* Benchmark for k8s services

* Adding k8s.io/client-go/kubernetes/fake dep
This commit is contained in:
Brad Beam
2018-09-29 10:43:09 -05:00
committed by Miek Gieben
parent 57197a49a4
commit a80ec6096f
199 changed files with 25884 additions and 11 deletions

View File

@@ -58,7 +58,7 @@ type dnsControl struct {
// aligned ( we use sync.LoadAtomic with this )
modified int64
client *kubernetes.Clientset
client kubernetes.Interface
selector labels.Selector
@@ -100,7 +100,7 @@ type dnsControlOpts struct {
}
// newDNSController creates a controller for CoreDNS.
func newdnsController(kubeClient *kubernetes.Clientset, opts dnsControlOpts) *dnsControl {
func newdnsController(kubeClient kubernetes.Interface, opts dnsControlOpts) *dnsControl {
dns := dnsControl{
client: kubeClient,
selector: opts.selector,
@@ -200,7 +200,7 @@ func epIPIndexFunc(obj interface{}) ([]string, error) {
return idx, nil
}
func serviceListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
func serviceListFunc(c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
if s != nil {
opts.LabelSelector = s.String()
@@ -210,7 +210,7 @@ func serviceListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func
}
}
func podListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
func podListFunc(c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
if s != nil {
opts.LabelSelector = s.String()
@@ -220,7 +220,7 @@ func podListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(met
}
}
func serviceWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
func serviceWatchFunc(c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
return func(options meta.ListOptions) (watch.Interface, error) {
if s != nil {
options.LabelSelector = s.String()
@@ -230,7 +230,7 @@ func serviceWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fun
}
}
func podWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
func podWatchFunc(c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
return func(options meta.ListOptions) (watch.Interface, error) {
if s != nil {
options.LabelSelector = s.String()
@@ -240,7 +240,7 @@ func podWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(op
}
}
func endpointsListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
func endpointsListFunc(c kubernetes.Interface, ns string, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
if s != nil {
opts.LabelSelector = s.String()
@@ -250,7 +250,7 @@ func endpointsListFunc(c *kubernetes.Clientset, ns string, s labels.Selector) fu
}
}
func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
func endpointsWatchFunc(c kubernetes.Interface, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
return func(options meta.ListOptions) (watch.Interface, error) {
if s != nil {
options.LabelSelector = s.String()
@@ -260,7 +260,7 @@ func endpointsWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) f
}
}
func namespaceListFunc(c *kubernetes.Clientset, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
func namespaceListFunc(c kubernetes.Interface, s labels.Selector) func(meta.ListOptions) (runtime.Object, error) {
return func(opts meta.ListOptions) (runtime.Object, error) {
if s != nil {
opts.LabelSelector = s.String()
@@ -270,7 +270,7 @@ func namespaceListFunc(c *kubernetes.Clientset, s labels.Selector) func(meta.Lis
}
}
func namespaceWatchFunc(c *kubernetes.Clientset, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
func namespaceWatchFunc(c kubernetes.Interface, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
return func(options meta.ListOptions) (watch.Interface, error) {
if s != nil {
options.LabelSelector = s.String()

View File

@@ -1,11 +1,18 @@
package kubernetes
import (
"context"
"net"
"strconv"
"strings"
"testing"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
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 endpointSubsets(addrs ...string) (eps []api.EndpointSubset) {
@@ -51,3 +58,155 @@ func TestEndpointsSubsetDiffs(t *testing.T) {
}
}
}
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()
dco := dnsControlOpts{
zones: []string{"cluster.local."},
}
controller := newdnsController(client, 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,
},
})
}