Search in sources :

Example 16 with KubernetesPluginException

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());
    }
}
Also used : DockerModel(org.ballerinax.kubernetes.models.DockerModel) DockerHandler(org.ballerinax.kubernetes.handlers.DockerHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 17 with KubernetesPluginException

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;
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder)

Example 18 with KubernetesPluginException

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);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 19 with KubernetesPluginException

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;
}
Also used : KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Example 20 with KubernetesPluginException

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

KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)20 IOException (java.io.IOException)13 HashMap (java.util.HashMap)6 File (java.io.File)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)2 DeploymentHandler (org.ballerinax.kubernetes.handlers.DeploymentHandler)2 IngressHandler (org.ballerinax.kubernetes.handlers.IngressHandler)2 ServiceHandler (org.ballerinax.kubernetes.handlers.ServiceHandler)2 DeploymentModel (org.ballerinax.kubernetes.models.DeploymentModel)2 IngressModel (org.ballerinax.kubernetes.models.IngressModel)2 SecretModel (org.ballerinax.kubernetes.models.SecretModel)2 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1