Skip to content

Commit

Permalink
Merge pull request #1866 from carolynvs/fix-local-registry
Browse files Browse the repository at this point in the history
Add back lost docker network logic to magefile
  • Loading branch information
carolynvs authored Jan 25, 2022
2 parents 93f27d3 + 63a9f6b commit fb15bd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 24 additions & 4 deletions mage/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
const (
// Container name of the local registry
DefaultRegistryName = "registry"

// Name of the docker network shared by the local registry and KIND
DefaultNetworkName = "porter"
)

// Ensure the docker daemon is started and ready to accept connections.
Expand Down Expand Up @@ -75,25 +78,36 @@ func isDockerReady() (bool, error) {
return err == nil, nil
}

func EnsurePorterNetwork() error {
if NetworkExists(DefaultNetworkName) {
return nil
}

return shx.RunE("docker", "network", "create", DefaultNetworkName, "-d=bridge")
}

func NetworkExists(name string) bool {
err := shx.RunE("docker", "network", "inspect", name)
return err == nil
}

// Start a Docker registry to use with the tests.
func StartDockerRegistry() error {
mg.SerialDeps(StartDocker)
if isContainerRunning(getRegistryName()) {
mg.SerialDeps(StartDocker, EnsurePorterNetwork)

// Check if we have the registry container on the proper network, if not recreate it
registryName := getRegistryName()
if isContainerRunning(registryName) && isOnDockerNetwork(registryName, DefaultNetworkName) {
return nil
}

err := RemoveContainer(getRegistryName())
err := RemoveContainer(registryName)
if err != nil {
return err
}

fmt.Println("Starting local docker registry")
return shx.RunE("docker", "run", "-d", "-p", "0.0.0.0:5000:5000", "--name", getRegistryName(), "registry:2")
return shx.RunE("docker", "run", "-d", "-p", "0.0.0.0:5000:5000", "--network="+DefaultNetworkName, "--name", registryName, "registry:2")
}

// Stop the Docker registry used by the tests.
Expand Down Expand Up @@ -140,3 +154,9 @@ func getRegistryName() string {
}
return DefaultRegistryName
}

func isOnDockerNetwork(container string, network string) bool {
networkId, _ := shx.OutputE("docker", "network", "inspect", network, "-f", "{{.Id}}")
networks, _ := shx.OutputE("docker", "inspect", container, "-f", "{{json .NetworkSettings.Networks}}")
return strings.Contains(networks, networkId)
}
4 changes: 3 additions & 1 deletion mage/tests/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func EnsureTestCluster() {
if !useCluster() {
CreateTestCluster()
}

mgx.Must(docker.StartDockerRegistry())
}

Expand Down Expand Up @@ -112,7 +113,8 @@ func CreateTestCluster() {
mgx.Must(errors.Wrap(err, "could not write kind config file"))
defer os.Remove("kind.config.yaml")

must.Run("kind", "create", "cluster", "--name", getKindClusterName(), "--config", "kind.config.yaml")
must.Command("kind", "create", "cluster", "--name", getKindClusterName(), "--config", "kind.config.yaml").
Env("KIND_EXPERIMENTAL_DOCKER_NETWORK=" + docker.DefaultNetworkName).Run()

// Document the local registry
kubectl("apply", "-f", "mage/tests/local-registry.yaml").Run()
Expand Down

0 comments on commit fb15bd9

Please sign in to comment.