@Test public void testMapApplicationSuffix() throws IOException { String suffix = "generate-mcm-10"; String serviceYaml = serviceYamlFormat.replaceAll("_sufix", ""); String expetedMappedServiceYaml = serviceYamlFormat.replaceAll("_sufix", "-" + suffix); KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = serviceYaml; kd = KubernetesUtil.mapApplicationAffix(kd, suffix); assertEquals(expetedMappedServiceYaml, kd.kubernetesEntity); }
switch (desc.type) { case DEPLOYMENT_TYPE: Deployment deployment = desc.getKubernetesEntity(Deployment.class); if (deployment.spec == null || deployment.spec.template == null) { return desc; deployment.spec.template.metadata.labels.put(KUBERNETES_LABEL_APP_ID, compositeComponentId); desc.merge(deployment); return desc; .getKubernetesEntity(ReplicationController.class); if (controller.spec == null || controller.spec.template == null) { return desc; controller.spec.template.metadata.labels.put(KUBERNETES_LABEL_APP_ID, compositeComponentId); desc.merge(controller); return desc; ReplicaSet replicaSet = desc.getKubernetesEntity(ReplicaSet.class); if (replicaSet.spec == null || replicaSet.spec.template == null) { return desc; replicaSet.spec.template.metadata.labels.put(KUBERNETES_LABEL_APP_ID, compositeComponentId); desc.merge(replicaSet); return desc;
KubernetesDescription kubernetesDescription = new KubernetesDescription(); kubernetesDescription.kubernetesEntity = serializeKubernetesEntity; kubernetesDescription.type = KubernetesUtil.DEPLOYMENT_TYPE;
@Test public void testCreateKubernetesDescriptionWithInvalidYamlInputShouldFail() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = "invalid\nyaml\ninput";
@Test public void testCreateKubernetesDescription() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = sampleYamlDefinition; Operation op = Operation.createPost(UriUtils.buildUri(host, KubernetesDescriptionService .FACTORY_LINK)) .setBody(description) .setCompletion((o, ex) -> { if (ex != null) { host.log("Creating kubernetes description failed."); host.failIteration(ex); return; } else { KubernetesDescription desc = o.getBody(KubernetesDescription.class); try { assertEquals(description.kubernetesEntity, desc.kubernetesEntity); assertEquals("Service", desc.type); assertEquals(description.getKubernetesEntityAsJson(), desc.getKubernetesEntityAsJson()); } catch (Throwable e) { host.log(Utils.toString(e)); host.failIteration(e); } host.completeIteration(); } }); host.testStart(1); host.send(op); host.testWait(); }
@Test public void testSetApplicationLabelOnReplicationController() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = replicationControllerYaml; kd.type = KubernetesUtil.REPLICATION_CONTROLLER_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); ReplicationController replicationController = kd .getKubernetesEntity(ReplicationController.class); assertNotNull(replicationController); assertNotNull(replicationController.metadata); assertNotNull(replicationController.metadata.labels); assertEquals(testCompositeId, replicationController.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(replicationController.spec); assertNotNull(replicationController.spec.template); assertNotNull(replicationController.spec.template.metadata); assertNotNull(replicationController.spec.template.metadata.labels); assertEquals(testCompositeId, replicationController.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
public static KubernetesDescription mapApplicationAffix(KubernetesDescription desc, String affix) { BaseKubernetesObject object; try { object = desc.getKubernetesEntity(BaseKubernetesObject.class); } catch (IOException e) { Utils.logWarning("Could not get kubernetes entity, reason %s", Utils.toString(e)); return desc; } if (affix.startsWith(ResourceNamePrefixState.PREFIX_DELIMITER)) { object.metadata.name = object.metadata.name + affix; } else if (affix.endsWith(ResourceNamePrefixState.PREFIX_DELIMITER)) { object.metadata.name = affix + object.metadata.name; } else { object.metadata.name = object.metadata.name + ResourceNamePrefixState.PREFIX_DELIMITER + affix; } // TODO: consider label and selector desc.merge(object); return desc; }
@Test public void testSetApplicationLabelOnReplicaSet() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = replicaSetYaml; kd.type = KubernetesUtil.REPLICA_SET_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); ReplicaSet replicaSet = kd.getKubernetesEntity(ReplicaSet.class); assertNotNull(replicaSet); assertNotNull(replicaSet.metadata); assertNotNull(replicaSet.metadata.labels); assertEquals(testCompositeId, replicaSet.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(replicaSet.spec); assertNotNull(replicaSet.spec.template); assertNotNull(replicaSet.spec.template.metadata); assertNotNull(replicaSet.spec.template.metadata.labels); assertEquals(testCompositeId, replicaSet.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
public static KubernetesDescription setApplicationLabel(KubernetesDescription desc, String compositeComponentId) { BaseKubernetesObject object; try { object = desc.getKubernetesEntity(BaseKubernetesObject.class); } catch (IOException e) { Utils.logWarning("Could not get kubernetes entity, reason %s", Utils.toString(e)); return desc; } if (object.metadata == null) { object.metadata = new ObjectMeta(); } if (object.metadata.labels == null) { object.metadata.labels = new HashMap<>(); } object.metadata.labels.put(KUBERNETES_LABEL_APP_ID, compositeComponentId); desc.merge(object); desc = setApplicationLabelOnTemplate(desc, compositeComponentId); return desc; }
@Test public void testCreateKubernetesDescriptionWithInvalidKubernetesShouldFail() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = sampleYamlInvalidKubernetesDefinition; Operation op = Operation.createPost(UriUtils.buildUri(host, KubernetesDescriptionService .FACTORY_LINK)) .setBody(description) .setCompletion((o, ex) -> { if (ex != null) { host.log("Creating kubernetes description failed."); host.completeIteration(); return; } else { host.failIteration(new IllegalStateException("Creation of Kubernetes " + "Description with invalid yaml succeeded")); } }); host.testStart(1); host.send(op); host.testWait(); }
@Test public void testSetApplicationLabelOnDeployment() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = deploymentYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Deployment deployment = kd.getKubernetesEntity(Deployment.class); assertNotNull(deployment); assertNotNull(deployment.metadata); assertNotNull(deployment.metadata.labels); assertEquals(testCompositeId, deployment.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(deployment.spec); assertNotNull(deployment.spec.template); assertNotNull(deployment.spec.template.metadata); assertNotNull(deployment.spec.template.metadata.labels); assertEquals(testCompositeId, deployment.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
private void validateDescription(KubernetesDescription description) throws IOException { if (!isValidYaml(description.kubernetesEntity)) { throw new LocalizableValidationException("Invalid YAML input.", "compute.template.yaml.invalid"); } BaseKubernetesObject kubernetesEntity = description .getKubernetesEntity(BaseKubernetesObject.class); assertNotNullOrEmpty(kubernetesEntity.apiVersion, "apiVersion"); assertNotNullOrEmpty(kubernetesEntity.kind, "kind"); assertNotNull(kubernetesEntity.metadata, "metadata"); assertNotNullOrEmpty(kubernetesEntity.metadata.name, "metadata.name"); description.type = kubernetesEntity.kind; description.apiVersion = kubernetesEntity.apiVersion; description.name = kubernetesEntity.metadata.name; description.namespace = kubernetesEntity.metadata.namespace; }
@Test public void testSetApplicationLabelOnDeploymentWithoutSpec() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = deploymentWithoutSpecYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; // Make sure the is no NPE kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Deployment deployment = kd.getKubernetesEntity(Deployment.class); assertNotNull(deployment); assertNotNull(deployment.metadata); assertNotNull(deployment.metadata.labels); assertEquals(testCompositeId, deployment.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
@Test public void testSetApplicationLabelOnService() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = serviceYamlFormat; kd.type = SERVICE_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Service service = kd.getKubernetesEntity(Service.class); assertNotNull(service); assertNotNull(service.metadata); assertNotNull(service.metadata.labels); assertEquals(testCompositeId, service.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
public void createEntity(KubernetesDescription description, KubernetesContext context, CompletionHandler completionHandler) throws IOException { URI uri = ApiUtil.buildKubernetesFactoryUri(description, context); sendRequest(Action.POST, uri, description.getKubernetesEntityAsJson(), context, completionHandler); }
public static KubernetesDescription createKubernetesEntityDescription( BaseKubernetesState state) { KubernetesDescription entityDescription = new KubernetesDescription(); entityDescription.documentSelfLink = state.descriptionLink; entityDescription.documentDescription = state.documentDescription; entityDescription.tenantLinks = state.tenantLinks; entityDescription.name = state.name; entityDescription.id = state.id; entityDescription.type = state.getType(); entityDescription.customProperties = state.customProperties; return entityDescription; }
private static KubernetesDescription createKubernetesDescription(String apiVersionPrefix, String namespace, String entityKind) { KubernetesDescription description = new KubernetesDescription(); description.apiVersion = apiVersionPrefix; description.namespace = namespace; description.type = entityKind; return description; } }
private List<Operation> createOperations(List<String> kubernetesDefinitions) { List<Operation> ops = kubernetesDefinitions.stream() .map(yaml -> { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = yaml; return Operation.createPost(this, KubernetesDescriptionService.FACTORY_LINK) .setBody(description); }).collect(Collectors.toList()); return ops; }
@Test public void testSetApplicationLabelOnInvalidDeployment() { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = invalidDeploymentYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; // Make sure the is no NPE kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); }