Skip to content

Commit

Permalink
Merge pull request alauda#64 from LinuxSuRen/fea/update-trigger
Browse files Browse the repository at this point in the history
Upgrade kubernetes-model
  • Loading branch information
LinuxSuRen committed Dec 4, 2018
2 parents c68ac8c + f76e04a commit 53f06a5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 59 deletions.
15 changes: 1 addition & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
<packaging>hpi</packaging>

<properties>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>2.83</jenkins.version>
<!-- Java Level to use. Java 7 required when using core >= 1.612 -->
<java.level>8</java.level>

<devops-client.version>0.2.6</devops-client.version>
Expand All @@ -50,7 +48,6 @@
<org.json.version>20180130</org.json.version>
<okhttp.bundle.version>3.9.1_2</okhttp.bundle.version>
<jsonschema2pojo.version>0.4.23</jsonschema2pojo.version>
<!--<junit.version>4.12</junit.version>-->
<validation-api.version>1.1.0.Final</validation-api.version>
<lombok.version>1.16.10</lombok.version>

Expand Down Expand Up @@ -132,7 +129,7 @@
<dependency>
<groupId>io.alauda</groupId>
<artifactId>kubernetes-model</artifactId>
<version>0.2.6</version>
<version>0.2.7</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -241,16 +238,6 @@
<groupId>org.csanchez.jenkins.plugins</groupId>
<artifactId>kubernetes</artifactId>
<version>1.5</version>
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>io.fabric8</groupId>-->
<!--<artifactId>kubernetes-client</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<groupId>org.apache.httpcomponent</groupId>-->
<!--<artifactId>httpclient</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,31 @@ private static PipelineTrigger findPipelineTriggers(@Nonnull PipelineConfig pipe
* @param pipelineConfig PipelineConfig
*/
private static void updateTrigger(WorkflowJob job, PipelineConfig pipelineConfig) {
// checking if there are triggers to be updated
List<PipelineTrigger> pipelineConfigTriggers = pipelineConfig.getSpec().getTriggers();
Map<TriggerDescriptor, Trigger<?>> triggers = job.getTriggers();

pipelineConfigTriggers.clear();
if (triggers == null) {
return;
}

// checking if there are triggers to be updated
List<PipelineTrigger> pipelineConfigTriggers = pipelineConfig.getSpec().getTriggers();
if(pipelineConfigTriggers == null) {
pipelineConfig.getSpec().setTriggers(new ArrayList<>());
} else {
pipelineConfigTriggers.clear();
}

triggers.forEach((desc, trigger) -> {
PipelineTrigger pipelineTrigger = null;
if (trigger instanceof SCMTrigger) {
pipelineTrigger = new PipelineTriggerBuilder().withType(Constants.PIPELINE_TRIGGER_TYPE_CODE_CHANGE).withNewCodeChange().withEnabled(true).withPeriodicCheck(trigger.getSpec()).endCodeChange().build();
pipelineTrigger = new PipelineTriggerBuilder().withType(Constants.PIPELINE_TRIGGER_TYPE_CODE_CHANGE)
.withNewCodeChange().withEnabled(true).withPeriodicCheck(trigger.getSpec()).endCodeChange().build();
} else if (trigger instanceof TimerTrigger) {
pipelineTrigger = new PipelineTriggerBuilder().withType(Constants.PIPELINE_TRIGGER_TYPE_CRON).withNewCron(true, trigger.getSpec()).build();
pipelineTrigger = new PipelineTriggerBuilder().withType(Constants.PIPELINE_TRIGGER_TYPE_CRON)
.withNewCron().withEnabled(true).withRule(trigger.getSpec()).endCron().build();
}

if (pipelineTrigger != null) {
pipelineConfigTriggers.add(pipelineTrigger);
pipelineConfig.getSpec().getTriggers().add(pipelineTrigger);
} else {
LOGGER.warning(() -> "Not support trigger type : " + trigger.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,6 @@ public PipelineSyncRunListener(long pollPeriodMs) {
this.pollPeriodMs = pollPeriodMs;
}

/**
* Joins all the given strings, ignoring nulls so that they form a URL with
* / between the paths without a // if the previous path ends with / and the
* next path starts with / unless a path item is blank
*
* @param strings the sequence of strings to join
* @return the strings concatenated together with / while avoiding a double
* // between non blank strings.
*/
public static String joinPaths(String... strings) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
sb.append(strings[i]);
if (i < strings.length - 1) {
sb.append("/");
}
}
String joined = sb.toString();

// And normalize it...
return joined.replaceAll("/+", "/").replaceAll("/\\?", "?").replaceAll("/#", "#").replaceAll(":/", "://");
}

@Override
public void onInitialize(Run run) {
super.onInitialize(run);
Expand All @@ -129,15 +106,6 @@ public void onInitialize(Run run) {
@Override
public void onStarted(Run run, TaskListener listener) {
if (shouldPollRun(run)) {
try {
JenkinsPipelineCause cause = (JenkinsPipelineCause) run.getCause(JenkinsPipelineCause.class);
if (cause != null) {
// TODO This should be a link to the Alauda DevOps
run.setDescription(cause.getShortDescription());
}
} catch (IOException e) {
logger.log(WARNING, "Cannot set build description: " + e);
}
if (runsToPoll.add(run)) {
logger.info("starting polling build " + run.getUrl());
}
Expand All @@ -147,7 +115,7 @@ public void onStarted(Run run, TaskListener listener) {
}
}

protected void checkTimerStarted() {
private void checkTimerStarted() {
if (timerStarted.compareAndSet(false, true)) {
Timer.get().scheduleAtFixedRate(new SafeTimerTask() {
@Override
Expand Down Expand Up @@ -274,7 +242,14 @@ private void pollLoop() {
try {
pollRun(run);

runsToPoll.remove(run);
StatusExt status = RunExt.create((WorkflowRun) run).getStatus();
switch(status) {
case IN_PROGRESS:
case PAUSED_PENDING_INPUT:
continue;
default:
runsToPoll.remove(run);
}
} catch (KubernetesClientException e) {
e.printStackTrace();
} catch (TimeoutException e) {
Expand Down Expand Up @@ -317,7 +292,7 @@ private boolean shouldUpdatePipeline(JenkinsPipelineCause cause, int latestStage
logger.fine(String.format("shouldUpdatePipeline curr time %s last update %s curr stage num %s last stage num %s" + "curr flow num %s last flow num %s status %s", String.valueOf(currTime), String.valueOf(cause.getLastUpdateToAlaudaDevOps()), String.valueOf(latestStageNum), String.valueOf(cause.getNumStages()), String.valueOf(latestNumFlowNodes), String.valueOf(cause.getNumFlowNodes()), status.toString()));

// if we have not updated in maxDelay time, update
if (currTime > (cause.getLastUpdateToAlaudaDevOps() + maxDelay)) {
if (currTime > (cause.getLastUpdateToAlaudaDevOps() + pollPeriodMs)) {
return true;
}

Expand Down Expand Up @@ -698,8 +673,32 @@ private int getRunNumber(Run run) {
* @param run the Run to test against
* @return true if the should poll the status of this build run
*/
protected boolean shouldPollRun(Run run) {
return run instanceof WorkflowRun && run.getCause(JenkinsPipelineCause.class) != null && AlaudaSyncGlobalConfiguration.get().isEnabled();
private boolean shouldPollRun(Run run) {
return run instanceof WorkflowRun && run.getCause(JenkinsPipelineCause.class) != null &&
AlaudaSyncGlobalConfiguration.get().isEnabled();
}

/**
* Joins all the given strings, ignoring nulls so that they form a URL with
* / between the paths without a // if the previous path ends with / and the
* next path starts with / unless a path item is blank
*
* @param strings the sequence of strings to join
* @return the strings concatenated together with / while avoiding a double
* // between non blank strings.
*/
public static String joinPaths(String... strings) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strings.length; i++) {
sb.append(strings[i]);
if (i < strings.length - 1) {
sb.append("/");
}
}
String joined = sb.toString();

// And normalize it...
return joined.replaceAll("/+", "/").replaceAll("/\\?", "?").replaceAll("/#", "#").replaceAll(":/", "://");
}

private static class BlueJsonStage {
Expand Down Expand Up @@ -750,7 +749,8 @@ private static class PipelineStage {
public Long pause_duration_millis;
public List<BluePipelineNode.Edge> edges;

PipelineStage(String id, String name, String status, String result, String start_time, Long duration_millis, Long pause_duration_millis, List<BluePipelineNode.Edge> edges) {
PipelineStage(String id, String name, String status, String result, String start_time, Long duration_millis,
Long pause_duration_millis, List<BluePipelineNode.Edge> edges) {
this.id = id;
this.name = name;
this.status = status;
Expand Down

0 comments on commit 53f06a5

Please sign in to comment.