podman compose: build for all arches

Machine only works on amd64 and arm64 but the compose command can still
be used without machine so split out the machine only logic to make it
build for all arches.

[NO NEW TESTS NEEDED]

Fixes #21757

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2024-02-26 14:57:21 +01:00
parent 6dd8454a54
commit 6d3571dcf5
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
3 changed files with 86 additions and 56 deletions

View File

@ -1,5 +1,3 @@
//go:build amd64 || arm64
package main
import (
@ -16,10 +14,6 @@ import (
"github.com/containers/podman/v5/cmd/podman/registry"
"github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -145,58 +139,11 @@ func composeDockerHost() (string, error) {
// machine which is the case for podman machines.
return strings.TrimSuffix(connection.URI, parsedConnection.Path), nil
}
machineProvider, err := provider.Get()
uri, err := getMachineConn(connection, parsedConnection)
if err != nil {
return "", fmt.Errorf("getting machine provider: %w", err)
return "", fmt.Errorf("get machine connection URI: %w", err)
}
dirs, err := machine.GetMachineDirs(machineProvider.VMType())
if err != nil {
return "", err
}
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
if err != nil {
return "", fmt.Errorf("listing machines: %w", err)
}
// Now we know that the connection points to a machine and we
// can find the machine by looking for the one with the
// matching port.
connectionPort, err := strconv.Atoi(parsedConnection.Port())
if err != nil {
return "", fmt.Errorf("parsing connection port: %w", err)
}
for _, item := range machineList {
if connectionPort != item.SSH.Port {
continue
}
state, err := machineProvider.State(item, false)
if err != nil {
return "", err
}
if state != define.Running {
return "", fmt.Errorf("machine %s is not running but in state %s", item.Name, state)
}
// TODO This needs to be wired back in when all providers are complete
// TODO Need someoone to plumb in the connection information below
// if machineProvider.VMType() == define.WSLVirt || machineProvider.VMType() == define.HyperVVirt {
// if info.ConnectionInfo.PodmanPipe == nil {
// return "", errors.New("pipe of machine is not set")
// }
// return strings.Replace(info.ConnectionInfo.PodmanPipe.Path, `\\.\pipe\`, "npipe:////./pipe/", 1), nil
// }
// if info.ConnectionInfo.PodmanSocket == nil {
// return "", errors.New("socket of machine is not set")
// }
// return "unix://" + info.ConnectionInfo.PodmanSocket.Path, nil
return "", nil
}
return "", fmt.Errorf("could not find a matching machine for connection %q", connection.URI)
return uri, nil
}
// composeEnv returns the compose-specific environment variables.

View File

@ -0,0 +1,69 @@
//go:build amd64 || arm64
package main
import (
"fmt"
"net/url"
"strconv"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v5/pkg/machine"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
)
func getMachineConn(connection *config.Connection, parsedConnection *url.URL) (string, error) {
machineProvider, err := provider.Get()
if err != nil {
return "", fmt.Errorf("getting machine provider: %w", err)
}
dirs, err := machine.GetMachineDirs(machineProvider.VMType())
if err != nil {
return "", err
}
machineList, err := vmconfigs.LoadMachinesInDir(dirs)
if err != nil {
return "", fmt.Errorf("listing machines: %w", err)
}
// Now we know that the connection points to a machine and we
// can find the machine by looking for the one with the
// matching port.
connectionPort, err := strconv.Atoi(parsedConnection.Port())
if err != nil {
return "", fmt.Errorf("parsing connection port: %w", err)
}
for _, item := range machineList {
if connectionPort != item.SSH.Port {
continue
}
state, err := machineProvider.State(item, false)
if err != nil {
return "", err
}
if state != define.Running {
return "", fmt.Errorf("machine %s is not running but in state %s", item.Name, state)
}
// TODO This needs to be wired back in when all providers are complete
// TODO Need someoone to plumb in the connection information below
// if machineProvider.VMType() == define.WSLVirt || machineProvider.VMType() == define.HyperVVirt {
// if info.ConnectionInfo.PodmanPipe == nil {
// return "", errors.New("pipe of machine is not set")
// }
// return strings.Replace(info.ConnectionInfo.PodmanPipe.Path, `\\.\pipe\`, "npipe:////./pipe/", 1), nil
// }
// if info.ConnectionInfo.PodmanSocket == nil {
// return "", errors.New("socket of machine is not set")
// }
// return "unix://" + info.ConnectionInfo.PodmanSocket.Path, nil
return "", nil
}
return "", fmt.Errorf("could not find a matching machine for connection %q", connection.URI)
}

View File

@ -0,0 +1,14 @@
//go:build !(amd64 || arm64)
package main
import (
"errors"
"net/url"
"github.com/containers/common/pkg/config"
)
func getMachineConn(connection *config.Connection, parsedConnection *url.URL) (string, error) {
return "", errors.New("podman machine not supported on this architecture")
}