Skip to content

Commit

Permalink
Moving informer registration to KubernetesCloud and make it thread
Browse files Browse the repository at this point in the history
safe.
  • Loading branch information
amuniz committed Dec 16, 2024
1 parent 5414ee3 commit 66e538e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.sanitizeLabel;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
Expand Down Expand Up @@ -51,11 +52,13 @@
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
Expand All @@ -72,6 +75,7 @@
import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateMap;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Default;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.PodRetention;
import org.csanchez.jenkins.plugins.kubernetes.watch.PodStatusEventHandler;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
Expand Down Expand Up @@ -931,10 +935,6 @@ public HttpResponse doCreate(StaplerRequest req, StaplerResponse rsp)
return FormApply.success("templates");
}

public Map<String, SharedIndexInformer<Pod>> getInformers() {
return informers;
}

@Extension
public static class DescriptorImpl extends Descriptor<Cloud> {
@Override
Expand Down Expand Up @@ -1320,6 +1320,31 @@ public Cloud reconfigure(@NonNull StaplerRequest req, JSONObject form) throws De
return newInstance;
}

public void registerPodInformer(KubernetesSlave node, KubernetesClient client, String namespace) {
if (informers.get(namespace) == null) {
synchronized (this) {
// sync recheck
if (informers.get(namespace) != null) {
return;
}
Map<String, String> labelsFilter =
new HashMap<>(node.getKubernetesCloud().getPodLabelsMap());
String jenkinsUrlLabel = sanitizeLabel(this.getJenkinsUrlOrNull());
if (jenkinsUrlLabel != null) {
labelsFilter.put(PodTemplateBuilder.LABEL_KUBERNETES_CONTROLLER, jenkinsUrlLabel);
}
SharedIndexInformer<Pod> inform = client.pods()
.inNamespace(namespace)
.withLabels(labelsFilter)
.inform(new PodStatusEventHandler(), TimeUnit.SECONDS.toMillis(30));
LOGGER.info(String.format(
"Registered informer to watch pod events on namespace [%s], with labels [%s]",
namespace, labelsFilter));
informers.put(namespace, inform);
}
}
}

@Extension
public static class PodTemplateSourceImpl extends PodTemplateSource {
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,8 @@ public synchronized void launch(SlaveComputer computer, TaskListener listener) {
.orElse(null);
node.setNamespace(namespace);

// register a namespace informer (if not registered yet) show relevant pod events in build logs
Map<String, SharedIndexInformer<Pod>> informers =
node.getKubernetesCloud().getInformers();
if (informers.get(namespace) == null) {
Map<String, String> labelsFilter =
new HashMap<>(node.getKubernetesCloud().getPodLabelsMap());
String jenkinsUrlLabel = sanitizeLabel(cloud.getJenkinsUrlOrNull());
if (jenkinsUrlLabel != null) {
labelsFilter.put(PodTemplateBuilder.LABEL_KUBERNETES_CONTROLLER, jenkinsUrlLabel);
}
SharedIndexInformer<Pod> inform = client.pods()
.inNamespace(namespace)
.withLabels(labelsFilter)
.inform(new PodStatusEventHandler(), TimeUnit.SECONDS.toMillis(30));
LOGGER.info(String.format(
"Registered informer to watch pod events on namespace [%s], with labels [%s]",
namespace, labelsFilter));
informers.put(namespace, inform);
}
// register a namespace informer (if not registered yet) to show relevant pod events in build logs
cloud.registerPodInformer(node, client, namespace);

// if the controller was interrupted after creating the pod but before it connected back, then
// the pod might already exist and the creating logic must be skipped.
Expand Down

0 comments on commit 66e538e

Please sign in to comment.