Compare commits

..

2 Commits

Author SHA1 Message Date
amery 7e6c53c5f5 zones: introduce Machine.PublicAddresses()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-21 22:20:30 +00:00
amery 5f924dcb00 zones: introduce Machine.FullName()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-21 22:20:28 +00:00
5 changed files with 48 additions and 36 deletions
-27
View File
@@ -1,27 +0,0 @@
package main
import (
"os"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := zones.New(cfg.Base, cfg.Domain)
if err != nil {
return err
}
return m.WriteEnv(os.Stdout)
},
}
func init() {
rootCmd.AddCommand(envCmd)
}
-9
View File
@@ -1,9 +0,0 @@
// Package zones abstracts the cluster zones
package zones
import "io"
// WriteEnv generates environment variables for shell scripts
func (*Zones) WriteEnv(io.Writer) error {
return nil
}
+18
View File
@@ -1,7 +1,9 @@
package zones
import (
"net/netip"
"strconv"
"strings"
"sync"
)
@@ -12,6 +14,8 @@ type Machine struct {
zone *Zone
id int
Name string
PublicAddresses []netip.Addr
}
func (m *Machine) String() string {
@@ -38,3 +42,17 @@ func (m *Machine) ID() int {
return m.id
}
// FullName returns the Name of the machine including domain name
func (m *Machine) FullName() string {
if domain := m.zone.zones.domain; domain != "" {
var s = []string{
m.Name,
domain,
}
return strings.Join(s, ".")
}
return m.Name
}
+26
View File
@@ -0,0 +1,26 @@
package zones
import (
"context"
"net/netip"
"time"
)
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
timeout := 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
}
func (m *Machine) updatePublicAddresses() error {
addrs, err := m.lookupNetIP()
if err != nil {
return err
}
m.PublicAddresses = addrs
return nil
}
+4
View File
@@ -43,6 +43,10 @@ func (z *Zone) scan() error {
Name: e.Name(),
}
if err := m.updatePublicAddresses(); err != nil {
return err
}
z.Machines = append(z.Machines, m)
}
}