Skip to content

Commit

Permalink
Add k3s WithManifest option (#1920)
Browse files Browse the repository at this point in the history
* Add WithManifest option to k3s

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>

* Use new copy file feature to copy manifest

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>

* Fix missing bracket

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>

---------

Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
pablochacin and mdelapenya committed Mar 5, 2024
1 parent 755bbb8 commit 85361fe
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
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


0 comments on commit 85361fe

Please sign in to comment.