Skip to content

Commit

Permalink
chore: enable new rebalacing features in customizer
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Dec 21, 2020
1 parent 44fb7ff commit 7b0b574
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,75 @@
import org.apache.camel.CamelContext;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.kubernetes.cluster.KubernetesClusterService;
import org.apache.camel.component.kubernetes.cluster.LeaseResourceType;
import org.apache.camel.k.ContextCustomizer;
import org.apache.camel.k.annotation.Customizer;
import org.apache.camel.support.cluster.RebalancingCamelClusterService;
import org.apache.camel.util.ObjectHelper;

@Customizer("master")
public class MasterContextCustomizer implements ContextCustomizer {
@Deprecated
private String configMapName;
private String kubernetesResourceName;
private LeaseResourceType leaseResourceType;
private Boolean rebalancing;
private String labelKey;
private String labelValue;

@Override
public void apply(CamelContext camelContext) {
try {
KubernetesClusterService clusterService = new KubernetesClusterService();
if (ObjectHelper.isNotEmpty(configMapName)) {
clusterService.setConfigMapName(this.configMapName);
String resourceName = this.kubernetesResourceName;
if (ObjectHelper.isEmpty(resourceName)) {
resourceName = this.configMapName;
}
if (ObjectHelper.isNotEmpty(resourceName)) {
clusterService.setKubernetesResourceName(resourceName);
}
if (ObjectHelper.isNotEmpty(this.labelKey) && ObjectHelper.isNotEmpty(this.labelValue)) {
clusterService.setClusterLabels(Collections.singletonMap(this.labelKey, this.labelValue));
}
camelContext.addService(clusterService);
if (this.leaseResourceType != null) {
clusterService.setLeaseResourceType(this.leaseResourceType);
}

if (this.rebalancing == null || this.rebalancing) {
RebalancingCamelClusterService rebalancingService = new RebalancingCamelClusterService(clusterService, clusterService.getRenewDeadlineMillis());
camelContext.addService(rebalancingService);
} else {
camelContext.addService(clusterService);
}
} catch (Exception ex) {
throw new RuntimeCamelException(ex);
}
}

public String getKubernetesResourceName() {
return kubernetesResourceName;
}

public void setKubernetesResourceName(String kubernetesResourceName) {
this.kubernetesResourceName = kubernetesResourceName;
}

public LeaseResourceType getLeaseResourceType() {
return leaseResourceType;
}

public void setLeaseResourceType(LeaseResourceType leaseResourceType) {
this.leaseResourceType = leaseResourceType;
}

public Boolean getRebalancing() {
return rebalancing;
}

public void setRebalancing(Boolean rebalancing) {
this.rebalancing = rebalancing;
}

public String getConfigMapName() {
return configMapName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.camel.CamelContext;
import org.apache.camel.component.kubernetes.cluster.KubernetesClusterService;
import org.apache.camel.support.cluster.RebalancingCamelClusterService;

@Path("/test")
@ApplicationScoped
Expand All @@ -38,11 +39,12 @@ public class Application {
@Path("/inspect")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject inspect() {
var service = context.hasService(KubernetesClusterService.class);
var rebalancingService = context.hasService(RebalancingCamelClusterService.class);
var service = (KubernetesClusterService) rebalancingService.getDelegate();

return Json.createObjectBuilder()
.add("cluster-service", service != null ? service.getClass().getName() : "")
.add("cluster-service-cm", service != null ? service.getConfigMapName() : "")
.add("cluster-service", service != null ? rebalancingService.getClass().getName() : "")
.add("cluster-service-res", service != null ? service.getKubernetesResourceName() : "")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ quarkus.camel.routes-discovery.enabled = false
# Camel K
#
camel.k.customizer.master.enabled = true
camel.k.customizer.master.config-map-name = camel-k-cm
camel.k.customizer.master.kubernetes-resource-name = camel-k-res
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import org.apache.camel.component.kubernetes.cluster.KubernetesClusterService;
import org.apache.camel.support.cluster.RebalancingCamelClusterService;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.when;
Expand All @@ -34,7 +34,7 @@ public void hasClusterService() {
.statusCode(200)
.extract().jsonPath();

assertThat(path.getString("cluster-service")).isEqualTo(KubernetesClusterService.class.getName());
assertThat(path.getString("cluster-service-cm")).isEqualTo("camel-k-cm");
assertThat(path.getString("cluster-service")).isEqualTo(RebalancingCamelClusterService.class.getName());
assertThat(path.getString("cluster-service-res")).isEqualTo("camel-k-res");
}
}

0 comments on commit 7b0b574

Please sign in to comment.