Created
April 9, 2023 23:13
-
-
Save sipsma/5b6202bcad13a4f674fac2b15ff6faf2 to your computer and use it in GitHub Desktop.
example of running kind inside dagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"os" | |
"dagger.io/dagger" | |
k8smeta "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
"sigs.k8s.io/kind/pkg/cluster" | |
kindCmd "sigs.k8s.io/kind/pkg/cmd" | |
) | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
runClientEnvName := "_RUN_CLIENT" | |
if _, ok := os.LookupEnv(runClientEnvName); ok { | |
if err := runKubernetesClient(ctx); err != nil { | |
panic(err) | |
} | |
return | |
} | |
c, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | |
if err != nil { | |
panic(err) | |
} | |
output, err := c.Container().From("golang:1.20.3-alpine"). | |
WithExec([]string{"apk", "add", "docker-cli"}). | |
WithMountedCache("/root/.cache/go-build", c.CacheVolume("go-build")). | |
WithMountedCache("/go/pkg/mod", c.CacheVolume("go-mod")). | |
WithMountedDirectory("/src", c.Host().Directory(".")). | |
WithServiceBinding("dockerd", dockerdService(ctx, c)). | |
WithServiceBinding("registry", registryService(ctx, c)). | |
WithEnvVariable("DOCKER_HOST", "tcp://dockerd:12992"). | |
WithEnvVariable(runClientEnvName, "yes"). | |
WithWorkdir("/src"). | |
WithExec([]string{"go", "run", "main.go"}). | |
Stdout(ctx) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(output) | |
} | |
func dockerdService(ctx context.Context, c *dagger.Client) *dagger.Container { | |
return c.Container().From("docker:23.0.1-dind"). | |
WithMountedCache("/var/lib/docker", c.CacheVolume("docker-state"), dagger.ContainerWithMountedCacheOpts{ | |
Sharing: dagger.Private, | |
}). | |
WithExposedPort(12992). | |
WithEntrypoint(nil). | |
WithExec( | |
[]string{"sh", "-c", | |
// TODO: shouldn't have to delete all dockerd state every time, just need the kind setup code to | |
// handle cluster already existing | |
"rm -rf /var/lib/docker/* && exec dockerd-entrypoint.sh dockerd --host=tcp://0.0.0.0:12992 --tls=false", | |
}, | |
dagger.ContainerWithExecOpts{ | |
InsecureRootCapabilities: true, | |
}, | |
) | |
} | |
func registryService(ctx context.Context, c *dagger.Client) *dagger.Container { | |
return c.Container(). | |
From("registry:2"). | |
WithMountedCache("/var/lib/registry", c.CacheVolume("load-test-registry")). | |
WithExposedPort(5000). | |
WithExec([]string{"registry", "serve", "/etc/docker/registry/config.yml"}) | |
} | |
func runKubernetesClient(ctx context.Context) error { | |
dockerdAddr, err := net.LookupIP("dockerd") | |
if err != nil { | |
return err | |
} | |
kindLogger := kindCmd.NewLogger() | |
kindProvider := cluster.NewProvider(cluster.ProviderWithLogger(kindLogger)) | |
if err := kindProvider.Create( | |
"test", | |
cluster.CreateWithRetain(true), | |
cluster.CreateWithRawConfig([]byte(fmt.Sprintf(`kind: Cluster | |
apiVersion: kind.x-k8s.io/v1alpha4 | |
networking: | |
apiServerAddress: %s | |
apiServerPort: 11492 | |
containerdConfigPatches: | |
- |- | |
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry:5000"] | |
endpoint = ["http://registry:5000"] | |
`, dockerdAddr[0].String()))), | |
); err != nil { | |
return err | |
} | |
k8sConfig, err := clientcmd.BuildConfigFromFlags("", "/root/.kube/config") | |
if err != nil { | |
return err | |
} | |
clientset, err := kubernetes.NewForConfig(k8sConfig) | |
if err != nil { | |
return err | |
} | |
nodes, err := clientset.CoreV1().Nodes().List(ctx, k8smeta.ListOptions{}) | |
if err != nil { | |
return err | |
} | |
for _, node := range nodes.Items { | |
fmt.Printf("Node: %s\n", node.Name) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment