mirror of
https://github.com/coredns/coredns.git
synced 2025-11-13 15:32:24 -05:00
plugin/metadata: add metadata plugin (#1894)
* plugin/metadata: add metadata plugin * plugin/metadata: Add MD struct, refactor code, fix doc * plugin/metadata: simplify metadata key * plugin/metadata: improve setup_test * Support of metadata by rewrite plugin. Move calculated variables to metadata. * Move variables from metadata to pkg, add UTs, READMEs change, metadata small fixes * Add client port validation to variables_test * plugin/metadata: improve README * plugin/metadata: rename methods * plugin/metadata: Update Metadataer interface, update doc, cosmetic code changes * plugin/metadata: move colllisions check to OnStartup(). Fix default variables metadataer. * plugin/metadata: Fix comment for method setValue * plugin/metadata: change variables order to fix linter warning * plugin/metadata: rename Metadataer to Provider
This commit is contained in:
committed by
Miek Gieben
parent
dae506b563
commit
17d807f05f
55
plugin/metadata/metadata.go
Normal file
55
plugin/metadata/metadata.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/pkg/variables"
|
||||
"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" }
|
||||
|
||||
// ServeDNS implements the plugin.Handler interface.
|
||||
func (m *Metadata) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
|
||||
md, ctx := newMD(ctx)
|
||||
|
||||
state := request.Request{W: w, Req: r}
|
||||
if plugin.Zones(m.Zones).Matches(state.Name()) != "" {
|
||||
// Go through all Providers and collect metadata
|
||||
for _, provider := range m.Providers {
|
||||
for _, varName := range provider.MetadataVarNames() {
|
||||
if val, ok := provider.Metadata(ctx, w, r, varName); ok {
|
||||
md.setValue(varName, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rcode, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, w, r)
|
||||
|
||||
return rcode, err
|
||||
}
|
||||
|
||||
// MetadataVarNames implements the plugin.Provider interface.
|
||||
func (m *Metadata) MetadataVarNames() []string { return variables.All }
|
||||
|
||||
// Metadata implements the plugin.Provider interface.
|
||||
func (m *Metadata) Metadata(ctx context.Context, w dns.ResponseWriter, r *dns.Msg, varName string) (interface{}, bool) {
|
||||
if val, err := variables.GetValue(varName, w, r); err == nil {
|
||||
return val, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user