use of org.ballerinax.kubernetes.models.istio.IstioGatewayModel in project kubernetes by ballerinax.
the class IstioGatewayAnnotationProcessor method processAnnotation.
@Override
public void processAnnotation(SimpleVariableNode variableNode, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
IstioGatewayModel gwModel = this.processIstioGatewayAnnotation(keyValues);
if (isBlank(gwModel.getName())) {
gwModel.setName(getValidName(variableNode.getName().getValue()) + ISTIO_GATEWAY_POSTFIX);
}
setDefaultValues(gwModel);
KubernetesContext.getInstance().getDataHolder().addIstioGatewayModel(variableNode.getName().getValue(), gwModel);
}
use of org.ballerinax.kubernetes.models.istio.IstioGatewayModel in project kubernetes by ballerinax.
the class IstioVirtualServiceHandler method createArtifacts.
@Override
public void createArtifacts() throws KubernetesPluginException {
Map<String, IstioVirtualServiceModel> istioVSModels = dataHolder.getIstioVirtualServiceModels();
int size = istioVSModels.size();
if (size > 0) {
OUT.println();
}
int count = 0;
for (Map.Entry<String, IstioVirtualServiceModel> vsModel : istioVSModels.entrySet()) {
count++;
IstioGatewayModel gwModel = KubernetesContext.getInstance().getDataHolder().getIstioGatewayModel(vsModel.getKey());
if ((null == vsModel.getValue().getGateways() || vsModel.getValue().getGateways().size() == 0) && null != gwModel) {
if (null == vsModel.getValue().getGateways()) {
vsModel.getValue().setGateways(new LinkedList<>());
}
if (vsModel.getValue().getGateways().size() == 0) {
vsModel.getValue().getGateways().add(gwModel.getName());
} else if (vsModel.getValue().getHosts().size() == 1 && vsModel.getValue().getHosts().contains("*")) {
throw new KubernetesPluginException("unable to resolve a gateway for '" + vsModel + "' " + "virtual service. Add @istio:Gateway{} annotation" + " to your listener or service, else explicitly state to " + "use the 'mesh' gateway.");
}
}
generate(vsModel.getKey(), vsModel.getValue());
OUT.print("\t@istio:VirtualService \t\t\t - complete " + count + "/" + size + "\r");
}
}
use of org.ballerinax.kubernetes.models.istio.IstioGatewayModel 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");
}
}
use of org.ballerinax.kubernetes.models.istio.IstioGatewayModel 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());
}
}
use of org.ballerinax.kubernetes.models.istio.IstioGatewayModel in project kubernetes by ballerinax.
the class IstioGatewayAnnotationProcessor method processAnnotation.
@Override
public void processAnnotation(ServiceNode serviceNode, AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
List<BLangRecordLiteral.BLangRecordKeyValueField> keyValues = convertRecordFields(((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getFields());
IstioGatewayModel gwModel = this.processIstioGatewayAnnotation(keyValues);
if (isBlank(gwModel.getName())) {
gwModel.setName(getValidName(serviceNode.getName().getValue()) + ISTIO_GATEWAY_POSTFIX);
}
setDefaultValues(gwModel);
KubernetesContext.getInstance().getDataHolder().addIstioGatewayModel(serviceNode.getName().getValue(), gwModel);
}
Aggregations