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