Skip to content

Commit

Permalink
do not reconcile pipeline, let Jenkins handles this itself
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed May 31, 2019
1 parent 8e47453 commit f3ebfae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package io.alauda.jenkins.devops.sync.listener;

import hudson.Extension;
import hudson.model.Cause;
import hudson.model.Queue;
import hudson.model.queue.QueueListener;
import io.alauda.jenkins.devops.sync.JenkinsPipelineCause;
import io.alauda.jenkins.devops.sync.util.AlaudaUtils;
import io.alauda.jenkins.devops.sync.util.PipelineUtils;

import java.util.List;
import java.util.logging.Logger;
Expand All @@ -38,19 +38,7 @@ public void onLeft(Queue.LeftItem leftItem) {
return;
}

JenkinsPipelineCause pipelineCause = null;
List<Cause> causes = leftItem.getCauses();
if (causes != null) {
for (Cause cause : causes) {
if (!(cause instanceof JenkinsPipelineCause)) {
continue;
}

pipelineCause = (JenkinsPipelineCause) cause;
}
}

String itemUrl = leftItem.getUrl();
JenkinsPipelineCause pipelineCause = PipelineUtils.findAlaudaCause(leftItem);
if (pipelineCause != null) {
String namespace = pipelineCause.getNamespace();
String name = pipelineCause.getName();
Expand All @@ -65,6 +53,7 @@ public void onLeft(Queue.LeftItem leftItem) {

logger.info("Item " + leftItem + " already sync with alauda'resource.");
} else {
String itemUrl = leftItem.getUrl();
logger.warning("Can not found JenkinsPipelineCause, item url: " + itemUrl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public boolean apply(@Nullable Run run) {
return false;
}

JenkinsPipelineCause cause = (JenkinsPipelineCause) run.getCause(JenkinsPipelineCause.class);
JenkinsPipelineCause cause = PipelineUtils.findAlaudaCause(run);
if(cause == null) {
return false;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public synchronized void onDeleted(Run run) {
return;
}

JenkinsPipelineCause cause = (JenkinsPipelineCause) run.getCause(JenkinsPipelineCause.class);
JenkinsPipelineCause cause = PipelineUtils.findAlaudaCause(run);
if (cause != null) {
String namespace = cause.getNamespace();
String pipelineName = cause.getName();
Expand Down Expand Up @@ -324,29 +324,6 @@ private String toBlueJson(@NotNull PipelineJson pipeJson) {
return null;
}

/**
* All job build caused by Alauda which will hold JenkinsPipelineCause
* @param run job build
* @return JenkinsPipelineCause
*/
private JenkinsPipelineCause findAlaudaCause(@NotNull Run run) {
List<CauseAction> causeActions = run.getActions(CauseAction.class);
if(causeActions == null) {
return null;
}

JenkinsPipelineCause jenkinsPipelineCause = null;
for(CauseAction action : causeActions) {
Optional<Cause> causeOption = action.getCauses().stream().filter(cause -> cause instanceof JenkinsPipelineCause).findFirst();
if(causeOption != null && causeOption.isPresent()) {
jenkinsPipelineCause = (JenkinsPipelineCause) causeOption.get();
break;
}
}

return jenkinsPipelineCause;
}

private void upsertPipeline(@NotNull Run run, RunExt wfRunExt, BlueRun blueRun) throws TimeoutException, InterruptedException {
final AlaudaDevOpsClient client = getAuthenticatedAlaudaClient();
if(client == null) {
Expand All @@ -359,7 +336,7 @@ private void upsertPipeline(@NotNull Run run, RunExt wfRunExt, BlueRun blueRun)
logger.fine(() -> "run " + run + " caused by " + causeItem);
});
}
JenkinsPipelineCause cause = findAlaudaCause(run);
JenkinsPipelineCause cause = PipelineUtils.findAlaudaCause(run);
if(cause == null) {
logger.warning("run " + run + " do not have JenkinsPipelineCause");
}
Expand Down
34 changes: 11 additions & 23 deletions src/main/java/io/alauda/jenkins/devops/sync/util/JenkinsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private static WorkflowRun getRun(WorkflowJob job, Pipeline pipeline) {

private static WorkflowRun getRun(WorkflowJob job, String pipelineUid) {
for (WorkflowRun run : job.getBuilds()) {
JenkinsPipelineCause cause = run.getCause(JenkinsPipelineCause.class);
JenkinsPipelineCause cause = PipelineUtils.findAlaudaCause(run);
if (cause != null && cause.getUid().equals(pipelineUid)) {
return run;
}
Expand Down Expand Up @@ -580,18 +580,6 @@ private static boolean cancelNotYetStartedPipeline(WorkflowJob job, Pipeline pip
return false;
}

private static void cancelNotYetStartedPipeliness(WorkflowJob job, String pcUid) {
cancelQueuedBuilds(job, pcUid);
for (WorkflowRun run : job.getBuilds()) {
if (run != null && run.hasntStartedYet()) {
JenkinsPipelineCause cause = run.getCause(JenkinsPipelineCause.class);
if (cause != null && cause.getPipelineConfigUid().equals(pcUid)) {
terminateRun(run);
}
}
}
}

private static void terminateRun(final WorkflowRun run) {
ACL.impersonate(ACL.SYSTEM, new NotReallyRoleSensitiveCallable<Void, RuntimeException>() {
@Override
Expand Down Expand Up @@ -639,16 +627,16 @@ public static void cancelQueuedBuilds(WorkflowJob job, String pcUid) {
LOGGER.info(() -> "cancelling queued pipeline by uuid: "+pcUid);
Queue pipelineQueue = Jenkins.getInstance().getQueue();
for (Queue.Item item : pipelineQueue.getItems()) {
for (Cause cause : item.getCauses()) {
if (cause instanceof JenkinsPipelineCause) {
JenkinsPipelineCause pipelineCause = (JenkinsPipelineCause) cause;
if (pipelineCause.getPipelineConfigUid().equals(pcUid)) {
Pipeline pipeline = new PipelineBuilder().withNewMetadata().withNamespace(pipelineCause.getNamespace())
.withName(pipelineCause.getName()).and().build();
cancelQueuedPipeline(job, pipeline);
}
}
}
JenkinsPipelineCause pipelineCause = PipelineUtils.findAlaudaCause(item);
if(pipelineCause == null) {
continue;
}

if (pipelineCause.getPipelineConfigUid().equals(pcUid)) {
Pipeline pipeline = new PipelineBuilder().withNewMetadata().withNamespace(pipelineCause.getNamespace())
.withName(pipelineCause.getName()).and().build();
cancelQueuedPipeline(job, pipeline);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.alauda.jenkins.devops.sync.util;

import hudson.model.Actionable;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Result;
import hudson.model.Run;
import hudson.util.RunList;
Expand All @@ -13,11 +16,39 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static io.alauda.jenkins.devops.sync.constants.Constants.ALAUDA_DEVOPS_LABELS_PIPELINE_CONFIG;

public class PipelineUtils {
/**
* All job build caused by Alauda which will hold JenkinsPipelineCause
* @param actionable actionable object
* @return JenkinsPipelineCause
*/
public static JenkinsPipelineCause findAlaudaCause(Actionable actionable) {
if(actionable == null) {
return null;
}
List<CauseAction> causeActions = actionable.getActions(CauseAction.class);
if(causeActions == null) {
return null;
}

JenkinsPipelineCause jenkinsPipelineCause = null;
for(CauseAction action : causeActions) {
Optional<Cause> causeOption = action.getCauses().stream().filter(cause -> cause instanceof JenkinsPipelineCause).findFirst();
if(causeOption != null && causeOption.isPresent()) {
jenkinsPipelineCause = (JenkinsPipelineCause) causeOption.get();
break;
}
}

return jenkinsPipelineCause;
}

public static boolean delete(String namespace, String name) {
AlaudaDevOpsClient client = AlaudaUtils.getAuthenticatedAlaudaClient();
if(client == null) {
Expand Down Expand Up @@ -51,8 +82,7 @@ public static void pipelinesCheck(PipelineConfig config) {
list.getItems().forEach(pipeline -> {
String uid = pipeline.getMetadata().getUid();
RunList<WorkflowRun> runList = job.getBuilds().filter(run -> {
JenkinsPipelineCause cause = run.getCause(JenkinsPipelineCause.class);

JenkinsPipelineCause cause = PipelineUtils.findAlaudaCause(run);

return cause != null && cause.getUid().equals(uid);// && phase.equals(pipeline.getStatus().getPhase());
});//.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import hudson.Extension;
import hudson.security.ACL;
import io.alauda.devops.client.AlaudaDevOpsClient;
import io.alauda.jenkins.devops.sync.JenkinsPipelineCause;
import io.alauda.jenkins.devops.sync.PipelineNumComparator;
import io.alauda.jenkins.devops.sync.WorkflowJobProperty;
import io.alauda.jenkins.devops.sync.WatcherCallback;
Expand All @@ -31,11 +30,9 @@
import io.alauda.jenkins.devops.sync.util.WorkflowJobUtils;
import io.alauda.kubernetes.api.model.*;
import io.alauda.kubernetes.client.Watcher;
import jenkins.model.Jenkins;
import jenkins.security.NotReallyRoleSensitiveCallable;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import javax.validation.constraints.NotNull;
import java.io.IOException;
Expand Down Expand Up @@ -109,8 +106,6 @@ public void init(String[] namespaces) {
logger.log(Level.SEVERE, "Failed to load initial Builds: " + e, e);
}
}

reconcileRunsAndPipelines();
}

private PipelineList filterNew(PipelineList list) {
Expand Down Expand Up @@ -399,55 +394,6 @@ private static synchronized void deleteEventToJenkinsJobRun(
innerDeleteEventToJenkinsJobRun(pipeline);
}

/**
* Reconciles Jenkins job runs and Alauda DevOps pipelines
* <p>
* Deletes all job runs that do not have an associated build in Alauda DevOps
*/
private static synchronized void reconcileRunsAndPipelines() {
logger.info("Reconciling job runs and pipelines");

List<WorkflowJob> jobs = Jenkins.getInstance().getAllItems(WorkflowJob.class);

for (WorkflowJob job : jobs) {
WorkflowJobProperty pcpp = WorkflowJobUtils.getAlaudaProperty(job);
if (pcpp == null) {
// If we encounter a job without a BuildConfig, skip the reconciliation logic
continue;
}

// all workflow jobs will have this property even they're created by manual
if(StringUtils.isBlank(pcpp.getUid())) {
try {
//
job.removeProperty(pcpp);
} catch (IOException e) {
e.printStackTrace();
}
continue;
}

PipelineList pipelineList = AlaudaUtils.getAuthenticatedAlaudaClient().pipelines()
.inNamespace(pcpp.getNamespace()).withLabel(Constants.ALAUDA_DEVOPS_LABELS_PIPELINE_CONFIG, pcpp.getName()).list();

logger.info("Checking runs for PipelineConfig " + pcpp.getNamespace() + "/" + pcpp.getName());

for (WorkflowRun run : job.getBuilds()) {
boolean found = false;
JenkinsPipelineCause cause = run.getCause(JenkinsPipelineCause.class);
for (Pipeline build : pipelineList.getItems()) {
if (cause != null && cause.getUid().equals(build.getMetadata().getUid())) {
found = true;
break;
}
}
if (!found) {
JenkinsUtils.deleteRun(run);
}
}
}
}

@Override
public final String getName() {
return "PipelineWatcher";
Expand Down

0 comments on commit f3ebfae

Please sign in to comment.