-
Notifications
You must be signed in to change notification settings - Fork 202
Ingress and Route generation refactor. #1730
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,19 +23,24 @@ | |
import io.fabric8.kubernetes.api.model.ServiceBuilder; | ||
import io.fabric8.kubernetes.api.model.ServicePort; | ||
import io.fabric8.kubernetes.api.model.ServiceSpec; | ||
import io.fabric8.kubernetes.api.model.extensions.Ingress; | ||
import io.fabric8.kubernetes.api.model.extensions.HTTPIngressPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I thought intellij does that automatically. need to check my ide configuration |
||
import io.fabric8.kubernetes.api.model.extensions.HTTPIngressPathBuilder; | ||
import io.fabric8.kubernetes.api.model.extensions.IngressBackendBuilder; | ||
import io.fabric8.kubernetes.api.model.extensions.IngressBuilder; | ||
import io.fabric8.kubernetes.api.model.extensions.IngressSpecBuilder; | ||
import io.fabric8.maven.core.config.PlatformMode; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.util.FileUtil; | ||
import io.fabric8.maven.core.util.kubernetes.Fabric8Annotations; | ||
import io.fabric8.maven.core.util.kubernetes.KubernetesHelper; | ||
import io.fabric8.maven.enricher.api.BaseEnricher; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
|
@@ -48,42 +53,63 @@ public IngressEnricher(MavenEnricherContext buildContext) { | |
super(buildContext, "fmp-ingress"); | ||
} | ||
|
||
private String routeDomainPostfix; | ||
|
||
@Override | ||
public void create(PlatformMode platformMode, final KubernetesListBuilder listBuilder) { | ||
ResourceConfig resourceConfig = getConfiguration().getResource().orElse(null); | ||
manusa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (resourceConfig != null && resourceConfig.getRouteDomain() != null) { | ||
routeDomainPostfix = resourceConfig.getRouteDomain(); | ||
} | ||
|
||
if (platformMode == PlatformMode.kubernetes) { | ||
final List<Ingress> ingresses = new ArrayList<>(); | ||
listBuilder.accept(new TypedVisitor<ServiceBuilder>() { | ||
@Override | ||
public void visit(ServiceBuilder serviceBuilder) { | ||
addIngress(listBuilder, serviceBuilder); | ||
} | ||
}); | ||
|
||
} | ||
} | ||
|
||
private void addIngress(KubernetesListBuilder listBuilder, ServiceBuilder serviceBuilder) { | ||
ObjectMeta metadata = serviceBuilder.getMetadata(); | ||
if (metadata != null && isExposedService(serviceBuilder)) { | ||
String name = metadata.getName(); | ||
if (!hasIngress(listBuilder, name)) { | ||
ObjectMeta serviceMetadata = serviceBuilder.getMetadata(); | ||
if (serviceMetadata != null && isExposedService(serviceMetadata) && shouldCreateExternalURLForService(serviceBuilder)) { | ||
String serviceName = serviceMetadata.getName(); | ||
if (!hasIngress(listBuilder, serviceName)) { | ||
Integer servicePort = getServicePort(serviceBuilder); | ||
if (servicePort != null) { | ||
ResourceConfig resourceConfig = getConfiguration().getResource().orElse(null); | ||
|
||
IngressBuilder ingressBuilder = new IngressBuilder(). | ||
withMetadata(serviceBuilder.getMetadata()). | ||
withMetadata(serviceMetadata). | ||
withNewSpec(). | ||
|
||
endSpec(); | ||
IngressSpecBuilder specBuilder = new IngressSpecBuilder().withBackend(new IngressBackendBuilder(). | ||
withNewServiceName(name). | ||
withNewServicePort(getServicePort(serviceBuilder)). | ||
build()); | ||
|
||
if (resourceConfig != null) { | ||
specBuilder.addAllToRules(resourceConfig.getIngressRules()); | ||
// removing `expose : true` label from metadata. | ||
ingressBuilder.withNewMetadataLike(removeExposeLabel(ingressBuilder.getMetadata())); | ||
|
||
if (StringUtils.isNotBlank(routeDomainPostfix)) { | ||
routeDomainPostfix = serviceName + "." + FileUtil.stripPrefix(routeDomainPostfix, "."); | ||
ingressBuilder = ingressBuilder.withSpec(new IngressSpecBuilder().addNewRule(). | ||
withHost(routeDomainPostfix). | ||
withNewHttp(). | ||
withPaths(new HTTPIngressPathBuilder() | ||
.withNewBackend() | ||
.withServiceName(serviceName) | ||
.withServicePort(KubernetesHelper.createIntOrString(getServicePort(serviceBuilder))) | ||
.endBackend() | ||
.build()) | ||
.endHttp(). | ||
endRule().build()); | ||
} else { | ||
ingressBuilder.withSpec(new IngressSpecBuilder().withBackend(new IngressBackendBuilder(). | ||
withNewServiceName(serviceName) | ||
.withNewServicePort(getServicePort(serviceBuilder)) | ||
.build()).build()); | ||
} | ||
|
||
listBuilder.addToIngressItems(ingressBuilder.build()); | ||
} | ||
} | ||
} | ||
|
@@ -126,13 +152,23 @@ public void visit(IngressBuilder builder) { | |
return answer.get(); | ||
} | ||
|
||
private boolean isExposedService(ServiceBuilder serviceBuilder) { | ||
Service service = serviceBuilder.build(); | ||
return isExposedService(service); | ||
private ObjectMeta removeExposeLabel(ObjectMeta metadata) { | ||
Map<String, String> labels = null; | ||
if (metadata != null) { | ||
labels = metadata.getLabels(); | ||
if (labels != null) { | ||
if ("true".equals(labels.get("expose"))) { | ||
labels.remove("expose"); | ||
} | ||
if("true".equals(labels.get(Fabric8Annotations.SERVICE_EXPOSE_URL.value()))) { | ||
labels.remove(Fabric8Annotations.SERVICE_EXPOSE_URL.value()); | ||
} | ||
} | ||
} | ||
return metadata; | ||
} | ||
|
||
private boolean isExposedService(Service service) { | ||
ObjectMeta metadata = service.getMetadata(); | ||
private boolean isExposedService(ObjectMeta metadata) { | ||
if (metadata != null) { | ||
Map<String, String> labels = metadata.getLabels(); | ||
if (labels != null) { | ||
|
@@ -141,8 +177,39 @@ private boolean isExposedService(Service service) { | |
} | ||
} | ||
} else { | ||
log.info("No Metadata for service! " + service); | ||
log.info("No Metadata for service! " + metadata.getName()); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Should we try to create an external URL for the given service? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either complete this javadoc or remove it. |
||
* <p/> | ||
* By default lets ignore the kubernetes services and any service which does not expose ports 80 and 443 | ||
* | ||
* @return true if we should create an Ingress for this service. | ||
*/ | ||
private boolean shouldCreateExternalURLForService(ServiceBuilder service) { | ||
String serviceName = service.getMetadata().getName(); | ||
if ("kubernetes".equals(serviceName) || "kubernetes-ro".equals(serviceName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we comparing Service names here like this? Do we even generate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these are the prefixes for the potential services generated by kubernetes system itself. I took it from the apply mojo. |
||
return false; | ||
} | ||
ServiceSpec spec = service.getSpec(); | ||
List<ServicePort> ports = spec.getPorts(); | ||
log.debug("Service " + serviceName + " has ports: " + ports); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is dumping whole object into log statement fine?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought its appropriate for log.debug. |
||
if (ports.size() == 1) { | ||
String type = null; | ||
if (spec != null) { | ||
type = spec.getType(); | ||
if (Objects.equals(type, "LoadBalancer")) { | ||
return true; | ||
} | ||
} | ||
log.info("Not generating Ingress for service " + serviceName + " type is not LoadBalancer: " + type); | ||
return false; | ||
} else { | ||
log.info("Not generating Ingress for service " + serviceName + " as only single port services are supported. Has ports: " + ports); | ||
return false; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this wrongfully in my previous PR here: dc5b213#diff-9238e96d57160d3b61190b609607b2d8R95
hence, removing it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove import above too