diff --git a/src/main/java/io/halkyon/ExposedAppReconciler.java b/src/main/java/io/halkyon/ExposedAppReconciler.java index 03ca1f1..b9ec2a9 100644 --- a/src/main/java/io/halkyon/ExposedAppReconciler.java +++ b/src/main/java/io/halkyon/ExposedAppReconciler.java @@ -1,9 +1,11 @@ package io.halkyon; +import io.fabric8.kubernetes.api.model.LoadBalancerIngress; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.fabric8.kubernetes.api.model.ServiceBuilder; import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; +import io.fabric8.kubernetes.api.model.networking.v1.Ingress; import io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder; import io.fabric8.kubernetes.client.KubernetesClient; import io.javaoperatorsdk.operator.api.reconciler.Constants; @@ -12,6 +14,7 @@ import io.javaoperatorsdk.operator.api.reconciler.Reconciler; import io.javaoperatorsdk.operator.api.reconciler.UpdateControl; import java.util.Map; +import java.util.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,6 +24,8 @@ public class ExposedAppReconciler implements Reconciler { static final Logger log = LoggerFactory.getLogger(ExposedAppReconciler.class); static final String APP_LABEL = "app.kubernetes.io/name"; private final KubernetesClient client; + + private static final ExposedAppStatus DEFAULT_STATUS = new ExposedAppStatus("processing", null); public ExposedAppReconciler(KubernetesClient client) { this.client = client; @@ -72,7 +77,7 @@ public UpdateControl reconcile(ExposedApp exposedApp, Context contex "nginx.ingress.kubernetes.io/rewrite-target", "/", "kubernetes.io/ingress.class", "nginx" )); - client.network().v1().ingresses().createOrReplace(new IngressBuilder() + final var ingress = client.network().v1().ingresses().createOrReplace(new IngressBuilder() .withMetadata(metadata) .withNewSpec() .addNewRule() @@ -91,10 +96,26 @@ public UpdateControl reconcile(ExposedApp exposedApp, Context contex .endRule() .endSpec() .build()); - - return UpdateControl.noUpdate(); - } + // add status to resource + final var maybeStatus = ingress.getStatus(); + final var status = Optional.ofNullable(maybeStatus).map(s -> { + var result = DEFAULT_STATUS; + final var ingresses = s.getLoadBalancer().getIngress(); + if (ingresses != null && !ingresses.isEmpty()) { + // only set the status if the ingress is ready to provide the info we need + LoadBalancerIngress ing = ingresses.get(0); + String hostname = ing.getHostname(); + final var url = "https://" + (hostname != null ? hostname : ing.getIp()); + log.info("App {} is exposed and ready to used at {}", name, url); + result = new ExposedAppStatus("exposed", url); + } + return result; + }).orElse(DEFAULT_STATUS); + + exposedApp.setStatus(status); + return UpdateControl.updateStatus(exposedApp); + } private ObjectMeta createMetadata(ExposedApp resource, Map labels) { final var metadata = resource.getMetadata(); return new ObjectMetaBuilder() diff --git a/src/main/java/io/halkyon/ExposedAppStatus.java b/src/main/java/io/halkyon/ExposedAppStatus.java index 03d59ae..d7a68dc 100644 --- a/src/main/java/io/halkyon/ExposedAppStatus.java +++ b/src/main/java/io/halkyon/ExposedAppStatus.java @@ -2,5 +2,30 @@ public class ExposedAppStatus { - // Add Status information here + private String host; + private String message; + + public ExposedAppStatus() { + } + + public ExposedAppStatus(String message, String hostname) { + this.message = message; + this.host = hostname; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } }