Skip to content

Commit d824898

Browse files
committed
shared/cliconfig: Add keepalive proxy support
Signed-off-by: Stéphane Graber <[email protected]>
1 parent 822ce1c commit d824898

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

shared/cliconfig/keepalive.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build !windows
2+
3+
package cliconfig
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"time"
11+
12+
"github.com/lxc/incus/client"
13+
"github.com/lxc/incus/shared/subprocess"
14+
"github.com/lxc/incus/shared/util"
15+
)
16+
17+
func (c *Config) handleKeepAlive(remote Remote, name string, args *incus.ConnectionArgs) (incus.InstanceServer, error) {
18+
// Create the socker directory if missing.
19+
socketDir := filepath.Join(c.ConfigDir, "keepalive")
20+
err := os.Mkdir(socketDir, 0700)
21+
if err != nil && !os.IsExist(err) {
22+
return nil, err
23+
}
24+
25+
// Attempt to use the existing socket.
26+
socketPath := filepath.Join(socketDir, fmt.Sprintf("%s.socket", name))
27+
d, err := incus.ConnectIncusUnix(socketPath, args)
28+
if err != nil {
29+
// Delete any existing sockets.
30+
_ = os.Remove(socketPath)
31+
32+
// Spawn the proxy.
33+
proc, err := subprocess.NewProcess("incus", []string{"remote", "proxy", name, socketPath, fmt.Sprintf("--timeout=%d", remote.KeepAlive)}, "", "")
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
err = proc.Start(context.Background())
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
// Try up to 10 times over 5s.
44+
for i := 0; i < 10; i++ {
45+
if util.PathExists(socketPath) {
46+
break
47+
}
48+
49+
time.Sleep(500 * time.Millisecond)
50+
}
51+
52+
// Connect to the proxy.
53+
d, err = incus.ConnectIncusUnix(socketPath, args)
54+
if err != nil {
55+
return nil, err
56+
}
57+
}
58+
59+
return d, nil
60+
}

shared/cliconfig/keepalive_windows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build windows
2+
3+
package cliconfig
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/lxc/incus/client"
9+
)
10+
11+
func (c *Config) handleKeepAlive(remote Remote, name string, args *incus.ConnectionArgs) (incus.InstanceServer, error) {
12+
return nil, fmt.Errorf("Keepalive isn't supported on Windows")
13+
}

shared/cliconfig/remote.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,21 @@ func (c *Config) GetInstanceServer(name string) (incus.InstanceServer, error) {
103103
return nil, fmt.Errorf("Missing TLS client certificate and key")
104104
}
105105

106-
d, err := incus.ConnectIncus(remote.Addr, args)
107-
if err != nil {
108-
return nil, err
106+
var d incus.InstanceServer
107+
if remote.KeepAlive > 0 {
108+
d, err = c.handleKeepAlive(remote, name, args)
109+
if err != nil {
110+
// On proxy failure, just fallback to regular client.
111+
d, err = incus.ConnectIncus(remote.Addr, args)
112+
if err != nil {
113+
return nil, err
114+
}
115+
}
116+
} else {
117+
d, err = incus.ConnectIncus(remote.Addr, args)
118+
if err != nil {
119+
return nil, err
120+
}
109121
}
110122

111123
if remote.Project != "" && remote.Project != "default" {

0 commit comments

Comments
 (0)