Skip to content

Commit

Permalink
pkg/machine: make only one AddConnection() call
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Luap99 committed Feb 16, 2024
1 parent d60757c commit 30a18fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
22 changes: 12 additions & 10 deletions pkg/machine/connection/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package connection

import (
"fmt"
"net/url"
"strconv"

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

uris := []*url.URL{uri, uriRoot}
names := []string{name, name + "-root"}
cons := []connection{
{
name: name,
uri: uri,
},
{
name: name + "-root",
uri: uriRoot,
},
}

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

for i := 0; i < 2; i++ {
if err := AddConnection(uris[i], names[i], identityPath, opts.IsDefault && i == 0); err != nil {
return err
}
}
return nil
return addConnection(cons, identityPath, opts.IsDefault)
}
43 changes: 25 additions & 18 deletions pkg/machine/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@ import (

const LocalhostIP = "127.0.0.1"

func AddConnection(uri *url.URL, name, identity string, isDefault bool) error {
type connection struct {
name string
uri *url.URL
}

func addConnection(cons []connection, identity string, isDefault bool) error {
if len(identity) < 1 {
return errors.New("identity must be defined")
}

return config.EditConnectionConfig(func(cfg *config.ConnectionsFile) error {
if _, ok := cfg.Connection.Connections[name]; ok {
return errors.New("cannot overwrite connection")
}
for i, con := range cons {
if _, ok := cfg.Connection.Connections[con.name]; ok {
return fmt.Errorf("cannot overwrite connection %q", con.name)
}

dst := config.Destination{
URI: uri.String(),
IsMachine: true,
Identity: identity,
}
dst := config.Destination{
URI: con.uri.String(),
IsMachine: true,
Identity: identity,
}

if isDefault {
cfg.Connection.Default = name
}
if isDefault && i == 0 {
cfg.Connection.Default = con.name
}

if cfg.Connection.Connections == nil {
cfg.Connection.Connections = map[string]config.Destination{
name: dst,
if cfg.Connection.Connections == nil {
cfg.Connection.Connections = map[string]config.Destination{
con.name: dst,
}
cfg.Connection.Default = con.name
} else {
cfg.Connection.Connections[con.name] = dst
}
cfg.Connection.Default = name
} else {
cfg.Connection.Connections[name] = dst
}

return nil
Expand Down

0 comments on commit 30a18fc

Please sign in to comment.