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

Add workspace option to pipeline start #826

Merged
merged 1 commit into from
Mar 30, 2020
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
12 changes: 9 additions & 3 deletions docs/cmd/tkn_pipeline_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ To run a Pipeline that has one git resource and no parameter.
$ tkn pipeline start --resource source=samples-git


To run a Pipeline that has one git resource, one image resource and
two parameters (foo and bar)
To run a Pipeline that has one git resource, one image resource,
two parameters (foo and bar) and four workspaces (my-config, my-pvc,
my-secret and my-empty-dir)


$ tkn pipeline start --resource source=samples-git \
--resource image=my-image \
--param foo=yay \
--param bar=10
--param bar=10 \
--workspace name=my-secret,secret=secret-name \
--workspace name=my-config,config=rpg,item=ultimav=1 \
--workspace name=my-empty-dir,emptyDir="" \
--workspace name=my-pvc,claimName=pvc1,subPath=dir

### Options

Expand All @@ -48,6 +53,7 @@ two parameters (foo and bar)
--task-serviceaccount strings pass the service account corresponding to the task
--timeout string timeout for pipelinerun (default "1h")
--use-pipelinerun string use this pipelinerun values to re-run the pipeline.
-w, --workspace stringArray pass the workspace.
```

### Options inherited from parent commands
Expand Down
15 changes: 12 additions & 3 deletions docs/man/man1/tkn-pipeline-start.1
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ Parameters, at least those that have no default value
\fB\-\-use\-pipelinerun\fP=""
use this pipelinerun values to re\-run the pipeline.

.PP
\fB\-w\fP, \fB\-\-workspace\fP=[]
pass the workspace.


.SH OPTIONS INHERITED FROM PARENT COMMANDS
.PP
Expand Down Expand Up @@ -116,8 +120,9 @@ $ tkn pipeline start \-\-resource source=samples\-git
.RE

.PP
To run a Pipeline that has one git resource, one image resource and
two parameters (foo and bar)
To run a Pipeline that has one git resource, one image resource,
two parameters (foo and bar) and four workspaces (my\-config, my\-pvc,
my\-secret and my\-empty\-dir)

.PP
.RS
Expand All @@ -126,7 +131,11 @@ two parameters (foo and bar)
$ tkn pipeline start \-\-resource source=samples\-git \\
\-\-resource image=my\-image \\
\-\-param foo=yay \\
\-\-param bar=10
\-\-param bar=10 \\
\-\-workspace name=my\-secret,secret=secret\-name \\
\-\-workspace name=my\-config,config=rpg,item=ultimav=1 \\
\-\-workspace name=my\-empty\-dir,emptyDir="" \\
\-\-workspace name=my\-pvc,claimName=pvc1,subPath=dir

.fi
.RE
Expand Down
11 changes: 8 additions & 3 deletions docs/reference/cmd/pipeline_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ To run a Pipeline that has one git resource and no parameter.
$ tkn pipeline start --resource source=samples-git


To run a Pipeline that has one git resource, one image resource and
two parameters (foo and bar)
To run a Pipeline that has one git resource, one image resource,
two parameters (foo and bar) and four workspaces (my-config, my-pvc,
my-secret and my-empty-dir)


$ tkn pipeline start --resource source=samples-git \
--resource image=my-image \
--param foo=yay \
--param bar=10
--param bar=10 \
--workspace name=my-secret,secret=secret-name \
--workspace name=my-config,config=rpg,item=ultimav=1 \
--workspace name=my-empty-dir,emptyDir="" \
--workspace name=my-pvc,claimName=pvc1,subPath=dir
9 changes: 9 additions & 0 deletions pkg/cmd/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/tektoncd/cli/pkg/pipeline"
hpipelinerun "github.com/tektoncd/cli/pkg/pipelinerun"
validate "github.com/tektoncd/cli/pkg/validate"
"github.com/tektoncd/cli/pkg/workspaces"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
versionedResource "github.com/tektoncd/pipeline/pkg/client/resource/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -70,6 +71,7 @@ type startOptions struct {
PrefixName string
TimeOut string
Filename string
Workspaces []string
}

type resourceOptionsFilter struct {
Expand Down Expand Up @@ -143,6 +145,7 @@ like cat,foo,bar
c.Flags().StringVarP(&opt.UsePipelineRun, "use-pipelinerun", "", "", "use this pipelinerun values to re-run the pipeline. ")
flags.AddShellCompletion(c.Flags().Lookup("use-pipelinerun"), "__tkn_get_pipelinerun")
c.Flags().StringSliceVarP(&opt.Labels, "labels", "l", []string{}, "pass labels as label=value.")
c.Flags().StringArrayVarP(&opt.Workspaces, "workspace", "w", []string{}, "pass the workspace.")
c.Flags().BoolVarP(&opt.DryRun, "dry-run", "", false, "preview pipelinerun without running it")
c.Flags().StringVarP(&opt.Output, "output", "", "", "format of pipelinerun dry-run (yaml or json)")
c.Flags().StringVarP(&opt.PrefixName, "prefix-name", "", "", "specify a prefix for the pipelinerun name (must be lowercase alphanumeric characters)")
Expand Down Expand Up @@ -252,6 +255,12 @@ func (opt *startOptions) startPipeline(pipelineStart *v1alpha1.Pipeline) error {
}
pr.Spec.Params = param

workspaces, err := workspaces.Merge(pr.Spec.Workspaces, opt.Workspaces)
if err != nil {
return err
}
pr.Spec.Workspaces = workspaces

if err := mergeSvc(pr, opt.ServiceAccounts); err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/pipeline/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,30 @@ func TestPipelineStart_ExecuteCommand(t *testing.T) {
"-r=source=scaffold-git",
"-p=pipeline-param=value1",
"-l=jemange=desfrites",
"-w=name=password-vault,secret=secret-name",
"-n", "ns",
},
namespace: "",
input: cs2,
wantError: false,
want: "Pipelinerun started: \n\nIn order to track the pipelinerun progress run:\ntkn pipelinerun logs -f -n ns\n",
},
{
name: "Start pipeline with showlog flag false",
command: []string{"start", "test-pipeline",
"-s=svc1",
"-r=source=scaffold-git",
"-p=pipeline-param=value1",
"-l=jemange=desfrites",
"-w=name=password-vault,secret=secret-name",
"-w=claimName=pvc3",
"-n", "ns",
},
namespace: "",
input: cs2,
wantError: true,
want: "Name not found for workspace",
},
{
name: "Wrong parameter name",
command: []string{"start", "test-pipeline",
Expand Down
239 changes: 239 additions & 0 deletions pkg/workspaces/workspaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
// Copyright © 2019 The Tekton Authors.
Copy link
Contributor

@savitaashture savitaashture Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be

Suggested change
// Copyright © 2019 The Tekton Authors.
// Copyright © 2020 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 workspaces

import (
"errors"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
corev1 "k8s.io/api/core/v1"
)

var (
nameParam = "name"
claimNameParam = "claimName"
subPathParam = "subPath"
emptyDirParam = "emptyDir"
configParam = "config"
secretParam = "secret"
configItemParam = "item"
)

const invalidWorkspace = "invalid input format for workspace : "

var errNotFoundParam = errors.New("param not found")

// Merge merges workspacebinding already in pipelineruns with given options
func Merge(ws []v1alpha1.WorkspaceBinding, optWS []string) ([]v1alpha1.WorkspaceBinding,
error) {
workspaces, err := parseWorkspace(optWS)
if err != nil {
return nil, err
}

if len(workspaces) == 0 {
return ws, nil
}

for i := range ws {
if v, ok := workspaces[ws[i].Name]; ok {
ws[i] = v
delete(workspaces, v.Name)
}
}

for _, v := range workspaces {
khrm marked this conversation as resolved.
Show resolved Hide resolved
ws = append(ws, v)
}

return ws, nil
}

func parseWorkspace(w []string) (map[string]v1alpha1.WorkspaceBinding, error) {
ws := map[string]v1alpha1.WorkspaceBinding{}
for _, v := range w {

r := strings.Split(v, ",")
name, err := getPar(r, nameParam)
if err != nil {
return nil, errors.New("Name not found for workspace")
}

wB := v1alpha1.WorkspaceBinding{
Name: name,
}
nWB := 0
subPath, err := getPar(r, subPathParam)
if err == nil {
wB.SubPath = subPath
} else if err != errNotFoundParam {
return nil, err
}

err = setWorkspaceConfig(r, &wB)
if err == nil {
ws[name] = wB
nWB++
} else if err != errNotFoundParam {
return nil, err
}

err = setWorkspaceSecret(r, &wB)
if err == nil {
ws[name] = wB
nWB++
} else if err != errNotFoundParam {
return nil, err
}

err = setWorkspaceEmptyDir(r, &wB)
if err == nil {
ws[name] = wB
nWB++
}

err = setWorkspacePVC(r, &wB)
if err == nil {
ws[name] = wB
nWB++
}
if nWB != 1 {
return nil, errors.New(invalidWorkspace + v)
}
}
return ws, nil
}

func setWorkspaceSecret(r []string, wB *v1alpha1.WorkspaceBinding) error {
secret, err := getPar(r, secretParam)
if err != nil {
return err
}
items, err := getItems(r)
if err != nil && err != errNotFoundParam {
return err
}
wB.Secret = &corev1.SecretVolumeSource{
SecretName: secret,
Items: items,
}
return nil
}

func setWorkspaceConfig(r []string, wB *v1alpha1.WorkspaceBinding) error {
config, err := getPar(r, configParam)
if err != nil {
return err
}
items, err := getItems(r)
if err != nil && err != errNotFoundParam {
return err
}

wB.ConfigMap = &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: config},
Items: items,
}
return nil
}

func getItems(r []string) ([]corev1.KeyToPath, error) {
var kp []corev1.KeyToPath
for i := range r {
if !strings.Contains(r[i], configItemParam) {
continue
}
s := strings.SplitN(r[i], "=", 2)
if s[0] != configItemParam {
continue
}
if len(s) != 2 {
return nil, errors.New("invalid item")
}
key, path, err := getKeyValue(s[1])
if err != nil {
return nil, err
}
kp = append(kp, corev1.KeyToPath{
Key: key,
Path: path,
})
}
return kp, nil
}

func setWorkspacePVC(r []string, wB *v1alpha1.WorkspaceBinding) error {
claimName, err := getPar(r, claimNameParam)
if err != nil {
return err
}
wB.PersistentVolumeClaim = &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: claimName,
}
return nil
}

func getKeyValue(s string) (string, string, error) {
r := strings.SplitN(s, "=", 2)
if len(r) != 2 {
return "", "", errors.New("invalid key value")
}
return r[0], r[1], nil
}

func setWorkspaceEmptyDir(r []string, wB *v1alpha1.WorkspaceBinding) error {
emptyDir, err := getPar(r, emptyDirParam)
if err != nil {
return err
}

var sM corev1.StorageMedium
switch emptyDir {
case "":
sM = corev1.StorageMediumDefault
case "Memory":
sM = corev1.StorageMediumMemory
case "HugePages":
sM = corev1.StorageMediumHugePages
default:
return errors.New(invalidWorkspace + emptyDirParam)
}

wB.EmptyDir = &corev1.EmptyDirVolumeSource{
Medium: sM,
}
return nil
}

func getPar(r []string, par string) (string, error) {
var p string
for i := range r {
if !strings.Contains(r[i], par) {
continue
}
s := strings.SplitN(r[i], "=", 2)
if s[0] != par {
continue
}
if len(s) != 2 {
return p, errors.New(invalidWorkspace + r[i])
}
p = s[1]
return p, nil
}
return p, errNotFoundParam
}
Loading