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 b15bc72 commit 3c03fe9
Show file tree
Hide file tree
Showing 5 changed files with 53 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 {

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);
}
}
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 {

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

@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 {
}
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);
}
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();

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

List<Condition> 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);

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());
}

/**
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,12 @@ 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")
public void cancel(CancelRequest cancelRequest) {
logger.info("Requested cancel: {}", cancelRequest.pipelineId());
driver.cancel(cancelRequest);
}

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

0 comments on commit 3c03fe9

Please sign in to comment.