Split the console from the resolver: fancy-dns-console writes the cluster
fancy-dns-console is a second binary holding the web UI, authentication and every edit. It writes ClientGroup, BlockList and DNSZone objects through the new kubeedit package and forwards all other /api routes to a running resolver with its own key, dropping the browser's credentials, so it can run in the cluster or on a workstation with a kubeconfig and nothing about the resolver's placement matters. The resolver never imports kubeedit and the console is not on the DNS path. kubeedit validates every spec with the runtime's own converters (kubesource.CheckGroup/CheckList/CheckZone) before writing, so the console cannot store an object the resolver would reject. Zone edits keep the file-based writer's behaviour: duplicate and below-a-delegation records are refused, a delegation in a new zone also gives the zone its servers, and a zone with no apex NS is never stored. Each object records the editing user in an annotation; updates retry on conflict. The resolver reports groups from ClientGroup objects as source "kubernetes" and counter-only rows (the "(default)" bucket, or a group removed since) as "history", and lists DNSZone zones as the editable ones. The console uses that to leave the YAML's groups alone and to drop history rows, which the UI would otherwise send back on save. Its GET /api/clients overlays the cluster's current objects on the resolver's list, because the resolver only sees an edit after its watch fires and a save made just after another would delete the first. An end-to-end test runs both halves against one fake apiserver: a group saved through the console refuses queries, a zone built record by record answers authoritatively, and a request with the resolver's own key is rejected by the console. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4d38945d94
commit
1a536e7d0e
25 files changed
+1950
-8
No files matched your search
@@ -0,0 +1,138 @@
|
||||
// Command fancy-dns-console is the editing half of fancy-dns: the web UI and
|
||||
// the API behind it, writing ClientGroup, BlockList and DNSZone objects to
|
||||
// Kubernetes and forwarding everything else to a running resolver.
|
||||
//
|
||||
// It can run in the cluster or on a workstation (with a kubeconfig); the
|
||||
// resolver it manages need not be in the same place.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/app"
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/config"
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/console"
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/kubeedit"
|
||||
"git.k8s-git-k6n.net/mats/fancy-dns/internal/kubesource"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
configPath = flag.String("config", envOr("FANCYDNS_CONSOLE_CONFIG", "/etc/fancy-dns/console.yaml"), "console configuration (api: listen, auth, ui)")
|
||||
runtimeURL = flag.String("runtime-url", os.Getenv("FANCYDNS_RUNTIME_URL"), "admin API of the resolver, e.g. http://fancy-dns.dns:9053")
|
||||
kubeconfig = flag.String("kubeconfig", os.Getenv("FANCYDNS_KUBECONFIG"), "kubeconfig (default: in-cluster service account, then $KUBECONFIG, then ~/.kube/config)")
|
||||
kubeContext = flag.String("kube-context", os.Getenv("FANCYDNS_KUBE_CONTEXT"), "kubeconfig context")
|
||||
kubeNS = flag.String("kube-namespace", os.Getenv("FANCYDNS_KUBE_NAMESPACE"), "namespace the objects live in (default: the pod's own)")
|
||||
showVersion = flag.Bool("version", false, "print the version and exit")
|
||||
checkOnly = flag.Bool("check", false, "validate the configuration and exit")
|
||||
)
|
||||
flag.Parse()
|
||||
if *showVersion {
|
||||
fmt.Println("fancy-dns-console", app.Version)
|
||||
return
|
||||
}
|
||||
if err := run(*configPath, *runtimeURL, *kubeconfig, *kubeContext, *kubeNS, *checkOnly); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "fancy-dns-console:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(configPath, runtimeURL, kubeconfig, kubeContext, ns string, checkOnly bool) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "blocklist is enabled") {
|
||||
err = fmt.Errorf("%w (the console has no blocklists of its own: put \"blocklist: {enabled: false}\" in its config)", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if runtimeURL == "" {
|
||||
return errors.New("-runtime-url (or FANCYDNS_RUNTIME_URL) is required")
|
||||
}
|
||||
rt, err := console.NewRuntime(runtimeURL, os.Getenv("FANCYDNS_RUNTIME_KEY"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ns == "" {
|
||||
ns = kubesource.Namespace()
|
||||
}
|
||||
if ns == "" {
|
||||
return errors.New("-kube-namespace is required when not running in a pod")
|
||||
}
|
||||
client, from, err := kubesource.NewClient(kubesource.ClientOptions{Kubeconfig: kubeconfig, Context: kubeContext})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
level := slog.LevelInfo
|
||||
if cfg.Log.Level == "debug" {
|
||||
level = slog.LevelDebug
|
||||
}
|
||||
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level})).With("service", "fancy-dns-console", "version", app.Version)
|
||||
|
||||
c, err := console.New(console.Options{
|
||||
Config: config.NewStore(cfg, configPath),
|
||||
Runtime: rt,
|
||||
Editor: kubeedit.New(client, ns),
|
||||
Logger: log,
|
||||
Version: app.Version,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
handler, err := c.Handler()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if checkOnly {
|
||||
fmt.Println("configuration is valid:", configPath)
|
||||
return nil
|
||||
}
|
||||
if len(cfg.API.TLSListen) > 0 {
|
||||
log.Warn("api.tls_listen is ignored by the console; terminate TLS at an ingress or proxy")
|
||||
}
|
||||
if len(cfg.API.Listen) == 0 {
|
||||
return errors.New("api.listen is empty: nowhere to serve the console")
|
||||
}
|
||||
|
||||
srv := &http.Server{Handler: handler, ReadHeaderTimeout: 15 * time.Second, IdleTimeout: 120 * time.Second}
|
||||
errc := make(chan error, len(cfg.API.Listen))
|
||||
for _, addr := range cfg.API.Listen {
|
||||
ln, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bind %s: %w", addr, err)
|
||||
}
|
||||
log.Info("console listening", "addr", ln.Addr().String(), "namespace", ns, "kubernetes", from, "resolver", runtimeURL)
|
||||
go func() { errc <- srv.Serve(ln) }()
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case err := <-errc:
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
sctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
return srv.Shutdown(sctx)
|
||||
}
|
||||
|
||||
func envOr(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
Reference in new issue
Block a user