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; }
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; }
switch (desc.type) { case DEPLOYMENT_TYPE: Deployment deployment = desc.getKubernetesEntity(Deployment.class); if (deployment.spec == null || deployment.spec.template == null) { return desc; .getKubernetesEntity(ReplicationController.class); if (controller.spec == null || controller.spec.template == null) { return desc; ReplicaSet replicaSet = desc.getKubernetesEntity(ReplicaSet.class); if (replicaSet.spec == null || replicaSet.spec.template == null) { return desc;
@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)); }
@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)); }
@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)); }
@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)); }
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 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)); }