josdk-spring-boot-starter icon indicating copy to clipboard operation
josdk-spring-boot-starter copied to clipboard

Received events from real cluster using @EnableMockOperator

Open jmformenti opened this issue 3 years ago • 4 comments

Running a simple test using @EnableMockOperator annotation:

@SpringBootTest
@ActiveProfiles("test")
@EnableMockOperator
public class MyReconcilerSpringBootIT {
    @Test
    void contextLoads() {
    }
}

At the beginning of the execution, my reconciler receives events for each custom resource from my real kubernetes cluster (configured in .kube/config).

Is this correct?

jmformenti avatar Sep 06 '22 14:09 jmformenti

yes, that should be the case AFAIK

csviri avatar Sep 07 '22 07:09 csviri

From my perspective, using @EnableMockOperator I would expect don't need access to a real kubernetes cluster.

Following the docs I've found that is possible with some changes:

@Configuration
public class Config {

    @Bean
    @Profile("!test")
    public KubernetesClient kubernetesClient() {
        Config config = new ConfigBuilder().withNamespace(null).build();
        return new DefaultKubernetesClient(config);
    }

    @Bean
    public WebPageReconciler customServiceController() {
        return new WebPageReconciler();
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    @SuppressWarnings("rawtypes")
    public Operator operator(List<Reconciler> controllers, KubernetesClient client) {
        Operator operator = new Operator(client);
        controllers.forEach(operator::register);
        return operator;
    }
}

In order to use the defined kubernetes client inside the operator. Then in the test we enable the profile test:

@SpringBootTest
@ActiveProfiles("test")
@EnableMockOperator
public class SpringBootStarterSampleApplicationTest {

    @Test
    void contextLoads() {
    }
}

In this way, during the test, the operator use the mocked kubernetes client defined by @EnableMockOperator and outside the test, the operator will use the original kubernetes client.

jmformenti avatar Sep 07 '22 17:09 jmformenti

From my perspective, using @EnableMockOperator I would expect don't need access to a real kubernetes cluster

It does not, pls take a look on the following test, that tests the starter:

https://github.com/java-operator-sdk/operator-framework-spring-boot-starter/blob/fabe51be27acdbd025278a7a7b4176a0cd2e6808/starter-test/src/test/java/io/javaoperatorsdk/operator/springboot/starter/test/EnableMockOperatorTests.java

It uses the mock api server in the background. Although in some more complex integration test cases it might be useful to use Minikube or Kind.

 @Bean(initMethod = "start", destroyMethod = "stop")
    @SuppressWarnings("rawtypes")
    public Operator operator(List<Reconciler> controllers, KubernetesClient client) {
        Operator operator = new Operator(client);
        controllers.forEach(operator::register);
        return operator;
    }

actually event this is not needed, will update the docs.

csviri avatar Sep 08 '22 07:09 csviri

Nice! it's simpler, thanks

jmformenti avatar Sep 12 '22 13:09 jmformenti