diff --git a/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java new file mode 100644 index 000000000..c1f2a5b24 --- /dev/null +++ b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CancelPipeline.java @@ -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 { + + private static final Logger logger = LoggerFactory.getLogger(CancelPipeline.class); + + @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(); + + driver.cancel(cancel); + } +} diff --git a/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/Pipeline.java b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java similarity index 90% rename from java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/Pipeline.java rename to java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java index 877bc93e2..919721f85 100644 --- a/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/Pipeline.java +++ b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/CreatePipeline.java @@ -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 { - private static final Logger logger = LoggerFactory.getLogger(Pipeline.class); + private static final Logger logger = LoggerFactory.getLogger(CreatePipeline.class); @Inject Driver driver; diff --git a/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java index 4af937c39..0aad27088 100644 --- a/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java +++ b/java-components/cli/src/main/java/com/redhat/hacbs/cli/driver/DriverCommand.java @@ -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 { } diff --git a/java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java b/java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java index deb6155d5..77b84ba65 100644 --- a/java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java +++ b/java-components/driver/src/main/java/com/redhat/hacbs/driver/Driver.java @@ -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; @@ -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); } @@ -126,13 +124,14 @@ public void cancel(CancelRequest request) { var tc = client.adapt(TektonClient.class); var pipeline = tc.v1beta1().pipelineRuns().inNamespace(request.namespace()).withName(request.pipelineId()).get(); - logger.info("Retrieved pipeline {}", pipeline); + logger.info("Retrieved pipeline {}", pipeline.getMetadata().getName()); List conditions = new ArrayList<>(); // https://tekton.dev/docs/pipelines/pipelineruns/#monitoring-execution-status Condition cancelCondition = new Condition(); cancelCondition.setType("Succeeded"); cancelCondition.setStatus("False"); + // 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); @@ -140,9 +139,6 @@ public void cancel(CancelRequest request) { pipeline.getStatus().setConditions(conditions); 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()); } /** diff --git a/java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java b/java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java index 5a3002c11..4b49e9fd1 100644 --- a/java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java +++ b/java-components/driver/src/main/java/com/redhat/hacbs/driver/endpoints/Public.java @@ -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; @@ -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; @@ -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 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); + } @Path("/version") @GET