diff --git a/pkg/pipelinerun/pipelinerun.go b/pkg/pipelinerun/pipelinerun.go index c21c882ad..c2a52fab4 100644 --- a/pkg/pipelinerun/pipelinerun.go +++ b/pkg/pipelinerun/pipelinerun.go @@ -25,8 +25,7 @@ import ( "github.com/tektoncd/cli/pkg/formatted" prsort "github.com/tektoncd/cli/pkg/pipelinerun/sort" "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" - "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" - "k8s.io/apimachinery/pkg/api/errors" + "github.com/tektoncd/pipeline/pkg/status" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -174,7 +173,7 @@ func Create(c *cli.Clients, pr *v1beta1.PipelineRun, opts metav1.CreateOptions, } func populatePipelineRunTaskStatuses(c *cli.Clients, ns string, pr v1beta1.PipelineRun) (*v1beta1.PipelineRun, error) { - taskRunMap, runMap, err := getFullPipelineTaskStatuses(context.Background(), c.Tekton, ns, &pr) + taskRunMap, runMap, err := status.GetFullPipelineTaskStatuses(context.Background(), c.Tekton, ns, &pr) if err != nil { fmt.Fprintf(os.Stderr, "Failed to get TaskRun and Run statuses for PipelineRun %s from namespace %s\n", pr.Name, ns) return nil, err @@ -184,59 +183,3 @@ func populatePipelineRunTaskStatuses(c *cli.Clients, ns string, pr v1beta1.Pipel return &pr, nil } - -// getFullPipelineTaskStatuses returns populated TaskRun and Run status maps for a PipelineRun from its ChildReferences. -// If the PipelineRun has no ChildReferences, its .Status.TaskRuns and .Status.Runs will be returned instead. -// TODO(abayer): Remove in favor of github.com/tektoncd/pipeline/pkg/status.GetFullPipelineTaskStatuses when CLI can move to Pipeline v0.36.0 or later. -func getFullPipelineTaskStatuses(ctx context.Context, client versioned.Interface, ns string, pr *v1beta1.PipelineRun) (map[string]*v1beta1.PipelineRunTaskRunStatus, - map[string]*v1beta1.PipelineRunRunStatus, error) { - // If the PipelineRun is nil, just return - if pr == nil { - return nil, nil, nil - } - - // If there are no child references or either TaskRuns or Runs is non-zero, return the existing TaskRuns and Runs maps - if len(pr.Status.ChildReferences) == 0 || len(pr.Status.TaskRuns) > 0 || len(pr.Status.Runs) > 0 { - return pr.Status.TaskRuns, pr.Status.Runs, nil - } - - trStatuses := make(map[string]*v1beta1.PipelineRunTaskRunStatus) - runStatuses := make(map[string]*v1beta1.PipelineRunRunStatus) - - for _, cr := range pr.Status.ChildReferences { - switch cr.Kind { - case "TaskRun": - tr, err := client.TektonV1beta1().TaskRuns(ns).Get(ctx, cr.Name, metav1.GetOptions{}) - if err != nil && !errors.IsNotFound(err) { - return nil, nil, err - } - - trStatuses[cr.Name] = &v1beta1.PipelineRunTaskRunStatus{ - PipelineTaskName: cr.PipelineTaskName, - WhenExpressions: cr.WhenExpressions, - } - - if tr != nil { - trStatuses[cr.Name].Status = &tr.Status - } - case "Run": - r, err := client.TektonV1alpha1().Runs(ns).Get(ctx, cr.Name, metav1.GetOptions{}) - if err != nil && !errors.IsNotFound(err) { - return nil, nil, err - } - - runStatuses[cr.Name] = &v1beta1.PipelineRunRunStatus{ - PipelineTaskName: cr.PipelineTaskName, - WhenExpressions: cr.WhenExpressions, - } - - if r != nil { - runStatuses[cr.Name].Status = &r.Status - } - default: - // Don't do anything for unknown types. - } - } - - return trStatuses, runStatuses, nil -} diff --git a/pkg/pipelinerun/tracker.go b/pkg/pipelinerun/tracker.go index 05e36621f..734d18df9 100644 --- a/pkg/pipelinerun/tracker.go +++ b/pkg/pipelinerun/tracker.go @@ -23,6 +23,7 @@ import ( "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" informers "github.com/tektoncd/pipeline/pkg/client/informers/externalversions" + "github.com/tektoncd/pipeline/pkg/status" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" @@ -83,7 +84,7 @@ func (t *Tracker) Monitor(allowed []string) <-chan []trh.Run { return } - trMap, runMap, err := getFullPipelineTaskStatuses(context.Background(), t.Tekton, t.Ns, pr) + trMap, runMap, err := status.GetFullPipelineTaskStatuses(context.Background(), t.Tekton, t.Ns, pr) if err != nil { return } diff --git a/test/e2e-common.sh b/test/e2e-common.sh index c27b06239..298ea5216 100755 --- a/test/e2e-common.sh +++ b/test/e2e-common.sh @@ -143,6 +143,8 @@ function install_pipeline_crd() { # If for whatever reason the nightly release wasnt there (nightly ci failure?), try the released version [[ -n ${latestreleaseyaml} ]] || latestreleaseyaml=https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml fi + # TODO: to remove this pinning of pipeline version after v1 support of Pipeline CRDs in cli, as this is done to make CI green till then + latestreleaseyaml="https://github.com/tektoncd/pipeline/releases/download/v0.42.0/release.yaml" [[ -z ${latestreleaseyaml} ]] && fail_test "Could not get latest released release.yaml" kubectl apply -f ${latestreleaseyaml} || fail_test "Build pipeline installation failed" diff --git a/vendor/github.com/tektoncd/pipeline/pkg/status/status.go b/vendor/github.com/tektoncd/pipeline/pkg/status/status.go new file mode 100644 index 000000000..4952ab276 --- /dev/null +++ b/vendor/github.com/tektoncd/pipeline/pkg/status/status.go @@ -0,0 +1,119 @@ +/* +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 status + +import ( + "context" + "fmt" + + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" + "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// GetTaskRunStatusForPipelineTask takes a minimal embedded status child reference and returns the actual TaskRunStatus +// for the PipelineTask. It returns an error if the child reference's kind isn't TaskRun. +func GetTaskRunStatusForPipelineTask(ctx context.Context, client versioned.Interface, ns string, childRef v1beta1.ChildStatusReference) (*v1beta1.TaskRunStatus, error) { + if childRef.Kind != "TaskRun" { + return nil, fmt.Errorf("could not fetch status for PipelineTask %s: should have kind TaskRun, but is %s", childRef.PipelineTaskName, childRef.Kind) + } + + tr, err := client.TektonV1beta1().TaskRuns(ns).Get(ctx, childRef.Name, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return nil, err + } + if tr == nil { + return nil, nil + } + + return &tr.Status, nil +} + +// GetRunStatusForPipelineTask takes a minimal embedded status child reference and returns the actual RunStatus for the +// PipelineTask. It returns an error if the child reference's kind isn't Run. +func GetRunStatusForPipelineTask(ctx context.Context, client versioned.Interface, ns string, childRef v1beta1.ChildStatusReference) (*v1alpha1.RunStatus, error) { + if childRef.Kind != "Run" { + return nil, fmt.Errorf("could not fetch status for PipelineTask %s: should have kind Run, but is %s", childRef.PipelineTaskName, childRef.Kind) + } + + r, err := client.TektonV1alpha1().Runs(ns).Get(ctx, childRef.Name, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return nil, err + } + if r == nil { + return nil, nil + } + + return &r.Status, nil +} + +// GetFullPipelineTaskStatuses returns populated TaskRun and Run status maps for a PipelineRun from its ChildReferences. +// If the PipelineRun has no ChildReferences, its .Status.TaskRuns and .Status.Runs will be returned instead. +func GetFullPipelineTaskStatuses(ctx context.Context, client versioned.Interface, ns string, pr *v1beta1.PipelineRun) (map[string]*v1beta1.PipelineRunTaskRunStatus, + map[string]*v1beta1.PipelineRunRunStatus, error) { + // If the PipelineRun is nil, just return + if pr == nil { + return nil, nil, nil + } + + // If there are no child references or either TaskRuns or Runs is non-zero, return the existing TaskRuns and Runs maps + if len(pr.Status.ChildReferences) == 0 || len(pr.Status.TaskRuns) > 0 || len(pr.Status.Runs) > 0 { + return pr.Status.TaskRuns, pr.Status.Runs, nil + } + + trStatuses := make(map[string]*v1beta1.PipelineRunTaskRunStatus) + runStatuses := make(map[string]*v1beta1.PipelineRunRunStatus) + + for _, cr := range pr.Status.ChildReferences { + switch cr.Kind { + case "TaskRun": + tr, err := client.TektonV1beta1().TaskRuns(ns).Get(ctx, cr.Name, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return nil, nil, err + } + + trStatuses[cr.Name] = &v1beta1.PipelineRunTaskRunStatus{ + PipelineTaskName: cr.PipelineTaskName, + WhenExpressions: cr.WhenExpressions, + } + + if tr != nil { + trStatuses[cr.Name].Status = &tr.Status + } + case "Run": + r, err := client.TektonV1alpha1().Runs(ns).Get(ctx, cr.Name, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return nil, nil, err + } + + runStatuses[cr.Name] = &v1beta1.PipelineRunRunStatus{ + PipelineTaskName: cr.PipelineTaskName, + WhenExpressions: cr.WhenExpressions, + } + + if r != nil { + runStatuses[cr.Name].Status = &r.Status + } + default: + // Don't do anything for unknown types. + } + } + + return trStatuses, runStatuses, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index a882a2092..49cde99a5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1499,6 +1499,7 @@ github.com/tektoncd/pipeline/pkg/remote github.com/tektoncd/pipeline/pkg/remote/oci github.com/tektoncd/pipeline/pkg/resolution/common github.com/tektoncd/pipeline/pkg/resolution/resource +github.com/tektoncd/pipeline/pkg/status github.com/tektoncd/pipeline/pkg/substitution github.com/tektoncd/pipeline/test github.com/tektoncd/pipeline/test/diff