Search in sources :

Example 1 with PodAutoscalerModel

use of org.ballerinax.kubernetes.models.PodAutoscalerModel in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPodAutoscalerAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
PodAutoscalerModel processPodAutoscalerAnnotation(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    PodAutoscalerModel podAutoscalerModel = new PodAutoscalerModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        PodAutoscalerConfiguration podAutoscalerConfiguration = PodAutoscalerConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(podAutoscalerConfiguration) {
            case name:
                podAutoscalerModel.setName(getValidName(annotationValue));
                break;
            case labels:
                podAutoscalerModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case cpuPercentage:
                podAutoscalerModel.setCpuPercentage(Integer.parseInt(annotationValue));
                break;
            case minReplicas:
                podAutoscalerModel.setMinReplicas(Integer.parseInt(annotationValue));
                break;
            case maxReplicas:
                podAutoscalerModel.setMaxReplicas(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    return podAutoscalerModel;
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel)

Example 2 with PodAutoscalerModel

use of org.ballerinax.kubernetes.models.PodAutoscalerModel in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method generatePodAutoscaler.

private void generatePodAutoscaler(DeploymentModel deploymentModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
    PodAutoscalerModel podAutoscalerModel = deploymentModel.getPodAutoscalerModel();
    if (podAutoscalerModel == null) {
        return;
    }
    String balxFileName = KubernetesUtils.extractBalxName(balxFilePath);
    podAutoscalerModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
    podAutoscalerModel.setDeployment(deploymentModel.getName());
    if (podAutoscalerModel.getMaxReplicas() == 0) {
        podAutoscalerModel.setMaxReplicas(deploymentModel.getReplicas() + 1);
    }
    if (podAutoscalerModel.getMinReplicas() == 0) {
        podAutoscalerModel.setMinReplicas(deploymentModel.getReplicas());
    }
    if (podAutoscalerModel.getName() == null || podAutoscalerModel.getName().length() == 0) {
        podAutoscalerModel.setName(getValidName(balxFileName) + HPA_POSTFIX);
    }
    String serviceContent = new HPAHandler(podAutoscalerModel).generate();
    try {
        out.println();
        KubernetesUtils.writeToFile(serviceContent, outputDir + File.separator + balxFileName + HPA_FILE_POSTFIX + YAML);
        out.print("@kubernetes:HPA \t\t\t - complete 1/1");
    } catch (IOException e) {
        throw new KubernetesPluginException("Error while writing HPA content", e);
    }
}
Also used : HPAHandler(org.ballerinax.kubernetes.handlers.HPAHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel)

Aggregations

PodAutoscalerModel (org.ballerinax.kubernetes.models.PodAutoscalerModel)2 IOException (java.io.IOException)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1 HPAHandler (org.ballerinax.kubernetes.handlers.HPAHandler)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1