Files
Ed Santiago 6a696cb8fd podman-registry: simpler, safer invocations
First: fix podman-registry script so it preserves the initial $PODMAN,
so all subsequent invocations of ps, logs, and stop will use the
same binary and arguments. Until now we've handled this by requiring
that our caller manage $PODMAN (and keep it the same), but that's
just wrong.

Next, simplify the golang interface: move the $PODMAN setting into
registry.go, instead of requiring e2e callers to set it. (This
could use some work: the local/remote conditional is icky).

IMPORTANT: To prevent registry.go from using the wrong podman binary,
the Start() call is gone. Only StartWithOptions() is valid now.

And, minor cleanup: comments, and add an actual error-message check

Reason for this PR is a recurring flake, #18355, whose multiple
failure modes I truly can't understand. I don't think this PR
is going to fix it, but this is still necessary work.

Signed-off-by: Ed Santiago <santiago@redhat.com>
2023-06-07 12:16:18 -06:00

130 lines
3.0 KiB
Go

package registry
import (
"fmt"
"os"
"strings"
"github.com/containers/podman/v4/utils"
"github.com/sirupsen/logrus"
)
const (
ImageKey = "PODMAN_REGISTRY_IMAGE"
UserKey = "PODMAN_REGISTRY_USER"
PassKey = "PODMAN_REGISTRY_PASS"
PortKey = "PODMAN_REGISTRY_PORT"
)
var binary = "podman-registry"
// Registry is locally running registry.
type Registry struct {
// Image - container image of the registry.
Image string
// User - the user to authenticate against the registry.
User string
// Password - the accompanying password for the user.
Password string
// Port - the port the registry is listening to on the host.
Port string
// running indicates if the registry is running.
running bool
}
// Options allows for customizing a registry.
type Options struct {
// PodmanPath - path to podman executable
PodmanPath string
// PodmanArgs - array of podman options
PodmanArgs []string
// Image - custom registry image.
Image string
}
// StartWithOptions a new registry and return it along with its image, user, password, and port.
func StartWithOptions(options *Options) (*Registry, error) {
if options == nil {
options = &Options{}
}
var args []string
if options.Image != "" {
args = append(args, "-i", options.Image)
}
args = append(args, "start")
podmanCmd := []string{"podman"}
if options.PodmanPath != "" {
podmanCmd[0] = options.PodmanPath
}
if len(options.PodmanArgs) != 0 {
podmanCmd = append(podmanCmd, options.PodmanArgs...)
}
// Start a registry.
os.Setenv("PODMAN", strings.Join(podmanCmd, " "))
out, err := utils.ExecCmd(binary, args...)
os.Unsetenv("PODMAN")
if err != nil {
return nil, fmt.Errorf("running %q: %s: %w", binary, out, err)
}
// Parse the output.
registry := Registry{}
for _, s := range strings.Split(out, "\n") {
if s == "" {
continue
}
spl := strings.Split(s, "=")
if len(spl) != 2 {
return nil, fmt.Errorf("unexpected output format %q: want 'PODMAN_...=...'", s)
}
key := spl[0]
val := strings.TrimSuffix(strings.TrimPrefix(spl[1], "\""), "\"")
switch key {
case ImageKey:
registry.Image = val
case UserKey:
registry.User = val
case PassKey:
registry.Password = val
case PortKey:
registry.Port = val
default:
logrus.Errorf("Unexpected podman-registry output: %q", s)
}
}
// Extra sanity check.
if registry.Image == "" {
return nil, fmt.Errorf("unexpected output %q: %q missing", out, ImageKey)
}
if registry.User == "" {
return nil, fmt.Errorf("unexpected output %q: %q missing", out, UserKey)
}
if registry.Password == "" {
return nil, fmt.Errorf("unexpected output %q: %q missing", out, PassKey)
}
if registry.Port == "" {
return nil, fmt.Errorf("unexpected output %q: %q missing", out, PortKey)
}
registry.running = true
return &registry, nil
}
// Stop the registry.
func (r *Registry) Stop() error {
// Stop a registry.
if !r.running {
return nil
}
if _, err := utils.ExecCmd(binary, "-P", r.Port, "stop"); err != nil {
return fmt.Errorf("stopping registry (%v) with %q: %w", *r, binary, err)
}
r.running = false
return nil
}