Skip to content

Commit

Permalink
Merge pull request #12 from jimmidyson/update-pipeline-plugins
Browse files Browse the repository at this point in the history
Various stability fixes discovered during testing
  • Loading branch information
jimmidyson authored Nov 3, 2016
2 parents 1c95788 + fbca919 commit c2ce9a0
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 50 deletions.
95 changes: 82 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<properties>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>1.642.2</jenkins.version>
<jenkins.version>1.651.2</jenkins.version>
<!-- Java Level to use. Java 7 required when using core >= 1.612 -->
<java.level>7</java.level>
<!-- Jenkins Test Harness version you use to test the plugin. -->
Expand All @@ -42,7 +42,7 @@
~ hpi-plugin.version: The HPI Maven Plugin version used by the plugin..
~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
-->
<openshift-client.version>1.4.11</openshift-client.version>
<openshift-client.version>1.4.17</openshift-client.version>
</properties>

<name>OpenShift Sync</name>
Expand Down Expand Up @@ -110,41 +110,110 @@
<version>${openshift-client.version}</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.8</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.4.2</version>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.0</version>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>2.0.0</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.0</version>
<artifactId>workflow-api</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.pipeline-stage-view</groupId>
<artifactId>pipeline-rest-api</artifactId>
<version>1.4</version>
<version>2.1</version>
</dependency>

<dependency>
<groupId>org.csanchez.jenkins.plugins</groupId>
<artifactId>kubernetes</artifactId>
<version>0.6</version>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.21</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.pipeline-stage-view</groupId>
<artifactId>pipeline-stage-view</artifactId>
<version>1.0</version>
<version>2.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.openshift.jenkins</groupId>
<artifactId>openshift-pipeline</artifactId>
<version>1.0.28</version>
<exclusions>
<exclusion>
<groupId>com.openshift</groupId>
<artifactId>openshift-restclient-java</artifactId>
</exclusion>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
<exclusion>
<groupId>io.fabric8.jenkins.plugins</groupId>
<artifactId>openshift-sync</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,28 @@
*/
package io.fabric8.jenkins.openshiftsync;

import hudson.model.Job;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.scm.SCM;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.BuildConfigSpec;
import io.fabric8.openshift.api.model.BuildSource;
import io.fabric8.openshift.api.model.BuildStrategy;
import io.fabric8.openshift.api.model.GitBuildSource;
import io.fabric8.openshift.api.model.JenkinsPipelineBuildStrategy;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
Expand All @@ -50,13 +49,11 @@ public class BuildConfigToJobMapper {
public static final String DEFAULT_JENKINS_FILEPATH = "Jenkinsfile";
private static final Logger LOGGER = Logger.getLogger(BuildConfigToJobMapper.class.getName());

public static Job<WorkflowJob, WorkflowRun> mapBuildConfigToJob(BuildConfig bc, String defaultNamespace) {
public static FlowDefinition mapBuildConfigToFlow(BuildConfig bc, String namespace) throws IOException {
if (!OpenShiftUtils.isJenkinsBuildConfig(bc)) {
return null;
}

WorkflowJob job = new WorkflowJob(Jenkins.getInstance(), OpenShiftUtils.jenkinsJobName(bc, defaultNamespace));

BuildConfigSpec spec = bc.getSpec();
BuildSource source = null;
String jenkinsfile = null;
Expand Down Expand Up @@ -96,20 +93,16 @@ public static Job<WorkflowJob, WorkflowRun> mapBuildConfigToJob(BuildConfig bc,
Collections.<SubmoduleConfig>emptyList(),
null,
null,
null
Collections.<GitSCMExtension>emptyList()
);
job.setDefinition(new CpsScmFlowDefinition(scm, jenkinsfilePath));
return new CpsScmFlowDefinition(scm, jenkinsfilePath);
} else {
LOGGER.warning("BuildConfig does not contain source repository information - cannot map BuildConfig to Jenkins job");
return null;
}
} else {
job.setDefinition(new CpsFlowDefinition(jenkinsfile, true));
return new CpsFlowDefinition(jenkinsfile, true);
}

job.addTrigger(new BuildTrigger());

return job;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import hudson.model.Computer;
import hudson.model.Job;
import hudson.security.ACL;
import hudson.triggers.SafeTimerTask;
import hudson.util.XStream2;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
Expand All @@ -28,22 +29,21 @@
import jenkins.model.Jenkins;
import jenkins.security.NotReallyRoleSensitiveCallable;
import jenkins.util.Timer;

import org.apache.tools.ant.filters.StringInputStream;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jvnet.hudson.reactor.ReactorException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static io.fabric8.jenkins.openshiftsync.BuildConfigToJobMapper.mapBuildConfigToJob;
import static io.fabric8.jenkins.openshiftsync.BuildConfigToJobMapper.mapBuildConfigToFlow;
import static io.fabric8.jenkins.openshiftsync.OpenShiftUtils.getOpenShiftClient;
import static io.fabric8.jenkins.openshiftsync.OpenShiftUtils.isJenkinsBuildConfig;
import static io.fabric8.jenkins.openshiftsync.OpenShiftUtils.parseResourceVersion;
Expand Down Expand Up @@ -76,9 +76,9 @@ public void start(final Callable<Void> completionCallback) {
logger.info("Now handling startup build configs!!");
// lets do this in a background thread to avoid errors like:
// Tried proxying io.fabric8.jenkins.openshiftsync.GlobalPluginConfiguration to support a circular dependency, but it is not an interface.
Runnable task = new Runnable() {
Runnable task = new SafeTimerTask() {
@Override
public void run() {
public void doRun() {
logger.info("Waiting for Jenkins to be started");
while (true) {
Jenkins jenkins = Jenkins.getInstance();
Expand Down Expand Up @@ -117,7 +117,7 @@ public void run() {
}
};
// lets give jenkins a while to get started ;)
Timer.get().schedule(task, 10, TimeUnit.SECONDS);
Timer.get().schedule(task, 1, TimeUnit.SECONDS);
}

public void stop() {
Expand Down Expand Up @@ -146,7 +146,7 @@ public void onInitialBuildConfigs(BuildConfigList buildConfigs) {
for (BuildConfig buildConfig : items) {
try {
upsertJob(buildConfig);
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -174,14 +174,20 @@ public void eventReceived(Watcher.Action action, BuildConfig buildConfig) {
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private void upsertJob(final BuildConfig buildConfig) throws IOException {
private void upsertJob(final BuildConfig buildConfig) throws Exception {
if (isJenkinsBuildConfig(buildConfig)) {
ACL.impersonate(ACL.SYSTEM, new NotReallyRoleSensitiveCallable<Void, IOException>() {
ACL.impersonate(ACL.SYSTEM, new NotReallyRoleSensitiveCallable<Void, Exception>() {
@Override
public Void call() throws IOException {
public Void call() throws Exception {
String jobName = OpenShiftUtils.jenkinsJobName(buildConfig, namespace);
Job jobFromBuildConfig = mapBuildConfigToJob(buildConfig, namespace);
if (jobFromBuildConfig == null) {
WorkflowJob job = (WorkflowJob) BuildTrigger.getDscp().getJobFromBuildConfigUid(buildConfig.getMetadata().getUid());
boolean newJob = job == null;
if (newJob) {
job = new WorkflowJob(Jenkins.getInstance(), jobName);
}

FlowDefinition flowFromBuildConfig = mapBuildConfigToFlow(buildConfig, namespace);
if (flowFromBuildConfig == null) {
return null;
}

Expand All @@ -190,7 +196,7 @@ public Void call() throws IOException {
contextDir = buildConfig.getSpec().getSource().getContextDir();
}

jobFromBuildConfig.addProperty(
job.addProperty(
new BuildConfigProjectProperty(
buildConfig.getMetadata().getNamespace(),
buildConfig.getMetadata().getName(),
Expand All @@ -199,19 +205,21 @@ public Void call() throws IOException {
contextDir
)
);
job.addTrigger(new BuildTrigger());
job.setDefinition(flowFromBuildConfig);

InputStream jobStream = new StringInputStream(new XStream2().toXML(jobFromBuildConfig));
InputStream jobStream = new StringInputStream(new XStream2().toXML(job));

Jenkins jenkins = Jenkins.getInstance();
Job job = BuildTrigger.getDscp().getJobFromBuildConfigUid(buildConfig.getMetadata().getUid());
if (job == null) {

if (newJob) {
jenkins.createProjectFromXML(
jobName,
jobStream
);
).save();
logger.info("Created job " + jobName + " from BuildConfig " + NamespaceName.create(buildConfig) + " with revision: " + buildConfig.getMetadata().getResourceVersion());
} else {
BuildConfigProjectProperty buildConfigProjectProperty = (BuildConfigProjectProperty) job.getProperty(BuildConfigProjectProperty.class);
BuildConfigProjectProperty buildConfigProjectProperty = job.getProperty(BuildConfigProjectProperty.class);
if (buildConfigProjectProperty != null) {
long updatedBCResourceVersion = parseResourceVersion(buildConfig);
long oldBCResourceVersion = parseResourceVersion(buildConfigProjectProperty.getResourceVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import hudson.triggers.SafeTimerTask;
import io.fabric8.openshift.api.model.Build;
import jenkins.util.Timer;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -125,9 +126,9 @@ public synchronized void onStarted(Run run, TaskListener listener) {

protected void checkTimerStarted() {
if (timerStarted.compareAndSet(false, true)) {
Runnable task = new Runnable() {
Runnable task = new SafeTimerTask() {
@Override
public void run() {
protected void doRun() throws Exception {
pollLoop();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.Job;
import io.fabric8.kubernetes.api.model.Status;
import hudson.triggers.SafeTimerTask;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
Expand Down Expand Up @@ -64,9 +64,9 @@ public void start() {
logger.info("Now handling startup builds!!");
// lets do this in a background thread to avoid errors like:
// Tried proxying io.fabric8.jenkins.openshiftsync.GlobalPluginConfiguration to support a circular dependency, but it is not an interface.
Runnable task = new Runnable() {
Runnable task = new SafeTimerTask() {
@Override
public void run() {
public void doRun() {
logger.info("Waiting for Jenkins to be started");
while (true) {
Jenkins jenkins = Jenkins.getInstance();
Expand Down

0 comments on commit c2ce9a0

Please sign in to comment.