Skip to content

Commit

Permalink
test: rework bind approach with k8s runner
Browse files Browse the repository at this point in the history
since it's a local cluster, use kind to bind the hostpath to the node
then bind the node path to the container path
  • Loading branch information
kruskall committed Dec 2, 2024
1 parent 20dde32 commit 467cdd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
28 changes: 23 additions & 5 deletions dev-tools/mage/kubernetes/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package kubernetes

import (
"fmt"
"io/ioutil"
"go/build"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -66,8 +67,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {
}

clusterName := kubernetesClusterName()
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand All @@ -86,9 +87,26 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {
return err
}

cfg, err := os.CreateTemp("", "kind-")
if _, err := cfg.WriteString(fmt.Sprintf(`
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
extraMounts:
- hostPath: %s
containerPath: /go/pkg/mod
`, filepath.Join(build.Default.GOPATH, "pkg", "mod"))); err != nil {
return err
}
if err := cfg.Close(); err != nil {
return err
}

args := []string{
"create",
"cluster",
"--config", cfg.Name(),
"--name", clusterName,
"--kubeconfig", kubeConfig,
"--wait",
Expand Down Expand Up @@ -116,8 +134,8 @@ func (m *KindIntegrationTestStep) Setup(env map[string]string) error {

// Teardown destroys the kubernetes cluster.
func (m *KindIntegrationTestStep) Teardown(env map[string]string) error {
stdOut := ioutil.Discard
stdErr := ioutil.Discard
stdOut := io.Discard
stdErr := io.Discard
if mg.Verbose() {
stdOut = os.Stdout
stdErr = os.Stderr
Expand Down
15 changes: 12 additions & 3 deletions dev-tools/mage/kubernetes/kuberemote.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ func (r *KubeRemote) Run(env map[string]string, stdout io.Writer, stderr io.Writ
if err := r.rsync(randomPort, stderr, stderr, r.syncDir, r.destDir); err != nil {
return fmt.Errorf("rsync failed: %w", err)
}
if err := r.rsync(randomPort, stderr, stderr, filepath.Join(build.Default.GOPATH, "pkg", "mod"), "/go/pkg/mod"); err != nil {
return fmt.Errorf("gomodcache rsync failed: %w", err)
}

// stop port forwarding
close(stopChannel)
Expand Down Expand Up @@ -502,6 +499,10 @@ func createPodManifest(name string, image string, env map[string]string, cmd []s
Name: "destdir",
MountPath: destDir,
},
{
Name: "gomodcache",
MountPath: "/go/pkg/mod",
},
},
},
},
Expand All @@ -521,6 +522,14 @@ func createPodManifest(name string, image string, env map[string]string, cmd []s
EmptyDir: &apiv1.EmptyDirVolumeSource{},
},
},
{
Name: "gomodcache",
VolumeSource: apiv1.VolumeSource{
HostPath: &apiv1.HostPathVolumeSource{
Path: "/go/pkg/mod",
},
},
},
},
},
}
Expand Down

0 comments on commit 467cdd4

Please sign in to comment.