use of io.fabric8.kubernetes.api.model.ServicePort in project camel by apache.
the class KubernetesServicesConsumerTest method createAndDeleteService.
@Test
public void createAndDeleteService() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
mockResultEndpoint.expectedMessageCount(2);
mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "DELETED");
Exchange ex = template.request("direct:createService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
ServiceSpec serviceSpec = new ServiceSpec();
List<ServicePort> lsp = new ArrayList<ServicePort>();
ServicePort sp = new ServicePort();
sp.setPort(8080);
sp.setTargetPort(new IntOrString(8080));
sp.setProtocol("TCP");
lsp.add(sp);
serviceSpec.setPorts(lsp);
Map<String, String> selectorMap = new HashMap<String, String>();
selectorMap.put("containter", "test");
serviceSpec.setSelector(selectorMap);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
}
});
Service serv = ex.getOut().getBody(Service.class);
assertEquals(serv.getMetadata().getName(), "test");
ex = template.request("direct:deleteService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
}
});
boolean servDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(servDeleted);
Thread.sleep(3000);
mockResultEndpoint.assertIsSatisfied();
}
use of io.fabric8.kubernetes.api.model.ServicePort in project camel by apache.
the class KubernetesServicesProducerTest method createAndDeleteService.
@Test
public void createAndDeleteService() throws Exception {
if (ObjectHelper.isEmpty(authToken)) {
return;
}
Exchange ex = template.request("direct:createService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
Map<String, String> labels = new HashMap<String, String>();
labels.put("this", "rocks");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_LABELS, labels);
ServiceSpec serviceSpec = new ServiceSpec();
List<ServicePort> lsp = new ArrayList<ServicePort>();
ServicePort sp = new ServicePort();
sp.setPort(8080);
sp.setTargetPort(new IntOrString(8080));
sp.setProtocol("TCP");
lsp.add(sp);
serviceSpec.setPorts(lsp);
Map<String, String> selectorMap = new HashMap<String, String>();
selectorMap.put("containter", "test");
serviceSpec.setSelector(selectorMap);
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_SPEC, serviceSpec);
}
});
Service serv = ex.getOut().getBody(Service.class);
assertEquals(serv.getMetadata().getName(), "test");
ex = template.request("direct:deleteService", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_SERVICE_NAME, "test");
}
});
boolean servDeleted = ex.getOut().getBody(Boolean.class);
assertTrue(servDeleted);
}
use of io.fabric8.kubernetes.api.model.ServicePort in project che by eclipse.
the class KubernetesServiceTest method shouldReturnServicePortListFromImageExposedPortList.
@Test
public void shouldReturnServicePortListFromImageExposedPortList() {
// Given
Map<String, ExposedPort> imageExposedPorts = new HashMap<>();
imageExposedPorts.put("8080/TCP", new ExposedPort());
// When
List<ServicePort> servicePorts = KubernetesService.getServicePortsFrom(imageExposedPorts.keySet());
// Then
List<String> portsAndProtocols = servicePorts.stream().map(p -> Integer.toString(p.getPort()) + "/" + p.getProtocol()).collect(Collectors.toList());
assertTrue(imageExposedPorts.keySet().stream().anyMatch(portsAndProtocols::contains));
}
use of io.fabric8.kubernetes.api.model.ServicePort in project che by eclipse.
the class OpenShiftConnector method createOpenShiftService.
private void createOpenShiftService(String workspaceID, Set<String> exposedPorts, Map<String, String> additionalLabels) {
Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID);
List<ServicePort> ports = KubernetesService.getServicePortsFrom(exposedPorts);
Service service = openShiftClient.services().inNamespace(this.openShiftCheProjectName).createNew().withNewMetadata().withName(CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID).withAnnotations(KubernetesLabelConverter.labelsToNames(additionalLabels)).endMetadata().withNewSpec().withType(OPENSHIFT_SERVICE_TYPE_NODE_PORT).withSelector(selector).withPorts(ports).endSpec().done();
LOG.info("OpenShift service {} created", service.getMetadata().getName());
}
use of io.fabric8.kubernetes.api.model.ServicePort in project che by eclipse.
the class KubernetesService method getServicePortsFrom.
/**
* Retrieves list of {@link ServicePort} based on ports defined in
* {@link ContainerConfig} and {@link ImageConfig}
*
* @param exposedPorts
* @return list of {@link ServicePort}
*/
public static List<ServicePort> getServicePortsFrom(Set<String> exposedPorts) {
List<ServicePort> servicePorts = new ArrayList<>(exposedPorts.size());
for (String exposedPort : exposedPorts) {
String[] portAndProtocol = exposedPort.split("/", 2);
String port = portAndProtocol[0];
String protocol = portAndProtocol[1];
int portNumber = Integer.parseInt(port);
String portName = CheServicePorts.get().get(portNumber);
portName = isNullOrEmpty(portName) ? exposedPort.replace("/", "-") : portName;
int targetPortNumber = portNumber;
ServicePort servicePort = new ServicePort();
servicePort.setName(portName);
servicePort.setProtocol(protocol.toUpperCase());
servicePort.setPort(portNumber);
servicePort.setTargetPort(new IntOrString(targetPortNumber));
servicePorts.add(servicePort);
}
return servicePorts;
}
Aggregations