Search in sources :

Example 1 with ServiceHandler

use of org.ballerinax.kubernetes.handlers.ServiceHandler in project kubernetes by ballerinax.

the class KubernetesServiceGeneratorTests method testServiceGenerate.

@Test
public void testServiceGenerate() {
    ServiceModel serviceModel = new ServiceModel();
    serviceModel.setName("MyService");
    serviceModel.setPort(9090);
    serviceModel.setServiceType("NodePort");
    serviceModel.setSelector("MyAPP");
    Map<String, String> labels = new HashMap<>();
    labels.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "TestAPP");
    serviceModel.setLabels(labels);
    ServiceHandler kubernetesServiceGenerator = new ServiceHandler(serviceModel);
    try {
        String serviceYAML = kubernetesServiceGenerator.generate();
        Assert.assertNotNull(serviceYAML);
        File artifactLocation = new File("target/kubernetes");
        artifactLocation.mkdir();
        File tempFile = File.createTempFile("temp", serviceModel.getName() + ".yaml", artifactLocation);
        KubernetesUtils.writeToFile(serviceYAML, tempFile.getPath());
        log.info("Generated YAML: \n" + serviceYAML);
        Assert.assertTrue(tempFile.exists());
    // tempFile.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file");
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml from service");
    }
}
Also used : ServiceModel(org.ballerinax.kubernetes.models.ServiceModel) HashMap(java.util.HashMap) ServiceHandler(org.ballerinax.kubernetes.handlers.ServiceHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) File(java.io.File) Test(org.junit.Test)

Example 2 with ServiceHandler

use of org.ballerinax.kubernetes.handlers.ServiceHandler in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method generateService.

private void generateService(ServiceModel serviceModel, String balxFilePath, String outputDir) throws KubernetesPluginException {
    String balxFileName = KubernetesUtils.extractBalxName(balxFilePath);
    serviceModel.addLabel(KubernetesConstants.KUBERNETES_SELECTOR_KEY, balxFileName);
    serviceModel.setSelector(balxFileName);
    String serviceContent = new ServiceHandler(serviceModel).generate();
    try {
        KubernetesUtils.writeToFile(serviceContent, outputDir + File.separator + balxFileName + SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        throw new KubernetesPluginException("Error while writing service content", e);
    }
}
Also used : ServiceHandler(org.ballerinax.kubernetes.handlers.ServiceHandler) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException)

Aggregations

IOException (java.io.IOException)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 ServiceHandler (org.ballerinax.kubernetes.handlers.ServiceHandler)2 File (java.io.File)1 HashMap (java.util.HashMap)1 ServiceModel (org.ballerinax.kubernetes.models.ServiceModel)1 Test (org.junit.Test)1