Search in sources :

Example 6 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class PersistentVolumeClaimHandler method generate.

@Override
public String generate() throws KubernetesPluginException {
    Quantity quantity = new QuantityBuilder().withAmount(volumeClaimModel.getVolumeClaimSize()).build();
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", quantity);
    PersistentVolumeClaim secret = new PersistentVolumeClaimBuilder().withNewMetadata().withName(volumeClaimModel.getName()).endMetadata().withNewSpec().withAccessModes(volumeClaimModel.getAccessMode()).withNewResources().withRequests(requests).endResources().endSpec().build();
    try {
        return SerializationUtils.dumpWithoutRuntimeStateAsYaml(secret);
    } catch (JsonProcessingException e) {
        String errorMessage = "Error while parsing yaml file for volume claim: " + volumeClaimModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : QuantityBuilder(io.fabric8.kubernetes.api.model.QuantityBuilder) HashMap(java.util.HashMap) Quantity(io.fabric8.kubernetes.api.model.Quantity) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesPlugin method process.

@Override
public void process(EndpointNode endpointNode, List<AnnotationAttachmentNode> annotations) {
    String endpointName = endpointNode.getName().getValue();
    ServiceModel serviceModel = null;
    setCanProcess(true);
    for (AnnotationAttachmentNode attachmentNode : annotations) {
        String annotationKey = attachmentNode.getAnnotationName().getValue();
        try {
            switch(annotationKey) {
                case "Service":
                    serviceModel = kubernetesAnnotationProcessor.processServiceAnnotation(endpointName, attachmentNode);
                    kubernetesDataHolder.addServiceModel(endpointName, serviceModel);
                    break;
                case "Deployment":
                    kubernetesDataHolder.setDeploymentModel(kubernetesAnnotationProcessor.processDeployment(attachmentNode));
                    break;
                default:
                    break;
            }
        } catch (KubernetesPluginException e) {
            dlog.logDiagnostic(Diagnostic.Kind.ERROR, endpointNode.getPosition(), e.getMessage());
        }
    }
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangEndpoint) endpointNode).configurationExpr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        String key = keyValue.getKey().toString();
        switch(key) {
            case "port":
                int port = Integer.parseInt(keyValue.getValue().toString());
                kubernetesDataHolder.addPort(port);
                if (serviceModel != null) {
                    serviceModel.setPort(port);
                }
                break;
            case "secureSocket":
                List<BLangRecordLiteral.BLangRecordKeyValue> sslKeyValues = ((BLangRecordLiteral) keyValue.valueExpr).getKeyValuePairs();
                try {
                    Set<SecretModel> secretModels = kubernetesAnnotationProcessor.processSecureSocketAnnotation(endpointName, sslKeyValues);
                    kubernetesDataHolder.addEndpointSecret(endpointName, secretModels);
                    kubernetesDataHolder.addSecrets(secretModels);
                } catch (KubernetesPluginException e) {
                    dlog.logDiagnostic(Diagnostic.Kind.ERROR, null, e.getMessage());
                }
                break;
            default:
                break;
        }
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 8 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesPlugin method process.

@Override
public void process(ServiceNode serviceNode, List<AnnotationAttachmentNode> annotations) {
    setCanProcess(true);
    Set<String> endpoints = extractEndpointName(serviceNode);
    for (AnnotationAttachmentNode attachmentNode : annotations) {
        String annotationKey = attachmentNode.getAnnotationName().getValue();
        try {
            switch(annotationKey) {
                case "Ingress":
                    kubernetesDataHolder.addIngressModel(kubernetesAnnotationProcessor.processIngressAnnotation(serviceNode.getName().getValue(), attachmentNode), endpoints);
                    break;
                case "HPA":
                    kubernetesDataHolder.setPodAutoscalerModel(kubernetesAnnotationProcessor.processPodAutoscalerAnnotation(attachmentNode));
                    break;
                case "Deployment":
                    kubernetesDataHolder.setDeploymentModel(kubernetesAnnotationProcessor.processDeployment(attachmentNode));
                    break;
                case "Secret":
                    kubernetesDataHolder.addSecrets(kubernetesAnnotationProcessor.processSecrets(attachmentNode));
                    break;
                case "ConfigMap":
                    try {
                        kubernetesDataHolder.addConfigMaps(kubernetesAnnotationProcessor.processConfigMap(attachmentNode));
                    } catch (KubernetesPluginException e) {
                        dlog.logDiagnostic(Diagnostic.Kind.ERROR, serviceNode.getPosition(), e.getMessage());
                    }
                    break;
                case "PersistentVolumeClaim":
                    try {
                        kubernetesDataHolder.addPersistentVolumeClaims(kubernetesAnnotationProcessor.processPersistentVolumeClaim(attachmentNode));
                    } catch (KubernetesPluginException e) {
                        dlog.logDiagnostic(Diagnostic.Kind.ERROR, serviceNode.getPosition(), e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        } catch (KubernetesPluginException e) {
            dlog.logDiagnostic(Diagnostic.Kind.ERROR, serviceNode.getPosition(), e.getMessage());
        }
    }
}
Also used : KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 9 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class IngressHandler method generate.

/**
 * Generate kubernetes ingress definition from annotation.
 *
 * @return Generated kubernetes {@link Ingress} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
public String generate() throws KubernetesPluginException {
    // generate ingress backend
    IngressBackend ingressBackend = new IngressBackendBuilder().withServiceName(ingressModel.getServiceName()).withNewServicePort(ingressModel.getServicePort()).build();
    // generate ingress path
    HTTPIngressPath ingressPath = new HTTPIngressPathBuilder().withBackend(ingressBackend).withPath(ingressModel.getPath()).build();
    // generate TLS
    IngressTLS ingressTLS;
    if (ingressModel.isEnableTLS()) {
        ingressTLS = new IngressTLSBuilder().withHosts(ingressModel.getHostname()).build();
    } else {
        ingressTLS = new IngressTLSBuilder().build();
    }
    // generate annotationMap
    Map<String, String> annotationMap = new HashMap<>();
    annotationMap.put("kubernetes.io/ingress.class", ingressModel.getIngressClass());
    annotationMap.put("nginx.ingress.kubernetes.io/ssl-passthrough", String.valueOf(ingressModel.isEnableTLS()));
    if (ingressModel.getTargetPath() != null) {
        annotationMap.put("nginx.ingress.kubernetes.io/rewrite-target", ingressModel.getTargetPath());
    }
    // generate ingress
    Ingress ingress = new IngressBuilder().withNewMetadata().withName(ingressModel.getName()).addToLabels(ingressModel.getLabels()).addToAnnotations(annotationMap).endMetadata().withNewSpec().withTls(ingressTLS).addNewRule().withHost(ingressModel.getHostname()).withNewHttp().withPaths(ingressPath).endHttp().endRule().endSpec().build();
    String ingressYAML;
    try {
        ingressYAML = SerializationUtils.dumpWithoutRuntimeStateAsYaml(ingress);
    } catch (JsonProcessingException e) {
        String errorMessage = "Error while generating yaml file for ingress: " + ingressModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
    return ingressYAML;
}
Also used : IngressBackendBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder) HashMap(java.util.HashMap) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) HTTPIngressPath(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath) IngressBuilder(io.fabric8.kubernetes.api.model.extensions.IngressBuilder) HTTPIngressPathBuilder(io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder) IngressTLSBuilder(io.fabric8.kubernetes.api.model.extensions.IngressTLSBuilder) IngressTLS(io.fabric8.kubernetes.api.model.extensions.IngressTLS) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IngressBackend(io.fabric8.kubernetes.api.model.extensions.IngressBackend)

Example 10 with KubernetesPluginException

use of org.ballerinax.kubernetes.exceptions.KubernetesPluginException in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method generateService.

private void generateService(ServiceModel serviceModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
    String balxFileName = KubernetesUtils.extractBalxName(balxFilePath);
    serviceModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
    serviceModel.setSelector(balxFileName);
    String serviceContent = new ServiceHandler(serviceModel).generate();
    try {
        KubernetesUtils.writeToFile(serviceContent, outputDir + File.separator + balxFileName + SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        throw new KubernetesPluginException("Error while writing service content", e);
    }
}
Also used : ServiceHandler(org.ballerinax.kubernetes.handlers.ServiceHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

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