Skip to content

[Feature]: @ServiceConnection integration for K3sContainer? #10278

@MichaelDausmann

Description

@MichaelDausmann

Module

K3S

Problem

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)
                );
    }

but results in this error

org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsNotFoundException: No ConnectionDetails found for source '@ServiceConnection source for Bean 'k3s' defined in my.app.MyAppConfigurationLocal'. You may need to add a 'name' to your @ServiceConnection annotation
	at org.springframework.boot.testcontainers.service.connection.ConnectionDetailsRegistrar.rethrowConnectionDetails(ConnectionDetailsRegistrar.java:90) ~[spring-boot-testcontainers-3.4.3.jar:3.4.3]
	at org.springframework.boot.testcontainers.service.connection.ConnectionDetailsRegistrar.registerBeanDefinitions(ConnectionDetailsRegistrar.java:80) ~[spring-boot-testcontainers-3.4.3.jar:3.4.3]

Solution

Implement @Serviceconnection attribute in the k3s container.

Benefit

Using test containers for local dev is awesome and has great DX.

Alternatives

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;
    }

Would you like to help contributing this feature?

Yes

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