Skip to content

Commit

Permalink
Remove PipelineResource support in pipeline
Browse files Browse the repository at this point in the history
As Pipelineresources have been removed in 0.46 pipeline
so this patch removes resource support in tkn pipeline
start command and updates unit test

Signed-off-by: Shiv Verma <[email protected]>
  • Loading branch information
pratap0007 authored and tekton-robot committed Apr 13, 2023
1 parent b109d7f commit a9d74cd
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 2,668 deletions.
241 changes: 5 additions & 236 deletions pkg/cmd/pipeline/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"
prcmd "github.com/tektoncd/cli/pkg/cmd/pipelinerun"
"github.com/tektoncd/cli/pkg/file"
"github.com/tektoncd/cli/pkg/flags"
Expand All @@ -44,8 +42,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"
versionedResource "github.com/tektoncd/pipeline/pkg/client/resource/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
Expand All @@ -57,16 +53,14 @@ var (
)

const (
invalidResource = "invalid input format for resource parameter: "
invalidSvc = "invalid service account parameter: "
invalidSvc = "invalid service account parameter: "
)

type startOptions struct {
cliparams cli.Params
stream *cli.Stream
askOpts survey.AskOpt
Params []string
Resources []string
ServiceAccountName string
ServiceAccounts []string
Last bool
Expand All @@ -88,13 +82,6 @@ type startOptions struct {
SkipOptionalWorkspace bool
}

type resourceOptionsFilter struct {
git []string
image []string
storage []string
pullRequest []string
}

func startCommand(p cli.Params) *cobra.Command {
opt := startOptions{
cliparams: p,
Expand Down Expand Up @@ -150,6 +137,9 @@ For passing the workspaces via flags:
if err := flags.InitParams(p, cmd); err != nil {
return err
}
if opt.Last && opt.UsePipelineRun != "" {
return errors.New("option --last and option --use-pipelinerun can't be specify together")
}
if opt.Filename != "" && opt.Last {
return errors.New("cannot use --last option with --filename option")
}
Expand Down Expand Up @@ -182,8 +172,6 @@ For passing the workspaces via flags:
}

c.Flags().BoolVarP(&opt.ShowLog, "showlog", "", false, "show logs right after starting the Pipeline")
c.Flags().StringSliceVarP(&opt.Resources, "resource", "r", []string{}, "pass the resource name and ref as name=ref")
_ = c.Flags().MarkDeprecated("resource", "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().BoolVarP(&opt.Last, "last", "L", false, "re-run the Pipeline using last PipelineRun values")
c.Flags().StringVarP(&opt.UsePipelineRun, "use-pipelinerun", "", "", "use this pipelinerun values to re-run the pipeline. ")
Expand Down Expand Up @@ -316,10 +304,6 @@ func (opt *startOptions) startPipeline(pipelineStart *v1beta1.Pipeline) error {
}
}

if err := mergeRes(pr, opt.Resources); err != nil {
return err
}

labels, err := labels.MergeLabels(pr.ObjectMeta.Labels, opt.Labels)
if err != nil {
return err
Expand Down Expand Up @@ -432,29 +416,6 @@ func (opt *startOptions) startPipeline(pipelineStart *v1beta1.Pipeline) error {
}

func (opt *startOptions) getInput(pipeline *v1beta1.Pipeline) error {
cs, err := opt.cliparams.Clients()
if err != nil {
return err
}

if opt.Last && opt.UsePipelineRun != "" {
fmt.Fprintf(opt.stream.Err, "option --last and option --use-pipelinerun are not compatible \n")
return err
}

if len(pipeline.Spec.Resources) > 0 && len(opt.Resources) == 0 && !opt.Last && opt.UsePipelineRun == "" {
pres, err := getPipelineResources(cs.Resource, opt.cliparams.Namespace())
if err != nil {
return fmt.Errorf("failed to list PipelineResources from namespace %s: %v", opt.cliparams.Namespace(), err)
}

resources := getPipelineResourcesByFormat(pres.Items)

if err = opt.getInputResources(resources, pipeline); err != nil {
return err
}
}

params.FilterParamsByType(pipeline.Spec.Params)
if !opt.Last && opt.UsePipelineRun == "" {
skipParams, err := params.ParseParams(opt.Params)
Expand All @@ -467,7 +428,7 @@ func (opt *startOptions) getInput(pipeline *v1beta1.Pipeline) error {
}

if len(opt.Workspaces) == 0 && !opt.Last && opt.UsePipelineRun == "" {
if err = opt.getInputWorkspaces(pipeline); err != nil {
if err := opt.getInputWorkspaces(pipeline); err != nil {
return err
}
}
Expand Down Expand Up @@ -504,57 +465,6 @@ func (opt *startOptions) getTimeouts(pr *v1beta1.PipelineRun) error {
return nil
}

func (opt *startOptions) getInputResources(resources resourceOptionsFilter, pipeline *v1beta1.Pipeline) error {
for _, res := range pipeline.Spec.Resources {
options := getOptionsByType(resources, string(res.Type))
// directly create resource
if len(options) == 0 {
ns := opt.cliparams.Namespace()
fmt.Fprintf(opt.stream.Out, "no PipelineResource of type \"%s\" found in namespace: %s\n", string(res.Type), ns)
fmt.Fprintf(opt.stream.Out, "Please create a new \"%s\" resource for PipelineResource \"%s\"\n", string(res.Type), res.Name)
newres, err := opt.createPipelineResource(res.Name, res.Type)
if err != nil {
return err
}
if newres.Status != nil {
fmt.Printf("resource status %s\n\n", newres.Status)
}
opt.Resources = append(opt.Resources, res.Name+"="+newres.Name)
continue
}

// shows create option in the resource list
resCreateOpt := fmt.Sprintf("create new \"%s\" resource", res.Type)
options = append(options, resCreateOpt)
var ans string
qs := []*survey.Question{
{
Name: "pipelineresource",
Prompt: &survey.Select{
Message: fmt.Sprintf("Choose the %s resource to use for %s:", res.Type, res.Name),
Options: options,
},
},
}

if err := survey.Ask(qs, &ans, opt.askOpts); err != nil {
return err
}

if ans == resCreateOpt {
newres, err := opt.createPipelineResource(res.Name, res.Type)
if err != nil {
return err
}
opt.Resources = append(opt.Resources, res.Name+"="+newres.Name)
continue
}
name := strings.TrimSpace(strings.Split(ans, " ")[0])
opt.Resources = append(opt.Resources, res.Name+"="+name)
}
return nil
}

func (opt *startOptions) getInputParams(pipeline *v1beta1.Pipeline, skipParams map[string]string, useParamDefaults bool) error {
for _, param := range pipeline.Spec.Params {
if param.Default == nil && useParamDefaults || !useParamDefaults {
Expand Down Expand Up @@ -596,93 +506,6 @@ func (opt *startOptions) getInputParams(pipeline *v1beta1.Pipeline, skipParams m
return nil
}

func getPipelineResources(client versionedResource.Interface, namespace string) (*v1alpha1.PipelineResourceList, error) {
pres, err := client.TektonV1alpha1().PipelineResources(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return nil, err
}
return pres, nil
}

func getPipelineResourcesByFormat(resources []v1alpha1.PipelineResource) (ret resourceOptionsFilter) {
for _, res := range resources {
output := ""
switch string(res.Spec.Type) {
case "git":
for _, param := range res.Spec.Params {
if param.Name == "url" {
output = param.Value + output
}
if param.Name == "revision" && param.Value != "master" {
output = output + "#" + param.Value
}
}
ret.git = append(ret.git, fmt.Sprintf("%s (%s)", res.Name, output))
case "image":
for _, param := range res.Spec.Params {
if param.Name == "url" {
output = param.Value + output
}
}
ret.image = append(ret.image, fmt.Sprintf("%s (%s)", res.Name, output))
case "pullRequest":
for _, param := range res.Spec.Params {
if param.Name == "url" {
output = param.Value + output
}
}
ret.pullRequest = append(ret.pullRequest, fmt.Sprintf("%s (%s)", res.Name, output))
case "storage":
for _, param := range res.Spec.Params {
if param.Name == "location" {
output = param.Value + output
}
}
ret.storage = append(ret.storage, fmt.Sprintf("%s (%s)", res.Name, output))
}
}
return
}

func getOptionsByType(resources resourceOptionsFilter, restype string) []string {
if restype == "git" {
return resources.git
}
if restype == "image" {
return resources.image
}
if restype == "pullRequest" {
return resources.pullRequest
}
if restype == "storage" {
return resources.storage
}
return []string{}
}

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

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

for i := range pr.Spec.Resources {
if v, ok := res[pr.Spec.Resources[i].Name]; ok {
pr.Spec.Resources[i] = v
delete(res, v.Name)
}
}
for _, v := range res {
pr.Spec.Resources = append(pr.Spec.Resources, v)
}
sort.Slice(pr.Spec.Resources, func(i, j int) bool { return pr.Spec.Resources[i].Name < pr.Spec.Resources[j].Name })
return nil
}

func mergeSvc(pr *v1beta1.PipelineRun, optSvc []string) error {
svcs, err := parseTaskSvc(optSvc)
if err != nil {
Expand All @@ -707,23 +530,6 @@ func mergeSvc(pr *v1beta1.PipelineRun, optSvc []string) error {
return nil
}

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

func parseTaskSvc(s []string) (map[string]v1beta1.PipelineTaskRunSpec, error) {
svcs := map[string]v1beta1.PipelineTaskRunSpec{}
for _, v := range s {
Expand All @@ -742,43 +548,6 @@ func parseTaskSvc(s []string) (map[string]v1beta1.PipelineTaskRunSpec, error) {
return svcs, nil
}

func (opt *startOptions) createPipelineResource(resName string, resType v1alpha1.PipelineResourceType) (*v1alpha1.PipelineResource, error) {
res := pipelineresource.Resource{
AskOpts: opt.askOpts,
Params: opt.cliparams,
PipelineResource: v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{Namespace: opt.cliparams.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 := opt.cliparams.Clients()
if err != nil {
return nil, err
}
newRes, err := cs.Resource.TektonV1alpha1().PipelineResources(opt.cliparams.Namespace()).Create(context.Background(), &res.PipelineResource, metav1.CreateOptions{})
if err != nil {
return nil, err
}
fmt.Fprintf(opt.stream.Out, "New %s resource \"%s\" has been created\n", newRes.Spec.Type, newRes.Name)
return newRes, nil
}

func printPipelineRun(output string, s *cli.Stream, pr interface{}) error {
format := strings.ToLower(output)
if format == "" || format == "yaml" {
Expand Down
Loading

0 comments on commit a9d74cd

Please sign in to comment.