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);
}
}
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);
}
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());
}
}
Aggregations