use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.
the class KubernetesAnnotationProcessor method generateDocker.
/**
* Create docker artifacts.
*
* @param deploymentModel Deployment model
* @param balxFilePath output ballerina file path
* @param outputDir output directory which data should be written
* @throws KubernetesPluginException If an error occurs while generating artifacts
*/
private void generateDocker(DeploymentModel deploymentModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
DockerModel dockerModel = new DockerModel();
String dockerImage = deploymentModel.getImage();
String imageTag = dockerImage.substring(dockerImage.lastIndexOf(":") + 1, dockerImage.length());
dockerModel.setBaseImage(deploymentModel.getBaseImage());
dockerModel.setName(dockerImage);
dockerModel.setTag(imageTag);
dockerModel.setEnableDebug(false);
dockerModel.setUsername(deploymentModel.getUsername());
dockerModel.setPassword(deploymentModel.getPassword());
dockerModel.setPush(deploymentModel.isPush());
dockerModel.setBalxFileName(KubernetesUtils.extractBalxName(balxFilePath) + BALX);
dockerModel.setPorts(deploymentModel.getPorts());
dockerModel.setService(true);
dockerModel.setDockerHost(deploymentModel.getDockerHost());
dockerModel.setDockerCertPath(deploymentModel.getDockerCertPath());
dockerModel.setBuildImage(deploymentModel.isBuildImage());
dockerModel.setCommandArg(deploymentModel.getCommandArgs());
DockerHandler dockerArtifactHandler = new DockerHandler(dockerModel);
String dockerContent = dockerArtifactHandler.generate();
try {
out.print("@kubernetes:Docker \t\t\t - complete 0/3 \r");
KubernetesUtils.writeToFile(dockerContent, outputDir + File.separator + "Dockerfile");
out.print("@kubernetes:Docker \t\t\t - complete 1/3 \r");
String balxDestination = outputDir + File.separator + KubernetesUtils.extractBalxName(balxFilePath) + BALX;
KubernetesUtils.copyFile(balxFilePath, balxDestination);
// check image build is enabled.
if (dockerModel.isBuildImage()) {
dockerArtifactHandler.buildImage(dockerModel, outputDir);
out.print("@kubernetes:Docker \t\t\t - complete 2/3 \r");
Files.delete(Paths.get(balxDestination));
// push only if image build is enabled.
if (dockerModel.isPush()) {
dockerArtifactHandler.pushImage(dockerModel);
}
out.print("@kubernetes:Docker \t\t\t - complete 3/3");
}
} catch (IOException e) {
throw new KubernetesPluginException("Unable to write Dockerfile content to " + outputDir);
} catch (InterruptedException e) {
throw new KubernetesPluginException("Unable to create docker images " + e.getMessage());
}
}
use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.
the class ServiceHandler method generate.
/**
* Generate kubernetes service definition from annotation.
*
* @return Generated kubernetes service yaml as a string
* @throws KubernetesPluginException If an error occurs while generating artifact.
*/
public String generate() throws KubernetesPluginException {
Service service = new ServiceBuilder().withNewMetadata().withName(serviceModel.getName()).addToLabels(serviceModel.getLabels()).endMetadata().withNewSpec().addNewPort().withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL).withPort(serviceModel.getPort()).withNewTargetPort(serviceModel.getPort()).endPort().addToSelector(KubernetesConstants.KUBERNETES_SELECTOR_KEY, serviceModel.getSelector()).withType(serviceModel.getServiceType()).endSpec().build();
String serviceYAML;
try {
serviceYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(service);
} catch (IOException e) {
String errorMessage = "Error while generating yaml file for service: " + serviceModel.getName();
throw new KubernetesPluginException(errorMessage, e);
}
return serviceYAML;
}
use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.
the class KubernetesUtils method copyFile.
/**
* Copy file from source to destination.
*
* @param source source file path
* @param destination destination file path
*/
public static void copyFile(String source, String destination) throws KubernetesPluginException {
File sourceFile = new File(source);
File destinationFile = new File(destination);
try (FileInputStream fileInputStream = new FileInputStream(sourceFile);
FileOutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
int bufferSize;
byte[] buffer = new byte[512];
while ((bufferSize = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bufferSize);
}
} catch (IOException e) {
throw new KubernetesPluginException("Error while copying file", e);
}
}
use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.
the class KubernetesUtils method resolveValue.
/**
* Resolve variable value from environment variable if $env{} is used. Else return the value.
*
* @param variable variable value
* @return Resolved variable
*/
public static String resolveValue(String variable) throws KubernetesPluginException {
if (variable.contains("$env{")) {
// remove white spaces
variable = variable.replace(" ", "");
// extract variable name
final String envVariable = variable.substring(variable.lastIndexOf("$env{") + 5, variable.lastIndexOf("}"));
// resolve value
String value = Optional.ofNullable(System.getenv(envVariable)).orElseThrow(() -> new KubernetesPluginException("error resolving value: " + envVariable + " is not set in " + "the environment."));
// substitute value
return variable.replace("$env{" + envVariable + "}", value);
}
return variable;
}
use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException 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