Search in sources :

Example 1 with IstioPortModel

use of org.ballerinax.kubernetes.models.istio.IstioPortModel in project kubernetes by ballerinax.

the class IstioGatewayAnnotationProcessor method setDefaultValues.

/**
 * Set default values for the gateway model.
 *
 * @param gwModel The gateway model.
 */
private void setDefaultValues(IstioGatewayModel gwModel) {
    if (null == gwModel.getServers() || gwModel.getServers().size() == 0) {
        List<IstioServerModel> serversModel = new LinkedList<>();
        IstioServerModel serverModel = new IstioServerModel();
        IstioPortModel portModel = new IstioPortModel();
        portModel.setNumber(80);
        portModel.setProtocol("HTTP");
        portModel.setName("http");
        serverModel.setPort(portModel);
        if (null == serverModel.getHosts() || serverModel.getHosts().size() == 0) {
            List<String> hosts = new LinkedList<>();
            hosts.add("*");
            serverModel.setHosts(hosts);
        }
        serversModel.add(serverModel);
        gwModel.setServers(serversModel);
    }
}
Also used : IstioPortModel(org.ballerinax.kubernetes.models.istio.IstioPortModel) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) LinkedList(java.util.LinkedList)

Example 2 with IstioPortModel

use of org.ballerinax.kubernetes.models.istio.IstioPortModel in project kubernetes by ballerinax.

the class IstioGatewayAnnotationProcessor method processIstioGatewayPortAnnotation.

/**
 * Process port fields of @istio:Gateway annotations's server field.
 *
 * @param server     The server model.
 * @param portFields The fields of the server's port.
 * @throws KubernetesPluginException Unable to process annotation
 */
private void processIstioGatewayPortAnnotation(IstioServerModel server, List<BLangRecordLiteral.BLangRecordKeyValueField> portFields) throws KubernetesPluginException {
    IstioPortModel portModel = new IstioPortModel();
    for (BLangRecordLiteral.BLangRecordKeyValueField portField : portFields) {
        switch(PortConfig.valueOf(portField.getKey().toString())) {
            case number:
                portModel.setNumber(Integer.parseInt(portField.getValue().toString()));
                break;
            case protocol:
                portModel.setProtocol(portField.getValue().toString());
                break;
            case name:
                portModel.setName(portField.getValue().toString());
                break;
            default:
                throw new KubernetesPluginException("unknown field found for istio gateway server port.");
        }
    }
    server.setPort(portModel);
}
Also used : IstioPortModel(org.ballerinax.kubernetes.models.istio.IstioPortModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)

Example 3 with IstioPortModel

use of org.ballerinax.kubernetes.models.istio.IstioPortModel in project kubernetes by ballerinax.

the class IstioGatewayGeneratorTests method testSimpleGateway.

@Test
public void testSimpleGateway() {
    IstioGatewayModel istioGatewayModel = new IstioGatewayModel();
    istioGatewayModel.setName("my-gateway");
    Map<String, String> selectors = new LinkedHashMap<>();
    selectors.put(KubernetesConstants.KUBERNETES_SELECTOR_KEY, "my-gatweway-controller");
    istioGatewayModel.setSelector(selectors);
    List<IstioServerModel> serverModels = new LinkedList<>();
    // First server
    IstioServerModel serverModel = new IstioServerModel();
    IstioPortModel portModel = new IstioPortModel();
    portModel.setNumber(80);
    portModel.setProtocol("HTTP");
    portModel.setName("http");
    serverModel.setPort(portModel);
    List<String> hostModels = new LinkedList<>();
    hostModels.add("uk.bookinfo.com");
    hostModels.add("eu.bookinfo.com");
    serverModel.setHosts(hostModels);
    IstioServerModel.TLSOptions tlsOptions = new IstioServerModel.TLSOptions();
    tlsOptions.setHttpsRedirect(true);
    serverModel.setTls(tlsOptions);
    serverModels.add(serverModel);
    istioGatewayModel.setServers(serverModels);
    dataHolder.addIstioGatewayModel("sample-svc", istioGatewayModel);
    try {
        new IstioGatewayHandler().createArtifacts();
        File gwYaml = dataHolder.getK8sArtifactOutputPath().resolve("hello" + ISTIO_GATEWAY_FILE_POSTFIX + YAML).toFile();
        Gateway gateway = Utils.loadYaml(gwYaml);
        // metadata
        Assert.assertNotNull(gateway.getMetadata());
        Assert.assertEquals(gateway.getMetadata().getName(), "my-gateway");
        Assert.assertNotNull(gateway.getSpec());
        Assert.assertEquals(gateway.getSpec().getSelector().get(KubernetesConstants.KUBERNETES_SELECTOR_KEY), "my-gatweway-controller");
        Assert.assertEquals(gateway.getSpec().getServers().size(), 1);
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getPort().getNumber().intValue(), 80);
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getPort().getProtocol(), "HTTP");
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getPort().getName(), "http");
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getHosts().size(), 2);
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getHosts().get(0), "uk.bookinfo.com");
        Assert.assertEquals(gateway.getSpec().getServers().get(0).getHosts().get(1), "eu.bookinfo.com");
        Assert.assertNotNull(gateway.getSpec().getServers().get(0).getTls());
        Assert.assertTrue(gateway.getSpec().getServers().get(0).getTls().getHttpsRedirect());
        gwYaml.deleteOnExit();
    } catch (IOException e) {
        Assert.fail("Unable to write to file: " + e.getMessage());
    } catch (KubernetesPluginException e) {
        Assert.fail("Unable to generate yaml: " + e.getMessage());
    }
}
Also used : IstioGatewayHandler(org.ballerinax.kubernetes.handlers.istio.IstioGatewayHandler) IstioGatewayModel(org.ballerinax.kubernetes.models.istio.IstioGatewayModel) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) IOException(java.io.IOException) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) IstioPortModel(org.ballerinax.kubernetes.models.istio.IstioPortModel) Gateway(me.snowdrop.istio.api.networking.v1alpha3.Gateway) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

IstioPortModel (org.ballerinax.kubernetes.models.istio.IstioPortModel)3 LinkedList (java.util.LinkedList)2 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)2 IstioServerModel (org.ballerinax.kubernetes.models.istio.IstioServerModel)2 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Gateway (me.snowdrop.istio.api.networking.v1alpha3.Gateway)1 IstioGatewayHandler (org.ballerinax.kubernetes.handlers.istio.IstioGatewayHandler)1 IstioGatewayModel (org.ballerinax.kubernetes.models.istio.IstioGatewayModel)1 Test (org.testng.annotations.Test)1 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)1