-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
readOnly flag on workspace declarations
Introduce a readOnly flag to workspace declarations on tasks. This flag allows a task to declare whether it needs to write files to a workspace or simply read from it. Setting this flag to true will result in a volumeMount on the container with its readOnly flag also set to true.
- Loading branch information
1 parent
2add7d2
commit 03d970c
Showing
9 changed files
with
192 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: my-pvc-2 | ||
spec: | ||
resources: | ||
requests: | ||
storage: 5Gi | ||
volumeMode: Filesystem | ||
accessModes: | ||
- ReadWriteOnce | ||
--- | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: TaskRun | ||
metadata: | ||
generateName: workspaces-readonly- | ||
spec: | ||
workspaces: | ||
- name: write-allowed | ||
persistentVolumeClaim: | ||
claimName: my-pvc-2 | ||
- name: write-disallowed | ||
persistentVolumeClaim: | ||
claimName: my-pvc-2 | ||
taskSpec: | ||
workspaces: | ||
- name: write-allowed | ||
- name: write-disallowed | ||
readOnly: true | ||
steps: | ||
- name: write-allowed | ||
image: ubuntu | ||
script: echo "hello" > $(workspaces.write-allowed.path)/foo | ||
- name: read-allowed | ||
image: ubuntu | ||
script: cat $(workspaces.write-allowed.path)/foo | grep "hello" | ||
- name: write-disallowed | ||
image: ubuntu | ||
script: | ||
echo "goodbye" > $(workspaces.write-disallowed.path)/foo || touch write-failed.txt | ||
test -f write-failed.txt | ||
- name: read-again | ||
# We should get "hello" when reading again because writing "goodbye" to | ||
# the file should have been disallowed. | ||
image: ubuntu | ||
script: | ||
cat $(workspaces.write-disallowed.path)/foo | grep "hello" |
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
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,85 @@ | ||
// +build e2e | ||
|
||
/* | ||
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 test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
tb "github.com/tektoncd/pipeline/test/builder" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
knativetest "knative.dev/pkg/test" | ||
) | ||
|
||
func TestWorkspaceReadOnlyDisallowsWrite(t *testing.T) { | ||
c, namespace := setup(t) | ||
|
||
taskName := "write-disallowed" | ||
taskRunName := "write-disallowed-tr" | ||
|
||
knativetest.CleanupOnInterrupt(func() { tearDown(t, c, namespace) }, t.Logf) | ||
defer tearDown(t, c, namespace) | ||
|
||
task := tb.Task(taskName, namespace, tb.TaskSpec( | ||
tb.Step("attempt-write", "alpine", tb.StepScript("echo foo > /workspace/test/file")), | ||
tb.TaskWorkspace("test", "test workspace", "/workspace/test", true), | ||
)) | ||
if _, err := c.TaskClient.Create(task); err != nil { | ||
t.Fatalf("Failed to create Task: %s", err) | ||
} | ||
|
||
taskRun := tb.TaskRun(taskRunName, namespace, tb.TaskRunSpec( | ||
tb.TaskRunTaskRef(taskName), tb.TaskRunServiceAccountName("default"), | ||
tb.TaskRunWorkspaceEmptyDir("test", ""), | ||
)) | ||
if _, err := c.TaskRunClient.Create(taskRun); err != nil { | ||
t.Fatalf("Failed to create TaskRun: %s", err) | ||
} | ||
|
||
t.Logf("Waiting for TaskRun in namespace %s to finish", namespace) | ||
if err := WaitForTaskRunState(c, taskRunName, TaskRunFailed(taskRunName), "error"); err != nil { | ||
t.Errorf("Error waiting for TaskRun to finish with error: %s", err) | ||
} | ||
|
||
tr, err := c.TaskRunClient.Get(taskRunName, metav1.GetOptions{}) | ||
if err != nil { | ||
t.Errorf("Error retrieving taskrun: %s", err) | ||
} | ||
if tr.Status.PodName == "" { | ||
t.Fatal("Error getting a PodName (empty)") | ||
} | ||
p, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(tr.Status.PodName, metav1.GetOptions{}) | ||
|
||
if err != nil { | ||
t.Fatalf("Error getting pod `%s` in namespace `%s`", tr.Status.PodName, namespace) | ||
} | ||
for _, stat := range p.Status.ContainerStatuses { | ||
if strings.Contains(stat.Name, "step-attempt-write") { | ||
req := c.KubeClient.Kube.CoreV1().Pods(namespace).GetLogs(p.Name, &corev1.PodLogOptions{Container: stat.Name}) | ||
logContent, err := req.Do().Raw() | ||
if err != nil { | ||
t.Fatalf("Error getting pod logs for pod `%s` and container `%s` in namespace `%s`", tr.Status.PodName, stat.Name, namespace) | ||
} | ||
if !strings.Contains(string(logContent), "Read-only file system") { | ||
t.Fatalf("Expected read-only file system error but received %v", logContent) | ||
} | ||
} | ||
} | ||
} |