From eba0340e32f84db04f7db61788af805a4c8c5c35 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 12 Sep 2023 15:54:28 +0000 Subject: [PATCH] jpictl: introduce `jpictl dns write` command it renders BIND config to describe the public view of the cluster Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 cmd/jpictl/dns.go diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go new file mode 100644 index 0000000..6520bbf --- /dev/null +++ b/cmd/jpictl/dns.go @@ -0,0 +1,82 @@ +package main + +import ( + "context" + "os" + + "github.com/spf13/cobra" + + "git.jpi.io/amery/jpictl/pkg/cluster" + "git.jpi.io/amery/jpictl/pkg/dns" +) + +func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { + ctx := context.TODO() + + domain := m.Domain + if m.Name != "" { + domain = m.Name + "." + domain + } + + mgr, err := dns.NewManager(dns.WithDomain(domain), dns.WithLogger(log)) + if err != nil { + return nil, err + } + + m.ForEachZone(func(z *cluster.Zone) bool { + z.ForEachMachine(func(p *cluster.Machine) bool { + err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...) + return err != nil + }) + + return err != nil + }) + if err != nil { + return nil, err + } + + m.ForEachRegion(func(r *cluster.Region) bool { + r.ForEachZone(func(z *cluster.Zone) bool { + err = mgr.AddRegion(ctx, r.Name, z.Name) + return err != nil + }) + + return err != nil + }) + if err != nil { + return nil, err + } + + return mgr, nil +} + +// Command +var dnsCmd = &cobra.Command{ + Use: "dns", +} + +var dnsWriteCmd = &cobra.Command{ + Use: "write", + Short: "dns write generates public DNS records", + PreRun: setVerbosity, + RunE: func(_ *cobra.Command, _ []string) error { + m, err := cfg.LoadZones(true) + if err != nil { + return err + } + + mgr, err := newDNSManager(m) + if err != nil { + return err + } + + _, err = mgr.WriteTo(os.Stdout) + return err + }, +} + +func init() { + rootCmd.AddCommand(dnsCmd) + + dnsCmd.AddCommand(dnsWriteCmd) +}