Skip to content

Commit

Permalink
registry: make registry creation more robust (#150)
Browse files Browse the repository at this point in the history
Use the fully qualified registry image name so that the
filter works on podman.
  • Loading branch information
nicks authored Oct 21, 2021
1 parent 551f852 commit c2a767a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 3 additions & 1 deletion internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type CmdRunner interface {
type RealCmdRunner struct{}

func (RealCmdRunner) Run(ctx context.Context, cmd string, args ...string) error {
return exec.CommandContext(ctx, cmd, args...).Run()
// For some reason, ExitError only gets populated with Stderr if we call Output().
_, err := exec.CommandContext(ctx, cmd, args...).Output()
return err
}

type FakeCmdRunner func(argv []string)
Expand Down
35 changes: 33 additions & 2 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package registry
import (
"context"
"fmt"
osexec "os/exec"
"sort"
"strings"
"time"
Expand All @@ -27,6 +28,8 @@ var typeMeta = api.TypeMeta{APIVersion: "ctlptl.dev/v1alpha1", Kind: "Registry"}
var listTypeMeta = api.TypeMeta{APIVersion: "ctlptl.dev/v1alpha1", Kind: "RegistryList"}
var groupResource = schema.GroupResource{Group: "ctlptl.dev", Resource: "registries"}

const registryImageRef = "docker.io/library/registry:2" // The registry everyone uses.

// https://github.com/moby/moby/blob/v20.10.3/api/types/types.go#L313
const containerStateRunning = "running"

Expand Down Expand Up @@ -104,7 +107,7 @@ func (c *Controller) List(ctx context.Context, options ListOptions) (*api.Regist
}

filterArgs := filters.NewArgs()
filterArgs.Add("ancestor", "registry:2") // The registry everyone uses.
filterArgs.Add("ancestor", registryImageRef)

containers, err := c.dockerClient.ContainerList(ctx, types.ContainerListOptions{
Filters: filterArgs,
Expand Down Expand Up @@ -174,6 +177,23 @@ func (c *Controller) ipAndPortsFrom(ports []types.Port) (listenAddress string, h
return "unknown", 0, 0
}

func (c *Controller) ensureContainerDeleted(ctx context.Context, name string) error {
container, err := c.dockerClient.ContainerInspect(ctx, name)
if err != nil {
if client.IsErrNotFound(err) {
return nil
}
return err
}
if container.ContainerJSONBase == nil {
return nil
}

return c.dockerClient.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
Force: true,
})
}

// Compare the desired registry against the existing registry, and reconcile
// the two to match.
func (c *Controller) Apply(ctx context.Context, desired *api.Registry) (*api.Registry, error) {
Expand Down Expand Up @@ -229,11 +249,22 @@ func (c *Controller) Apply(ctx context.Context, desired *api.Registry) (*api.Reg
portSpec := fmt.Sprintf("%s:%d:5000", ListenAddress, hostPort)

_, _ = fmt.Fprintf(c.iostreams.ErrOut, "Creating registry %q...\n", desired.Name)
err = c.runner.Run(ctx, "docker", "run", "-d", "--restart=always", "-p", portSpec, "--name", desired.Name, "registry:2")

err = c.ensureContainerDeleted(ctx, desired.Name)
if err != nil {
return nil, err
}

err = c.runner.Run(ctx, "docker", "run", "-d", "--restart=always", "-p", portSpec, "--name",
desired.Name, registryImageRef)
if err != nil {
exitErr, ok := err.(*osexec.ExitError)
if ok {
_, _ = fmt.Fprintf(c.iostreams.ErrOut, "Error: %s", string(exitErr.Stderr))
}
return nil, err
}

err = c.maybeCreateForwarder(ctx, hostPort)
if err != nil {
return nil, err
Expand Down

0 comments on commit c2a767a

Please sign in to comment.