Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class KeycloakIngress extends OperatorManagedResource implements StatusUpdater<KeycloakStatusBuilder> {

private Ingress existingIngress;
private final Ingress existingIngress;
private final Keycloak keycloak;

public KeycloakIngress(KubernetesClient client, Keycloak keycloakCR) {
Expand All @@ -40,19 +40,21 @@ public KeycloakIngress(KubernetesClient client, Keycloak keycloakCR) {

@Override
protected Optional<HasMetadata> getReconciledResource() {
var defaultIngress = newIngress();
if (keycloak.getSpec().isDisableDefaultIngress() && existingIngress != null) {
client.network().v1().ingresses().inNamespace(getNamespace()).delete(existingIngress);
if (keycloak.getSpec().isDisableDefaultIngress()) {
if (existingIngress != null) {
deleteExistingIngress();
}
return Optional.empty();
} else if (existingIngress == null) {
return Optional.of(defaultIngress);
} else {
if (existingIngress.getMetadata().getAnnotations() == null) {
existingIngress.getMetadata().setAnnotations(new HashMap<>());
var defaultIngress = newIngress();
var resultIngress = (existingIngress != null) ? existingIngress : defaultIngress;

if (resultIngress.getMetadata().getAnnotations() == null) {
resultIngress.getMetadata().setAnnotations(new HashMap<>());
}
existingIngress.getMetadata().getAnnotations().putAll(defaultIngress.getMetadata().getAnnotations());
existingIngress.setSpec(defaultIngress.getSpec());
return Optional.of(existingIngress);
resultIngress.getMetadata().getAnnotations().putAll(defaultIngress.getMetadata().getAnnotations());
resultIngress.setSpec(defaultIngress.getSpec());
return Optional.of(resultIngress);
}
}

Expand Down Expand Up @@ -101,7 +103,11 @@ private Ingress newIngress() {
return ingress;
}

private Ingress fetchExistingIngress() {
protected void deleteExistingIngress() {
client.network().v1().ingresses().inNamespace(getNamespace()).delete(existingIngress);
}

protected Ingress fetchExistingIngress() {
return client
.network()
.v1()
Expand All @@ -112,7 +118,7 @@ private Ingress fetchExistingIngress() {
}

public void updateStatus(KeycloakStatusBuilder status) {
if (existingIngress == null) {
if (!keycloak.getSpec().isDisableDefaultIngress() && existingIngress == null) {
status.addNotReadyMessage("No existing Keycloak Ingress found, waiting for creating a new one");
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.keycloak.operator;

import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder;
import org.junit.jupiter.api.Test;
import org.keycloak.operator.controllers.KeycloakIngress;
import org.keycloak.operator.crds.v2alpha1.deployment.Keycloak;
import org.keycloak.operator.utils.K8sUtils;

import static org.junit.jupiter.api.Assertions.*;

public class IngressLogicTest {

static class MockKeycloakIngress extends KeycloakIngress {

private static Keycloak getKeycloak(boolean defaultIngressDisabled) {
var kc = K8sUtils.getDefaultKeycloakDeployment();
kc.getSpec().setDisableDefaultIngress(defaultIngressDisabled);
return kc;
}

public static MockKeycloakIngress build(boolean defaultIngressDisabled, boolean ingressExists) {
MockKeycloakIngress.ingressExists = ingressExists;
return new MockKeycloakIngress(defaultIngressDisabled);
}

public static boolean ingressExists = false;
private boolean deleted = false;
public MockKeycloakIngress(boolean defaultIngressDisabled) {
super(null, getKeycloak(defaultIngressDisabled));
}

public boolean reconciled() {
return getReconciledResource().isPresent();
}

public boolean deleted() {
return deleted;
}

@Override
protected Ingress fetchExistingIngress() {
if (ingressExists) {
return new IngressBuilder().withNewMetadata().endMetadata().build();
} else {
return null;
}
}

@Override
protected void deleteExistingIngress() {
deleted = true;
}
}

@Test
public void testIngressEnabledExisting() {
var kc = MockKeycloakIngress.build(true, true);
assertFalse(kc.reconciled());
assertTrue(kc.deleted());
}

@Test
public void testIngressEnabledNotExisting() {
var kc = MockKeycloakIngress.build(true, false);
assertFalse(kc.reconciled());
assertFalse(kc.deleted());
}

@Test
public void testIngressDisabledExisting() {
var kc = MockKeycloakIngress.build(false, true);
assertTrue(kc.reconciled());
assertFalse(kc.deleted());
}

@Test
public void testIngressDisabledNotExisting() {
var kc = MockKeycloakIngress.build(false, false);
assertTrue(kc.reconciled());
assertFalse(kc.deleted());
}
}