From 881c315f7cbb68be09d568ef0dc3a78b3f003e13 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Fri, 30 Oct 2020 21:42:18 +0530 Subject: [PATCH] Added a Quickstart for Implementing your own Custom Enricher Added a Quickstart for JKube Kit Enricher API on how you can extend JKube Kit and add an enricher for your own Custom Use case. This quickstart contains a simple Spring Boot application which uses a custom made enricher to generate dummy Istio Gateway. --- CHANGELOG.md | 1 + .../kit/custom-istio-enricher/README.md | 71 ++++++++++++++ .../kit/custom-istio-enricher/app/pom.xml | 92 +++++++++++++++++++ .../kit/enricher/app/Application.java | 24 +++++ .../kit/enricher/app/HelloController.java | 26 ++++++ .../istio-enricher/pom.xml | 52 +++++++++++ .../kit/enricher/istio/IstioEnricher.java | 69 ++++++++++++++ .../main/resources/META-INF/jkube/enricher | 1 + quickstarts/kit/custom-istio-enricher/pom.xml | 68 ++++++++++++++ 9 files changed, 404 insertions(+) create mode 100644 quickstarts/kit/custom-istio-enricher/README.md create mode 100644 quickstarts/kit/custom-istio-enricher/app/pom.xml create mode 100644 quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/Application.java create mode 100644 quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/HelloController.java create mode 100644 quickstarts/kit/custom-istio-enricher/istio-enricher/pom.xml create mode 100644 quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/java/org/eclipse/jkube/quickstart/kit/enricher/istio/IstioEnricher.java create mode 100644 quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/resources/META-INF/jkube/enricher create mode 100644 quickstarts/kit/custom-istio-enricher/pom.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cfaac280c..dee7d22e42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Usage: ``` ### 1.1.0-SNAPSHOT * Fix #467: Upgrade assertj-core to 3.18.0 +* Added a Quickstart for implementing and using a Custom Enricher based on Eclipse JKube Kit Enricher API ### 1.0.2 (2020-10-30) * Fix #429: Added quickstart for Micronaut framework diff --git a/quickstarts/kit/custom-istio-enricher/README.md b/quickstarts/kit/custom-istio-enricher/README.md new file mode 100644 index 0000000000..e7426375b7 --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/README.md @@ -0,0 +1,71 @@ +# JKube Kit - Custom Enricher Using Eclipse JKube Enricher API + +This example demonstrates how you can extend Eclipse JKube Kit's Enricher API to make your own enricher and use it to enrich or generate manifests as per your requirements. This is a multi module project which contains these modules: + +- istio-enricher : A basic IstioEnricher which generates a dummy `networking.istio.io/v1alpha3` Gateway manifest +- app : A basic spring boot application which uses this enricher with Eclipse JKube + +# How to Build: +Just need to run: +```bash +mvn clean install +``` + +# How to Run: +This project demonstrates use of Custom Enricher. You can check `pom.xml` of `app/` project to see how Custom Enricher is integrated into it. When you would run resource goal, you should be able to see enricher in action: + +``` +custom-istio-enricher : $ cd app/ +app : $ mvn k8s:resource +[INFO] Scanning for projects... +[INFO] +[INFO] --< org.eclipse.jkube.quickstarts.kit:eclipse-jkube-sample-custom-enricher-app >-- +[INFO] Building Eclipse JKube :: Sample :: Custom Enricher :: App 1.1.0-SNAPSHOT +[INFO] --------------------------------[ jar ]--------------------------------- +[INFO] +[INFO] --- kubernetes-maven-plugin:1.1.0-SNAPSHOT:resource (default-cli) @ eclipse-jkube-sample-custom-enricher-app --- +[WARNING] k8s: Cannot access cluster for detecting mode: No route to host (Host unreachable) +[INFO] k8s: Running generator spring-boot +[INFO] k8s: spring-boot: Using Docker image quay.io/jkube/jkube-java-binary-s2i:0.0.8 as base / builder +[INFO] k8s: Using resource templates from /home/rohaan/work/repos/jkube/quickstarts/kit/custom-istio-enricher/app/src/main/jkube +[INFO] k8s: jkube-service: Adding a default service 'eclipse-jkube-sample-custom-enricher-app' with ports [8080] +[INFO] k8s: jkube-revision-history: Adding revision history limit to 2 +[INFO] k8s: istio-enricher: Added dummy networking.istio.io/v1alpha3 Gateway +[INFO] k8s: istio-enricher: Exiting Istio Enricher +[INFO] k8s: validating /home/rohaan/work/repos/jkube/quickstarts/kit/custom-istio-enricher/app/target/classes/META-INF/jkube/kubernetes/eclipse-jkube-sample-custom-enricher-app.yml resource +[WARNING] k8s: Failed to validate resources: null +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD SUCCESS +[INFO] ------------------------------------------------------------------------ +[INFO] Total time: 6.108 s +[INFO] Finished at: 2020-10-30T22:25:51+05:30 +[INFO] ------------------------------------------------------------------------ +``` +After running resource goal, you should be able to see a dummy `Gateway` manifest in target directory: +``` +app : $ cat target/classes/META-INF/jkube/kubernetes/eclipse-jkube-sample-custom-enricher-app.yml +--- +apiVersion: networking.istio.io/v1alpha3 +kind: Gateway +metadata: + labels: + app: eclipse-jkube-sample-custom-enricher-app + provider: jkube + version: 1.1.0-SNAPSHOT + group: org.eclipse.jkube.quickstarts.kit + name: eclipse-jkube-sample-custom-enricher-app +spec: + selector: + app: test-app + servers: + - hosts: + - uk.bookinfo.com + - in.bookinfo.com + port: + name: http + number: 80 + protocol: HTTP + tls: + httpsRedirect: true + +``` diff --git a/quickstarts/kit/custom-istio-enricher/app/pom.xml b/quickstarts/kit/custom-istio-enricher/app/pom.xml new file mode 100644 index 0000000000..e20f4df0a9 --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/app/pom.xml @@ -0,0 +1,92 @@ + + + + + 4.0.0 + + eclipse-jkube-sample-custom-enricher-app + jar + 1.1.0-SNAPSHOT + + + org.eclipse.jkube.quickstarts.kit + eclipse-jkube-sample-custom-enricher-parent + 1.1.0-SNAPSHOT + ../pom.xml + + + Eclipse JKube :: Quickstarts :: Kit :: Custom Enricher App + Spring Boot example with a custom enricher + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.eclipse.jkube + kubernetes-maven-plugin + ${jkube.version} + + + + + istio-enricher + + + + ${project.artifactId} + + + + + + + + + resource + + + + + + + + org.eclipse.jkube.quickstarts.kit + eclipse-jkube-sample-custom-enricher-istio + ${project.version} + + + + + + + diff --git a/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/Application.java b/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/Application.java new file mode 100644 index 0000000000..a30fe985cf --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/Application.java @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.quickstarts.kit.enricher.app; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/HelloController.java b/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/HelloController.java new file mode 100644 index 0000000000..382ea1244d --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/app/src/main/java/org/eclipse/jkube/quickstarts/kit/enricher/app/HelloController.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.quickstarts.kit.enricher.app; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @GetMapping("/") + public String index() { + return "Hello World!"; + } +} \ No newline at end of file diff --git a/quickstarts/kit/custom-istio-enricher/istio-enricher/pom.xml b/quickstarts/kit/custom-istio-enricher/istio-enricher/pom.xml new file mode 100644 index 0000000000..d7aaeb64b2 --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/istio-enricher/pom.xml @@ -0,0 +1,52 @@ + + + + + 4.0.0 + + eclipse-jkube-sample-custom-enricher-istio + jar + 1.1.0-SNAPSHOT + + + org.eclipse.jkube.quickstarts.kit + eclipse-jkube-sample-custom-enricher-parent + 1.1.0-SNAPSHOT + ../pom.xml + + + Eclipse JKube :: Quickstarts :: Kit :: Custom Istio Enricher + Custom Istio Enricher which extends Eclipse JKube Enricher API and Fabric8 Istio Java Client to create Istio Gateway Manifest + + + + org.eclipse.jkube + jkube-kit-enricher-api + + + + commons-codec + commons-codec + + + + me.snowdrop + istio-client + + + diff --git a/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/java/org/eclipse/jkube/quickstart/kit/enricher/istio/IstioEnricher.java b/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/java/org/eclipse/jkube/quickstart/kit/enricher/istio/IstioEnricher.java new file mode 100644 index 0000000000..686cf2b112 --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/java/org/eclipse/jkube/quickstart/kit/enricher/istio/IstioEnricher.java @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2019 Red Hat, Inc. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at: + * + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + */ +package org.eclipse.jkube.quickstart.kit.enricher.istio; + +import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import me.snowdrop.istio.api.networking.v1alpha3.GatewayBuilder; +import org.eclipse.jkube.kit.common.Configs; +import org.eclipse.jkube.kit.config.resource.PlatformMode; +import org.eclipse.jkube.kit.enricher.api.BaseEnricher; +import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; + +public class IstioEnricher extends BaseEnricher { + + public IstioEnricher(JKubeEnricherContext enricherContext) { + super(enricherContext, "istio-enricher"); + } + + // Available configuration keys + private enum Config implements Configs.Config { + // name of the gateway to create + name; + + public String def() { return d; } protected String d; + } + + @Override + public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { + log.info("Added dummy networking.istio.io/v1alpha3 Gateway"); + builder.addToItems(createGatewayBuilder()); + log.info("Exiting Istio Enricher"); + } + + private GatewayBuilder createGatewayBuilder() { + return new GatewayBuilder() + .withNewMetadata() + .withName(getGatewayName()) + .endMetadata() + .withNewSpec() + .addToSelector("app", "test-app") + .addNewServer() + .withNewPort() + .withNumber(80) + .withName("http") + .withProtocol("HTTP") + .endPort() + .addNewHost("uk.bookinfo.com") + .addNewHost("in.bookinfo.com") + .withNewTls() + .withHttpsRedirect(true) + .endTls() + .endServer() + .endSpec(); + } + + private String getGatewayName() { + return getConfig(Config.name); + } +} diff --git a/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/resources/META-INF/jkube/enricher b/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/resources/META-INF/jkube/enricher new file mode 100644 index 0000000000..6bb944d67c --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/istio-enricher/src/main/resources/META-INF/jkube/enricher @@ -0,0 +1 @@ +org.eclipse.jkube.quickstart.kit.enricher.istio.IstioEnricher \ No newline at end of file diff --git a/quickstarts/kit/custom-istio-enricher/pom.xml b/quickstarts/kit/custom-istio-enricher/pom.xml new file mode 100644 index 0000000000..312f5a8931 --- /dev/null +++ b/quickstarts/kit/custom-istio-enricher/pom.xml @@ -0,0 +1,68 @@ + + + + + 4.0.0 + + eclipse-jkube-sample-custom-enricher-parent + org.eclipse.jkube.quickstarts.kit + 1.1.0-SNAPSHOT + pom + + + org.springframework.boot + spring-boot-starter-parent + 2.3.5.RELEASE + + + Eclipse JKube :: Quickstarts :: Kit :: Custom Istio Enricher Parent + Eclipse JKube Kit example to build and use a Custom Enricher in a SpringBoot Application. It will demonstrate how you can extend Eclipse JKube Kit's Enricher API to make your own enricher and use it to enrich or generate manifests as per your requirements. + + + ${project.version} + 1.10 + 1.5.5 + + + + istio-enricher + app + + + + + + org.eclipse.jkube + jkube-kit-enricher-api + ${jkube.version} + + + + commons-codec + commons-codec + ${commons-codec.version} + + + + me.snowdrop + istio-client + ${istio-client.version} + + + +