Skip to content

Commit

Permalink
support VolumeClaimTemplates with --workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand committed Jul 16, 2020
1 parent c226d0b commit 209581e
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 58 deletions.
6 changes: 3 additions & 3 deletions pkg/cmd/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (opt *startOptions) startPipeline(pipelineStart *v1beta1.Pipeline) error {
}
pr.Spec.Params = param

workspaces, err := workspaces.Merge(pr.Spec.Workspaces, opt.Workspaces)
workspaces, err := workspaces.Merge(pr.Spec.Workspaces, opt.Workspaces, opt.cliparams)
if err != nil {
return err
}
Expand Down Expand Up @@ -736,8 +736,8 @@ func NameArg(args []string, p cli.Params, file string) (*v1beta1.Pipeline, error
return pipelineFile, nil
}

func parsePipeline(taskLocation string, p cli.Params) (*v1beta1.Pipeline, error) {
b, err := file.LoadFileContent(p, taskLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", taskLocation))
func parsePipeline(pipelineLocation string, p cli.Params) (*v1beta1.Pipeline, error) {
b, err := file.LoadFileContent(p, pipelineLocation, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", pipelineLocation))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/task/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func startTask(opt startOptions, args []string) error {
}
tr.ObjectMeta.Labels = labels

workspaces, err := workspaces.Merge(tr.Spec.Workspaces, opt.Workspaces)
workspaces, err := workspaces.Merge(tr.Spec.Workspaces, opt.Workspaces, opt.cliparams)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ func LoadFileContent(p cli.Params, target string, validate TypeValidator, errorM
}

var content []byte
cs, err := p.Clients()
if err != nil {
return nil, fmt.Errorf("failed to create tekton client")
var cs *cli.Clients
var err error
if p != nil {
cs, err = p.Clients()
if err != nil {
return nil, fmt.Errorf("failed to create tekton client")
}
}

if strings.HasPrefix(target, "http") {
if strings.HasPrefix(target, "http") && cs != nil {
content, err = getRemoteContent(cs, target)
} else {
content, err = ioutil.ReadFile(target)
Expand Down
25 changes: 25 additions & 0 deletions pkg/workspaces/testdata/pvc-typo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: vctname
spec:
storageClassNam: "storageclassname"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi
25 changes: 25 additions & 0 deletions pkg/workspaces/testdata/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: vctname
spec:
storageClassName: "storageclassname"
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi
115 changes: 80 additions & 35 deletions pkg/workspaces/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,35 @@ package workspaces

import (
"errors"
"fmt"
"strings"

"github.com/tektoncd/cli/pkg/cli"
"github.com/tektoncd/cli/pkg/file"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

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

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 []v1beta1.WorkspaceBinding, optWS []string) ([]v1beta1.WorkspaceBinding,
func Merge(ws []v1beta1.WorkspaceBinding, optWS []string, p cli.Params) ([]v1beta1.WorkspaceBinding,
error) {
workspaces, err := parseWorkspace(optWS)
workspaces, err := parseWorkspace(optWS, p)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +67,7 @@ func Merge(ws []v1beta1.WorkspaceBinding, optWS []string) ([]v1beta1.WorkspaceBi
return ws, nil
}

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

Expand All @@ -83,37 +88,49 @@ func parseWorkspace(w []string) (map[string]v1beta1.WorkspaceBinding, error) {
return nil, err
}

err = setWorkspaceConfig(r, &wB)
if err == nil {
ws[name] = wB
nWB++
} else if err != errNotFoundParam {
return nil, err
}
// check for volumeClaimTemplate
vctFile, err := getPar(r, volumeClaimTemplateFile)
if err != nil {
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 = 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 = setWorkspaceEmptyDir(r, &wB)
if err == nil {
ws[name] = wB
nWB++
}

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

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

return ws, nil
}

Expand Down Expand Up @@ -219,6 +236,34 @@ func setWorkspaceEmptyDir(r []string, wB *v1beta1.WorkspaceBinding) error {
return nil
}

func setWorkspaceVCTemplate(r []string, wB *v1beta1.WorkspaceBinding, vctFile string, p cli.Params) error {
pvc, err := parseVolumeClaimTemplate(vctFile, p)
if err != nil {
return err
}

wB.VolumeClaimTemplate = pvc
return nil
}

func parseVolumeClaimTemplate(filePath string, p cli.Params) (*corev1.PersistentVolumeClaim, error) {
b, err := file.LoadFileContent(p, filePath, file.IsYamlFile(), fmt.Errorf("invalid file format for %s: .yaml or .yml file extension and format required", filePath))
if err != nil {
return nil, err
}
m := map[string]interface{}{}
err = yaml.UnmarshalStrict(b, &m)
if err != nil {
return nil, err
}

pvc := corev1.PersistentVolumeClaim{}
if err := yaml.UnmarshalStrict(b, &pvc); err != nil {
return nil, err
}
return &pvc, nil
}

func getPar(r []string, par string) (string, error) {
var p string
for i := range r {
Expand Down
Loading

0 comments on commit 209581e

Please sign in to comment.