Files
coredns/plugin/kubernetes/metadata.go
Arthur Outhenin-Chalandre 5c71bd0b87 kubernetes: add multicluster support (#7266)
* kubernetes: add multicluster support

Add multicluster support via Multi-Cluster Services API (MCS-API) via a
new option `multiclusterZones` in the kubernetes plugin.

When some multicluster zones are passed to the kubernetes plugin, it
will start watching the ServiceImport objects and its associated
EndpointSlices.

Signed-off-by: Arthur Outhenin-Chalandre <arthur@cri.epita.fr>

* kubernetes: implement xfr support for multicluster zones

Signed-off-by: Arthur Outhenin-Chalandre <arthur@cri.epita.fr>

---------

Signed-off-by: Arthur Outhenin-Chalandre <arthur@cri.epita.fr>
2025-05-18 22:58:16 -07:00

80 lines
1.8 KiB
Go

package kubernetes
import (
"context"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/request"
)
// Metadata implements the metadata.Provider interface.
func (k *Kubernetes) Metadata(ctx context.Context, state request.Request) context.Context {
pod := k.podWithIP(state.IP())
if pod != nil {
metadata.SetValueFunc(ctx, "kubernetes/client-namespace", func() string {
return pod.Namespace
})
metadata.SetValueFunc(ctx, "kubernetes/client-pod-name", func() string {
return pod.Name
})
for k, v := range pod.Labels {
v := v
metadata.SetValueFunc(ctx, "kubernetes/client-label/"+k, func() string {
return v
})
}
}
zone := plugin.Zones(k.Zones).Matches(state.Name())
if zone == "" {
return ctx
}
multicluster := false
if z := plugin.Zones(k.opts.multiclusterZones).Matches(state.Zone); z != "" {
multicluster = true
}
// possible optimization: cache r so it doesn't need to be calculated again in ServeDNS
r, err := parseRequest(state.Name(), zone, multicluster)
if err != nil {
metadata.SetValueFunc(ctx, "kubernetes/parse-error", func() string {
return err.Error()
})
return ctx
}
metadata.SetValueFunc(ctx, "kubernetes/port-name", func() string {
return r.port
})
metadata.SetValueFunc(ctx, "kubernetes/protocol", func() string {
return r.protocol
})
metadata.SetValueFunc(ctx, "kubernetes/endpoint", func() string {
return r.endpoint
})
if multicluster {
metadata.SetValueFunc(ctx, "kubernetes/cluster", func() string {
return r.cluster
})
}
metadata.SetValueFunc(ctx, "kubernetes/service", func() string {
return r.service
})
metadata.SetValueFunc(ctx, "kubernetes/namespace", func() string {
return r.namespace
})
metadata.SetValueFunc(ctx, "kubernetes/kind", func() string {
return r.podOrSvc
})
return ctx
}