Skip to content

Commit 30a18fc

Browse files
committed
pkg/machine: make only one AddConnection() call
This function has to read/write the connections file as such it should only ever be called once otherwise we read/write the same file twice which makes no sense. Also cleanup the fucntion a bit and make it private as there are no external callers. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
1 parent d60757c commit 30a18fc

2 files changed

Lines changed: 37 additions & 28 deletions

File tree

pkg/machine/connection/add.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package connection
22

33
import (
44
"fmt"
5-
"net/url"
65
"strconv"
76

87
"github.com/containers/podman/v5/pkg/machine/define"
@@ -18,19 +17,22 @@ func AddSSHConnectionsToPodmanSocket(uid, port int, identityPath, name, remoteUs
1817
uri := makeSSHURL(LocalhostIP, fmt.Sprintf("/run/user/%d/podman/podman.sock", uid), strconv.Itoa(port), remoteUsername)
1918
uriRoot := makeSSHURL(LocalhostIP, "/run/podman/podman.sock", strconv.Itoa(port), "root")
2019

21-
uris := []*url.URL{uri, uriRoot}
22-
names := []string{name, name + "-root"}
20+
cons := []connection{
21+
{
22+
name: name,
23+
uri: uri,
24+
},
25+
{
26+
name: name + "-root",
27+
uri: uriRoot,
28+
},
29+
}
2330

2431
// The first connection defined when connections is empty will become the default
2532
// regardless of IsDefault, so order according to rootful
2633
if opts.Rootful {
27-
uris[0], names[0], uris[1], names[1] = uris[1], names[1], uris[0], names[0]
34+
cons[0], cons[1] = cons[1], cons[0]
2835
}
2936

30-
for i := 0; i < 2; i++ {
31-
if err := AddConnection(uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil {
32-
return err
33-
}
34-
}
35-
return nil
37+
return addConnection(cons, identityPath, opts.IsDefault)
3638
}

pkg/machine/connection/connection.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,40 @@ import (
1515

1616
const LocalhostIP = "127.0.0.1"
1717

18-
func AddConnection(uri *url.URL, name, identity string, isDefault bool) error {
18+
type connection struct {
19+
name string
20+
uri *url.URL
21+
}
22+
23+
func addConnection(cons []connection, identity string, isDefault bool) error {
1924
if len(identity) < 1 {
2025
return errors.New("identity must be defined")
2126
}
2227

2328
return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
24-
if _, ok := cfg.Connection.Connections[name]; ok {
25-
return errors.New("cannot overwrite connection")
26-
}
29+
for i, con := range cons {
30+
if _, ok := cfg.Connection.Connections[con.name]; ok {
31+
return fmt.Errorf("cannot overwrite connection %q", con.name)
32+
}
2733

28-
dst := config.Destination{
29-
URI: uri.String(),
30-
IsMachine: true,
31-
Identity: identity,
32-
}
34+
dst := config.Destination{
35+
URI: con.uri.String(),
36+
IsMachine: true,
37+
Identity: identity,
38+
}
3339

34-
if isDefault {
35-
cfg.Connection.Default = name
36-
}
40+
if isDefault && i == 0 {
41+
cfg.Connection.Default = con.name
42+
}
3743

38-
if cfg.Connection.Connections == nil {
39-
cfg.Connection.Connections = map[string]config.Destination{
40-
name: dst,
44+
if cfg.Connection.Connections == nil {
45+
cfg.Connection.Connections = map[string]config.Destination{
46+
con.name: dst,
47+
}
48+
cfg.Connection.Default = con.name
49+
} else {
50+
cfg.Connection.Connections[con.name] = dst
4151
}
42-
cfg.Connection.Default = name
43-
} else {
44-
cfg.Connection.Connections[name] = dst
4552
}
4653

4754
return nil

0 commit comments

Comments
 (0)