Browse Source

jpictl: introduce `jpictl dns write` command

it renders BIND config to describe the public view of the cluster

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/24/head
Alejandro Mery 1 year ago
parent
commit
eba0340e32
  1. 82
      cmd/jpictl/dns.go

82
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)
}
Loading…
Cancel
Save