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

feat: test container for special IT #1970

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions operator-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@
<artifactId>operator-framework</artifactId>
<name>Operator SDK - Framework - Plain Java</name>


<dependencies>
<!-- https://mvnrepository.com/artifact/org.testcontainers/k3s -->
<dependency>
<groupId>com.dajudge.kindcontainer</groupId>
<artifactId>kindcontainer</artifactId>
<version>1.4.2</version>
<scope>test</scope>
</dependency>
<!-- todo move to dependency management -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-core</artifactId>
Expand Down Expand Up @@ -81,11 +95,6 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>jenvtest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.fabric8.kubernetes.api.model.rbac.Role;
import io.fabric8.kubernetes.api.model.rbac.RoleBinding;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.javaoperatorsdk.jenvtest.junit.EnableKubeAPIServer;
import io.javaoperatorsdk.operator.health.InformerHealthIndicator;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerResourceEventSource;
import io.javaoperatorsdk.operator.sample.informerrelatedbehavior.ConfigMapDependentResource;
import io.javaoperatorsdk.operator.sample.informerrelatedbehavior.InformerRelatedBehaviorTestCustomResource;
import io.javaoperatorsdk.operator.sample.informerrelatedbehavior.InformerRelatedBehaviorTestReconciler;

import com.dajudge.kindcontainer.ApiServerContainer;

import static io.javaoperatorsdk.operator.sample.informerrelatedbehavior.InformerRelatedBehaviorTestReconciler.CONFIG_MAP_DEPENDENT_RESOURCE;
import static io.javaoperatorsdk.operator.sample.informerrelatedbehavior.InformerRelatedBehaviorTestReconciler.INFORMER_RELATED_BEHAVIOR_TEST_RECONCILER;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -46,23 +48,30 @@
* The test ends with "ITS" (Special) since it needs to run separately from other ITs
* </p>
*/
@EnableKubeAPIServer(apiServerFlags = {"--min-request-timeout", "1"}, updateKubeConfigFile = true)
// @EnableKubeAPIServer(apiServerFlags = {"--min-request-timeout", "1"}, updateKubeConfigFile =
// true)
class InformerRelatedBehaviorITS {

private static final Logger log = LoggerFactory.getLogger(InformerRelatedBehaviorITS.class);

public static final String TEST_RESOURCE_NAME = "test1";
public static final String ADDITIONAL_NAMESPACE_SUFFIX = "-additional";

KubernetesClient adminClient = new KubernetesClientBuilder().build();
KubernetesClient adminClient;
InformerRelatedBehaviorTestReconciler reconciler;
String actualNamespace;
String additionalNamespace;
Operator operator;
ApiServerContainer<?> apiServerContainer = new ApiServerContainer<>();
volatile boolean replacementStopHandlerCalled = false;

@BeforeEach
void beforeEach(TestInfo testInfo) {
apiServerContainer.start();

adminClient = new KubernetesClientBuilder()
.withConfig(Config.fromKubeconfig(apiServerContainer.getKubeconfig()))
.build();
LocallyRunOperatorExtension.applyCrd(InformerRelatedBehaviorTestCustomResource.class,
adminClient);
testInfo.getTestMethod().ifPresent(method -> {
Expand All @@ -81,6 +90,7 @@ void cleanup() {
}
adminClient.resource(dependentConfigMap()).delete();
adminClient.resource(testCustomResource()).delete();
apiServerContainer.stop();
}

@Test
Expand Down Expand Up @@ -299,13 +309,12 @@ private void assertRuntimeInfoForSecondaryPermission(Operator operator) {
}

KubernetesClient clientUsingServiceAccount() {
KubernetesClient client = new KubernetesClientBuilder()
.withConfig(new ConfigBuilder()
return new KubernetesClientBuilder()
.withConfig(new ConfigBuilder(Config.fromKubeconfig(apiServerContainer.getKubeconfig()))
.withImpersonateUsername("rbac-test-user")
.withNamespace(actualNamespace)
.build())
.build();
return client;
}

Operator startOperator(boolean stopOnInformerErrorDuringStartup) {
Expand Down