Search in sources :

Example 1 with DeploymentHandler

use of org.ballerinax.kubernetes.handlers.DeploymentHandler in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method generateDeployment.

private void generateDeployment(DeploymentModel deploymentModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
    String balxFileName = KubernetesUtils.extractBalxName(balxFilePath);
    if (deploymentModel.getName() == null) {
        deploymentModel.setName(getValidName(balxFileName) + DEPLOYMENT_POSTFIX);
    }
    if (deploymentModel.getImage() == null) {
        deploymentModel.setImage(balxFileName + DOCKER_LATEST_TAG);
    }
    deploymentModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
    if ("enable".equals(deploymentModel.getEnableLiveness()) && deploymentModel.getLivenessPort() == 0) {
        // set first port as liveness port
        deploymentModel.setLivenessPort(deploymentModel.getPorts().iterator().next());
    }
    String deploymentContent = new DeploymentHandler(deploymentModel).generate();
    try {
        KubernetesUtils.writeToFile(deploymentContent, outputDir + File.separator + balxFileName + DEPLOYMENT_FILE_POSTFIX + YAML);
        // generate dockerfile and docker image
        generateDocker(deploymentModel, balxFilePath, outputDir + File.separator + DOCKER);
        // generate HPA
        generatePodAutoscaler(deploymentModel, balxFilePath, outputDir);
    } catch (IOException e) {
        throw new KubernetesPluginException("Error while writing deployment content", e);
    }
}
Also used : DeploymentHandler(org.ballerinax.kubernetes.handlers.DeploymentHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 2 with DeploymentHandler

use of org.ballerinax.kubernetes.handlers.DeploymentHandler in project kubernetes by ballerinax.

the class KubernetesDeploymentGeneratorTests method testServiceGenerate.

@Test
public void testServiceGenerate() {
    DeploymentModel deploymentModel = new DeploymentModel();
    deploymentModel.setName("MyDeployment");
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "TestAPP");
    Set<Integer> ports = new HashSet<>();
    ports.add(9090);
    ports.add(9091);
    ports.add(9092);
    deploymentModel.setLabels(labels);
    deploymentModel.setImage("SampleImage:v1.0.0");
    deploymentModel.setImagePullPolicy("Always");
    deploymentModel.setReplicas(3);
    deploymentModel.setPorts(ports);
    try {
        String deploymentYAML = new DeploymentHandler(deploymentModel).generate();
        Assert.assertNotNull(deploymentYAML);
        File artifactLocation = new File("target/kubernetes");
        artifactLocation.mkdir();
        File tempFile = File.createTempFile("temp", deploymentModel.getName() + ".yaml", artifactLocation);
        KubernetesUtils.writeToFile(deploymentYAML, tempFile.getPath());
        log.info("Generated YAML: \n" + deploymentYAML);
        Assert.assertTrue(tempFile.exists());
        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) DeploymentHandler(org.ballerinax.kubernetes.handlers.DeploymentHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 DeploymentHandler (org.ballerinax.kubernetes.handlers.DeploymentHandler)2 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)1 Test (org.junit.Test)1