Search in sources :

Example 1 with ResourceQuotaModel

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

the class ResourceQuotaAnnotationPreprocessor method processResourceQuotaAnnotation.

private void processResourceQuotaAnnotation(BLangAnnotationAttachment attachmentNode) throws KubernetesPluginException {
    Set<ResourceQuotaModel> resourceQuotaModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) attachmentNode.expr).getFields());
    for (BLangRecordLiteral.BLangRecordKeyValueField keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangListConstructorExpr) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            ResourceQuotaModel resourceQuotaModel = new ResourceQuotaModel();
            List<BLangRecordLiteral.BLangRecordKeyValueField> annotationValues = convertRecordFields(((BLangRecordLiteral) bLangExpression).getFields());
            for (BLangRecordLiteral.BLangRecordKeyValueField annotation : annotationValues) {
                ResourceQuotaConfig resourceQuotaConfig = ResourceQuotaConfig.valueOf(annotation.getKey().toString());
                switch(resourceQuotaConfig) {
                    case name:
                        resourceQuotaModel.setName(getValidName(getStringValue(annotation.getValue())));
                        break;
                    case labels:
                        resourceQuotaModel.setLabels(getMap(annotation.getValue()));
                        break;
                    case annotations:
                        resourceQuotaModel.setAnnotations(getMap(annotation.getValue()));
                        break;
                    case hard:
                        resourceQuotaModel.setHard(getMap(annotation.getValue()));
                        break;
                    case scopes:
                        resourceQuotaModel.setScopes(new HashSet<>(getList(annotation.getValue())));
                        break;
                    default:
                        break;
                }
            }
            resourceQuotaModels.add(resourceQuotaModel);
        }
    }
    KubernetesContext.getInstance().getDataHolder().setResourceQuotaModels(resourceQuotaModels);
}
Also used : ResourceQuotaModel(org.ballerinax.kubernetes.models.ResourceQuotaModel) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 2 with ResourceQuotaModel

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

the class ResourceQuotaHandler method createArtifacts.

/**
 * {@inheritDoc}
 */
@Override
public void createArtifacts() throws KubernetesPluginException {
    int count = 0;
    Set<ResourceQuotaModel> resourceQuotas = dataHolder.getResourceQuotaModels();
    if (resourceQuotas.size() > 0) {
        OUT.println();
    }
    for (ResourceQuotaModel resourceQuotaModel : resourceQuotas) {
        count++;
        generate(resourceQuotaModel);
        OUT.print("\t@kubernetes:ResourceQuota \t\t - complete " + count + "/" + resourceQuotas.size() + "\r");
    }
}
Also used : ResourceQuotaModel(org.ballerinax.kubernetes.models.ResourceQuotaModel)

Example 3 with ResourceQuotaModel

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

the class KubernetesResourceQuotaGeneratorTests method testResourceQuota.

@Test
public void testResourceQuota() {
    ResourceQuotaModel resourceQuotaModel = new ResourceQuotaModel();
    resourceQuotaModel.setName("MyResourceQuota");
    Map<String, String> quotas = new LinkedHashMap<>();
    quotas.put("cpu", "1000");
    quotas.put("memory", "200Gi");
    quotas.put("pods", "10");
    resourceQuotaModel.setHard(quotas);
    Set<String> scopes = new LinkedHashSet<>();
    scopes.add("high");
    resourceQuotaModel.setScopes(scopes);
    Set<ResourceQuotaModel> resourceQuotaModels = new LinkedHashSet<>();
    resourceQuotaModels.add(resourceQuotaModel);
    dataHolder.setResourceQuotaModels(resourceQuotaModels);
    try {
        new ResourceQuotaHandler().createArtifacts();
        File yamlFile = dataHolder.getK8sArtifactOutputPath().resolve("hello" + RESOURCE_QUOTA_FILE_POSTFIX + YAML).toFile();
        Assert.assertTrue(yamlFile.exists(), "Generated file not found.");
        ResourceQuota resourceQuota = Utils.loadYaml(yamlFile);
        // metadata
        Assert.assertEquals("MyResourceQuota", resourceQuota.getMetadata().getName());
        // quotas
        Assert.assertEquals(3, resourceQuota.getSpec().getHard().size());
        Assert.assertEquals("1000", resourceQuota.getSpec().getHard().get("cpu").getAmount());
        Assert.assertEquals("200", resourceQuota.getSpec().getHard().get("memory").getAmount());
        Assert.assertEquals("Gi", resourceQuota.getSpec().getHard().get("memory").getFormat());
        Assert.assertEquals("10", resourceQuota.getSpec().getHard().get("pods").getAmount());
        // scopes
        Assert.assertEquals(1, resourceQuota.getSpec().getScopes().size());
        Assert.assertEquals("high", resourceQuota.getSpec().getScopes().get(0));
        yamlFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file: " + e.getMessage());
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service: " + e.getMessage());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResourceQuotaModel(org.ballerinax.kubernetes.models.ResourceQuotaModel) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

ResourceQuotaModel (org.ballerinax.kubernetes.models.ResourceQuotaModel)3 ResourceQuota (io.fabric8.kubernetes.api.model.ResourceQuota)1 File (java.io.File)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)1 Test (org.testng.annotations.Test)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangListConstructorExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1