use of me.snowdrop.istio.api.networking.v1alpha3.VirtualService in project kubernetes by ballerinax.
the class IstioVirtualServiceHandler method generate.
/**
* Generate artifact for istio virtual service model.
*
* @param serviceName The name of the service in which the virtual service routes to.
* @param vsModel The virtual service model.
* @throws KubernetesPluginException Error when writing artifact files.
*/
private void generate(String serviceName, IstioVirtualServiceModel vsModel) throws KubernetesPluginException {
try {
VirtualService virtualService = new VirtualServiceBuilder().withNewMetadata().withName(vsModel.getName()).withNamespace(dataHolder.getNamespace()).withLabels(vsModel.getLabels()).withAnnotations(vsModel.getAnnotations()).endMetadata().withNewSpec().withHosts(vsModel.getHosts()).withGateways(vsModel.getGateways()).withHttp(populateHttp(serviceName, vsModel.getHttp())).endSpec().build();
String gatewayContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(virtualService);
KubernetesUtils.writeToFile(gatewayContent, ISTIO_VIRTUAL_SERVICE_FILE_POSTFIX + YAML);
} catch (IOException e) {
String errorMessage = "error while generating yaml file for istio virtual service: " + vsModel.getName();
throw new KubernetesPluginException(errorMessage, e);
}
}
use of me.snowdrop.istio.api.networking.v1alpha3.VirtualService in project kubernetes by ballerinax.
the class IstioVirtualServiceTest method destinationWeightTest.
/**
* Build bal file with istio virtual service annotation with destination weight.
*
* @throws IOException Error when loading the generated yaml.
* @throws InterruptedException Error when compiling the ballerina file.
* @throws KubernetesPluginException Error when deleting the generated artifacts folder.
*/
@Test(groups = { "istio" })
public void destinationWeightTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "destination_weight.bal"), 0);
// Check if docker image exists and correct
validateDockerfile();
validateDockerImage();
// Validate virtual service yaml
File vsFile = KUBERNETES_TARGET_PATH.resolve("destination_weight_istio_virtual_service.yaml").toFile();
Assert.assertTrue(vsFile.exists());
VirtualService virtualService = KubernetesTestUtils.loadYaml(vsFile);
Assert.assertNotNull(virtualService.getMetadata());
Assert.assertEquals(virtualService.getMetadata().getName(), "reviews-route", "Invalid virtual service name");
Assert.assertNotNull(virtualService.getSpec());
Assert.assertEquals(virtualService.getSpec().getHosts().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHosts().get(0), "reviews.prod.svc.cluster.local", "Invalid host value.");
Assert.assertEquals(virtualService.getSpec().getHttp().size(), 1, "Invalid number of http items");
Assert.assertNotNull(virtualService.getSpec().getHttp().get(0).getRoute());
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().size(), 2);
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getHost(), "reviews.prod.svc.cluster.local", "Invalid route destination host");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getSubset(), "v2", "Invalid route destination subset");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getWeight().intValue(), 25, "Invalid route weight");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(1).getDestination().getHost(), "reviews.prod.svc.cluster.local", "Invalid route destination host");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(1).getDestination().getSubset(), "v1", "Invalid route destination subset");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(1).getWeight().intValue(), 75, "Invalid route weight");
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
use of me.snowdrop.istio.api.networking.v1alpha3.VirtualService in project kubernetes by ballerinax.
the class IstioVirtualServiceTest method useServiceAnnotationPortTest.
/**
* Build bal file with istio virtual service annotations. Check if service annotation port is used.
*
* @throws IOException Error when loading the generated yaml.
* @throws InterruptedException Error when compiling the ballerina file.
* @throws KubernetesPluginException Error when deleting the generated artifacts folder.
*/
@Test(groups = { "istio" })
public void useServiceAnnotationPortTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "svc_port.bal"), 0);
// Check if docker image exists and correct
validateDockerfile();
validateDockerImage();
// Validate virtual service yaml
File vsFile = KUBERNETES_TARGET_PATH.resolve("svc_port_istio_virtual_service.yaml").toFile();
Assert.assertTrue(vsFile.exists());
VirtualService virtualService = KubernetesTestUtils.loadYaml(vsFile);
Assert.assertNotNull(virtualService.getMetadata());
Assert.assertEquals(virtualService.getMetadata().getName(), "helloep-istio-vs", "Invalid virtual service name");
Assert.assertNotNull(virtualService.getSpec());
Assert.assertEquals(virtualService.getSpec().getHosts().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHosts().get(0), "*", "Invalid host value.");
Assert.assertEquals(virtualService.getSpec().getHttp().size(), 1, "Invalid number of http items");
Assert.assertNotNull(virtualService.getSpec().getHttp().get(0).getRoute());
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getHost(), "hello", "Invalid route destination host");
Assert.assertTrue(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getPort().getPort() instanceof NumberPort);
NumberPort numberPort = (NumberPort) virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getPort().getPort();
Assert.assertEquals(numberPort.getNumber().intValue(), 8080, "Invalid port found");
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
use of me.snowdrop.istio.api.networking.v1alpha3.VirtualService in project kubernetes by ballerinax.
the class IstioVirtualServiceTest method destinationTest.
/**
* Build bal file with istio virtual service annotation with destination timeout.
*
* @throws IOException Error when loading the generated yaml.
* @throws InterruptedException Error when compiling the ballerina file.
* @throws KubernetesPluginException Error when deleting the generated artifacts folder.
*/
@Test(groups = { "istio" })
public void destinationTest() throws IOException, InterruptedException, KubernetesPluginException, DockerTestException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(BAL_DIRECTORY, "destination.bal"), 0);
// Check if docker image exists and correct
validateDockerfile();
validateDockerImage();
// Validate virtual service yaml
File vsFile = KUBERNETES_TARGET_PATH.resolve("destination_istio_virtual_service.yaml").toFile();
Assert.assertTrue(vsFile.exists());
VirtualService virtualService = KubernetesTestUtils.loadYaml(vsFile);
Assert.assertNotNull(virtualService.getMetadata());
Assert.assertEquals(virtualService.getMetadata().getName(), "reviews-route", "Invalid virtual service name");
Assert.assertNotNull(virtualService.getSpec());
Assert.assertEquals(virtualService.getSpec().getHosts().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHosts().get(0), "reviews.prod.svc.cluster.local", "Invalid host value.");
Assert.assertEquals(virtualService.getSpec().getHttp().size(), 1, "Invalid number of http items");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getTimeout().getSeconds().longValue(), 5L, "Invalid timeout value");
Assert.assertNotNull(virtualService.getSpec().getHttp().get(0).getRoute());
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getHost(), "reviews.prod.svc.cluster.local", "Invalid route destination host");
KubernetesUtils.deleteDirectory(KUBERNETES_TARGET_PATH);
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
KubernetesTestUtils.deleteDockerImage(DOCKER_IMAGE);
}
use of me.snowdrop.istio.api.networking.v1alpha3.VirtualService in project kubernetes by ballerinax.
the class Sample16Test method validateTravelAgencyVirtualService.
@Test
public void validateTravelAgencyVirtualService() {
Assert.assertNotNull(virtualService);
Assert.assertNotNull(virtualService.getMetadata());
Assert.assertEquals(virtualService.getMetadata().getName(), "travelagencyep-istio-vs", "Invalid virtual service name");
Assert.assertNotNull(virtualService.getSpec());
Assert.assertNotNull(virtualService.getSpec().getGateways());
Assert.assertEquals(virtualService.getSpec().getGateways().size(), 1);
Assert.assertEquals(virtualService.getSpec().getGateways().get(0), "travelagencyep-istio-gw", "Invalid gateway");
Assert.assertNotNull(virtualService.getSpec().getHosts());
Assert.assertEquals(virtualService.getSpec().getHosts().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHosts().get(0), "*", "Invalid host value.");
Assert.assertNotNull(virtualService.getSpec().getHttp());
Assert.assertEquals(virtualService.getSpec().getHttp().size(), 1, "Invalid number of http items");
Assert.assertNotNull(virtualService.getSpec().getHttp().get(0).getRoute());
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().size(), 1);
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getHost(), "travelagencyep-svc", "Invalid route destination host");
PortSelector.Port destinationPort = virtualService.getSpec().getHttp().get(0).getRoute().get(0).getDestination().getPort().getPort();
Assert.assertTrue(destinationPort instanceof NumberPort);
NumberPort destinationPortNumber = (NumberPort) destinationPort;
Assert.assertEquals(destinationPortNumber.getNumber().intValue(), 9090, "Invalid port");
Assert.assertEquals(virtualService.getSpec().getHttp().get(0).getRoute().get(0).getWeight().intValue(), 100, "Invalid weight");
}
Aggregations