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