From 94354a26e63008fbef9314cc6cbe4623fc5ba672 Mon Sep 17 00:00:00 2001 From: Andrea Frittoli Date: Thu, 9 Apr 2020 22:03:36 +0100 Subject: [PATCH] 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". --- pkg/reconciler/taskrun/cancel.go | 77 +++++++++++++++++++++++++++++++ pkg/reconciler/taskrun/taskrun.go | 18 ++++++++ 2 files changed, 95 insertions(+) create mode 100644 pkg/reconciler/taskrun/cancel.go diff --git a/pkg/reconciler/taskrun/cancel.go b/pkg/reconciler/taskrun/cancel.go new file mode 100644 index 00000000000..8eff915d7da --- /dev/null +++ b/pkg/reconciler/taskrun/cancel.go @@ -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) +} diff --git a/pkg/reconciler/taskrun/taskrun.go b/pkg/reconciler/taskrun/taskrun.go index 1f51b1e434d..0ab304c0ddf 100644 --- a/pkg/reconciler/taskrun/taskrun.go +++ b/pkg/reconciler/taskrun/taskrun.go @@ -164,22 +164,36 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error { // If the TaskRun is cancelled, kill resources and update status if tr.IsCancelled() { before := tr.Status.GetCondition(apis.ConditionSucceeded) +<<<<<<< HEAD message := fmt.Sprintf("TaskRun %q was cancelled", tr.Name) err := c.failTaskRun(tr, v1beta1.TaskRunReasonCancelled, message) after := tr.Status.GetCondition(apis.ConditionSucceeded) reconciler.EmitEvent(c.Recorder, before, after, tr) return multierror.Append(err, c.updateStatusLabelsAndAnnotations(tr, original)).ErrorOrNil() +======= + err := cancelTaskRun(tr, c.KubeClientSet, c.Logger) + after := tr.Status.GetCondition(apis.ConditionSucceeded) + reconciler.EmitEvent(c.Recorder, before, after, tr) + return err +>>>>>>> Consolidate cancel and timeout logic } // Check if the TaskRun has timed out; if it is, this will set its status // accordingly. if tr.HasTimedOut() { before := tr.Status.GetCondition(apis.ConditionSucceeded) +<<<<<<< HEAD message := fmt.Sprintf("TaskRun %q failed to finish within %q", tr.Name, tr.GetTimeout()) err := c.failTaskRun(tr, podconvert.ReasonTimedOut, message) after := tr.Status.GetCondition(apis.ConditionSucceeded) reconciler.EmitEvent(c.Recorder, before, after, tr) return multierror.Append(err, c.updateStatusLabelsAndAnnotations(tr, original)).ErrorOrNil() +======= + err := timeoutTaskRun(tr, c.KubeClientSet, c.Logger) + after := tr.Status.GetCondition(apis.ConditionSucceeded) + reconciler.EmitEvent(c.Recorder, before, after, tr) + return err +>>>>>>> Consolidate cancel and timeout logic } // Reconcile this copy of the task run and then write back any status @@ -196,6 +210,10 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1alpha1.TaskRun) error // and may not have had all of the assumed default specified. tr.SetDefaults(contexts.WithUpgradeViaDefaulting(ctx)) + if tr.Spec.Timeout == nil { + tr.Spec.Timeout = &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute} + } + if err := tr.ConvertTo(ctx, &v1beta1.TaskRun{}); err != nil { if ce, ok := err.(*v1beta1.CannotConvertError); ok { tr.Status.MarkResourceNotConvertible(ce)