Skip to content

Commit

Permalink
Merge pull request #1806 from carolynvs/ipv6-hell
Browse files Browse the repository at this point in the history
Use an ipv4 network for test cluster and registry
  • Loading branch information
carolynvs authored Oct 19, 2021
2 parents 349d3a0 + 767d601 commit bd756a7
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions mage/tests/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
// Name of the KIND cluster used for testing
DefaultKindClusterName = "porter"

DefaultNetworkName = "porter"

// Relative location of the KUBECONFIG for the test cluster
Kubeconfig = "kind.config"

Expand All @@ -47,6 +49,9 @@ func EnsureTestCluster() {
if !useCluster() {
CreateTestCluster()
}
if !isOnDockerNetwork(getRegistryName(), DefaultNetworkName) {
mgx.Must(RestartDockerRegistry())
}
}

// get the config of the current kind cluster, if available
Expand Down Expand Up @@ -119,10 +124,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")

// Connect the kind and registry containers on the same network
must.Run("docker", "network", "connect", "kind", getRegistryName())
must.Command("kind", "create", "cluster", "--name", getKindClusterName(), "--config", "kind.config.yaml").
Env("KIND_EXPERIMENTAL_DOCKER_NETWORK=" + DefaultNetworkName).Run()

// Document the local registry
kubectl("apply", "-f", "mage/tests/local-registry.yaml").Run()
Expand All @@ -133,10 +136,6 @@ func DeleteTestCluster() {
mg.Deps(tools.EnsureKind)

must.RunE("kind", "delete", "cluster", "--name", getKindClusterName())

if isOnDockerNetwork(getRegistryName(), "kind") {
must.RunE("docker", "network", "disconnect", "kind", getRegistryName())
}
}

func kubectl(args ...string) shx.PreparedCommand {
Expand Down Expand Up @@ -204,9 +203,22 @@ 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", "-o", "com.docker.network.bridge.enable_ip_masquerade=true")
}

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.Deps(StartDocker)
mg.SerialDeps(StartDocker, EnsurePorterNetwork)
if isContainerRunning(getRegistryName()) {
return nil
}
Expand All @@ -217,7 +229,7 @@ func StartDockerRegistry() error {
}

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

// Stop the Docker registry used by the tests.
Expand Down Expand Up @@ -250,7 +262,7 @@ func containerExists(name string) bool {
// Remove the specified container, if it is present.
func RemoveContainer(name string) error {
stderr := bytes.Buffer{}
_, _, err := shx.Command("docker", "rm", "-f", name).Stderr(&stderr).Stdout(nil).Exec()
_, _, err := shx.Command("docker", "rm", "-vf", name).Stderr(&stderr).Stdout(nil).Exec()
// Gracefully handle the container already being gone
if err != nil && !strings.Contains(stderr.String(), "No such container") {
return err
Expand Down

0 comments on commit bd756a7

Please sign in to comment.