forked from vmware-archive/admiral
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VBV-1919][Part1] Introduce converter service
- introduce a service for converting container descriptions to kubernetes descriptions Change-Id: Ic7073ff2f5f6be95cfdfd506cce6c643f52f47f9 Reviewed-on: https://bellevue-ci.eng.vmware.com:8080/32460 Upgrade-Verified: jenkins <[email protected]> Closures-Verified: jenkins <[email protected]> Bellevue-Verified: jenkins <[email protected]> CS-Verified: jenkins <[email protected]> Reviewed-by: Stanislav Hadjiiski <[email protected]>
- Loading branch information
Showing
5 changed files
with
171 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...mpute/kubernetes/service/ContainerDescriptionToKubernetesDescriptionConverterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
* | ||
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this product except in compliance with the License. | ||
* | ||
* This product may include a number of subcomponents with separate copyright notices | ||
* and license terms. Your use of these subcomponents is subject to the terms and | ||
* conditions of the subcomponent's license, as noted in the LICENSE file. | ||
*/ | ||
|
||
package com.vmware.admiral.compute.kubernetes.service; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
|
||
import com.vmware.admiral.common.ManagementUriParts; | ||
import com.vmware.admiral.compute.container.ContainerDescriptionService.ContainerDescription; | ||
import com.vmware.admiral.compute.content.kubernetes.KubernetesConverter; | ||
import com.vmware.admiral.compute.content.kubernetes.KubernetesUtil; | ||
import com.vmware.admiral.compute.kubernetes.entities.deployments.Deployment; | ||
import com.vmware.admiral.compute.kubernetes.service.KubernetesDescriptionService.KubernetesDescription; | ||
import com.vmware.xenon.common.Operation; | ||
import com.vmware.xenon.common.StatelessService; | ||
import com.vmware.xenon.common.UriUtils; | ||
|
||
public class ContainerDescriptionToKubernetesDescriptionConverterService extends StatelessService { | ||
public static final String SELF_LINK = ManagementUriParts.CONTAINER_DESCRIPTION_TO_KUBERNETES_DESCRIPTION_CONVERTER; | ||
|
||
@Override | ||
public void handlePost(Operation post) { | ||
if (!post.hasBody()) { | ||
post.fail(new IllegalArgumentException("body is required")); | ||
return; | ||
} | ||
|
||
ContainerDescription containerDescription = post.getBody(ContainerDescription.class); | ||
|
||
Deployment deployment = KubernetesConverter | ||
.fromContainerDescriptionToDeployment(containerDescription, containerDescription.name); | ||
try { | ||
String serializeKubernetesEntity = KubernetesUtil.serializeKubernetesEntity(deployment); | ||
|
||
log(Level.INFO, "serializeKubernetesEntity: [%s]", serializeKubernetesEntity); | ||
|
||
KubernetesDescription kubernetesDescription = new KubernetesDescription(); | ||
kubernetesDescription.kubernetesEntity = serializeKubernetesEntity; | ||
kubernetesDescription.type = KubernetesUtil.DEPLOYMENT_TYPE; | ||
|
||
sendRequest(Operation | ||
.createPost(UriUtils.buildUri(getHost(), KubernetesDescriptionService.FACTORY_LINK)) | ||
.setReferer(getHost().getUri()) | ||
.setBody(kubernetesDescription) | ||
.setCompletion((o, e) -> { | ||
if (e != null) { | ||
logSevere("Failed to create kubernetes description: [%s]", e.getMessage()); | ||
o.fail(e); | ||
return; | ||
} | ||
|
||
KubernetesDescription createdKubDesc = o.getBody(KubernetesDescription.class); | ||
post.setBody(createdKubDesc); | ||
post.complete(); | ||
})); | ||
} catch (IOException e) { | ||
logSevere("Failed to serialize kubernetes entity: [%s]", e); | ||
post.fail(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...l/compute/kubernetes/ContainerDescriptionToKubernetesDescriptionConverterServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
* | ||
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this product except in compliance with the License. | ||
* | ||
* This product may include a number of subcomponents with separate copyright notices | ||
* and license terms. Your use of these subcomponents is subject to the terms and | ||
* conditions of the subcomponent's license, as noted in the LICENSE file. | ||
*/ | ||
|
||
package com.vmware.admiral.compute.kubernetes; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.util.logging.Level; | ||
|
||
import org.junit.Test; | ||
|
||
import com.vmware.admiral.compute.container.ComputeBaseTest; | ||
import com.vmware.admiral.compute.container.ContainerDescriptionService; | ||
import com.vmware.admiral.compute.container.ContainerDescriptionService.ContainerDescription; | ||
import com.vmware.admiral.compute.kubernetes.service.ContainerDescriptionToKubernetesDescriptionConverterService; | ||
import com.vmware.admiral.compute.kubernetes.service.KubernetesDescriptionService.KubernetesDescription; | ||
import com.vmware.xenon.common.UriUtils; | ||
|
||
public class ContainerDescriptionToKubernetesDescriptionConverterServiceTest extends ComputeBaseTest { | ||
private static final String CONTAINER_DESC_NAME = "dcp-test"; | ||
private static final String CONTAINER_DESC_IMAGE = "dcp-test:latest"; | ||
private static final int CONTAINER_DESC_CLUSTER_SIZE = 4; | ||
|
||
private String expectedYamlDefinition = String.format("---\n" | ||
+ "apiVersion: \"extensions/v1beta1\"\n" | ||
+ "kind: \"Deployment\"\n" | ||
+ "metadata:\n" | ||
+ " name: \"%1$s\"\n" | ||
+ " labels:\n" | ||
+ " app: \"%1$s\"\n" | ||
+ "spec:\n" | ||
+ " replicas: 4\n" | ||
+ " template:\n" | ||
+ " metadata:\n" | ||
+ " labels:\n" | ||
+ " app: \"%1$s\"\n" | ||
+ " tier: \"%1$s\"\n" | ||
+ " spec:\n" | ||
+ " containers:\n" | ||
+ " - name: \"%1$s\"\n" | ||
+ " image: \"%2$s\"", CONTAINER_DESC_NAME, CONTAINER_DESC_IMAGE); | ||
|
||
@Test | ||
public void testConvertContainerDescriptionToKubernetesDescription() throws Throwable { | ||
|
||
host.log(Level.INFO, "Creating container description."); | ||
ContainerDescription cd = createContainerDescription(CONTAINER_DESC_NAME); | ||
cd = doPost(cd, ContainerDescriptionService.FACTORY_LINK); | ||
|
||
host.log(Level.INFO, "Converting..."); | ||
KubernetesDescription kd = doPost(cd, ContainerDescriptionToKubernetesDescriptionConverterService.SELF_LINK, | ||
KubernetesDescription.class); | ||
assertNotNull(kd); | ||
assertEquals(expectedYamlDefinition, kd.kubernetesEntity); | ||
} | ||
|
||
private ContainerDescription createContainerDescription(String name) { | ||
ContainerDescription containerDesc = new ContainerDescription(); | ||
containerDesc.documentSelfLink = UriUtils.buildUriPath( | ||
ContainerDescriptionService.FACTORY_LINK, name);; | ||
containerDesc.name = name; | ||
containerDesc.image = CONTAINER_DESC_IMAGE; | ||
containerDesc._cluster = CONTAINER_DESC_CLUSTER_SIZE; | ||
|
||
return containerDesc; | ||
} | ||
} |