Skip to content

Commit

Permalink
JENKINS-56133: Add build url as default annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
MattLud committed Feb 18, 2019
1 parent 41545f6 commit 8bbabb9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import hudson.slaves.Cloud;
import jenkins.model.Jenkins;
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils;

public class PodTemplateStepExecution extends AbstractStepExecutionImpl {
Expand Down Expand Up @@ -95,6 +96,9 @@ public boolean start() throws Exception {
newTemplate.setNodeUsageMode(step.getNodeUsageMode());
newTemplate.setServiceAccount(step.getServiceAccount());
newTemplate.setAnnotations(step.getAnnotations());
if(run!=null) {
newTemplate.getAnnotations().add(new PodAnnotation("buildUrl", ((KubernetesCloud)cloud).getJenkinsUrl()+run.getUrl()));
}
newTemplate.setImagePullSecrets(
step.getImagePullSecrets().stream().map(x -> new PodImagePullSecret(x)).collect(toList()));
newTemplate.setYaml(step.getYaml());
Expand Down Expand Up @@ -126,7 +130,7 @@ public boolean start() throws Exception {

/**
* Check if the current Job is permitted to use the cloud.
*
*
* @param run
* @param kubernetesCloud
* @throws AbortException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,39 @@ public static Map<String, String> getLabels(KubernetesCloud cloud, Object o, Tes
return l;
}

public static Map<String, String> getAnnotations(KubernetesCloud cloud, String podName) {
HashMap<String, String> l = Maps.newHashMap(DEFAULT_LABELS);
if (cloud != null) {
try {
KubernetesClient client = cloud.connect();
PodList pods = client.pods().list();
for (Pod pod:pods.getItems())
{
for(Map.Entry<String, String> label : pod.getMetadata().getLabels().entrySet()){
if(label.getKey().contains(podName))
{
return pod.getMetadata().getAnnotations();
}
}
}
} catch (UnrecoverableKeyException ex) {
Logger.getLogger(KubernetesTestUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(KubernetesTestUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (KeyStoreException ex) {
Logger.getLogger(KubernetesTestUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KubernetesTestUtil.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertificateEncodingException ex) {
Logger.getLogger(KubernetesTestUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}

/**
* Delete pods with matching labels
*
*
* @param client
* @param labels
* @param wait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import java.util.Map;
import static org.junit.Assert.*;

import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.plugins.git.GitStep;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -56,6 +58,9 @@ public void declarative() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarative.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
Map<String,String> annotations = KubernetesTestUtil.getAnnotations(cloud, "declarative");
assertNotNull(annotations);
assertTrue(annotations.get("buildUrl").contains(b.getUrl()));
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodListBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil;

/**
* @author Carlos Sanchez
Expand All @@ -77,6 +78,9 @@ public void runInPod() throws Exception {

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName() + ".groovy"), true));
Map<String,String> annotations = KubernetesTestUtil.getAnnotations(cloud, "runInPod");
assertNotNull(annotations);
assertTrue(annotations.get("buildUrl").contains(p.getUrl()));

logs.capture(1000);
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
Expand Down

0 comments on commit 8bbabb9

Please sign in to comment.