fix: adding ucind cluster with tcp connections to config

This commit is contained in:
Pavel Sviderski
2025-04-02 19:45:15 +10:00
parent 20b2ca2ea5
commit 3ada89c4f8
8 changed files with 21 additions and 16 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ func main() {
return fmt.Errorf("parse TCP address: %w", err) return fmt.Errorf("parse TCP address: %w", err)
} }
conn = &config.MachineConnection{ conn = &config.MachineConnection{
TCP: addrPort, TCP: &addrPort,
} }
} else { } else {
dest := opts.connect dest := opts.connect
+2 -1
View File
@@ -21,6 +21,7 @@ require (
github.com/docker/docker v27.4.0-rc.2+incompatible github.com/docker/docker v27.4.0-rc.2+incompatible
github.com/docker/go-connections v0.5.0 github.com/docker/go-connections v0.5.0
github.com/docker/go-units v0.5.0 github.com/docker/go-units v0.5.0
github.com/goccy/go-yaml v1.17.1
github.com/google/go-containerregistry v0.20.2 github.com/google/go-containerregistry v0.20.2
github.com/hashicorp/memberlist v0.5.1 github.com/hashicorp/memberlist v0.5.1
github.com/hashicorp/serf v0.10.1 github.com/hashicorp/serf v0.10.1
@@ -52,7 +53,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576
google.golang.org/grpc v1.68.1 google.golang.org/grpc v1.68.1
google.golang.org/protobuf v1.36.3 google.golang.org/protobuf v1.36.3
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.36.3 modernc.org/sqlite v1.36.3
) )
@@ -311,6 +311,7 @@ require (
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20241209162323-e6fa225c2576 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gvisor.dev/gvisor v0.0.0-20230927004350-cbd86285d259 // indirect gvisor.dev/gvisor v0.0.0-20230927004350-cbd86285d259 // indirect
howett.net/plist v1.0.0 // indirect howett.net/plist v1.0.0 // indirect
lukechampine.com/blake3 v1.3.0 // indirect lukechampine.com/blake3 v1.3.0 // indirect
+2
View File
@@ -343,6 +343,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZ
github.com/go-viper/mapstructure/v2 v2.0.0 h1:dhn8MZ1gZ0mzeodTG3jt5Vj/o87xZKuNAprG2mQfMfc= github.com/go-viper/mapstructure/v2 v2.0.0 h1:dhn8MZ1gZ0mzeodTG3jt5Vj/o87xZKuNAprG2mQfMfc=
github.com/go-viper/mapstructure/v2 v2.0.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/go-viper/mapstructure/v2 v2.0.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E= github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E=
+2 -2
View File
@@ -137,8 +137,8 @@ func connectCluster(ctx context.Context, conn config.MachineConnection) (*client
KeyPath: keyPath, KeyPath: keyPath,
} }
return client.New(ctx, connector.NewSSHConnector(sshConfig)) return client.New(ctx, connector.NewSSHConnector(sshConfig))
} else if conn.TCP.IsValid() { } else if conn.TCP != nil && conn.TCP.IsValid() {
return client.New(ctx, connector.NewTCPConnector(conn.TCP)) return client.New(ctx, connector.NewTCPConnector(*conn.TCP))
} }
return nil, errors.New("connection configuration is invalid") return nil, errors.New("connection configuration is invalid")
+3 -4
View File
@@ -5,7 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"gopkg.in/yaml.v3" "github.com/goccy/go-yaml"
) )
type Config struct { type Config struct {
@@ -45,7 +45,7 @@ func (c *Config) Read() error {
return fmt.Errorf("read config file '%s': %w", c.path, err) return fmt.Errorf("read config file '%s': %w", c.path, err)
} }
if err = yaml.Unmarshal(data, c); err != nil { if err = yaml.Unmarshal(data, c); err != nil {
return fmt.Errorf("parse config file '%s': %w", c.path, err) return fmt.Errorf("parse config file '%s': %s", c.path, yaml.FormatError(err, true, true))
} }
return nil return nil
@@ -62,8 +62,7 @@ func (c *Config) Save() error {
return fmt.Errorf("write config file '%s': %w", c.path, err) return fmt.Errorf("write config file '%s': %w", c.path, err)
} }
encoder := yaml.NewEncoder(f) encoder := yaml.NewEncoder(f, yaml.Indent(2), yaml.IndentSequence(true))
encoder.SetIndent(2)
if err = encoder.Encode(c); err != nil { if err = encoder.Encode(c); err != nil {
_ = f.Close() _ = f.Close()
return fmt.Errorf("encode config file '%s': %w", c.path, err) return fmt.Errorf("encode config file '%s': %w", c.path, err)
+5 -3
View File
@@ -17,9 +17,11 @@ const (
type MachineConnection struct { type MachineConnection struct {
SSH SSHDestination `yaml:"ssh,omitempty"` SSH SSHDestination `yaml:"ssh,omitempty"`
SSHKeyFile string `yaml:"ssh_key_file,omitempty"` SSHKeyFile string `yaml:"ssh_key_file,omitempty"`
TCP netip.AddrPort `yaml:"tcp,omitempty"` // TCP is the address and port of the machine's API server.
Host string `yaml:"host,omitempty"` // The pointer is used to omit the field when not set. Otherwise, yaml marshalling includes an empty object.
PublicKey secret.Secret `yaml:"public_key,omitempty"` TCP *netip.AddrPort `yaml:"tcp,omitempty"`
Host string `yaml:"host,omitempty"`
PublicKey secret.Secret `yaml:"public_key,omitempty"`
} }
// SSHDestination represents an SSH destination string in the canonical form of "user@host:port". // SSHDestination represents an SSH destination string in the canonical form of "user@host:port".
+5 -4
View File
@@ -4,17 +4,18 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net/netip"
"time"
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
"google.golang.org/protobuf/types/known/emptypb"
"net/netip"
"time"
"github.com/psviderski/uncloud/internal/machine" "github.com/psviderski/uncloud/internal/machine"
"github.com/psviderski/uncloud/internal/machine/api/pb" "github.com/psviderski/uncloud/internal/machine/api/pb"
"github.com/psviderski/uncloud/internal/machine/cluster" "github.com/psviderski/uncloud/internal/machine/cluster"
"google.golang.org/protobuf/types/known/emptypb"
) )
const ( const (
@@ -79,7 +80,7 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat
if err = p.configUpdater.AddCluster(c); err != nil { if err = p.configUpdater.AddCluster(c); err != nil {
return c, fmt.Errorf("add cluster to Uncloud config: %w", err) return c, fmt.Errorf("add cluster to Uncloud config: %w", err)
} }
fmt.Printf("Cluster '%s' added to Uncloud config.\n", c.Name) fmt.Printf("Cluster '%s' added to Uncloud config as the current context.\n", name)
} }
return c, nil return c, nil
+1 -1
View File
@@ -30,7 +30,7 @@ func (u *ConfigUpdater) AddCluster(c Cluster) error {
} }
for i, m := range c.Machines { for i, m := range c.Machines {
clusterCfg.Connections[i] = config.MachineConnection{ clusterCfg.Connections[i] = config.MachineConnection{
TCP: m.APIAddress, TCP: &m.APIAddress,
} }
} }