Skip to content

ServiceConnection for K3sContainer #45717

Open
@MichaelDausmann

Description

@MichaelDausmann

I would like to use K3sContainer for local Development, I already have Kafka and Neo4j working with @Serviceconnection. This would be nice...

    @Bean
    @ServiceConnection
    K3sContainer k3s() {
        return new K3sContainer(DockerImageName.parse("rancher/k3s:v1.21.3-k3s1"))
                .withCommand("server", "--disable", "metrics-server") // we don't need metrics
                .withLogConsumer(new Slf4jLogConsumer(logger)
                );
    }

K3sContainer is here (modules/k3s/src/main/java/org/testcontainers/k3s/K3sContainer.java)

Looks like it would require work here (spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection)
and here (spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure).

Workaround

Here is my workaround

    @Bean
    K3sContainer k3s() {
        K3sContainer k3sContainer = new K3sContainer(DockerImageName.parse("rancher/k3s:v1.21.3-k3s1"))
                .withCommand("server", "--disable", "metrics-server") // we don't need metrics
                .withLogConsumer(new Slf4jLogConsumer(logger)
                );

        // don't normally need to start the container in these @Bean methods but can't get the config unless its started
        k3sContainer.start();

        String kubeConfigYaml = k3sContainer.getKubeConfigYaml();
        Config config = Config.fromKubeconfig(kubeConfigYaml);  // requires io.fabric8:kubernetes-client:5.11.0 or higher

        // in the absence of @ServiceConnection integration for this testcontainer, Jack the test container URL into properties so it's picked up when I create a client in main app
        System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, config.getMasterUrl());
        System.setProperty(Config.KUBERNETES_CA_CERTIFICATE_DATA_SYSTEM_PROPERTY, config.getCaCertData());
        System.setProperty(Config.KUBERNETES_CLIENT_CERTIFICATE_DATA_SYSTEM_PROPERTY, config.getClientCertData());
        System.setProperty(Config.KUBERNETES_CLIENT_KEY_DATA_SYSTEM_PROPERTY, config.getClientKeyData());
        System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");

        return k3sContainer;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions