-
Notifications
You must be signed in to change notification settings - Fork 27
[WIP] [Operator] Add Route #2418
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
Draft
jankalinic
wants to merge
7
commits into
streamshub:main
Choose a base branch
from
jankalinic:route-ingress
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ac7ea5
[WIP] operator route
jankalinic 624845b
[WIP] operator route - fix
jankalinic 847c001
[WIP] operator route - fix2
jankalinic 60e099f
[WIP] operator route - fix2
jankalinic 9475f76
[WIP] operator route-working dev
jankalinic 3324ae5
add rbac for routes and custom hostname
jankalinic 767879d
throw error for no hostname on ingress and remove nativeAPI routes.op…
jankalinic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
operator/src/main/java/com/github/streamshub/console/dependents/ConsoleRoute.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package com.github.streamshub.console.dependents; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.inject.Inject; | ||
| import com.github.streamshub.console.api.v1alpha1.Console; | ||
| import io.fabric8.openshift.api.model.Route; | ||
| import io.fabric8.openshift.api.model.RouteBuilder; | ||
| import io.javaoperatorsdk.operator.api.config.informer.Informer; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; | ||
| import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource; | ||
| import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent; | ||
| import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| @ApplicationScoped | ||
| @KubernetesDependent(informer = @Informer(labelSelector = ConsoleResource.MANAGEMENT_SELECTOR)) | ||
| public class ConsoleRoute extends CRUDKubernetesDependentResource<Route, Console> | ||
| implements ConsoleResource<Route> { | ||
|
|
||
| public static final String NAME = "console-route"; | ||
|
|
||
| @Inject | ||
| ConsoleService service; | ||
|
|
||
| public ConsoleRoute() { | ||
| super(Route.class); | ||
| } | ||
|
|
||
| @Override | ||
| public String resourceName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Route> getSecondaryResource(Console primary, Context<Console> context) { | ||
| return ConsoleResource.super.getSecondaryResource(primary, context); | ||
| } | ||
|
|
||
| @Override | ||
| protected Route desired(Console primary, Context<Console> context) { | ||
| // hostname is optional on openshift — when null, the router auto-assigns one from the route | ||
| // name, namespace, and cluster domain (issue #1471) | ||
| String host = primary.getSpec().getHostname(); | ||
| String serviceName = service.instanceName(primary); | ||
|
|
||
| // Only set INGRESS_URL_KEY now, if we already know the hostname | ||
| // when missing, RouteReadyCondition sets it once the router has set | ||
| // route.status.ingress[0].host so that ConsoleDeployment gets the right NEXTAUTH_URL | ||
| if (host != null) { | ||
| setAttribute(context, INGRESS_URL_KEY, "https://" + host); | ||
| } | ||
|
|
||
| return new RouteBuilder() | ||
| .withNewMetadata() | ||
| .withName(instanceName(primary)) | ||
| .withNamespace(primary.getMetadata().getNamespace()) | ||
| .withLabels(commonLabels("console")) | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| // null is valid — OpenShift assigns the host automatically | ||
| .withHost(host) | ||
| .withNewTo() | ||
| .withKind("Service") | ||
| .withName(serviceName) | ||
| .withWeight(100) | ||
| .endTo() | ||
| .withNewTls() | ||
| .withTermination("edge") | ||
| .withInsecureEdgeTerminationPolicy("Redirect") | ||
| .endTls() | ||
| .endSpec() | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Used as BOTH {@code reconcilePrecondition} AND {@code activationCondition} | ||
| * in the workflow so that: | ||
| * <ul> | ||
| * <li>No Route is reconciled on plain Kubernetes clusters.</li> | ||
| * <li>No informer/watch for {@link Route} is registered on clusters that | ||
| * lack the Route API, avoiding API-discovery errors on startup.</li> | ||
| * </ul> | ||
| * Note: not a CDI bean — conditions are instantiated by the operator SDK. | ||
| */ | ||
| public static class Precondition implements Condition<Route, Console> { | ||
jankalinic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public boolean isMet(DependentResource<Route, Console> dependentResource, Console primary, Context<Console> context) { | ||
| return context.getClient().supports(Route.class); | ||
| } | ||
| } | ||
| } | ||
104 changes: 104 additions & 0 deletions
104
...ava/com/github/streamshub/console/dependents/conditions/IngressOrRouteReadyCondition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.github.streamshub.console.dependents.conditions; | ||
|
|
||
| import com.github.streamshub.console.api.v1alpha1.Console; | ||
| import com.github.streamshub.console.dependents.ConsoleIngress; | ||
| import com.github.streamshub.console.dependents.ConsoleResource; | ||
| import com.github.streamshub.console.dependents.ConsoleRoute; | ||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
| import io.fabric8.kubernetes.api.model.networking.v1.Ingress; | ||
| import io.fabric8.kubernetes.api.model.networking.v1.IngressLoadBalancerStatus; | ||
| import io.fabric8.kubernetes.api.model.networking.v1.IngressStatus; | ||
| import io.fabric8.openshift.api.model.Route; | ||
| import io.fabric8.openshift.api.model.RouteIngress; | ||
| import io.fabric8.openshift.api.model.RouteStatus; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; | ||
| import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedWorkflowAndDependentResourceContext; | ||
| import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition; | ||
| import org.jboss.logging.Logger; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| public class IngressOrRouteReadyCondition implements Condition<Deployment, Console> { | ||
|
|
||
| private static final Logger LOGGER = Logger.getLogger(IngressOrRouteReadyCondition.class); | ||
|
|
||
| @Override | ||
| public boolean isMet(DependentResource<Deployment, Console> dependentResource, Console primary, Context<Console> context) { | ||
| if (context.getClient().supports(Route.class)) { | ||
| return getSecondaryResource(primary, context, ConsoleRoute.NAME, Route.class) | ||
| .map(route -> isRouteReady(route, context)) | ||
| .orElse(false); | ||
| } else { | ||
| return getSecondaryResource(primary, context, ConsoleIngress.NAME, Ingress.class) | ||
| .map(this::isIngressReady) | ||
| .orElse(false); | ||
| } | ||
| } | ||
|
|
||
| private <R extends HasMetadata> Optional<R> getSecondaryResource(Console primary, Context<Console> context, String resourceName, Class<R> type) { | ||
| String instanceName = primary.getMetadata().getName() + "-" + resourceName; | ||
| return context.getSecondaryResourcesAsStream(type) | ||
| .filter(r -> Objects.equals(instanceName, r.getMetadata().getName())) | ||
| .findFirst(); | ||
| } | ||
|
|
||
| // Route | ||
| private boolean isRouteReady(Route route, Context<Console> context) { | ||
| String routeName = route.getMetadata().getName(); | ||
|
|
||
| Optional<RouteIngress> admittedIngress = Optional.ofNullable(route.getStatus()) | ||
| .map(RouteStatus::getIngress) | ||
| .filter(list -> !list.isEmpty()) | ||
| .flatMap(ingresses -> ingresses.stream() | ||
| .filter(this::isRouteAdmitted) | ||
| .findFirst()); | ||
|
|
||
| boolean ready = admittedIngress.isPresent(); | ||
|
|
||
| if (ready) { | ||
| // When the user did not specify hostname in the Console spec, openshift auto-assigns it | ||
| // Set INGRESS_URL_KEY so ConsoleDeployment can use it for NEXTAUTH_URL | ||
| admittedIngress | ||
| .map(RouteIngress::getHost) | ||
| .filter(host -> !host.isBlank()) | ||
| .ifPresent(host -> { | ||
| ManagedWorkflowAndDependentResourceContext ctx = context.managedWorkflowAndDependentResourceContext(); | ||
| if (ctx.get(ConsoleResource.INGRESS_URL_KEY, String.class).isEmpty()) { | ||
| LOGGER.debugf("Route %s: auto-assigned host %s", routeName, host); | ||
| ctx.put(ConsoleResource.INGRESS_URL_KEY, "https://" + host); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| LOGGER.debugf("Route %s ready: %s", routeName, ready); | ||
| return ready; | ||
| } | ||
|
|
||
| private boolean isRouteAdmitted(RouteIngress ingress) { | ||
| return Optional.ofNullable(ingress.getConditions()) | ||
| .map(conditions -> conditions.stream() | ||
| .anyMatch(condition -> | ||
| "Admitted".equals(condition.getType()) && | ||
| "True".equals(condition.getStatus()) | ||
| ) | ||
| ) | ||
| .orElse(false); | ||
| } | ||
|
|
||
| // Ingress | ||
| private boolean isIngressReady(Ingress ingress) { | ||
| Boolean ready = Optional.ofNullable(ingress.getStatus()) | ||
| .map(IngressStatus::getLoadBalancer) | ||
| .map(IngressLoadBalancerStatus::getIngress) | ||
| .map(Collection::isEmpty) | ||
| .map(Boolean.FALSE::equals) | ||
| .orElse(false); | ||
|
|
||
| LOGGER.debugf("Ingress %s ready: %s", ingress.getMetadata().getName(), ready); | ||
| return ready; | ||
| } | ||
| } |
37 changes: 0 additions & 37 deletions
37
.../main/java/com/github/streamshub/console/dependents/conditions/IngressReadyCondition.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.