Search in sources :

Example 1 with IstioServerModel

use of org.ballerinax.kubernetes.models.istio.IstioServerModel 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 IstioServerModel

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

the class IstioGatewayAnnotationProcessor method processIstioGatewayServerAnnotation.

/**
 * Process server field of @istio:Gateway annotation.
 *
 * @param gatewayModel The gateway model.
 * @param serversField List of servers of the gateway.
 * @throws KubernetesPluginException Unable to process annotation
 */
private void processIstioGatewayServerAnnotation(IstioGatewayModel gatewayModel, BLangListConstructorExpr serversField) throws KubernetesPluginException {
    List<IstioServerModel> servers = new LinkedList<>();
    for (ExpressionNode serverRecord : serversField.getExpressions()) {
        if (serverRecord instanceof BLangRecordLiteral) {
            BLangRecordLiteral serverFieldRecord = (BLangRecordLiteral) serverRecord;
            IstioServerModel server = new IstioServerModel();
            for (BLangRecordLiteral.BLangRecordKeyValueField serverField : convertRecordFields(serverFieldRecord.getFields())) {
                switch(ServerConfig.valueOf(serverField.getKey().toString())) {
                    case port:
                        BLangRecordLiteral portRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayPortAnnotation(server, convertRecordFields(portRecord.getFields()));
                        break;
                    case hosts:
                        server.setHosts(getList(serverField.getValue()));
                        break;
                    case tls:
                        BLangRecordLiteral tlsRecord = (BLangRecordLiteral) serverField.getValue();
                        processIstioGatewayTLSAnnotation(server, convertRecordFields(tlsRecord.getFields()));
                        break;
                    default:
                        throw new KubernetesPluginException("unknown field found for istio gateway server.");
                }
            }
            servers.add(server);
        }
    }
    gatewayModel.setServers(servers);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) LinkedList(java.util.LinkedList)

Example 3 with IstioServerModel

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

the class IstioGatewayHandler method createArtifacts.

/**
 * {@inheritDoc}
 * Performs validations and creates the artifacts.
 */
@Override
public void createArtifacts() throws KubernetesPluginException {
    Map<String, IstioGatewayModel> gatewayModels = dataHolder.getIstioGatewayModels();
    int size = gatewayModels.size();
    if (size > 0) {
        OUT.println();
    }
    int count = 0;
    for (IstioGatewayModel gatewayModel : gatewayModels.values()) {
        count++;
        // Validate number of selectors.
        if (null == gatewayModel.getSelector() || gatewayModel.getSelector().size() == 0) {
            Map<String, String> selectors = new LinkedHashMap<>();
            selectors.put(KubernetesConstants.ISTIO_GATEWAY_SELECTOR, "ingressgateway");
            gatewayModel.setSelector(selectors);
        }
        // Validate number of servers.
        if (null == gatewayModel.getServers() || gatewayModel.getServers().size() == 0) {
            throw new KubernetesPluginException("'" + gatewayModel.getName() + "' istio gateway needs one or more" + " servers.");
        }
        // Validate server.hosts
        for (IstioServerModel serverModel : gatewayModel.getServers()) {
            if (null == serverModel.getHosts() || serverModel.getHosts().size() == 0) {
                throw new KubernetesPluginException("'" + gatewayModel.getName() + "' istio gateway needs one or" + " more server hosts.");
            }
            if (null == serverModel.getPort() || serverModel.getPort().getNumber() < 0) {
                throw new KubernetesPluginException("'" + gatewayModel.getName() + "' istio gateway ports cannot" + " be less than 0. found: " + serverModel.getPort().getNumber());
            }
            if (serverModel.getTls() != null && ("SIMPLE".equals(serverModel.getTls().getMode()) && (serverModel.getTls().getServerCertificate() == null || serverModel.getTls().getPrivateKey() == null))) {
                throw new KubernetesPluginException("'" + gatewayModel.getName() + "' istio gateway TLS mode is" + " SIMPLE, hence serverCertificate and privateKey fields are" + " required.");
            }
            if (serverModel.getTls() != null && ("MUTUAL".equals(serverModel.getTls().getMode()) && (serverModel.getTls().getServerCertificate() == null || serverModel.getTls().getPrivateKey() == null || serverModel.getTls().getCaCertificates() == null))) {
                throw new KubernetesPluginException("'" + gatewayModel.getName() + "' istio gateway TLS mode is" + " MUTUAL, hence serverCertificate, privateKey and" + " caCertificates fields are required.");
            }
        }
        generate(gatewayModel);
        OUT.print("\t@istio:Gateway \t\t\t\t - complete " + count + "/" + size + "\r");
    }
}
Also used : IstioGatewayModel(org.ballerinax.kubernetes.models.istio.IstioGatewayModel) IstioServerModel(org.ballerinax.kubernetes.models.istio.IstioServerModel) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with IstioServerModel

use of org.ballerinax.kubernetes.models.istio.IstioServerModel 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

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