Skip to content

Commit

Permalink
fix: fix k3s module when running from inside a Docker container (#1289)
Browse files Browse the repository at this point in the history
The k3s module previously didn't work properly inside of a Docker
container; the caller would connect to its HTTPS server via the Docker
network gateway, which wasn't included in the list of SANS from the TLS
certificate exposed by the k3s container.

This change determines what the DaemonHost address is prior to creating
the container request, and then injects that address as one of the TLS
SANs.

When not running in a Docker container, the k3s module continues to
function as it did before, as the DaemonHost address in that scenario is
"localhost".
  • Loading branch information
rfratto authored Jun 15, 2023
1 parent f1c8cc3 commit c175df3
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion modules/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type K3sContainer struct {

// RunContainer creates an instance of the K3s container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K3sContainer, error) {
host, err := getContainerHost(ctx, opts...)
if err != nil {
return nil, err
}

req := testcontainers.ContainerRequest{
Image: "docker.io/rancher/k3s:v1.27.1-k3s1",
ExposedPorts: []string{
Expand All @@ -47,7 +52,7 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
Cmd: []string{
"server",
"--disable=traefik",
"--tls-san=localhost",
"--tls-san=" + host, // Host which will be used to access the Kubernetes server from tests.
},
Env: map[string]string{
"K3S_KUBECONFIG_MODE": "644",
Expand All @@ -72,6 +77,31 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return &K3sContainer{Container: container}, nil
}

func getContainerHost(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (string, error) {
// Use a dummy request to get the provider from options.
var req testcontainers.GenericContainerRequest
for _, opt := range opts {
opt.Customize(&req)
}

logging := req.Logger
if logging == nil {
logging = testcontainers.Logger
}
p, err := req.ProviderType.GetProvider(testcontainers.WithLogger(logging))
if err != nil {
return "", err
}

switch p := p.(type) {
case *testcontainers.DockerProvider:
return p.DaemonHost(ctx)
}

// Fall back to localhost.
return "localhost", nil
}

// GetKubeConfig returns the modified kubeconfig with server url
func (c *K3sContainer) GetKubeConfig(ctx context.Context) ([]byte, error) {
hostIP, err := c.Host(ctx)
Expand Down

0 comments on commit c175df3

Please sign in to comment.