plugin/federation: handle missing avail-zone/region labels better (#2092)
* handle missing avail-zone/region labels better * oops forgot a file
This commit is contained in:
parent
b42eae7a04
commit
0bf8b81cb7
3 changed files with 49 additions and 5 deletions
|
@ -54,7 +54,7 @@ func TestFederationKubernetes(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
k := kubernetes.New([]string{"cluster.local."})
|
k := kubernetes.New([]string{"cluster.local."})
|
||||||
k.APIConn = &APIConnFederationTest{}
|
k.APIConn = &APIConnFederationTest{zone: "fd-az", region: "fd-r"}
|
||||||
|
|
||||||
fed := New()
|
fed := New()
|
||||||
fed.zones = []string{"cluster.local."}
|
fed.zones = []string{"cluster.local."}
|
||||||
|
@ -79,3 +79,39 @@ func TestFederationKubernetes(t *testing.T) {
|
||||||
test.SortAndCheck(t, resp, tc)
|
test.SortAndCheck(t, resp, tc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFederationKubernetesMissingLabels(t *testing.T) {
|
||||||
|
tests := []test.Case{
|
||||||
|
{
|
||||||
|
// service does not exist, do the federation dance.
|
||||||
|
Qname: "svc0.testns.prod.svc.cluster.local.", Qtype: dns.TypeA,
|
||||||
|
Rcode: dns.RcodeSuccess,
|
||||||
|
Answer: []dns.RR{
|
||||||
|
test.CNAME("svc0.testns.prod.svc.cluster.local. 303 IN CNAME svc0.testns.prod.svc.fd-az.fd-r.federal.example."),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
k := kubernetes.New([]string{"cluster.local."})
|
||||||
|
k.APIConn = &APIConnFederationTest{zone: "", region: ""}
|
||||||
|
|
||||||
|
fed := New()
|
||||||
|
fed.zones = []string{"cluster.local."}
|
||||||
|
fed.Federations = k.Federations
|
||||||
|
fed.Next = k
|
||||||
|
fed.f = map[string]string{
|
||||||
|
"prod": "federal.example.",
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := context.TODO()
|
||||||
|
for _, tc := range tests {
|
||||||
|
m := tc.Msg()
|
||||||
|
|
||||||
|
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||||
|
_, err := fed.ServeDNS(ctx, rec, m)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected an error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,9 @@ import (
|
||||||
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type APIConnFederationTest struct{}
|
type APIConnFederationTest struct {
|
||||||
|
zone, region string
|
||||||
|
}
|
||||||
|
|
||||||
func (APIConnFederationTest) HasSynced() bool { return true }
|
func (APIConnFederationTest) HasSynced() bool { return true }
|
||||||
func (APIConnFederationTest) Run() { return }
|
func (APIConnFederationTest) Run() { return }
|
||||||
|
@ -176,13 +178,13 @@ func (APIConnFederationTest) EndpointsList() []*api.Endpoints {
|
||||||
return eps
|
return eps
|
||||||
}
|
}
|
||||||
|
|
||||||
func (APIConnFederationTest) GetNodeByName(name string) (*api.Node, error) {
|
func (a APIConnFederationTest) GetNodeByName(name string) (*api.Node, error) {
|
||||||
return &api.Node{
|
return &api.Node{
|
||||||
ObjectMeta: meta.ObjectMeta{
|
ObjectMeta: meta.ObjectMeta{
|
||||||
Name: "test.node.foo.bar",
|
Name: "test.node.foo.bar",
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
kubernetes.LabelRegion: "fd-r",
|
kubernetes.LabelRegion: a.region,
|
||||||
kubernetes.LabelZone: "fd-az",
|
kubernetes.LabelZone: a.zone,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package kubernetes
|
package kubernetes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||||
"github.com/coredns/coredns/request"
|
"github.com/coredns/coredns/request"
|
||||||
|
@ -37,6 +39,10 @@ func (k *Kubernetes) Federations(state request.Request, fname, fzone string) (ms
|
||||||
lz := node.Labels[LabelZone]
|
lz := node.Labels[LabelZone]
|
||||||
lr := node.Labels[LabelRegion]
|
lr := node.Labels[LabelRegion]
|
||||||
|
|
||||||
|
if lz == "" || lr == "" {
|
||||||
|
return msg.Service{}, errors.New("local node missing zone/region labels")
|
||||||
|
}
|
||||||
|
|
||||||
if r.endpoint == "" {
|
if r.endpoint == "" {
|
||||||
return msg.Service{Host: dnsutil.Join([]string{r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone})}, nil
|
return msg.Service{Host: dnsutil.Join([]string{r.service, r.namespace, fname, r.podOrSvc, lz, lr, fzone})}, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue