-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate cancel and timeout logic
Cancel and timeout do very similar things when they happen: they update the status of the taskrun, set the completion time and try and delete the pod. Today this is done for the two cases in different places, the code structured differently and the behaviour slightly different: - log levels of the messages are different - cancel does not set the completion time - cancel does not check if the error on pod deletion is a NotFound This commit introduces "HasTimedOut" to tasktun_types, which matches what "IsCancelled" does. It introduces a "killTaskRun" function that can be used by both cancel and timeout, with the only different being the "Reason" and termination message. The timeout_check module is not necessary anymore. The check for IsCancelled and HasTimedOut are move out of "reconcile" into "Reconcile", so that now "Reconcile" checks: - HasStarted - isDone - IsCancelled - HasTimedOut and finally, if applicable, it invokes "reconcile".
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2019 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 taskrun | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
podconvert "github.com/tektoncd/pipeline/pkg/pod" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
type logger interface { | ||
Warn(args ...interface{}) | ||
Warnf(template string, args ...interface{}) | ||
Infof(template string, args ...interface{}) | ||
} | ||
|
||
func killTaskRun(tr *v1alpha1.TaskRun, clientSet kubernetes.Interface, | ||
logger logger, reason, message string) error { | ||
|
||
logger.Warn("stopping task run %q because of %q", tr.Name, reason) | ||
tr.Status.SetCondition(&apis.Condition{ | ||
Type: apis.ConditionSucceeded, | ||
Status: corev1.ConditionFalse, | ||
Reason: reason, | ||
Message: message, | ||
}) | ||
|
||
// update tr completed time | ||
tr.Status.CompletionTime = &metav1.Time{Time: time.Now()} | ||
|
||
if tr.Status.PodName == "" { | ||
logger.Warnf("task run %q has no pod running yet", tr.Name) | ||
return nil | ||
} | ||
|
||
// tr.Status.PodName will be empty if the pod was never successfully created. This condition | ||
// can be reached, for example, by the pod never being schedulable due to limits imposed by | ||
// a namespace's ResourceQuota. | ||
err := clientSet.CoreV1().Pods(tr.Namespace).Delete(tr.Status.PodName, &metav1.DeleteOptions{}) | ||
if err != nil && !errors.IsNotFound(err) { | ||
logger.Warnf("Failed to terminate pod: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// cancelTaskRun marks the TaskRun as cancelled and delete pods linked to it. | ||
func cancelTaskRun(tr *v1alpha1.TaskRun, clientSet kubernetes.Interface, logger logger) error { | ||
message := fmt.Sprintf("TaskRun %q was cancelled", tr.Name) | ||
return killTaskRun(tr, clientSet, logger, "TaskRunCancelled", message) | ||
} | ||
|
||
func timeoutTaskRun(tr *v1alpha1.TaskRun, clientSet kubernetes.Interface, logger logger) error { | ||
message := fmt.Sprintf("TaskRun %q failed to finish within %q", tr.Name, tr.Spec.Timeout.Duration.String()) | ||
return killTaskRun(tr, clientSet, logger, podconvert.ReasonTimedOut, message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters