From 0756685ea15c8cb3b08086a0ed0b23ffbcb76c29 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Wed, 15 Nov 2023 19:49:09 +0100 Subject: [PATCH] Add k3s WithManifest option Signed-off-by: Pablo Chacin --- modules/k3s/k3s.go | 20 +++++++++++++++ modules/k3s/k3s_test.go | 44 +++++++++++++++++++++++++++++++++ modules/k3s/nginx-manifest.yaml | 14 +++++++++++ 3 files changed, 78 insertions(+) create mode 100644 modules/k3s/nginx-manifest.yaml diff --git a/modules/k3s/k3s.go b/modules/k3s/k3s.go index 0f7ccb4d168..1f021adfd31 100644 --- a/modules/k3s/k3s.go +++ b/modules/k3s/k3s.go @@ -29,6 +29,26 @@ type K3sContainer struct { testcontainers.Container } +// WithManifest loads the manifest into the cluster and applies it +func WithManifest(manifestPath string) testcontainers.CustomizeRequestOption { + return func(req *testcontainers.GenericContainerRequest) { + manifest := filepath.Base(manifestPath) + target := "/var/lib/rancher/k3s/server/manifests/" + manifest + + // Add a post start hook to copy the manifest. + // We cannot use the Files option in the GenericRequest because the target + // path is created when the container starts + manifestHook := testcontainers.ContainerLifecycleHooks{ + PostStarts: []testcontainers.ContainerHook{ + func(ctx context.Context, c testcontainers.Container) error { + return c.CopyFileToContainer(ctx, manifest, target, 0x644) + }, + }, + } + req.LifecycleHooks = append(req.LifecycleHooks, manifestHook) + } +} + // RunContainer creates an instance of the K3s container type func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K3sContainer, error) { host, err := getContainerHost(ctx, opts...) diff --git a/modules/k3s/k3s_test.go b/modules/k3s/k3s_test.go index 65510c037cd..7ea1444c751 100644 --- a/modules/k3s/k3s_test.go +++ b/modules/k3s/k3s_test.go @@ -1,6 +1,7 @@ package k3s_test import ( + "bytes" "context" "testing" "time" @@ -161,3 +162,46 @@ func Test_APIServerReady(t *testing.T) { t.Fatalf("failed to create pod %v", err) } } + +func Test_WithManifestOption(t *testing.T) { + ctx := context.Background() + + k3sContainer, err := k3s.RunContainer(ctx, + testcontainers.WithImage("docker.io/rancher/k3s:v1.27.1-k3s1"), + k3s.WithManifest("nginx-manifest.yaml"), + ) + if err != nil { + t.Fatal(err) + } + + // Clean up the container + defer func() { + if err := k3sContainer.Terminate(ctx); err != nil { + t.Fatal(err) + } + }() + + // FIXME: while we don't have a WaitFor condition for the pods to get ready + // give some time for the pod to be created + time.Sleep(10 * time.Second) + + rc, stdout, err := k3sContainer.Exec( + ctx, + []string{ + "kubectl", + "wait", + "pod", + "nginx", + "--for=condition=Ready", + "--timeout=90s", + }, + ) + if err != nil { + t.Fatalf("waiting for pods ready %v", err) + } + if rc != 0 { + output := bytes.Buffer{} + output.ReadFrom(stdout) + t.Fatalf("pods not ready \n%s\n", output.String()) + } +} diff --git a/modules/k3s/nginx-manifest.yaml b/modules/k3s/nginx-manifest.yaml new file mode 100644 index 00000000000..fd552a1a24f --- /dev/null +++ b/modules/k3s/nginx-manifest.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Pod +metadata: + labels: + run: pod + name: nginx + namespace: default +spec: + containers: + - name: pod + image: nginx + imagePullPolicy: Always + + \ No newline at end of file