Skip to content

Commit

Permalink
Update createInstance to read probe values
Browse files Browse the repository at this point in the history
  • Loading branch information
anuruddhal authored and isurulucky committed Jun 26, 2019
1 parent a894d6f commit 9962941
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public class CelleryConstants {
public static final String AUTO_SCALING_METRIC_RESOURCE_CPU = "cpu";
public static final String INSTANCE_NAME = "instanceName";

//Ballerina object name constants for probes
// Ballerina object name constants for probes
public static final String INGRESSES = "ingresses";
public static final String LABELS = "labels";
public static final String AUTO_SCALING = "autoscaling";
public static final String ENV_VARS = "envVars";
public static final String PROBES = "probes";
public static final String KIND = "kind";
public static final String LIVENESS = "liveness";
public static final String READINESS = "readiness";

// These should match the Ballerina object names of the Auto Scaling Metrics Objects
public static final String ENVOY_GATEWAY = "Envoy";
Expand Down
72 changes: 72 additions & 0 deletions components/lang/src/main/java/io/cellery/CelleryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import io.cellery.models.Component;
import io.cellery.models.OIDC;
import io.cellery.models.Web;
import io.fabric8.kubernetes.api.model.HTTPGetActionBuilder;
import io.fabric8.kubernetes.api.model.HTTPHeader;
import io.fabric8.kubernetes.api.model.HTTPHeaderBuilder;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.ballerinalang.model.values.BInteger;
Expand All @@ -39,13 +44,19 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.IntStream;

import static io.cellery.CelleryConstants.DEFAULT_PARAMETER_VALUE;
import static io.cellery.CelleryConstants.KIND;
import static io.cellery.CelleryConstants.LIVENESS;
import static io.cellery.CelleryConstants.READINESS;
import static io.cellery.CelleryConstants.RESOURCES;
import static io.cellery.CelleryConstants.TARGET;

Expand Down Expand Up @@ -131,6 +142,67 @@ public static API getApi(Component component, LinkedHashMap attributeMap) {
return httpAPI;
}

/**
* Extract the Readiness Probe & Liveness Probe.
*
* @param probes Scale policy to be processed
* @param component current component
*/
public static void processProbes(LinkedHashMap<?, ?> probes, Component component) {
if (probes.containsKey(LIVENESS)) {
LinkedHashMap livenessConf = ((BMap) probes.get(LIVENESS)).getMap();
component.setLivenessProbe(getProbe(livenessConf));
}
if (probes.containsKey(READINESS)) {
LinkedHashMap readinessConf = ((BMap) probes.get(READINESS)).getMap();
component.setReadinessProbe(getProbe(readinessConf));
}
}

/**
* Create ProbeBuilder with given Liveness/Readiness Probe config.
*
* @param probeConf probeConfig map
* @return ProbeBuilder
*/
public static Probe getProbe(LinkedHashMap probeConf) {
ProbeBuilder probeBuilder = new ProbeBuilder();
final BMap probeKindMap = (BMap) probeConf.get(KIND);
LinkedHashMap probeKindConf = probeKindMap.getMap();
String probeKind = probeKindMap.getType().getName();
if ("TcpSocket".equals(probeKind)) {
probeBuilder.withNewTcpSocket()
.withNewPort((int) ((BInteger) probeKindConf.get("port")).intValue())
.endTcpSocket();
} else if ("HttpGet".equals(probeKind)) {
List<HTTPHeader> headers = new ArrayList<>();
((BMap<?, ?>) probeKindConf.get("httpHeaders")).getMap().forEach((key, value) -> {
HTTPHeader header = new HTTPHeaderBuilder()
.withName(key.toString())
.withValue(value.stringValue())
.build();
headers.add(header);
});
probeBuilder.withHttpGet(new HTTPGetActionBuilder()
.withNewPort((int) ((BInteger) probeKindConf.get("port")).intValue())
.withPath(((BString) probeKindConf.get("path")).stringValue())
.withHttpHeaders(headers)
.build()
);
} else {
final BValueArray commandList = (BValueArray) probeKindConf.get("commands");
String[] commands = Arrays.copyOfRange(commandList.getStringArray(), 0, (int) commandList.size());
probeBuilder.withNewExec().addToCommand(commands).endExec();
}
return probeBuilder
.withInitialDelaySeconds((int) (((BInteger) probeConf.get("initialDelaySeconds")).intValue()))
.withPeriodSeconds((int) (((BInteger) probeConf.get("periodSeconds")).intValue()))
.withFailureThreshold((int) (((BInteger) probeConf.get("failureThreshold")).intValue()))
.withTimeoutSeconds((int) (((BInteger) probeConf.get("timeoutSeconds")).intValue()))
.withSuccessThreshold((int) (((BInteger) probeConf.get("successThreshold")).intValue())).build();
}


/**
* Process envVars and add to component.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.HTTPGetActionBuilder;
import io.fabric8.kubernetes.api.model.HTTPHeader;
import io.fabric8.kubernetes.api.model.HTTPHeaderBuilder;
import io.fabric8.kubernetes.api.model.HorizontalPodAutoscalerSpecBuilder;
import io.fabric8.kubernetes.api.model.MetricSpecBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import org.apache.commons.lang3.StringUtils;
import org.ballerinalang.bre.Context;
import org.ballerinalang.bre.bvm.BLangVMErrors;
Expand All @@ -77,7 +73,6 @@
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -96,7 +91,6 @@
import static io.cellery.CelleryConstants.IMAGE_SOURCE;
import static io.cellery.CelleryConstants.INGRESSES;
import static io.cellery.CelleryConstants.INSTANCE_NAME_PLACEHOLDER;
import static io.cellery.CelleryConstants.KIND;
import static io.cellery.CelleryConstants.LABELS;
import static io.cellery.CelleryConstants.METADATA_FILE_NAME;
import static io.cellery.CelleryConstants.MICRO_GATEWAY;
Expand All @@ -112,6 +106,7 @@
import static io.cellery.CelleryUtils.getValidName;
import static io.cellery.CelleryUtils.printWarning;
import static io.cellery.CelleryUtils.processEnvVars;
import static io.cellery.CelleryUtils.processProbes;
import static io.cellery.CelleryUtils.processWebIngress;
import static io.cellery.CelleryUtils.toYaml;
import static io.cellery.CelleryUtils.writeToFile;
Expand Down Expand Up @@ -297,67 +292,6 @@ private void processHttpIngress(Component component, LinkedHashMap attributeMap)
component.addApi(httpAPI);
}

/**
* Extract the Readiness Probe & Liveness Probe.
*
* @param probes Scale policy to be processed
* @param component current component
*/
private void processProbes(LinkedHashMap<?, ?> probes, Component component) {
if (probes.containsKey("liveness")) {
LinkedHashMap livenessConf = ((BMap) probes.get("liveness")).getMap();
ProbeBuilder probeBuilder = getProbeBuilder(livenessConf);
component.setLivenessProbe(probeBuilder.build());
}
if (probes.containsKey("readiness")) {
LinkedHashMap readinessConf = ((BMap) probes.get("readiness")).getMap();
ProbeBuilder probeBuilder = getProbeBuilder(readinessConf);
component.setReadinessProbe(probeBuilder.build());
}
}

/**
* Create ProbeBuilder with given Liveness/Readiness Probe config.
*
* @param probeConf probeConfig map
* @return ProbeBuilder
*/
private ProbeBuilder getProbeBuilder(LinkedHashMap probeConf) {
ProbeBuilder probeBuilder = new ProbeBuilder();
final BMap probeKindMap = (BMap) probeConf.get(KIND);
LinkedHashMap probeKindConf = probeKindMap.getMap();
String probeKind = probeKindMap.getType().getName();
if ("TcpSocket".equals(probeKind)) {
probeBuilder.withNewTcpSocket()
.withNewPort((int) ((BInteger) probeKindConf.get("port")).intValue())
.endTcpSocket();
} else if ("HttpGet".equals(probeKind)) {
List<HTTPHeader> headers = new ArrayList<>();
((BMap<?, ?>) probeKindConf.get("httpHeaders")).getMap().forEach((key, value) -> {
HTTPHeader header = new HTTPHeaderBuilder()
.withName(key.toString())
.withValue(value.stringValue())
.build();
headers.add(header);
});
probeBuilder.withHttpGet(new HTTPGetActionBuilder()
.withNewPort((int) ((BInteger) probeKindConf.get("port")).intValue())
.withPath(((BString) probeKindConf.get("path")).value())
.withHttpHeaders(headers)
.build()
);
} else {
final BValueArray commandList = (BValueArray) probeKindConf.get("commands");
String[] commands = Arrays.copyOfRange(commandList.getStringArray(), 0, (int) commandList.size());
probeBuilder.withNewExec().addToCommand(commands).endExec();
}
return probeBuilder
.withInitialDelaySeconds((int) (((BInteger) probeConf.get("initialDelaySeconds")).intValue()))
.withPeriodSeconds((int) (((BInteger) probeConf.get("periodSeconds")).intValue()))
.withFailureThreshold((int) (((BInteger) probeConf.get("failureThreshold")).intValue()))
.withTimeoutSeconds((int) (((BInteger) probeConf.get("timeoutSeconds")).intValue()))
.withSuccessThreshold((int) (((BInteger) probeConf.get("successThreshold")).intValue()));
}

/**
* Extract the scale policy.
Expand Down
18 changes: 12 additions & 6 deletions components/lang/src/main/java/io/cellery/impl/CreateInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@

import static io.cellery.CelleryConstants.ANNOTATION_CELL_IMAGE_DEPENDENCIES;
import static io.cellery.CelleryConstants.CELLERY_IMAGE_DIR_ENV_VAR;
import static io.cellery.CelleryConstants.ENV_VARS;
import static io.cellery.CelleryConstants.INGRESSES;
import static io.cellery.CelleryConstants.INSTANCE_NAME;
import static io.cellery.CelleryConstants.INSTANCE_NAME_PLACEHOLDER;
import static io.cellery.CelleryConstants.PROBES;
import static io.cellery.CelleryConstants.YAML;
import static io.cellery.CelleryUtils.printWarning;
import static io.cellery.CelleryUtils.processEnvVars;
import static io.cellery.CelleryUtils.processProbes;
import static io.cellery.CelleryUtils.processWebIngress;
import static io.cellery.CelleryUtils.toYaml;
import static io.cellery.CelleryUtils.writeToFile;
Expand Down Expand Up @@ -236,12 +240,15 @@ private void processComponents(BMap<?, ?> components) {
// Set mandatory fields.
component.setName(((BString) attributeMap.get("name")).stringValue());

//Process Optional fields
if (attributeMap.containsKey("ingresses")) {
processIngress(((BMap<?, ?>) attributeMap.get("ingresses")).getMap(), component);
//Process modifiable fields
if (attributeMap.containsKey(PROBES)) {
processProbes(((BMap<?, ?>) attributeMap.get(PROBES)).getMap(), component);
}
if (attributeMap.containsKey("envVars")) {
processEnvVars(((BMap<?, ?>) attributeMap.get("envVars")).getMap(), component);
if (attributeMap.containsKey(INGRESSES)) {
processIngress(((BMap<?, ?>) attributeMap.get(INGRESSES)).getMap(), component);
}
if (attributeMap.containsKey(ENV_VARS)) {
processEnvVars(((BMap<?, ?>) attributeMap.get(ENV_VARS)).getMap(), component);
}
cellImage.addComponent(component);
});
Expand All @@ -263,7 +270,6 @@ private void processIngress(LinkedHashMap<?, ?> ingressMap, Component component)
});
}


/**
* Removes yaml tags.
*
Expand Down
24 changes: 17 additions & 7 deletions test-cases/employee-portal/cellery/employee/employee.bal
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import ballerina/config;
import ballerina/io;
import ballerina/log;
import celleryio/cellery;


public function build(cellery:ImageName iName) returns error? {
int salaryContainerPort = 8080;

Expand Down Expand Up @@ -35,26 +32,39 @@ public function build(cellery:ImageName iName) returns error? {
};

// Employee Component
int empPort = 8080;
cellery:Component employeeComponent = {
name: "employee",
source: {
image: "wso2cellery/sampleapp-employee:0.3.0"
},
ingresses: {
employee: <cellery:HttpApiIngress>{
port: 8080,
port:empPort,
context: "employee",
expose: "local",
definition: <cellery:ApiDefinition>cellery:readSwaggerFile("./resources/employee.swagger.json")
}
},
probes: {
liveness: {
initialDelaySeconds: 30,
kind: <cellery:TcpSocket>{
port:empPort
}
},
readiness: {
initialDelaySeconds: 10,
timeoutSeconds: 50,
kind: <cellery:Exec>{
commands: ["bin", "bash", "-c"]
}
}
},
envVars: {
SALARY_HOST: {
value: cellery:getHost(salaryComponent)
}
},
labels: {
team: "HR"
}
};

Expand Down
Loading

0 comments on commit 9962941

Please sign in to comment.