DEV Community

Cover image for Canceling a Tekton TaskRun with Fabric8 Kubernetes Java Client
Tyson Lawrie
Tyson Lawrie

Posted on

Canceling a Tekton TaskRun with Fabric8 Kubernetes Java Client

This work is being done as part of my open-source contribution to Boomerang Flow, 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

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 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"
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

There is a really great table in the Tekton TaskRun documentation for the different execution statuses you can expect.

Show your support

Do not hesitate to support us if this helped you:

Top comments (0)