From 3ada89c4f81ecf0a1c35d3ed452dff1636a2bce3 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Wed, 2 Apr 2025 19:45:15 +1000 Subject: [PATCH] fix: adding ucind cluster with tcp connections to config --- cmd/uncloud/main.go | 2 +- go.mod | 3 ++- go.sum | 2 ++ internal/cli/cli.go | 4 ++-- internal/cli/config/config.go | 7 +++---- internal/cli/config/connection.go | 8 +++++--- internal/ucind/cluster.go | 9 +++++---- internal/ucind/config.go | 2 +- 8 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cmd/uncloud/main.go b/cmd/uncloud/main.go index c2f3c21b..4804791e 100644 --- a/cmd/uncloud/main.go +++ b/cmd/uncloud/main.go @@ -38,7 +38,7 @@ func main() { return fmt.Errorf("parse TCP address: %w", err) } conn = &config.MachineConnection{ - TCP: addrPort, + TCP: &addrPort, } } else { dest := opts.connect diff --git a/go.mod b/go.mod index 32856a52..8e05a7fc 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/docker/docker v27.4.0-rc.2+incompatible github.com/docker/go-connections 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/hashicorp/memberlist v0.5.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/grpc v1.68.1 google.golang.org/protobuf v1.36.3 - gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.36.3 ) @@ -311,6 +311,7 @@ require ( golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // 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 howett.net/plist v1.0.0 // indirect lukechampine.com/blake3 v1.3.0 // indirect diff --git a/go.sum b/go.sum index ac62e5ce..b608695c 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= 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/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E= diff --git a/internal/cli/cli.go b/internal/cli/cli.go index c91dafc6..30daa98e 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -137,8 +137,8 @@ func connectCluster(ctx context.Context, conn config.MachineConnection) (*client KeyPath: keyPath, } return client.New(ctx, connector.NewSSHConnector(sshConfig)) - } else if conn.TCP.IsValid() { - return client.New(ctx, connector.NewTCPConnector(conn.TCP)) + } else if conn.TCP != nil && conn.TCP.IsValid() { + return client.New(ctx, connector.NewTCPConnector(*conn.TCP)) } return nil, errors.New("connection configuration is invalid") diff --git a/internal/cli/config/config.go b/internal/cli/config/config.go index 98dd70eb..b551e9cc 100644 --- a/internal/cli/config/config.go +++ b/internal/cli/config/config.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "gopkg.in/yaml.v3" + "github.com/goccy/go-yaml" ) type Config struct { @@ -45,7 +45,7 @@ func (c *Config) Read() error { return fmt.Errorf("read config file '%s': %w", c.path, err) } 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 @@ -62,8 +62,7 @@ func (c *Config) Save() error { return fmt.Errorf("write config file '%s': %w", c.path, err) } - encoder := yaml.NewEncoder(f) - encoder.SetIndent(2) + encoder := yaml.NewEncoder(f, yaml.Indent(2), yaml.IndentSequence(true)) if err = encoder.Encode(c); err != nil { _ = f.Close() return fmt.Errorf("encode config file '%s': %w", c.path, err) diff --git a/internal/cli/config/connection.go b/internal/cli/config/connection.go index 2fb46c7f..6d798315 100644 --- a/internal/cli/config/connection.go +++ b/internal/cli/config/connection.go @@ -17,9 +17,11 @@ const ( type MachineConnection struct { SSH SSHDestination `yaml:"ssh,omitempty"` SSHKeyFile string `yaml:"ssh_key_file,omitempty"` - TCP netip.AddrPort `yaml:"tcp,omitempty"` - Host string `yaml:"host,omitempty"` - PublicKey secret.Secret `yaml:"public_key,omitempty"` + // TCP is the address and port of the machine's API server. + // The pointer is used to omit the field when not set. Otherwise, yaml marshalling includes an empty object. + 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". diff --git a/internal/ucind/cluster.go b/internal/ucind/cluster.go index 2c292d54..2c92682d 100644 --- a/internal/ucind/cluster.go +++ b/internal/ucind/cluster.go @@ -4,17 +4,18 @@ import ( "context" "errors" "fmt" + "net/netip" + "time" + "github.com/cenkalti/backoff/v4" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/network" 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/api/pb" "github.com/psviderski/uncloud/internal/machine/cluster" + "google.golang.org/protobuf/types/known/emptypb" ) const ( @@ -79,7 +80,7 @@ func (p *Provisioner) CreateCluster(ctx context.Context, name string, opts Creat if err = p.configUpdater.AddCluster(c); err != nil { 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 diff --git a/internal/ucind/config.go b/internal/ucind/config.go index 5136b205..305be681 100644 --- a/internal/ucind/config.go +++ b/internal/ucind/config.go @@ -30,7 +30,7 @@ func (u *ConfigUpdater) AddCluster(c Cluster) error { } for i, m := range c.Machines { clusterCfg.Connections[i] = config.MachineConnection{ - TCP: m.APIAddress, + TCP: &m.APIAddress, } }