Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive cancel patch bytes once at controller startup #3316

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions pkg/reconciler/pipelinerun/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,44 @@ package pipelinerun
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"

"go.uber.org/zap"
jsonpatch "gomodules.xyz/jsonpatch/v2"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"go.uber.org/zap"
jsonpatch "gomodules.xyz/jsonpatch/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"knative.dev/pkg/apis"
)

// cancelPipelineRun marks the PipelineRun as cancelled and any resolved TaskRun(s) too.
func cancelPipelineRun(logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) error {
errs := []string{}
var cancelPatchBytes []byte

// Use Patch to update the TaskRuns since the TaskRun controller may be operating on the
// TaskRuns at the same time and trying to update the entire object may cause a race
b, err := getCancelPatch()
func init() {
var err error
cancelPatchBytes, err = json.Marshal([]jsonpatch.JsonPatchOperation{{
Operation: "add",
Path: "/spec/status",
Value: v1beta1.TaskRunSpecStatusCancelled,
}})
if err != nil {
return fmt.Errorf("couldn't make patch to update TaskRun cancellation: %v", err)
log.Fatalf("failed to marshal cancel patch bytes: %v", err)
}
}

// cancelPipelineRun marks the PipelineRun as cancelled and any resolved TaskRun(s) too.
func cancelPipelineRun(logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) error {
errs := []string{}

// Loop over the TaskRuns in the PipelineRun status.
// If a TaskRun is not in the status yet we should not cancel it anyways.
for taskRunName := range pr.Status.TaskRuns {
logger.Infof("cancelling TaskRun %s", taskRunName)

if _, err := clientSet.TektonV1beta1().TaskRuns(pr.Namespace).Patch(taskRunName, types.JSONPatchType, b, ""); err != nil {
if _, err := clientSet.TektonV1beta1().TaskRuns(pr.Namespace).Patch(taskRunName, types.JSONPatchType, cancelPatchBytes, ""); err != nil {
errs = append(errs, fmt.Errorf("Failed to patch TaskRun `%s` with cancellation: %s", taskRunName, err).Error())
continue
}
Expand Down Expand Up @@ -77,16 +84,3 @@ func cancelPipelineRun(logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clien
}
return nil
}

func getCancelPatch() ([]byte, error) {
patches := []jsonpatch.JsonPatchOperation{{
Operation: "add",
Path: "/spec/status",
Value: v1beta1.TaskRunSpecStatusCancelled,
}}
patchBytes, err := json.Marshal(patches)
if err != nil {
return nil, fmt.Errorf("failed to marshal patch bytes in order to cancel: %v", err)
}
return patchBytes, nil
}