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