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

Remove PipelineResource support in task #1984

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
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
120 changes: 0 additions & 120 deletions pkg/cmd/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"os"
"sort"
"strings"
"time"

Expand All @@ -30,7 +29,6 @@ import (
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/actions"
"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/cmd/pipelineresource"
"github.com/tektoncd/cli/pkg/cmd/taskrun"
"github.com/tektoncd/cli/pkg/file"
"github.com/tektoncd/cli/pkg/flags"
Expand All @@ -43,7 +41,6 @@ import (
"github.com/tektoncd/cli/pkg/workspaces"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
Expand All @@ -55,14 +52,10 @@ var (
errTaskAlreadyPresent = "Task with name %s already exists in namespace %s"
)

const invalidResource = "invalid input format for resource parameter: "

type startOptions struct {
cliparams cli.Params
stream *cli.Stream
Params []string
InputResources []string
OutputResources []string
ServiceAccountName string
Last bool
Labels []string
Expand Down Expand Up @@ -201,10 +194,6 @@ For passing the workspaces via flags:
},
}

c.Flags().StringSliceVarP(&opt.InputResources, "inputresource", "i", []string{}, "pass the input resource name and ref as name=ref")
c.Flags().StringSliceVarP(&opt.OutputResources, "outputresource", "o", []string{}, "pass the output resource name and ref as name=ref")
_ = c.Flags().MarkDeprecated("inputresource", "pipelineresources have been deprecated, this flag will be removed soon")
_ = c.Flags().MarkDeprecated("outputresource", "pipelineresources have been deprecated, this flag will be removed soon")
c.Flags().StringArrayVarP(&opt.Params, "param", "p", []string{}, "pass the param as key=value for string type, or key=value1,value2,... for array type, or key=\"key1:value1, key2:value2\" for object type")
c.Flags().StringVarP(&opt.ServiceAccountName, "serviceaccount", "s", "", "pass the serviceaccount name")
_ = c.RegisterFlagCompletionFunc("serviceaccount",
Expand Down Expand Up @@ -343,21 +332,6 @@ func startTask(opt startOptions, args []string) error {
tr.Spec.Timeout = &metav1.Duration{Duration: timeoutDuration}
}

if tr.Spec.Resources == nil {
tr.Spec.Resources = &v1beta1.TaskRunResources{}
}
inputRes, err := mergeRes(tr.Spec.Resources.Inputs, opt.InputResources)
if err != nil {
return err
}
tr.Spec.Resources.Inputs = inputRes

outRes, err := mergeRes(tr.Spec.Resources.Outputs, opt.OutputResources)
if err != nil {
return err
}
tr.Spec.Resources.Outputs = outRes

labels, err := labels.MergeLabels(tr.ObjectMeta.Labels, opt.Labels)
if err != nil {
return err
Expand Down Expand Up @@ -454,49 +428,6 @@ func startTask(opt startOptions, args []string) error {
return taskrun.Run(runLogOpts)
}

func mergeRes(r []v1beta1.TaskResourceBinding, optRes []string) ([]v1beta1.TaskResourceBinding, error) {
res, err := parseRes(optRes)
if err != nil {
return nil, err
}

if len(res) == 0 {
return r, nil
}

for i := range r {
if v, ok := res[r[i].Name]; ok {
r[i] = v
delete(res, v.Name)
}
}
for _, v := range res {
r = append(r, v)
}

sort.Slice(r, func(i, j int) bool { return r[i].Name < r[j].Name })
return r, nil
}

func parseRes(res []string) (map[string]v1beta1.TaskResourceBinding, error) {
resources := map[string]v1beta1.TaskResourceBinding{}
for _, v := range res {
r := strings.SplitN(v, "=", 2)
if len(r) != 2 {
return nil, errors.New(invalidResource + v)
}
resources[r[0]] = v1beta1.TaskResourceBinding{
PipelineResourceBinding: v1beta1.PipelineResourceBinding{
Name: r[0],
ResourceRef: &v1beta1.PipelineResourceRef{
Name: r[1],
},
},
}
}
return resources, nil
}

func printTaskRun(output string, s *cli.Stream, tr interface{}) error {
format := strings.ToLower(output)
if format == "" || format == "yaml" {
Expand Down Expand Up @@ -532,21 +463,6 @@ func (opt *startOptions) getInputs() error {
SkipOptionalWorkspace: opt.SkipOptionalWorkspace,
}

if opt.task.Spec.Resources != nil && !opt.Last && opt.UseTaskRun == "" {
if len(opt.InputResources) == 0 {
if err := intOpts.TaskInputResources(opt.task, createPipelineResource); err != nil {
return err
}
opt.InputResources = append(opt.InputResources, intOpts.InputResources...)
}
if len(opt.OutputResources) == 0 {
if err := intOpts.TaskOutputResources(opt.task, createPipelineResource); err != nil {
return err
}
opt.OutputResources = append(opt.OutputResources, intOpts.OutputResources...)
}
}

params.FilterParamsByType(opt.task.Spec.Params)
if !opt.Last && opt.UseTaskRun == "" {
skipParams, err := params.ParseParams(opt.Params)
Expand All @@ -569,42 +485,6 @@ func (opt *startOptions) getInputs() error {
return nil
}

func createPipelineResource(resType v1alpha1.PipelineResourceType, askOpt survey.AskOpt, p cli.Params, s *cli.Stream) (*v1alpha1.PipelineResource, error) {
res := pipelineresource.Resource{
AskOpts: askOpt,
Params: p,
PipelineResource: v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{Namespace: p.Namespace()},
Spec: v1alpha1.PipelineResourceSpec{Type: resType},
}}

if err := res.AskMeta(); err != nil {
return nil, err
}

resourceTypeParams := map[v1alpha1.PipelineResourceType]func() error{
v1alpha1.PipelineResourceTypeGit: res.AskGitParams,
v1alpha1.PipelineResourceTypeStorage: res.AskStorageParams,
v1alpha1.PipelineResourceTypeImage: res.AskImageParams,
v1alpha1.PipelineResourceTypePullRequest: res.AskPullRequestParams,
}
if res.PipelineResource.Spec.Type != "" {
if err := resourceTypeParams[res.PipelineResource.Spec.Type](); err != nil {
return nil, err
}
}
cs, err := p.Clients()
if err != nil {
return nil, err
}
newRes, err := cs.Resource.TektonV1alpha1().PipelineResources(p.Namespace()).Create(context.Background(), &res.PipelineResource, metav1.CreateOptions{})
if err != nil {
return nil, err
}
fmt.Fprintf(s.Out, "New %s resource \"%s\" has been created\n", newRes.Spec.Type, newRes.Name)
return newRes, nil
}

func getTaskV1beta1(gr schema.GroupVersionResource, c *cli.Clients, tName, ns string) (*v1beta1.Task, error) {
var task v1beta1.Task
gvr, err := actions.GetGroupVersionResource(gr, c.Tekton.Discovery())
Expand Down
Loading