Skip the build step in the Realm import job if using a custom image (#11755)

This commit is contained in:
Andrea Peruffo 2022-05-17 14:52:41 +01:00 committed by GitHub
parent 1a95a58893
commit c2cb1ef260
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 4 deletions

View file

@ -32,12 +32,10 @@ import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers; import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
import io.quarkus.logging.Log; import io.quarkus.logging.Log;
import org.keycloak.operator.v2alpha1.crds.Keycloak;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatus; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatus;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusBuilder; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusBuilder;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusCondition; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusCondition;
import org.keycloak.operator.v2alpha1.crds.KeycloakStatusCondition;
import javax.inject.Inject; import javax.inject.Inject;

View file

@ -31,6 +31,7 @@ import io.fabric8.kubernetes.client.ResourceNotFoundException;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
import io.quarkus.logging.Log; import io.quarkus.logging.Log;
import org.keycloak.operator.OperatorManagedResource; import org.keycloak.operator.OperatorManagedResource;
import org.keycloak.operator.v2alpha1.crds.Keycloak;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusBuilder; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusBuilder;
@ -39,6 +40,7 @@ import java.util.Optional;
public class KeycloakRealmImportJob extends OperatorManagedResource { public class KeycloakRealmImportJob extends OperatorManagedResource {
private final Keycloak keycloak;
private final KeycloakRealmImport realmCR; private final KeycloakRealmImport realmCR;
private final Deployment existingDeployment; private final Deployment existingDeployment;
private final Job existingJob; private final Job existingJob;
@ -53,6 +55,7 @@ public class KeycloakRealmImportJob extends OperatorManagedResource {
this.existingJob = fetchExistingJob(); this.existingJob = fetchExistingJob();
this.existingDeployment = fetchExistingDeployment(); this.existingDeployment = fetchExistingDeployment();
this.keycloak = fetchExistingKeycloak();
} }
@Override @Override
@ -87,6 +90,14 @@ public class KeycloakRealmImportJob extends OperatorManagedResource {
.get(); .get();
} }
private Keycloak fetchExistingKeycloak() {
return client
.resources(Keycloak.class)
.inNamespace(getNamespace())
.withName(getKeycloakName())
.get();
}
private Job buildJob(PodTemplateSpec keycloakPodTemplate) { private Job buildJob(PodTemplateSpec keycloakPodTemplate) {
keycloakPodTemplate.getSpec().setRestartPolicy("Never"); keycloakPodTemplate.getSpec().setRestartPolicy("Never");
@ -129,9 +140,10 @@ public class KeycloakRealmImportJob extends OperatorManagedResource {
var override = "--override=false"; var override = "--override=false";
var runBuild = (keycloak.getSpec().getImage() == null) ? "/opt/keycloak/bin/kc.sh build && " : "";
var commandArgs = List.of("-c", var commandArgs = List.of("-c",
"/opt/keycloak/bin/kc.sh build && " + runBuild + "/opt/keycloak/bin/kc.sh import --file='" + importMntPath + getRealmName() + "-realm.json' " + override);
"/opt/keycloak/bin/kc.sh import --file='" + importMntPath + getRealmName() + "-realm.json' " + override);
keycloakContainer keycloakContainer
.setCommand(command); .setCommand(command);

View file

@ -5,12 +5,16 @@ import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
import io.quarkus.logging.Log; import io.quarkus.logging.Log;
import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTest;
import org.awaitility.Awaitility; import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.keycloak.operator.utils.CRAssert; import org.keycloak.operator.utils.CRAssert;
import org.keycloak.operator.v2alpha1.KeycloakService; import org.keycloak.operator.v2alpha1.KeycloakService;
import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport; import org.keycloak.operator.v2alpha1.crds.KeycloakRealmImport;
import org.keycloak.operator.v2alpha1.crds.keycloakspec.Unsupported; import org.keycloak.operator.v2alpha1.crds.keycloakspec.Unsupported;
import java.util.stream.Collectors;
import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -25,6 +29,35 @@ import static org.keycloak.operator.v2alpha1.crds.KeycloakRealmImportStatusCondi
@QuarkusTest @QuarkusTest
public class RealmImportE2EIT extends ClusterOperatorTest { public class RealmImportE2EIT extends ClusterOperatorTest {
@Override
@BeforeEach
public void beforeEach() {
super.beforeEach();
// Recreating the database and the realm import CR to keep this test isolated
k8sclient.load(getClass().getResourceAsStream("/example-realm.yaml")).inNamespace(namespace).delete();
k8sclient.load(getClass().getResourceAsStream("/incorrect-realm.yaml")).inNamespace(namespace).delete();
deleteDB();
deployDB();
}
private String getJobArgs() {
return k8sclient
.batch()
.v1()
.jobs()
.inNamespace(namespace)
.withName("example-count0-kc")
.get()
.getSpec()
.getTemplate()
.getSpec()
.getContainers()
.get(0)
.getArgs()
.stream()
.collect(Collectors.joining());
}
@Test @Test
public void testWorkingRealmImport() { public void testWorkingRealmImport() {
// Arrange // Arrange
@ -79,6 +112,41 @@ public class RealmImportE2EIT extends ClusterOperatorTest {
Log.info("Output from curl: '" + curlOutput + "'"); Log.info("Output from curl: '" + curlOutput + "'");
assertThat(curlOutput).isEqualTo("200"); assertThat(curlOutput).isEqualTo("200");
}); });
assertThat(getJobArgs()).contains("build");
}
@Test
@EnabledIfSystemProperty(named = OPERATOR_CUSTOM_IMAGE, matches = ".+")
public void testWorkingRealmImportWithCustomImage() {
// Arrange
var keycloak = getDefaultKeycloakDeployment();
keycloak.getSpec().setImage(customImage);
// Removing the Database so that a subsequent build will by default act on h2
// TODO: uncomment the following line after resolution of: https://github.com/keycloak/keycloak/issues/11767
// keycloak.getSpec().getServerConfiguration().removeIf(sc -> sc.getName().equals("db"));
deployKeycloak(k8sclient, keycloak, false);
// Act
k8sclient.load(getClass().getResourceAsStream("/example-realm.yaml")).inNamespace(namespace).createOrReplace();
// Assert
var crSelector = k8sclient
.resources(KeycloakRealmImport.class)
.inNamespace(namespace)
.withName("example-count0-kc");
Awaitility.await()
.atMost(3, MINUTES)
.pollDelay(5, SECONDS)
.ignoreExceptions()
.untilAsserted(() -> {
CRAssert.assertKeycloakRealmImportStatusCondition(crSelector.get(), DONE, true);
CRAssert.assertKeycloakRealmImportStatusCondition(crSelector.get(), STARTED, false);
CRAssert.assertKeycloakRealmImportStatusCondition(crSelector.get(), HAS_ERRORS, false);
});
assertThat(getJobArgs()).doesNotContain("build");
} }
@Test @Test