Canceling a Tekton TaskRun with Fabric8 Kubernetes Java Client
Tags: java, fabric8, tekton, workflow
Published: 2021-05-13 00:16:04

> This work is being done as part of my open-source contribution to [Boomerang Flow](https://useboomerang.io), a low-code cloud-native workflow automation project.

The Fabric8 Java Kubernetes Client helps you integrate with Kubernetes and it has an extension for integrating with Tekton. 

It makes building out Java code extremely easy, however, you still need to know what or how to update the objects. This is where examples and references really help and is a great place to start as a contributor!

## The Code

As the name says, this article is all about how to Cancel TaskRuns in Tekton using the Fabric8 Tekton client. You can see the [latest example code here](https://github.com/fabric8io/kubernetes-client/blob/master/extensions/tekton/examples/src/main/java/io/fabric8/tekton/api/examples/TaskRunCancel.java)

## But there is no Cancel action

Unlike `create()`, `list()`, `delete()`, etc there is no corresponding action for cancel instead you need to use `updateStatus()`.

This comes down to the fact that Kubernetes itself doesn't understand Cancel, and in fact you are inserting a new Status for the Kubernetes object.

## Let's update the Status

In reading the [Tekton TaskRun documentation](https://tekton.dev/docs/pipelines/taskruns/#cancelling-a-taskrun) all you have to do is stick "TaskRunCancelled" in the Status object.

```
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: go-example-git
spec:
  # […]
  status: "TaskRunCancelled"
```

Seems easy enough, right?

🚧 ...not so fast!

This doesn't actually work...or at least I could not get it to work.

## So what does work?

What worked for me was updating the Condition block in the Status.

1. Retrieve the particular TaskRun
2. Build up a Condition block and set the type, status, reason, and message.
3. Replace all existing Conditions. _If you don't replace the existing conditions, you will receive a message back saying "Not all Steps in the Task have finished executing"._

```
TaskRun taskRun = client.v1beta1().taskRuns().inNamespace("default").list().getItems().get(0);
    
List<Condition> taskRunConditions = new ArrayList<>();
Condition taskRunCancelCondition = new Condition();
taskRunCancelCondition.setType("Succeeded");
taskRunCancelCondition.setStatus("False");
taskRunCancelCondition.setReason("TaskRunCancelled");
taskRunCancelCondition.setMessage("The TaskRun was cancelled successfully.");
taskRunConditions.add(taskRunCancelCondition);
    
taskRun.getStatus().setConditions(taskRunConditions);

client.v1beta1().taskRuns().updateStatus(taskRun);
```

There is a really great table in the [Tekton TaskRun documentation](https://tekton.dev/docs/pipelines/taskruns/#monitoring-execution-status) for the different execution statuses you can expect.

## Show your support
Do not hesitate to support us if this helped you:

- [⭐️ the GitHub repository](https://github.com/boomerang-io/flow.client.web)
- [💬 Follow us on Twitter](https://twitter.com/useboomerangio)
- [Install the project](https://www.useboomerang.io/docs/boomerang-flow/installing/installing) on your own Kubernetes cluster
- 🙏 Join us and help contribute