-
Notifications
You must be signed in to change notification settings - Fork 249
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Copyright © 2019 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
🤔