Search in sources :

Example 1 with ProbeModel

use of org.ballerinax.kubernetes.models.ProbeModel in project kubernetes by ballerinax.

the class KubernetesHPAGeneratorTests method testHPAGenerate.

@Test
public void testHPAGenerate() {
    DeploymentModel deploymentModel = new DeploymentModel();
    deploymentModel.setName(deploymentName);
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, selector);
    deploymentModel.addPort(9090);
    deploymentModel.addPort(9091);
    deploymentModel.addPort(9092);
    deploymentModel.setLabels(labels);
    ProbeModel probeModel = new ProbeModel();
    probeModel.setPort(9090);
    deploymentModel.setLivenessProbe(probeModel);
    deploymentModel.setSingleYAML(false);
    Map<String, EnvVarValueModel> env = new HashMap<>();
    EnvVarValueModel testEnvVar = new EnvVarValueModel("ENV");
    env.put("ENV_VAR", testEnvVar);
    deploymentModel.setEnv(env);
    PodAutoscalerModel podAutoscalerModel = new PodAutoscalerModel();
    podAutoscalerModel.setName(hpaName);
    podAutoscalerModel.setCpuPercentage(cpuPercentage);
    podAutoscalerModel.setMaxReplicas(maxReplicas);
    podAutoscalerModel.setMinReplicas(minReplicas);
    podAutoscalerModel.setDeployment(deploymentName);
    podAutoscalerModel.setLabels(labels);
    deploymentModel.setPodAutoscalerModel(podAutoscalerModel);
    dataHolder.setPodAutoscalerModel(podAutoscalerModel);
    dataHolder.setDeploymentModel(deploymentModel);
    try {
        new HPAHandler().createArtifacts();
        File tempFile = dataHolder.getK8sArtifactOutputPath().resolve("hello_hpa.yaml").toFile();
        Assert.assertTrue(tempFile.exists());
        assertGeneratedYAML(tempFile);
        tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service");
    }
}
Also used : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) HashMap(java.util.HashMap) ProbeModel(org.ballerinax.kubernetes.models.ProbeModel) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with ProbeModel

use of org.ballerinax.kubernetes.models.ProbeModel in project kubernetes by ballerinax.

the class DeploymentAnnotationProcessor method parseProbeConfiguration.

/**
 * Parse probe configuration from a record.
 *
 * @param probeValue Probe configuration record.
 * @return Parse probe model.
 * @throws KubernetesPluginException When an unknown field is found.
 */
private ProbeModel parseProbeConfiguration(BLangExpression probeValue) throws KubernetesPluginException {
    if ((probeValue instanceof BLangSimpleVarRef || probeValue instanceof BLangLiteral) && getBooleanValue(probeValue)) {
        return new ProbeModel();
    } else {
        if (probeValue instanceof BLangRecordLiteral) {
            List<BLangRecordLiteral.BLangRecordKeyValueField> buildExtensionRecord = convertRecordFields(((BLangRecordLiteral) probeValue).getFields());
            ProbeModel probeModel = new ProbeModel();
            for (BLangRecordLiteral.BLangRecordKeyValueField probeField : buildExtensionRecord) {
                ProbeConfiguration probeConfiguration = ProbeConfiguration.valueOf(probeField.getKey().toString());
                switch(probeConfiguration) {
                    case port:
                        probeModel.setPort(getIntValue(probeField.getValue()));
                        break;
                    case initialDelaySeconds:
                        probeModel.setInitialDelaySeconds(getIntValue(probeField.getValue()));
                        break;
                    case periodSeconds:
                        probeModel.setPeriodSeconds(getIntValue(probeField.getValue()));
                        break;
                    default:
                        throw new KubernetesPluginException("unknown probe field found: " + probeField.getKey().toString());
                }
            }
            return probeModel;
        }
    }
    return null;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ProbeModel(org.ballerinax.kubernetes.models.ProbeModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 3 with ProbeModel

use of org.ballerinax.kubernetes.models.ProbeModel in project kubernetes by ballerinax.

the class KubernetesDeploymentGeneratorTests method testDeploymentGeneration.

@Test
public void testDeploymentGeneration() {
    DeploymentModel deploymentModel = new DeploymentModel();
    deploymentModel.setName(deploymentName);
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, selector);
    deploymentModel.addPort(9090);
    deploymentModel.addPort(9091);
    deploymentModel.addPort(9092);
    deploymentModel.setLabels(labels);
    deploymentModel.setImage(imageName);
    deploymentModel.setImagePullPolicy(imagePullPolicy);
    ProbeModel probeModel = new ProbeModel();
    probeModel.setPort(9090);
    deploymentModel.setLivenessProbe(probeModel);
    deploymentModel.setSingleYAML(false);
    Map<String, EnvVarValueModel> env = new HashMap<>();
    EnvVarValueModel testEnvVar = new EnvVarValueModel("ENV");
    env.put("ENV_VAR", testEnvVar);
    deploymentModel.setEnv(env);
    deploymentModel.setReplicas(replicas);
    dataHolder.setDeploymentModel(deploymentModel);
    try {
        new DeploymentHandler().createArtifacts();
        File tempFile = dataHolder.getK8sArtifactOutputPath().resolve("hello_deployment.yaml").toFile();
        Assert.assertTrue(tempFile.exists());
        testGeneratedYAML(tempFile);
        tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service");
    }
}
Also used : DeploymentModel(org.ballerinax.kubernetes.models.DeploymentModel) HashMap(java.util.HashMap) ProbeModel(org.ballerinax.kubernetes.models.ProbeModel) EnvVarValueModel(org.ballerinax.kubernetes.models.EnvVarValueModel) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)3 ProbeModel (org.ballerinax.kubernetes.models.ProbeModel)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)2 EnvVarValueModel (org.ballerinax.kubernetes.models.EnvVarValueModel)2 Test (org.testng.annotations.Test)2 PodAutoscalerModel (org.ballerinax.kubernetes.models.PodAutoscalerModel)1 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1