aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris O'Haver <cohaver@infoblox.com> 2020-05-04 04:17:26 -0400
committerGravatar GitHub <noreply@github.com> 2020-05-04 10:17:26 +0200
commita5e286ac4e1ce203c0c67b77f4042b3cf8c46ba8 (patch)
tree378c8f6249b34eeb05c563422f8b6c4215a446a0
parent5347bc38e733a0ff084aafe7713d24903596f440 (diff)
downloadcoredns-a5e286ac4e1ce203c0c67b77f4042b3cf8c46ba8.tar.gz
coredns-a5e286ac4e1ce203c0c67b77f4042b3cf8c46ba8.tar.zst
coredns-a5e286ac4e1ce203c0c67b77f4042b3cf8c46ba8.zip
plugin/kubernetes: Don't panic with metadata enabled and pods mode not verified (#3869)
* prevent panic in podWithIP Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add unit test, correct existing unit test Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * unit tests make more sense this way Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
-rw-r--r--plugin/kubernetes/autopath.go3
-rw-r--r--plugin/kubernetes/metadata_test.go38
2 files changed, 37 insertions, 4 deletions
diff --git a/plugin/kubernetes/autopath.go b/plugin/kubernetes/autopath.go
index 33bf401f5..6bca35d39 100644
--- a/plugin/kubernetes/autopath.go
+++ b/plugin/kubernetes/autopath.go
@@ -51,6 +51,9 @@ func (k *Kubernetes) AutoPath(state request.Request) []string {
// podWithIP return the api.Pod for source IP. It returns nil if nothing can be found.
func (k *Kubernetes) podWithIP(ip string) *object.Pod {
+ if k.podMode != podModeVerified {
+ return nil
+ }
ps := k.APIConn.PodIndex(ip)
if len(ps) == 0 {
return nil
diff --git a/plugin/kubernetes/metadata_test.go b/plugin/kubernetes/metadata_test.go
index 1e3d823bc..c55258335 100644
--- a/plugin/kubernetes/metadata_test.go
+++ b/plugin/kubernetes/metadata_test.go
@@ -32,8 +32,6 @@ var metadataCases = []struct {
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "10-240-0-1",
- "kubernetes/client-namespace": "podns",
- "kubernetes/client-pod-name": "foo",
},
},
{
@@ -45,8 +43,6 @@ var metadataCases = []struct {
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "s",
- "kubernetes/client-namespace": "podns",
- "kubernetes/client-pod-name": "foo",
},
},
{
@@ -124,3 +120,37 @@ func TestMetadata(t *testing.T) {
}
}
}
+
+func TestMetadataPodsVerified(t *testing.T) {
+ k := New([]string{"cluster.local."})
+ k.podMode = podModeVerified
+ k.APIConn = &APIConnServeTest{}
+
+ ctx := metadata.ContextWithMetadata(context.Background())
+ state := request.Request{
+ Req: &dns.Msg{Question: []dns.Question{{Name: "s.ns.svc.cluster.local.", Qtype: dns.TypeA}}},
+ Zone: ".",
+ W: &test.ResponseWriter{},
+ }
+
+ k.Metadata(ctx, state)
+
+ expect := map[string]string{
+ "kubernetes/endpoint": "",
+ "kubernetes/kind": "svc",
+ "kubernetes/namespace": "ns",
+ "kubernetes/port-name": "*",
+ "kubernetes/protocol": "*",
+ "kubernetes/service": "s",
+ "kubernetes/client-namespace": "podns",
+ "kubernetes/client-pod-name": "foo",
+ }
+
+ md := make(map[string]string)
+ for _, l := range metadata.Labels(ctx) {
+ md[l] = metadata.ValueFunc(ctx, l)()
+ }
+ if mapsDiffer(expect, md) {
+ t.Errorf("Expected metadata %v and got %v", expect, md)
+ }
+}