Skip to content

Commit

Permalink
[VBV-1919][Part1] Introduce converter service
Browse files Browse the repository at this point in the history
- 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
afilipov1 committed May 13, 2018
1 parent 2af7ba8 commit c8b1eda
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,25 @@ protected <T extends ServiceDocument> T doPost(T inState, URI uri, boolean expec
return outState;
}

protected <T> T doPost(Object body, String factoryLink, Class<T> stateType) {
TestContext ctx = testCreate(1);
final Object[] responseBody = new Object[1];
Operation create = Operation.createPost(host, factoryLink)
.setBody(body)
.setCompletion((o, ex) -> {
if (ex != null) {
ctx.failIteration(ex);
return;
}
responseBody[0] = o.getBodyRaw();
ctx.completeIteration();
});

host.send(create);
ctx.await();
return Utils.fromJson(responseBody[0], stateType);
}

protected <T> T doPostWithProjectHeader(Object body, String factoryLink, String projectLink,
Class<T> stateType) {
TestContext ctx = testCreate(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public interface ManagementUriParts {
String KUBERNETES_REPLICATION_CONTROLLERS = RESOURCES + "/kubernetes-replication-controllers";
String KUBERNETES_REPLICA_SETS = RESOURCES + "/kubernetes-replica-sets";
String KUBERNETES_POD_LOGS = RESOURCES + "/kubernetes-pod-logs";
String CONTAINER_DESCRIPTION_TO_KUBERNETES_DESCRIPTION_CONVERTER = RESOURCES
+ "/container-description-to-kubernetes-description-converter";


// Request tasks:
String REQUEST = "/request";
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.vmware.admiral.compute.content.CompositeDescriptionContentService;
import com.vmware.admiral.compute.content.TemplateComputeDescription;
import com.vmware.admiral.compute.kubernetes.KubernetesEntityDataCollection;
import com.vmware.admiral.compute.kubernetes.service.ContainerDescriptionToKubernetesDescriptionConverterService;
import com.vmware.admiral.compute.kubernetes.service.DeploymentService;
import com.vmware.admiral.compute.kubernetes.service.DeploymentService.DeploymentState;
import com.vmware.admiral.compute.kubernetes.service.KubernetesDescriptionContentService;
Expand Down Expand Up @@ -116,7 +117,8 @@ public static void startServices(ServiceHost host, boolean startMockContainerHos
KubernetesDescriptionContentService.class,
PodLogService.class,
ClusterService.class,
DanglingDescriptionsCleanupService.class);
DanglingDescriptionsCleanupService.class,
ContainerDescriptionToKubernetesDescriptionConverterService.class);

startServiceFactories(host, CaSigningCertService.class,
GroupResourcePlacementService.class,
Expand Down
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;
}
}

0 comments on commit c8b1eda

Please sign in to comment.