coredns/plugin/metadata/metadata.go
John Belamaric ffcd2f61cf Publish metadata from kubernetes plugin (#2829)
* Publish metadata from kubernetes plugin

* stickler fix

* Add a couple tests

* Add metadata section to README

* Update plugin/kubernetes/README.md

Co-Authored-By: Chris O'Haver <cohaver@infoblox.com>

* Address nit
2019-06-09 08:10:57 +01:00

44 lines
1.1 KiB
Go

package metadata
import (
"context"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
// Metadata implements collecting metadata information from all plugins that
// implement the Provider interface.
type Metadata struct {
Zones []string
Providers []Provider
Next plugin.Handler
}
// Name implements the Handler interface.
func (m *Metadata) Name() string { return "metadata" }
// ContextWithMetadata is exported for use by provider tests
func ContextWithMetadata(ctx context.Context) context.Context {
return context.WithValue(ctx, key{}, md{})
}
// ServeDNS implements the plugin.Handler interface.
func (m *Metadata) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
ctx = ContextWithMetadata(ctx)
state := request.Request{W: w, Req: r}
if plugin.Zones(m.Zones).Matches(state.Name()) != "" {
// Go through all Providers and collect metadata.
for _, p := range m.Providers {
ctx = p.Metadata(ctx, state)
}
}
rcode, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, w, r)
return rcode, err
}