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: operator disables Ingress when spec.(component).host is empty #5305

Merged
merged 4 commits into from
Oct 7, 2024
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
@@ -1,6 +1,7 @@
package io.apicurio.registry.operator;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.resource.ActivationConditions.UIIngressActivationCondition;
import io.apicurio.registry.operator.resource.LabelDiscriminators.AppDeploymentDiscriminator;
import io.apicurio.registry.operator.resource.app.AppDeploymentResource;
import io.apicurio.registry.operator.resource.app.AppIngressResource;
Expand All @@ -14,6 +15,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.apicurio.registry.operator.resource.ActivationConditions.*;
import static io.apicurio.registry.operator.resource.ResourceKey.*;

// spotless:off
Expand All @@ -32,13 +34,13 @@
@Dependent(
type = AppIngressResource.class,
name = APP_INGRESS_ID,
dependsOn = {APP_SERVICE_ID}
dependsOn = {APP_SERVICE_ID},
activationCondition = AppIngressActivationCondition.class
),
// UI
@Dependent(
type = UIDeploymentResource.class,
name = UI_DEPLOYMENT_ID,
dependsOn = {APP_INGRESS_ID}
name = UI_DEPLOYMENT_ID
),
@Dependent(
type = UIServiceResource.class,
Expand All @@ -48,7 +50,8 @@
@Dependent(
type = UIIngressResource.class,
name = UI_INGRESS_ID,
dependsOn = {UI_SERVICE_ID}
dependsOn = {UI_SERVICE_ID},
activationCondition = UIIngressActivationCondition.class
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.apicurio.registry.operator.resource;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.resource.app.AppIngressResource;
import io.apicurio.registry.operator.resource.ui.UIIngressResource;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

import static io.apicurio.registry.operator.util.Util.isBlank;

public class ActivationConditions {

private ActivationConditions() {
}

public static class AppIngressActivationCondition implements Condition<Ingress, ApicurioRegistry3> {

@Override
public boolean isMet(DependentResource<Ingress, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var disabled = isBlank(primary.getSpec().getApp().getHost());
if (disabled) {
((AppIngressResource) resource).delete(primary, context);
}
return !disabled;
}
}

public static class UIIngressActivationCondition implements Condition<Ingress, ApicurioRegistry3> {

@Override
public boolean isMet(DependentResource<Ingress, ApicurioRegistry3> resource,
ApicurioRegistry3 primary, Context<ApicurioRegistry3> context) {

var disabled = isBlank(primary.getSpec().getUi().getHost());
if (disabled) {
((UIIngressResource) resource).delete(primary, context);
}
return !disabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ private void addSelectorLabels(Map<String, String> labels, ApicurioRegistry3 pri
// spotless:on
}

private <T extends HasMetadata> T deserialize(String path, Class<T> klass) {
public static <T extends HasMetadata> T deserialize(String path, Class<T> klass) {
try {
return YAML_MAPPER.readValue(load(path), klass);
} catch (JsonProcessingException ex) {
throw new OperatorException("Could not deserialize default resource: " + path, ex);
}
}

private String load(String path) {
private static String load(String path) {
try (var stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) {
return new String(stream.readAllBytes(), Charset.defaultCharset());
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.ArrayList;

import static io.apicurio.registry.operator.Mapper.toYAML;
import static io.apicurio.registry.operator.resource.LabelDiscriminators.*;
import static io.apicurio.registry.operator.resource.LabelDiscriminators.UIDeploymentDiscriminator;
import static io.apicurio.registry.operator.resource.ResourceFactory.COMPONENT_UI;
import static io.apicurio.registry.operator.resource.ResourceKey.*;
import static io.apicurio.registry.operator.util.IngressUtil.withIngressRule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ private IngressUtil() {
public static String getHost(String component, ApicurioRegistry3 p) {
String host = null;
if (COMPONENT_APP.equals(component)) {
if (!isBlank(p.getSpec().getAppHost())) {
host = p.getSpec().getAppHost();
if (!isBlank(p.getSpec().getApp().getHost())) {
host = p.getSpec().getApp().getHost();
}
} else if (COMPONENT_UI.equals(component)) {
if (!isBlank(p.getSpec().getUiHost())) {
host = p.getSpec().getUiHost();
if (!isBlank(p.getSpec().getUi().getHost())) {
host = p.getSpec().getUi().getHost();
}
} else {
throw new OperatorException("Unexpected value: " + component);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry3
metadata:
name: example-apicurioregistry3
name: simple
spec: {}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,16 @@ private static void createGeneratedResources() throws Exception {
resources.getItems().stream().forEach(r -> {
if (r.getKind().equals("ClusterRoleBinding") && r instanceof ClusterRoleBinding) {
var crb = (ClusterRoleBinding) r;
crb.getSubjects().stream().forEach(s -> s.setNamespace(getNamespace()));
crb.getSubjects().stream().forEach(s -> s.setNamespace(namespace));
// TODO: We need to patch the generated resources, because the referenced ClusterRole name
// is
// wrong.
if ("apicurioregistry3reconciler-cluster-role-binding"
.equals(crb.getMetadata().getName())) {
crb.getRoleRef().setName("apicurioregistry3reconciler-cluster-role");
}
}
client.resource(r).inNamespace(getNamespace()).createOrReplace();
client.resource(r).inNamespace(namespace).createOrReplace();
});
}
}
Expand All @@ -122,9 +129,9 @@ private static void cleanGeneratedResources() throws Exception {
resources.getItems().stream().forEach(r -> {
if (r.getKind().equals("ClusterRoleBinding") && r instanceof ClusterRoleBinding) {
var crb = (ClusterRoleBinding) r;
crb.getSubjects().stream().forEach(s -> s.setNamespace(getNamespace()));
crb.getSubjects().stream().forEach(s -> s.setNamespace(namespace));
}
client.resource(r).inNamespace(getNamespace()).delete();
client.resource(r).inNamespace(namespace).delete();
});
}
}
Expand Down Expand Up @@ -206,9 +213,4 @@ public static void after() throws Exception {
}
client.close();
}

public static String getNamespace() {
return namespace;
}

}
Loading
Loading