josdk-spring-boot-starter
josdk-spring-boot-starter copied to clipboard
Received events from real cluster using @EnableMockOperator
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?
yes, that should be the case AFAIK
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.
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.
Nice! it's simpler, thanks