Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add k3s WithManifest option #1920

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/modules/k3s.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ for K3s. E.g. `testcontainers.WithImage("docker.io/rancher/k3s:v1.27.1-k3s1")`.

{% include "../features/common_functional_options.md" %}

## WithManifest

The `WithManifest` option loads a manifest obtained from a local file into the cluster. K3s applies it automatically during the startup process

```golang
func WithManifest(manifestPath string) testcontainers.CustomizeRequestOption
```

Example:

```golang
WithManifest("nginx-manifest.yaml")
```

### Container Methods

The K3s container exposes the following methods:
Expand Down
16 changes: 16 additions & 0 deletions modules/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ type K3sContainer struct {
testcontainers.Container
}

// path to the k3s manifests directory
const k3sManifests = "/var/lib/rancher/k3s/server/manifests/"

// WithManifest loads the manifest into the cluster. K3s applies it automatically during the startup process
func WithManifest(manifestPath string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) {
manifest := filepath.Base(manifestPath)
target := k3sManifests + manifest

req.Files = append(req.Files, testcontainers.ContainerFile{
HostFilePath: manifestPath,
ContainerFilePath: target,
})
}
}

// RunContainer creates an instance of the K3s container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K3sContainer, error) {
host, err := getContainerHost(ctx, opts...)
Expand Down
21 changes: 21 additions & 0 deletions modules/k3s/k3s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/k3s"
"github.com/testcontainers/testcontainers-go/wait"
)

func Test_LoadImages(t *testing.T) {
Expand Down Expand Up @@ -161,3 +162,23 @@ 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"),
testcontainers.WithWaitStrategy(wait.ForExec([]string{"kubectl", "wait", "pod", "nginx","--for=condition=Ready"})),
)
if err != nil {
t.Fatal(err)
}

// Clean up the container
defer func() {
if err := k3sContainer.Terminate(ctx); err != nil {
t.Fatal(err)
}
}()
}
14 changes: 14 additions & 0 deletions modules/k3s/nginx-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod
name: nginx
namespace: default
spec:
containers:
- name: pod
image: nginx
imagePullPolicy: Always


Loading