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: initial status support #1

Draft
wants to merge 1 commit 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
29 changes: 25 additions & 4 deletions src/main/java/io/halkyon/ExposedAppReconciler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -21,6 +24,8 @@ public class ExposedAppReconciler implements Reconciler<ExposedApp> {
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;
Expand Down Expand Up @@ -72,7 +77,7 @@ public UpdateControl<ExposedApp> 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()
Expand All @@ -91,10 +96,26 @@ public UpdateControl<ExposedApp> 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<String, String> labels) {
final var metadata = resource.getMetadata();
return new ObjectMetaBuilder()
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/io/halkyon/ExposedAppStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}