aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/kubernetes_test.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2018-10-09 21:56:09 +0100
committerGravatar GitHub <noreply@github.com> 2018-10-09 21:56:09 +0100
commit830e97f8008ed83abb6b293a12a87f3fce34e423 (patch)
treed1a020aa3e7b232a52018479ab7972d350bd74e7 /plugin/kubernetes/kubernetes_test.go
parent298b860a97481630ec65698ab0465675bf58b43a (diff)
downloadcoredns-830e97f8008ed83abb6b293a12a87f3fce34e423.tar.gz
coredns-830e97f8008ed83abb6b293a12a87f3fce34e423.tar.zst
coredns-830e97f8008ed83abb6b293a12a87f3fce34e423.zip
plugin/kubernetes: allow trimming down of cached items. (#2128)
* Convert to runtime.Object to smaller structs This adds conversion for all the objects we want to keep in the cache. It keeps the minimum for CoreDNS to function and throws away the rest. The conversion: api.Endpoints -> object.Endpoints api.Pod -> object.Pod api.Serivce -> object.Service We needed to copy some client-go stuff to insert a conversion function into NewIndexInformers. Some unrelated cleanups in the watch functionality as that needed to be touched because of the above translation of objects. Signed-off-by: Miek Gieben <miek@miek.nl> * Reduce test line-count Signed-off-by: Miek Gieben <miek@miek.nl> * ....and fix test Signed-off-by: Miek Gieben <miek@miek.nl> * Drop use of append Signed-off-by: Miek Gieben <miek@miek.nl> * cosmetic changes Signed-off-by: Miek Gieben <miek@miek.nl> * that was a typo Signed-off-by: Miek Gieben <miek@miek.nl> * re-introduce append here We can't really use len() here because we don't know the number before hand. Signed-off-by: Miek Gieben <miek@miek.nl> * comment in better place Signed-off-by: Miek Gieben <miek@miek.nl> * Make the timestamp a bool; thats where it is used for Signed-off-by: Miek Gieben <miek@miek.nl> * Set incoming object to nil Explicataliy discard the converted object; we did a deep copy it's not needed anymore. Signed-off-by: Miek Gieben <miek@miek.nl> * Per Chris's comment Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/kubernetes/kubernetes_test.go')
-rw-r--r--plugin/kubernetes/kubernetes_test.go327
1 files changed, 112 insertions, 215 deletions
diff --git a/plugin/kubernetes/kubernetes_test.go b/plugin/kubernetes/kubernetes_test.go
index 36d00a92f..f35c9cd2c 100644
--- a/plugin/kubernetes/kubernetes_test.go
+++ b/plugin/kubernetes/kubernetes_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
+ "github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/coredns/coredns/plugin/pkg/watch"
"github.com/coredns/coredns/request"
@@ -49,7 +50,7 @@ func TestEndpointHostname(t *testing.T) {
{"10.11.12.13", "", "hello-abcde", "hello-abcde", true},
}
for _, test := range tests {
- result := endpointHostname(api.EndpointAddress{IP: test.ip, Hostname: test.hostname, TargetRef: &api.ObjectReference{Name: test.podName}}, test.endpointNameMode)
+ result := endpointHostname(object.EndpointAddress{IP: test.ip, Hostname: test.hostname, TargetRefName: test.podName}, test.endpointNameMode)
if result != test.expected {
t.Errorf("Expected endpoint name for (ip:%v hostname:%v) to be '%v', but got '%v'", test.ip, test.hostname, test.expected, result)
}
@@ -58,184 +59,122 @@ func TestEndpointHostname(t *testing.T) {
type APIConnServiceTest struct{}
-func (APIConnServiceTest) HasSynced() bool { return true }
-func (APIConnServiceTest) Run() { return }
-func (APIConnServiceTest) Stop() error { return nil }
-func (APIConnServiceTest) PodIndex(string) []*api.Pod { return nil }
-func (APIConnServiceTest) SvcIndexReverse(string) []*api.Service { return nil }
-func (APIConnServiceTest) EpIndexReverse(string) []*api.Endpoints { return nil }
-func (APIConnServiceTest) Modified() int64 { return 0 }
-func (APIConnServiceTest) SetWatchChan(watch.Chan) {}
-func (APIConnServiceTest) Watch(string) error { return nil }
-func (APIConnServiceTest) StopWatching(string) {}
+func (APIConnServiceTest) HasSynced() bool { return true }
+func (APIConnServiceTest) Run() { return }
+func (APIConnServiceTest) Stop() error { return nil }
+func (APIConnServiceTest) PodIndex(string) []*object.Pod { return nil }
+func (APIConnServiceTest) SvcIndexReverse(string) []*object.Service { return nil }
+func (APIConnServiceTest) EpIndexReverse(string) []*object.Endpoints { return nil }
+func (APIConnServiceTest) Modified() int64 { return 0 }
+func (APIConnServiceTest) SetWatchChan(watch.Chan) {}
+func (APIConnServiceTest) Watch(string) error { return nil }
+func (APIConnServiceTest) StopWatching(string) {}
-func (APIConnServiceTest) SvcIndex(string) []*api.Service {
- svcs := []*api.Service{
+func (APIConnServiceTest) SvcIndex(string) []*object.Service {
+ svcs := []*object.Service{
{
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ClusterIP: "10.0.0.1",
- Ports: []api.ServicePort{{
- Name: "http",
- Protocol: "tcp",
- Port: 80,
- }},
+ Name: "svc1",
+ Namespace: "testns",
+ ClusterIP: "10.0.0.1",
+ Ports: []api.ServicePort{
+ {Name: "http", Protocol: "tcp", Port: 80},
},
},
{
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ClusterIP: api.ClusterIPNone,
- },
+ Name: "hdls1",
+ Namespace: "testns",
+ ClusterIP: api.ClusterIPNone,
},
{
- ObjectMeta: meta.ObjectMeta{
- Name: "external",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ExternalName: "coredns.io",
- Ports: []api.ServicePort{{
- Name: "http",
- Protocol: "tcp",
- Port: 80,
- }},
- Type: api.ServiceTypeExternalName,
+ Name: "external",
+ Namespace: "testns",
+ ExternalName: "coredns.io",
+ Type: api.ServiceTypeExternalName,
+ Ports: []api.ServicePort{
+ {Name: "http", Protocol: "tcp", Port: 80},
},
},
}
return svcs
}
-func (APIConnServiceTest) ServiceList() []*api.Service {
- svcs := []*api.Service{
+func (APIConnServiceTest) ServiceList() []*object.Service {
+ svcs := []*object.Service{
{
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ClusterIP: "10.0.0.1",
- Ports: []api.ServicePort{{
- Name: "http",
- Protocol: "tcp",
- Port: 80,
- }},
+ Name: "svc1",
+ Namespace: "testns",
+ ClusterIP: "10.0.0.1",
+ Ports: []api.ServicePort{
+ {Name: "http", Protocol: "tcp", Port: 80},
},
},
{
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ClusterIP: api.ClusterIPNone,
- },
+ Name: "hdls1",
+ Namespace: "testns",
+ ClusterIP: api.ClusterIPNone,
},
{
- ObjectMeta: meta.ObjectMeta{
- Name: "external",
- Namespace: "testns",
- },
- Spec: api.ServiceSpec{
- ExternalName: "coredns.io",
- Ports: []api.ServicePort{{
- Name: "http",
- Protocol: "tcp",
- Port: 80,
- }},
- Type: api.ServiceTypeExternalName,
+ Name: "external",
+ Namespace: "testns",
+ ExternalName: "coredns.io",
+ Type: api.ServiceTypeExternalName,
+ Ports: []api.ServicePort{
+ {Name: "http", Protocol: "tcp", Port: 80},
},
},
}
return svcs
}
-func (APIConnServiceTest) EpIndex(string) []*api.Endpoints {
- n := "test.node.foo.bar"
-
- eps := []*api.Endpoints{
+func (APIConnServiceTest) EpIndex(string) []*object.Endpoints {
+ eps := []*object.Endpoints{
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.1",
- Hostname: "ep1a",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.1", Hostname: "ep1a"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
+ Name: "svc1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.2",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.2"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
+ Name: "hdls1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.3",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.3"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
+ Name: "hdls1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "10.9.8.7",
- NodeName: &n,
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "10.9.8.7", NodeName: "test.node.foo.bar"},
},
},
},
@@ -244,85 +183,55 @@ func (APIConnServiceTest) EpIndex(string) []*api.Endpoints {
return eps
}
-func (APIConnServiceTest) EndpointsList() []*api.Endpoints {
- n := "test.node.foo.bar"
-
- eps := []*api.Endpoints{
+func (APIConnServiceTest) EndpointsList() []*object.Endpoints {
+ eps := []*object.Endpoints{
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.1",
- Hostname: "ep1a",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.1", Hostname: "ep1a"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
+ Name: "svc1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.2",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.2"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
+ Name: "hdls1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "172.0.0.3",
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "172.0.0.3"},
},
- Ports: []api.EndpointPort{
- {
- Port: 80,
- Protocol: "tcp",
- Name: "http",
- },
+ Ports: []object.EndpointPort{
+ {Port: 80, Protocol: "tcp", Name: "http"},
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "hdls1",
- Namespace: "testns",
- },
+ Name: "hdls1",
+ Namespace: "testns",
},
{
- Subsets: []api.EndpointSubset{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
- {
- IP: "10.9.8.7",
- NodeName: &n,
- },
+ Addresses: []object.EndpointAddress{
+ {IP: "10.9.8.7", NodeName: "test.node.foo.bar"},
},
},
},
@@ -397,11 +306,9 @@ func TestServices(t *testing.T) {
func TestServiceFQDN(t *testing.T) {
fqdn := serviceFQDN(
- &api.Service{
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
+ &object.Service{
+ Name: "svc1",
+ Namespace: "testns",
}, "cluster.local")
expected := "svc1.testns.svc.cluster.local."
@@ -412,14 +319,10 @@ func TestServiceFQDN(t *testing.T) {
func TestPodFQDN(t *testing.T) {
fqdn := podFQDN(
- &api.Pod{
- ObjectMeta: meta.ObjectMeta{
- Name: "pod1",
- Namespace: "testns",
- },
- Status: api.PodStatus{
- PodIP: "10.10.0.10",
- },
+ &object.Pod{
+ Name: "pod1",
+ Namespace: "testns",
+ PodIP: "10.10.0.10",
}, "cluster.local")
expected := "10-10-0-10.testns.pod.cluster.local."
@@ -427,14 +330,10 @@ func TestPodFQDN(t *testing.T) {
t.Errorf("Expected '%v', got '%v'.", expected, fqdn)
}
fqdn = podFQDN(
- &api.Pod{
- ObjectMeta: meta.ObjectMeta{
- Name: "pod1",
- Namespace: "testns",
- },
- Status: api.PodStatus{
- PodIP: "aaaa:bbbb:cccc::zzzz",
- },
+ &object.Pod{
+ Name: "pod1",
+ Namespace: "testns",
+ PodIP: "aaaa:bbbb:cccc::zzzz",
}, "cluster.local")
expected = "aaaa-bbbb-cccc--zzzz.testns.pod.cluster.local."
@@ -445,10 +344,10 @@ func TestPodFQDN(t *testing.T) {
func TestEndpointFQDN(t *testing.T) {
fqdns := endpointFQDN(
- &api.Endpoints{
- Subsets: []api.EndpointSubset{
+ &object.Endpoints{
+ Subsets: []object.EndpointSubset{
{
- Addresses: []api.EndpointAddress{
+ Addresses: []object.EndpointAddress{
{
IP: "172.0.0.1",
Hostname: "ep1a",
@@ -459,10 +358,8 @@ func TestEndpointFQDN(t *testing.T) {
},
},
},
- ObjectMeta: meta.ObjectMeta{
- Name: "svc1",
- Namespace: "testns",
- },
+ Name: "svc1",
+ Namespace: "testns",
}, "cluster.local", false)
expected := []string{