use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method createIngressForService.
private Ingress createIngressForService(String routeDomainPostfix, String namespace, Service service) {
Ingress ingress = null;
String serviceName = KubernetesHelper.getName(service);
ServiceSpec serviceSpec = service.getSpec();
if (serviceSpec != null && Strings.isNotBlank(serviceName) && shouldCreateExternalURLForService(service, serviceName)) {
String ingressId = serviceName;
String host = "";
if (Strings.isNotBlank(routeDomainPostfix)) {
host = serviceName + "." + namespace + "." + Strings.stripPrefix(routeDomainPostfix, ".");
}
List<HTTPIngressPath> paths = new ArrayList<>();
List<ServicePort> ports = serviceSpec.getPorts();
if (ports != null) {
for (ServicePort port : ports) {
Integer portNumber = port.getPort();
if (portNumber != null) {
HTTPIngressPath path = new HTTPIngressPathBuilder().withNewBackend().withServiceName(serviceName).withServicePort(createIntOrString(portNumber.intValue())).endBackend().build();
paths.add(path);
}
}
}
if (paths.isEmpty()) {
return ingress;
}
ingress = new IngressBuilder().withNewMetadata().withName(ingressId).withNamespace(namespace).endMetadata().withNewSpec().addNewRule().withHost(host).withNewHttp().withPaths(paths).endHttp().endRule().endSpec().build();
String json;
try {
json = KubernetesHelper.toJson(ingress);
} catch (JsonProcessingException e) {
json = e.getMessage() + ". object: " + ingress;
}
log.debug("Created ingress: " + json);
}
return ingress;
}
use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method serviceHasIngressRule.
/**
* Returns true if there is an existing ingress rule for the given service
*/
private boolean serviceHasIngressRule(List<Ingress> ingresses, Service service) {
String serviceName = KubernetesHelper.getName(service);
for (Ingress ingress : ingresses) {
IngressSpec spec = ingress.getSpec();
if (spec == null) {
break;
}
List<IngressRule> rules = spec.getRules();
if (rules == null) {
break;
}
for (IngressRule rule : rules) {
HTTPIngressRuleValue http = rule.getHttp();
if (http == null) {
break;
}
List<HTTPIngressPath> paths = http.getPaths();
if (paths == null) {
break;
}
for (HTTPIngressPath path : paths) {
IngressBackend backend = path.getBackend();
if (backend == null) {
break;
}
if (Objects.equals(serviceName, backend.getServiceName())) {
return true;
}
}
}
}
return false;
}
use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.
the class ApplyMojo method createRoutes.
protected void createRoutes(Controller controller, Collection<HasMetadata> collection) {
String routeDomainPostfix = this.routeDomain;
Log log = getLog();
String namespace = clusterAccess.getNamespace();
// lets get the routes first to see if we should bother
try {
OpenShiftClient openshiftClient = controller.getOpenShiftClientOrNull();
if (openshiftClient == null) {
return;
}
RouteList routes = openshiftClient.routes().inNamespace(namespace).list();
if (routes != null) {
routes.getItems();
}
} catch (Exception e) {
log.warn("Cannot load OpenShift Routes; maybe not connected to an OpenShift platform? " + e, e);
return;
}
List<Route> routes = new ArrayList<>();
for (Object object : collection) {
if (object instanceof Service) {
Service service = (Service) object;
Route route = createRouteForService(routeDomainPostfix, namespace, service);
if (route != null) {
routes.add(route);
}
}
}
collection.addAll(routes);
}
use of io.fabric8.patch.Service in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method getGogsURL.
private String getGogsURL(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
try {
Endpoints endpoints = kubernetes.endpoints().inNamespace(namespace).withName(ServiceNames.GOGS).get();
int runningEndpoints = 0;
if (endpoints != null) {
List<EndpointSubset> subsets = endpoints.getSubsets();
for (EndpointSubset subset : subsets) {
List<EndpointAddress> addresses = subset.getAddresses();
if (addresses != null) {
runningEndpoints += addresses.size();
}
}
}
if (runningEndpoints == 0) {
return null;
}
log.info("Running %s endpoints of %s in namespace %s", runningEndpoints, ServiceNames.GOGS, namespace);
return KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GOGS, namespace, "http", true);
} catch (Exception e) {
String errorMessage = e.getMessage();
if (errorMessage.contains(String.format("No service gogs running in namespace %s", namespace))) {
log.info("Gogs service does not exists, defaulting to GitHub");
return null;
} else {
throw new MojoExecutionException(e.getMessage());
}
}
}
use of io.fabric8.patch.Service in project strimzi by strimzi.
the class KafkaConnectClusterTest method testGenerateService.
@Test
public void testGenerateService() {
Service svc = kc.generateService();
assertEquals("ClusterIP", svc.getSpec().getType());
Map<String, String> expectedLabels = ResourceUtils.labels(Labels.STRIMZI_CLUSTER_LABEL, this.cluster, Labels.STRIMZI_TYPE_LABEL, "kafka-connect", "my-user-label", "cromulent", Labels.STRIMZI_NAME_LABEL, kc.kafkaConnectClusterName(cluster));
assertEquals(expectedLabels, svc.getMetadata().getLabels());
assertEquals(expectedLabels, svc.getSpec().getSelector());
assertEquals(1, svc.getSpec().getPorts().size());
assertEquals(new Integer(KafkaConnectCluster.REST_API_PORT), svc.getSpec().getPorts().get(0).getPort());
assertEquals(KafkaConnectCluster.REST_API_PORT_NAME, svc.getSpec().getPorts().get(0).getName());
assertEquals("TCP", svc.getSpec().getPorts().get(0).getProtocol());
}
Aggregations