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);
}
}
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");
}
}
Aggregations