| Jan Schär | a48bd3c | 2024-07-29 17:22:18 +0200 | [diff] [blame^] | 1 | // Package kubernetes provides the kubernetes backend. |
| 2 | package kubernetes |
| 3 | |
| 4 | // Taken and modified from the Kubernetes plugin of CoreDNS, under Apache 2.0. |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "net/netip" |
| 9 | |
| 10 | "github.com/miekg/dns" |
| 11 | "k8s.io/client-go/kubernetes" |
| 12 | |
| 13 | "source.monogon.dev/osbase/supervisor" |
| 14 | ) |
| 15 | |
| 16 | // Kubernetes is a DNS handler that implements the Kubernetes |
| 17 | // DNS-Based Service Discovery specification. |
| 18 | // https://github.com/kubernetes/dns/blob/master/docs/specification.md |
| 19 | type Kubernetes struct { |
| 20 | clusterDomain string |
| 21 | nsDomain string |
| 22 | ipRanges []netip.Prefix |
| 23 | // A Kubernetes ClientSet with read access to endpoints and services |
| 24 | ClientSet kubernetes.Interface |
| 25 | apiConn dnsController |
| 26 | } |
| 27 | |
| 28 | // New returns an initialized Kubernetes. Kubernetes DNS records will be served |
| 29 | // under the clusterDomain. Additionally, reverse queries for services and pods |
| 30 | // are served under the given ipRanges. |
| 31 | func New(clusterDomain string, ipRanges []netip.Prefix) *Kubernetes { |
| 32 | k := new(Kubernetes) |
| 33 | k.clusterDomain = dns.CanonicalName(clusterDomain) |
| 34 | k.nsDomain = "ns.dns." + k.clusterDomain |
| 35 | k.ipRanges = ipRanges |
| 36 | return k |
| 37 | } |
| 38 | |
| 39 | // Run maintains the in-memory cache of Kubernetes services and endpoints. |
| 40 | func (k *Kubernetes) Run(ctx context.Context) error { |
| 41 | k.apiConn = newdnsController(ctx, k.ClientSet) |
| 42 | k.apiConn.Start(ctx.Done()) |
| 43 | |
| 44 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 45 | <-ctx.Done() |
| 46 | return ctx.Err() |
| 47 | } |