use of io.fabric8.kubernetes.api.model.Quantity in project syndesis by syndesisio.
the class OpenShiftServiceImplTest method testDeploy.
@SuppressWarnings({ "PMD.ExcessiveMethodLength", "PMD.JUnitTestsShouldIncludeAssert" })
@Test
public void testDeploy() {
String name = "test-deployment";
OpenShiftConfigurationProperties config = new OpenShiftConfigurationProperties();
OpenShiftServiceImpl service = new OpenShiftServiceImpl(client, config);
DeploymentData deploymentData = new DeploymentData.Builder().addAnnotation("testName", testName.getMethodName()).addLabel("type", "test").addSecretEntry("secret-key", "secret-val").withImage("testimage").build();
ImageStream expectedImageStream = new ImageStreamBuilder().withNewMetadata().withName(name).endMetadata().build();
Secret expectedSecret = new SecretBuilder().withNewMetadata().withName(name).addToAnnotations(deploymentData.getAnnotations()).addToLabels(deploymentData.getLabels()).endMetadata().withStringData(deploymentData.getSecret()).build();
DeploymentConfig expectedDeploymentConfig = new DeploymentConfigBuilder().withNewMetadata().withName(OpenShiftServiceImpl.openshiftName(name)).addToAnnotations(deploymentData.getAnnotations()).addToLabels(deploymentData.getLabels()).endMetadata().withNewSpec().withReplicas(1).addToSelector("integration", name).withNewStrategy().withType("Recreate").withNewResources().addToLimits("memory", new Quantity(config.getDeploymentMemoryLimitMi() + "Mi")).addToRequests("memory", new Quantity(config.getDeploymentMemoryRequestMi() + "Mi")).endResources().endStrategy().withRevisionHistoryLimit(0).withNewTemplate().withNewMetadata().addToLabels("integration", name).addToLabels(OpenShiftServiceImpl.COMPONENT_LABEL, "integration").addToLabels(deploymentData.getLabels()).addToAnnotations(deploymentData.getAnnotations()).addToAnnotations("prometheus.io/scrape", "true").addToAnnotations("prometheus.io/port", "9779").endMetadata().withNewSpec().addNewContainer().withImage(deploymentData.getImage()).withImagePullPolicy("Always").withName(name).addToEnv(new EnvVarBuilder().withName("LOADER_HOME").withValue(config.getIntegrationDataPath()).build()).addToEnv(new EnvVarBuilder().withName("AB_JMX_EXPORTER_CONFIG").withValue("/tmp/src/prometheus-config.yml").build()).addNewPort().withName("jolokia").withContainerPort(8778).endPort().addNewVolumeMount().withName("secret-volume").withMountPath("/deployments/config").withReadOnly(false).endVolumeMount().endContainer().addNewVolume().withName("secret-volume").withNewSecret().withSecretName(expectedSecret.getMetadata().getName()).endSecret().endVolume().endSpec().endTemplate().addNewTrigger().withType("ConfigChange").endTrigger().endSpec().withNewStatus().withLatestVersion(1L).endStatus().build();
server.expect().withPath("/oapi/v1/namespaces/test/imagestreams").andReturn(200, expectedImageStream).always();
server.expect().withPath("/api/v1/namespaces/test/secrets").andReturn(200, expectedSecret).always();
server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs").andReturn(200, expectedDeploymentConfig).always();
server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs/i-test-deployment").andReturn(200, expectedDeploymentConfig).always();
server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs/test-deployment").andReturn(200, expectedDeploymentConfig).always();
service.deploy(name, deploymentData);
}
use of io.fabric8.kubernetes.api.model.Quantity in project syndesis-qe by syndesisio.
the class OperatorValidationSteps method checkMemoryLimits.
@Then("check correct memory limits")
public void checkMemoryLimits() {
final String file = "src/test/resources/operator/spec/components/resources.limits.requests.memory.cpu.yml";
Yaml yaml = new Yaml();
Object data = null;
try (FileInputStream fis = FileUtils.openInputStream(new File(file))) {
data = yaml.load(fis);
} catch (IOException e) {
fail("Unable to load file " + file, e);
}
SoftAssertions softAssertions = new SoftAssertions();
JSONObject components = new JSONObject((Map) data).getJSONObject("spec").getJSONObject("components");
components.keySet().forEach(component -> {
String expectedMemoryLimit = components.getJSONObject(component).getJSONObject("resources").getJSONObject("limit").getString("memory");
String expectedCpuLimit = components.getJSONObject(component).getJSONObject("resources").getJSONObject("limit").getString("cpu");
String expectedMemoryRequests = components.getJSONObject(component).getJSONObject("resources").getJSONObject("request").getString("memory");
String expectedCpuRequests = components.getJSONObject(component).getJSONObject("resources").getJSONObject("request").getString("cpu");
List<DeploymentConfig> dcList = OpenShiftUtils.getInstance().deploymentConfigs().withLabel("syndesis.io/component", "syndesis-" + ("database".equals(component) ? "db" : component)).list().getItems();
softAssertions.assertThat(dcList).hasSize(1);
final Quantity currentMemoryLimit = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits().get("memory");
softAssertions.assertThat(currentMemoryLimit).as(component + " memory limit is null").isNotNull();
if (currentMemoryLimit != null) {
softAssertions.assertThat(currentMemoryLimit.getAmount() + currentMemoryLimit.getFormat()).as(component + " memory limit").isEqualTo(expectedMemoryLimit);
}
final Quantity currentCpuLimit = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits().get("cpu");
softAssertions.assertThat(currentCpuLimit).as(component + " cpu limit is null").isNotNull();
if (currentCpuLimit != null) {
softAssertions.assertThat(currentCpuLimit.getAmount() + currentCpuLimit.getFormat()).as(component + " cpu limit").isEqualTo(expectedCpuLimit);
}
final Quantity currentMemoryRequests = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests().get("memory");
softAssertions.assertThat(currentMemoryRequests).as(component + " memory requests is null").isNotNull();
if (currentMemoryRequests != null) {
softAssertions.assertThat(currentMemoryRequests.getAmount() + currentMemoryRequests.getFormat()).as(component + " memory requests").isEqualTo(expectedMemoryRequests);
}
final Quantity currentCpuRequests = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests().get("cpu");
softAssertions.assertThat(currentCpuRequests).as(component + " cpu requests is null").isNotNull();
if (currentCpuRequests != null) {
softAssertions.assertThat(currentCpuRequests.getAmount() + currentCpuRequests.getFormat()).as(component + " cpu requests").isEqualTo(expectedCpuRequests);
}
});
softAssertions.assertAll();
}
use of io.fabric8.kubernetes.api.model.Quantity in project syndesis-qe by syndesisio.
the class OpenshiftValidationSteps method setResourceValues.
@When("set resources for deployment config {string}")
public void setResourceValues(String dcName, DataTable dataTable) {
DeploymentConfig dc = OpenShiftUtils.getInstance().getDeploymentConfig(dcName);
Map<String, Quantity> currentLimits = dc.getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits();
Map<String, Quantity> currentRequests = dc.getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests();
Map<String, Quantity> limits = currentLimits == null ? new HashMap<>() : new HashMap<>(currentLimits);
Map<String, Quantity> requests = currentRequests == null ? new HashMap<>() : new HashMap<>(currentRequests);
for (List<String> l : dataTable.asLists()) {
if ("limits".equals(l.get(0))) {
limits.put(l.get(1), new Quantity(l.get(2)));
} else {
requests.put(l.get(1), new Quantity(l.get(2)));
}
}
// @formatter:off
OpenShiftUtils.getInstance().deploymentConfigs().withName(dcName).edit().editSpec().editTemplate().editSpec().editFirstContainer().editResources().withLimits(limits).withRequests(requests).endResources().endContainer().endSpec().endTemplate().endSpec().done();
// @formatter:on
}
use of io.fabric8.kubernetes.api.model.Quantity in project camel by apache.
the class KubernetesPersistentVolumesClaimsProducerTest method createListAndDeletePersistentVolumeClaim.
@Test
public void createListAndDeletePersistentVolumeClaim() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:create", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, labels);
PersistentVolumeClaimSpec pvcSpec = new PersistentVolumeClaimSpec();
ResourceRequirements rr = new ResourceRequirements();
Map<String, Quantity> mp = new HashMap<String, Quantity>();
mp.put("storage", new Quantity("100"));
rr.setLimits(mp);
Map<String, Quantity> req = new HashMap<String, Quantity>();
req.put("storage", new Quantity("100"));
rr.setRequests(req);
pvcSpec.setResources(rr);
pvcSpec.setVolumeName("vol001");
List<String> access = new ArrayList<String>();
access.add("ReadWriteOnce");
pvcSpec.setAccessModes(access);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_SPEC, pvcSpec);
}
});
PersistentVolumeClaim pvc = ex.getOut().getBody(PersistentVolumeClaim.class);
assertEquals(pvc.getMetadata().getName(), "test");
ex = template.request("direct:listByLabels", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, labels);
}
});
List<PersistentVolumeClaim> result = ex.getOut().getBody(List.class);
boolean pvcExists = false;
Iterator<PersistentVolumeClaim> it = result.iterator();
while (it.hasNext()) {
PersistentVolumeClaim pvcLocal = it.next();
if ("test".equalsIgnoreCase(pvcLocal.getMetadata().getName())) {
pvcExists = true;
}
}
assertTrue(pvcExists);
ex = template.request("direct:delete", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, "test");
}
});
boolean pvcDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(pvcDeleted);
}
use of io.fabric8.kubernetes.api.model.Quantity in project camel by apache.
the class KubernetesResourcesQuotaProducerTest method createAndDeleteResourceQuota.
@Test
public void createAndDeleteResourceQuota() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:create", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_LABELS, labels);
ResourceQuotaSpec rsSpec = new ResourceQuotaSpec();
Map<String, Quantity> mp = new HashMap<String, Quantity>();
mp.put("pods", new Quantity("100"));
rsSpec.setHard(mp);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCE_QUOTA_SPEC, rsSpec);
}
});
ResourceQuota rs = ex.getOut().getBody(ResourceQuota.class);
assertEquals(rs.getMetadata().getName(), "test");
ex = template.request("direct:get", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, "test");
}
});
ResourceQuota rsGet = ex.getOut().getBody(ResourceQuota.class);
assertEquals(rsGet.getMetadata().getName(), "test");
assertEquals(rsGet.getSpec().getHard().get("pods"), new Quantity("100"));
ex = template.request("direct:delete", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, "test");
}
});
boolean rqDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(rqDeleted);
}
Aggregations