From 977e5abf170b065704d486a6308785d6ed5d1ed1 Mon Sep 17 00:00:00 2001 From: Krzysztof Taborski Date: Tue, 20 Apr 2021 08:17:01 +0200 Subject: [PATCH] Add support to remote kafka plugin --- plugin/pom.xml | 7 ++- .../openstack/compute/JCloudsCloud.java | 2 + .../openstack/compute/JCloudsSlave.java | 13 +++-- .../compute/JCloudsSlaveTemplate.java | 5 ++ .../compute/UserDataVariableResolver.java | 6 +++ .../compute/slaveopts/LauncherFactory.java | 48 +++++++++++++++++++ .../LauncherFactory/KAFKA/config.jelly | 9 ++++ 7 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 plugin/src/main/resources/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory/KAFKA/config.jelly diff --git a/plugin/pom.xml b/plugin/pom.xml index 282065e9..304635cb 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -26,7 +26,7 @@ 20.0 1.3.9 3.8 - 3.9.1 + 3.12.0 1.38 @@ -201,6 +201,11 @@ ${configuration-as-code.version} test + + io.jenkins.plugins.remoting-kafka + remoting-kafka + 2.0.1 + diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsCloud.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsCloud.java index 62ca656d..7e79496e 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsCloud.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsCloud.java @@ -164,6 +164,8 @@ private Object readResolve() { LauncherFactory lf = null; if ("JNLP".equals(slaveOptions.slaveType)) { lf = LauncherFactory.JNLP.JNLP; + } else if ("KAFKA".equals(slaveOptions.slaveType)) { + lf = LauncherFactory.KAFKA.KAFKA; } else if (!"JNLP".equals(slaveOptions.slaveType) && slaveOptions.credentialsId != null) { // user configured credentials and clearly rely on SSH launcher that used to be the default so bring it back lf = new LauncherFactory.SSH(slaveOptions.credentialsId); diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java index 029c0709..aff83693 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java @@ -159,10 +159,15 @@ protected Object readResolve() { .jvmOptions(Util.fixEmpty(jvmOptions)) ; - LauncherFactory lf = "SSH".equals(slaveType) - ? new LauncherFactory.SSH(credentialsId) - : LauncherFactory.JNLP.JNLP - ; + LauncherFactory lf = null; + if ("SSH".equals(slaveType)) { + lf = new LauncherFactory.SSH(credentialsId); + } else if ("JNLP".equals(slaveType)) { + lf = LauncherFactory.JNLP.JNLP; + } else if ("KAFKA".equals(slaveType)) { + lf = LauncherFactory.KAFKA.KAFKA; + } + builder.launcherFactory(lf); if (overrideRetentionTime > 0) { diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java index 6f837bed..70903163 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java @@ -122,6 +122,8 @@ private Object readResolve() { lf = new LauncherFactory.SSH(credentialsId); } else if("JNLP".equals(slaveType)) { lf = LauncherFactory.JNLP.JNLP; + } else if("KAFKA".equals(slaveType)) { + lf = LauncherFactory.KAFKA.KAFKA; } BootSource.Image bs = imageId == null ? null : new BootSource.Image(imageId); @@ -153,6 +155,9 @@ private Object readResolve() { } else if ("SSH".equals(slaveOptions.slaveType)) { lf = new LauncherFactory.SSH(slaveOptions.credentialsId); slaveOptions.slaveType = slaveOptions.credentialsId = null; + } else if ("KAFKA".equals(slaveOptions.slaveType)) { + lf = LauncherFactory.KAFKA.KAFKA; + slaveOptions.slaveType = null; } if (lf != null) { slaveOptions = slaveOptions.getBuilder().launcherFactory(lf).build(); diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/UserDataVariableResolver.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/UserDataVariableResolver.java index b3861500..a8a7ce43 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/UserDataVariableResolver.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/UserDataVariableResolver.java @@ -26,6 +26,7 @@ import hudson.Util; import hudson.util.VariableResolver; import jenkins.slaves.JnlpAgentReceiver; +import io.jenkins.plugins.remotingkafka.KafkaSecretManager; import javax.annotation.Nonnull; import java.util.LinkedHashMap; @@ -77,6 +78,11 @@ "Labels of the node.", r -> r.labelString ); + stub( + "SLAVE_KAFKA_SECRET", + "Labels of the node.", + r -> Util.fixNull(KafkaSecretManager.getConnectionSecret(r.serverName)) + ); } private static void stub(@Nonnull String name, @Nonnull String doc, @Nonnull ValueCalculator vc) { STUB.put(name, new Entry(name, doc, vc)); diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory.java index c6aaa0c3..cfe9cff8 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory.java @@ -69,6 +69,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import io.jenkins.plugins.remotingkafka.KafkaComputerLauncher; /** * Node launcher factory. * @@ -272,6 +273,53 @@ public LauncherFactory newInstance(StaplerRequest req, @Nonnull JSONObject formD } } + /** + * Wait for KAFKA connection to be made. + */ + public static final class KAFKA extends LauncherFactory { + private static final long serialVersionUID = -1112849796889317240L; + + public static final LauncherFactory KAFKA = new KAFKA(); + + @DataBoundConstructor // Needed for JCasC + public KAFKA() {} + + @Override + public ComputerLauncher createLauncher(@Nonnull JCloudsSlave slave) throws IOException { + Jenkins.get().addNode(slave); + return new KafkaComputerLauncher(false); + } + + @Override + public @CheckForNull String isWaitingFor(@Nonnull JCloudsSlave slave) { + // The address might not be visible at all so let's just wait for connection. + return slave.getChannel() != null ? null : "KAFKA connection was not established yet"; + } + + @Override + public int hashCode() { + return 31; + } + + @Override + public boolean equals(Object obj) { + return obj != null && getClass() == obj.getClass(); + } + + private Object readResolve() { + return KAFKA; // Let's avoid creating instances where we can + } + + @Extension + @Symbol("kafka") + public static final class Desc extends Descriptor { + @Override + public LauncherFactory newInstance(StaplerRequest req, @Nonnull JSONObject formData) { + return KAFKA; // Let's avoid creating instances where we can + } + } + } + /** * No slave type specified. This exists only as a field in UI dropdown to be read by stapler and converted to plain old null. */ diff --git a/plugin/src/main/resources/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory/KAFKA/config.jelly b/plugin/src/main/resources/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory/KAFKA/config.jelly new file mode 100644 index 00000000..64a736d8 --- /dev/null +++ b/plugin/src/main/resources/jenkins/plugins/openstack/compute/slaveopts/LauncherFactory/KAFKA/config.jelly @@ -0,0 +1,9 @@ + + + + +

Jenkins will wait for the Kafka connection to be initiated from the slave itself. Note this plugin does not initiate + that. See help for User Data field for more information on the available options.

+
+
+