-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
timeout.go
139 lines (121 loc) · 4.99 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pipelinerun
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"gomodules.xyz/jsonpatch/v2"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
clientset "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
var timeoutTaskRunPatchBytes, timeoutRunPatchBytes []byte
func init() {
var err error
timeoutTaskRunPatchBytes, err = json.Marshal([]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/spec/status",
Value: v1beta1.TaskRunSpecStatusCancelled,
},
{
Operation: "add",
Path: "/spec/statusMessage",
Value: v1beta1.TaskRunCancelledByPipelineTimeoutMsg,
}})
if err != nil {
log.Fatalf("failed to marshal TaskRun timeout patch bytes: %v", err)
}
timeoutRunPatchBytes, err = json.Marshal([]jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/spec/status",
Value: v1alpha1.RunSpecStatusCancelled,
},
{
Operation: "add",
Path: "/spec/statusMessage",
Value: v1alpha1.RunCancelledByPipelineTimeoutMsg,
}})
if err != nil {
log.Fatalf("failed to marshal Run timeout patch bytes: %v", err)
}
}
// timeoutPipelineRun marks the PipelineRun as timed out and any resolved TaskRun(s) too.
func timeoutPipelineRun(ctx context.Context, logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) error {
errs := timeoutPipelineTasks(ctx, logger, pr, clientSet)
// If we successfully timed out all the TaskRuns and Runs, we can consider the PipelineRun timed out.
if len(errs) == 0 {
reason := v1beta1.PipelineRunReasonTimedOut.String()
pr.Status.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: reason,
Message: fmt.Sprintf("PipelineRun %q failed to finish within %q", pr.Name, pr.PipelineTimeout(ctx).String()),
})
// update pr completed time
pr.Status.CompletionTime = &metav1.Time{Time: time.Now()}
} else {
e := strings.Join(errs, "\n")
// Indicate that we failed to time out the PipelineRun
pr.Status.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: ReasonCouldntTimeOut,
Message: fmt.Sprintf("PipelineRun %q was timed out but had errors trying to time out TaskRuns and/or Runs: %s", pr.Name, e),
})
return fmt.Errorf("error(s) from timing out TaskRun(s) from PipelineRun %s: %s", pr.Name, e)
}
return nil
}
func timeoutRun(ctx context.Context, runName string, namespace string, clientSet clientset.Interface) error {
_, err := clientSet.TektonV1alpha1().Runs(namespace).Patch(ctx, runName, types.JSONPatchType, timeoutRunPatchBytes, metav1.PatchOptions{}, "")
return err
}
// timeoutPipelineTaskRuns patches `TaskRun` and `Run` with canceled status and an appropriate message
func timeoutPipelineTasks(ctx context.Context, logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface) []string {
return timeoutPipelineTasksForTaskNames(ctx, logger, pr, clientSet, sets.NewString())
}
// timeoutPipelineTasksForTaskNames patches `TaskRun`s and `Run`s for the given task names, or all if no task names are given, with canceled status and appropriate message
func timeoutPipelineTasksForTaskNames(ctx context.Context, logger *zap.SugaredLogger, pr *v1beta1.PipelineRun, clientSet clientset.Interface, taskNames sets.String) []string {
errs := []string{}
trNames, runNames, err := getChildObjectsFromPRStatusForTaskNames(ctx, pr.Status, taskNames)
if err != nil {
errs = append(errs, err.Error())
}
for _, taskRunName := range trNames {
logger.Infof("cancelling TaskRun %s for timeout", taskRunName)
if _, err := clientSet.TektonV1beta1().TaskRuns(pr.Namespace).Patch(ctx, taskRunName, types.JSONPatchType, timeoutTaskRunPatchBytes, metav1.PatchOptions{}, ""); err != nil {
errs = append(errs, fmt.Errorf("Failed to patch TaskRun `%s` with cancellation: %s", taskRunName, err).Error())
continue
}
}
for _, runName := range runNames {
logger.Infof("cancelling Run %s for timeout", runName)
if err := timeoutRun(ctx, runName, pr.Namespace, clientSet); err != nil {
errs = append(errs, fmt.Errorf("Failed to patch Run `%s` with cancellation: %s", runName, err).Error())
continue
}
}
return errs
}