Skip to content

Commit

Permalink
Add back lost docker network logic to magefile
Browse files Browse the repository at this point in the history
I accidentally committed changes to the magefile that broke
connectivitiy between the test kind cluster and the local registry
running in docker. They need to be on the same network.

This adds back code that I thought wasn't needed anymore to get docker
pulls from localhost:5000 from inside the cluster working again.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Jan 19, 2022
1 parent 93f27d3 commit 63a9f6b
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 63a9f6b

Please sign in to comment.