mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
feat: add CLI connector for unix domain socket (#186)
* feat: Add cli connector for unix domain socket * Update cmd/uncloud/main.go --------- Co-authored-by: Pasha Sviderski <me@psviderski.name>
This commit is contained in:
co-authored by
Pasha Sviderski
parent
e87207eae3
commit
91a09d5d37
@@ -22,10 +22,12 @@ type MachineConnection struct {
|
||||
SSHKeyFile string `yaml:"ssh_key_file,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"`
|
||||
MachineID string `yaml:"machine_id,omitempty"`
|
||||
TCP *netip.AddrPort `yaml:"tcp,omitempty"`
|
||||
// Unix is the path to the machine's API unix socket.
|
||||
Unix string `yaml:"unix,omitempty"`
|
||||
Host string `yaml:"host,omitempty"`
|
||||
PublicKey secret.Secret `yaml:"public_key,omitempty"`
|
||||
MachineID string `yaml:"machine_id,omitempty"`
|
||||
}
|
||||
|
||||
func (c MachineConnection) String() string {
|
||||
@@ -35,6 +37,8 @@ func (c MachineConnection) String() string {
|
||||
return "ssh+cli://" + string(c.SSHCLI)
|
||||
} else if c.TCP != nil && c.TCP.IsValid() {
|
||||
return fmt.Sprintf("tcp://%s", c.TCP)
|
||||
} else if c.Unix != "" {
|
||||
return fmt.Sprintf("unix://%s", c.Unix)
|
||||
}
|
||||
return "unknown connection"
|
||||
}
|
||||
@@ -50,12 +54,15 @@ func (c *MachineConnection) Validate() error {
|
||||
if c.TCP != nil && c.TCP.IsValid() {
|
||||
setCount++
|
||||
}
|
||||
if c.Unix != "" {
|
||||
setCount++
|
||||
}
|
||||
|
||||
if setCount == 0 {
|
||||
return errors.New("no connection method specified (ssh, ssh_cli, or tcp required)")
|
||||
return errors.New("no connection method specified (ssh, ssh_cli, tcp, or unix required)")
|
||||
}
|
||||
if setCount > 1 {
|
||||
return errors.New("only one connection method allowed per connection (ssh, ssh_cli, or tcp)")
|
||||
return errors.New("only one connection method allowed per connection (ssh, ssh_cli, tcp, or unix)")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -53,6 +53,13 @@ func TestMachineConnection_String(t *testing.T) {
|
||||
},
|
||||
want: "tcp://10.0.0.1:8080",
|
||||
},
|
||||
{
|
||||
name: "unix connection",
|
||||
conn: MachineConnection{
|
||||
Unix: "/run/uncloud/uncloud.sock",
|
||||
},
|
||||
want: "unix:///run/uncloud/uncloud.sock",
|
||||
},
|
||||
{
|
||||
name: "no connection",
|
||||
conn: MachineConnection{},
|
||||
@@ -112,6 +119,13 @@ func TestMachineConnection_Validate(t *testing.T) {
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "unix only - valid",
|
||||
conn: MachineConnection{
|
||||
Unix: "/path/to/socket",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "no connection method - error",
|
||||
conn: MachineConnection{},
|
||||
@@ -127,6 +141,15 @@ func TestMachineConnection_Validate(t *testing.T) {
|
||||
wantErr: true,
|
||||
errMsg: "only one connection method allowed",
|
||||
},
|
||||
{
|
||||
name: "ssh and unix - error",
|
||||
conn: MachineConnection{
|
||||
SSH: "user@host",
|
||||
Unix: "/path/to/socket",
|
||||
},
|
||||
wantErr: true,
|
||||
errMsg: "only one connection method allowed",
|
||||
},
|
||||
{
|
||||
name: "ssh and tcp - error",
|
||||
conn: MachineConnection{
|
||||
|
||||
@@ -73,6 +73,8 @@ func connectCluster(ctx context.Context, conn config.MachineConnection) (*client
|
||||
useSSHCLI = true
|
||||
} else if conn.TCP != nil && conn.TCP.IsValid() {
|
||||
return client.New(ctx, connector.NewTCPConnector(*conn.TCP))
|
||||
} else if conn.Unix != "" {
|
||||
return client.New(ctx, connector.NewUnixConnector(conn.Unix))
|
||||
} else {
|
||||
return nil, errors.New("connection configuration is invalid")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user