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

Use an IngressController per cloud availability zone #450

Merged
merged 3 commits into from
Aug 23, 2021
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
@@ -0,0 +1,54 @@
package org.bf2.operator.resources.v1alpha1;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.sundr.builder.annotations.Buildable;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@Buildable(
builderPackage = "io.fabric8.kubernetes.api.builder",
editableEnabled = false
)
@ToString
@EqualsAndHashCode
@JsonInclude(value = JsonInclude.Include.NON_NULL)
grdryn marked this conversation as resolved.
Show resolved Hide resolved
public class ManagedKafkaRoute {

private String name;
private String prefix;
private String router;
grdryn marked this conversation as resolved.
Show resolved Hide resolved

public ManagedKafkaRoute(String name, String prefix, String router) {
this.name = name;
this.prefix = prefix;
this.router = router;
}

public ManagedKafkaRoute() {
this(null, null, null);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getRouter() {
return router;
}

public void setRouter(String router) {
this.router = router;
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class ManagedKafkaStatus {

private List<ManagedKafkaCondition> conditions;
private List<ManagedKafkaRoute> routes;
private ManagedKafkaCapacity capacity;
private Versions versions;
private String adminServerURI;
Expand All @@ -32,6 +33,14 @@ public void setConditions(List<ManagedKafkaCondition> conditions) {
this.conditions = conditions;
}

public List<ManagedKafkaRoute> getRoutes() {
return routes;
}

public void setRoutes(List<ManagedKafkaRoute> routes) {
this.routes = routes;
}

public ManagedKafkaCapacity getCapacity() {
return capacity;
}
Expand Down
5 changes: 4 additions & 1 deletion common/src/main/java/org/bf2/common/OperandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

public class OperandUtils {

public static final String K8S_NAME_LABEL = "app.kubernetes.io/name";
public static final String MANAGED_BY_LABEL = "app.kubernetes.io/managed-by";
public static final String STRIMZI_OPERATOR_NAME = "strimzi-cluster-operator";
public static final String FLEETSHARD_OPERATOR_NAME = "kas-fleetshard-operator";

/**
Expand All @@ -33,7 +36,7 @@ public static void setAsOwner(HasMetadata owner, HasMetadata resource) {

public static Map<String, String> getDefaultLabels() {
LinkedHashMap<String, String> result = new LinkedHashMap<>(1);
result.put("app.kubernetes.io/managed-by", FLEETSHARD_OPERATOR_NAME);
result.put(MANAGED_BY_LABEL, FLEETSHARD_OPERATOR_NAME);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
import org.bf2.common.ConditionUtils;
import org.bf2.common.ManagedKafkaResourceClient;
import org.bf2.operator.events.ResourceEventSource;
import org.bf2.operator.managers.IngressControllerManager;
import org.bf2.operator.operands.KafkaInstance;
import org.bf2.operator.operands.OperandReadiness;
import org.bf2.operator.resources.v1alpha1.ManagedKafka;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacityBuilder;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Reason;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Status;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus;
import org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder;
import org.bf2.operator.resources.v1alpha1.VersionsBuilder;
import org.jboss.logging.Logger;
import org.jboss.logging.NDC;

import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import java.util.ArrayList;
Expand All @@ -43,6 +46,9 @@ public class ManagedKafkaController implements ResourceController<ManagedKafka>
@Inject
KafkaInstance kafkaInstance;

@Inject
Instance<IngressControllerManager> ingressControllerManagerInstance;
grdryn marked this conversation as resolved.
Show resolved Hide resolved

@Override
@Timed(value = "controller.delete", extraTags = {"resource", "ManagedKafka"}, description = "Time spent processing delete events")
@Counted(value = "controller.delete", extraTags = {"resource", "ManagedKafka"}, description = "The number of delete events")
Expand Down Expand Up @@ -131,6 +137,9 @@ private void updateManagedKafkaStatus(ManagedKafka managedKafka) {

ConditionUtils.updateConditionStatus(ready, readiness.getStatus(), readiness.getReason(), readiness.getMessage());

// routes should always be set on the CR status, even if it's just an empty list
status.setRoutes(List.of());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never used it that way, I always prefer more explicit Collections.emptyList() ... but no need to change, just saying ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why I used List.of() rather than Collections.emptyList(). Since I'm not really a Java person, I must have seen someone else doing it. I guess maybe I preferred it because it's slightly shorter & might avoid adding an additional import to say the same thing. Those are questionable benifits though, if Collections.emptyList() is considered more explicit.


if (Status.True.equals(readiness.getStatus())) {
status.setCapacity(new ManagedKafkaCapacityBuilder(managedKafka.getSpec().getCapacity()).build());
if (!Reason.StrimziUpdating.equals(readiness.getReason())) {
Expand All @@ -140,6 +149,12 @@ private void updateManagedKafkaStatus(ManagedKafka managedKafka) {
}
status.setAdminServerURI(kafkaInstance.getAdminServer().uri(managedKafka));
status.setServiceAccounts(managedKafka.getSpec().getServiceAccounts());

if (ingressControllerManagerInstance.isResolvable()) {
ppatierno marked this conversation as resolved.
Show resolved Hide resolved
IngressControllerManager ingressControllerManager = ingressControllerManagerInstance.get();
List<ManagedKafkaRoute> routes = ingressControllerManager.getManagedKafkaRoutesFor(managedKafka);
status.setRoutes(routes);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.inject.Inject;

import java.util.List;
import java.util.stream.Stream;

@Startup
@ApplicationScoped
Expand Down Expand Up @@ -59,14 +60,14 @@ boolean isOpenShift() {
protected void onStart() {
deploymentInformer = resourceInformerFactory.create(Deployment.class, filter(kubernetesClient.apps().deployments()), eventSource);

serviceInformer = resourceInformerFactory.create(Service.class, filter(kubernetesClient.services()), eventSource);
serviceInformer = resourceInformerFactory.create(Service.class, filterManagedByFleetshardOrStrimzi(kubernetesClient.services()), eventSource);

configMapInformer = resourceInformerFactory.create(ConfigMap.class, filter(kubernetesClient.configMaps()), eventSource);

secretInformer = resourceInformerFactory.create(Secret.class, filter(kubernetesClient.secrets()), eventSource);

if (isOpenShift()) {
routeInformer = resourceInformerFactory.create(Route.class, filter(kubernetesClient.adapt(OpenShiftClient.class).routes()), eventSource);
routeInformer = resourceInformerFactory.create(Route.class, filterManagedByFleetshardOrStrimzi(kubernetesClient.adapt(OpenShiftClient.class).routes()), eventSource);
}
}

Expand All @@ -75,6 +76,11 @@ protected void onStart() {
return mixedOperation.inAnyNamespace().withLabels(OperandUtils.getDefaultLabels());
}

static <T extends HasMetadata> FilterWatchListDeletable<T, ? extends KubernetesResourceList<T>> filterManagedByFleetshardOrStrimzi(
MixedOperation<T, ? extends KubernetesResourceList<T>, ?> mixedOperation) {
return mixedOperation.inAnyNamespace().withLabelIn(OperandUtils.MANAGED_BY_LABEL, OperandUtils.FLEETSHARD_OPERATOR_NAME, OperandUtils.STRIMZI_OPERATOR_NAME);
}

public Kafka getLocalKafka(String namespace, String name) {
return kafkaInformer != null ? kafkaInformer.getByKey(Cache.namespaceKeyFunc(namespace, name)) : null;
}
Expand Down Expand Up @@ -104,6 +110,16 @@ public Route getLocalRoute(String namespace, String name) {
}
}

protected Stream<Route> getRoutesInNamespace(String namespace) {
if (isOpenShift()) {
return routeInformer.getList().stream()
.filter(r -> r.getMetadata().getNamespace().equals(namespace));
} else {
log.warn("Not running on OpenShift cluster, Routes are not available");
return Stream.empty();
}
}

/**
* Create the Kafka informer
* NOTE: it's called when a Strimzi bundle is installed and Kafka related CRDs are available to be listed/watched
Expand Down
Loading