Skip to content

Commit

Permalink
Add support to remote kafka plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ktaborski committed Apr 20, 2021
1 parent 38315dc commit 977e5ab
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
7 changes: 6 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<guava.version>20.0</guava.version> <!-- version compatible with openstack4j -->
<jsr305.version>1.3.9</jsr305.version>
<openstack4j.version>3.8</openstack4j.version>
<okhttp.version>3.9.1</okhttp.version>
<okhttp.version>3.12.0</okhttp.version>
<configuration-as-code.version>1.38</configuration-as-code.version>
</properties>

Expand Down Expand Up @@ -201,6 +201,11 @@
<version>${configuration-as-code.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.plugins.remoting-kafka</groupId>
<artifactId>remoting-kafka</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import io.jenkins.plugins.remotingkafka.KafkaComputerLauncher;
/**
* Node launcher factory.
*
Expand Down Expand Up @@ -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<LauncherFactory> {
@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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:section title="KAFKA remote agent launch">
<f:entry>
<p>Jenkins will wait for the Kafka connection to be initiated from the slave itself. Note this plugin does not initiate
that. See help for <i>User Data</i> field for more information on the available options.</p>
</f:entry>
</f:section>
</j:jelly>

0 comments on commit 977e5ab

Please sign in to comment.