Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Received events from real cluster using @EnableMockOperator #27

Open
jmformenti opened this issue Sep 6, 2022 · 4 comments
Open

Received events from real cluster using @EnableMockOperator #27

jmformenti opened this issue Sep 6, 2022 · 4 comments

Comments

@jmformenti
Copy link

jmformenti commented Sep 6, 2022

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?

@csviri
Copy link
Contributor

csviri commented Sep 7, 2022

yes, that should be the case AFAIK

@jmformenti
Copy link
Author

jmformenti commented Sep 7, 2022

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.

@csviri
Copy link
Contributor

csviri commented Sep 8, 2022

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.

@jmformenti
Copy link
Author

Nice! it's simpler, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants