From c33d0dab16c4733cb20b9324ab89ce97333d4955 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 12 Sep 2023 16:19:57 +0000 Subject: [PATCH] jpictl: refactor dns.Manager factory to support Provider data Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index 6520bbf..9ce62ec 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -10,9 +10,7 @@ import ( "git.jpi.io/amery/jpictl/pkg/dns" ) -func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { - ctx := context.TODO() - +func newDNSManager(m *cluster.Cluster, provider dns.Provider) (*dns.Manager, error) { domain := m.Domain if m.Name != "" { domain = m.Name + "." + domain @@ -23,6 +21,26 @@ func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { return nil, err } + if provider != nil { + // set provider only if specified + err = dns.WithProvider(provider)(mgr) + if err != nil { + return nil, err + } + } + + if err := populateDNSManager(mgr, m); err != nil { + return nil, err + } + + return mgr, nil +} + +func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error { + var err error + + ctx := context.TODO() + 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...) @@ -32,7 +50,7 @@ func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { return err != nil }) if err != nil { - return nil, err + return err } m.ForEachRegion(func(r *cluster.Region) bool { @@ -43,11 +61,8 @@ func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { return err != nil }) - if err != nil { - return nil, err - } - return mgr, nil + return err } // Command @@ -65,7 +80,7 @@ var dnsWriteCmd = &cobra.Command{ return err } - mgr, err := newDNSManager(m) + mgr, err := newDNSManager(m, nil) if err != nil { return err }