Fix Ingress creation/deletion loop
This commit is contained in:
parent
361a813d81
commit
a40acd413a
2 changed files with 104 additions and 15 deletions
|
@ -29,7 +29,7 @@ import java.util.Optional;
|
|||
|
||||
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) {
|
||||
|
@ -40,19 +40,21 @@ public class KeycloakIngress extends OperatorManagedResource implements StatusUp
|
|||
|
||||
@Override
|
||||
protected Optional<HasMetadata> getReconciledResource() {
|
||||
var defaultIngress = newIngress();
|
||||
if (keycloak.getSpec().isDisableDefaultIngress() && existingIngress != null) {
|
||||
client.network().v1().ingresses().inNamespace(getNamespace()).delete(existingIngress);
|
||||
return Optional.empty();
|
||||
} else if (existingIngress == null) {
|
||||
return Optional.of(defaultIngress);
|
||||
} else {
|
||||
if (existingIngress.getMetadata().getAnnotations() == null) {
|
||||
existingIngress.getMetadata().setAnnotations(new HashMap<>());
|
||||
if (keycloak.getSpec().isDisableDefaultIngress()) {
|
||||
if (existingIngress != null) {
|
||||
deleteExistingIngress();
|
||||
}
|
||||
existingIngress.getMetadata().getAnnotations().putAll(defaultIngress.getMetadata().getAnnotations());
|
||||
existingIngress.setSpec(defaultIngress.getSpec());
|
||||
return Optional.of(existingIngress);
|
||||
return Optional.empty();
|
||||
} else {
|
||||
var defaultIngress = newIngress();
|
||||
var resultIngress = (existingIngress != null) ? existingIngress : defaultIngress;
|
||||
|
||||
if (resultIngress.getMetadata().getAnnotations() == null) {
|
||||
resultIngress.getMetadata().setAnnotations(new HashMap<>());
|
||||
}
|
||||
resultIngress.getMetadata().getAnnotations().putAll(defaultIngress.getMetadata().getAnnotations());
|
||||
resultIngress.setSpec(defaultIngress.getSpec());
|
||||
return Optional.of(resultIngress);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,7 +103,11 @@ public class KeycloakIngress extends OperatorManagedResource implements StatusUp
|
|||
return ingress;
|
||||
}
|
||||
|
||||
private Ingress fetchExistingIngress() {
|
||||
protected void deleteExistingIngress() {
|
||||
client.network().v1().ingresses().inNamespace(getNamespace()).delete(existingIngress);
|
||||
}
|
||||
|
||||
protected Ingress fetchExistingIngress() {
|
||||
return client
|
||||
.network()
|
||||
.v1()
|
||||
|
@ -112,7 +118,7 @@ public class KeycloakIngress extends OperatorManagedResource implements StatusUp
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue