Skip to content

Commit

Permalink
Add cancel support
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Dec 4, 2024
1 parent 622be10 commit 85b16e4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.redhat.hacbs.cli.driver;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redhat.hacbs.driver.Driver;
import com.redhat.hacbs.driver.dto.CancelRequest;

import picocli.CommandLine;

@CommandLine.Command(name = "cancel-pipeline", mixinStandardHelpOptions = true, description = "Creates a pipeline")
public class CancelPipeline implements Runnable {

Check warning on line 15 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java#L15

Added line #L15 was not covered by tests

private static final Logger logger = LoggerFactory.getLogger(CancelPipeline.class);

Check warning on line 17 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java#L17

Added line #L17 was not covered by tests

@Inject
Driver driver;

@CommandLine.Option(names = "-n", description = "Namespace", defaultValue = "pnc-devel-tenant")
String namespace;

@CommandLine.Option(names = "-p", description = "Pipeline name")
String pipeline;

@ActivateRequestContext // https://github.com/quarkusio/quarkus/issues/8758
@Override
public void run() {
var cancel = CancelRequest.builder()
.namespace(namespace)
.pipelineId(pipeline)
.build();

Check warning on line 34 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java#L31-L34

Added lines #L31 - L34 were not covered by tests

driver.cancel(cancel);
}

Check warning on line 37 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java#L36-L37

Added lines #L36 - L37 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import picocli.CommandLine;

@CommandLine.Command(name = "pipeline", mixinStandardHelpOptions = true, description = "Creates a pipeline")
public class Pipeline extends Base implements Runnable {
@CommandLine.Command(name = "create-pipeline", mixinStandardHelpOptions = true, description = "Creates a pipeline")
public class CreatePipeline extends Base implements Runnable {

Check warning on line 16 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java#L16

Added line #L16 was not covered by tests

private static final Logger logger = LoggerFactory.getLogger(Pipeline.class);
private static final Logger logger = LoggerFactory.getLogger(CreatePipeline.class);

Check warning on line 18 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java#L18

Added line #L18 was not covered by tests

@Inject
Driver driver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import picocli.CommandLine;

@CommandLine.Command(name = "driver", subcommands = {
Fabric8.class, Pipeline.class }, mixinStandardHelpOptions = true)
Fabric8.class, CreatePipeline.class, CancelPipeline.class }, mixinStandardHelpOptions = true)
public class DriverCommand {

Check warning on line 7 in java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java

View check run for this annotation

Codecov / codecov/patch

java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java#L7

Added line #L7 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import static org.apache.commons.lang3.StringUtils.isEmpty;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -96,7 +94,7 @@ public BuildResponse create(BuildRequest buildRequest) {
// Various ways to create the initial PipelineRun object. We can use an objectmapper,
// client.getKubernetesSerialization() or the load calls on the Fabric8 objects.
pipelineRun = tc.v1().pipelineRuns()
.load(IOUtils.resourceToURL("pipeline.yaml", Thread.currentThread().getContextClassLoader())).item();
.load(IOUtils.resourceToURL("pipeline.yaml", Thread.currentThread().getContextClassLoader())).item();
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 99 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L98-L99

Added lines #L98 - L99 were not covered by tests
}
Expand Down Expand Up @@ -126,23 +124,21 @@ public void cancel(CancelRequest request) {
var tc = client.adapt(TektonClient.class);
var pipeline = tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).withName(request.pipelineId()).get();

Check warning on line 125 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L124-L125

Added lines #L124 - L125 were not covered by tests

logger.info("Retrieved pipeline {}", pipeline);
logger.info("Retrieved pipeline {}", pipeline.getMetadata().getName());

Check warning on line 127 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L127

Added line #L127 was not covered by tests

List<Condition> conditions = new ArrayList<>();

Check warning on line 129 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L129

Added line #L129 was not covered by tests
// https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status
Condition cancelCondition = new Condition();
cancelCondition.setType("Succeeded");
cancelCondition.setStatus("False");

Check warning on line 133 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L131-L133

Added lines #L131 - L133 were not covered by tests
// https://github.com/tektoncd/community/blob/main/teps/0058-graceful-pipeline-run-termination.md
cancelCondition.setReason("CancelledRunFinally");
cancelCondition.setMessage("The PipelineRun was cancelled");
conditions.add(cancelCondition);

Check warning on line 137 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L135-L137

Added lines #L135 - L137 were not covered by tests

pipeline.getStatus().setConditions(conditions);

Check warning on line 139 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L139

Added line #L139 was not covered by tests

tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).resource(pipeline).updateStatus();
// https://tekton.dev/docs/pipelines/pipelineruns/#gracefully-cancelling-a-pipelinerun
// tc.v1beta1().pipelineRuns().updateStatus().inNamespace(request.namespace()).withName(request.pipelineId()).patch()
// .edit(p -> p.edit().editOrNewSpec().withStatus("CancelledRunFinally").endSpec().build());
}

Check warning on line 142 in java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java#L141-L142

Added lines #L141 - L142 were not covered by tests

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
Expand All @@ -33,6 +34,7 @@
import com.redhat.hacbs.driver.Driver;
import com.redhat.hacbs.driver.dto.BuildRequest;
import com.redhat.hacbs.driver.dto.BuildResponse;
import com.redhat.hacbs.driver.dto.CancelRequest;
import com.redhat.hacbs.driver.util.Info;

import io.smallrye.common.annotation.RunOnVirtualThread;
Expand Down Expand Up @@ -66,17 +68,13 @@ public BuildResponse build(BuildRequest buildRequest) {
return result;
}

// TODO: Is delete possible in konflux?
//
// /**
// * Cancel the build execution.
// */
// @PUT
// @Path("/cancel")
// public CompletionStage<Response> cancel(BuildCancelRequest buildCancelRequest) {
// logger.info("Requested cancel: {}", buildCancelRequest.getBuildExecutionId());
// return driver.cancel(buildCancelRequest).thenApply((r) -> Response.status(r.getCode()).build());
// }
@PUT
@Path("/cancel")
@RunOnVirtualThread
public void cancel(CancelRequest cancelRequest) {
logger.info("Requested cancel: {}", cancelRequest.pipelineId());
driver.cancel(cancelRequest);
}

Check warning on line 77 in java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java

View check run for this annotation

Codecov / codecov/patch

java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java#L75-L77

Added lines #L75 - L77 were not covered by tests

@Path("/version")
@GET
Expand Down

0 comments on commit 85b16e4

Please sign in to comment.