From 74fcc6d19e0293ec3c7d3b6c47f8fd8a803439e5 Mon Sep 17 00:00:00 2001
From: Marniks7 <9289172+marniks7@users.noreply.github.com>
Date: Sat, 10 Sep 2022 14:59:16 -0400
Subject: [PATCH] Feature: Bind Workspaces for PipelineRun

UI: Add workspaces dropdown with all possible volume sources for PipelineRun
Support volume sources: configmap, secret, pvc, emptydir
Backend: intercept k8s response for configmap, secret or pvc and modify response to contain only non-sensitive data
RBAC: allow to list \ watch those volume sources

Support Optional Workspaces
---
 .../write/create-pipeline-run-with-ws.cy.js   |  4 +-
 .../CreatePipelineRun/CreatePipelineRun.js    | 42 ++++---------------
 test/e2e-tests.sh                             |  1 -
 .../envsubst/pipelinerun-with-ws.yaml         | 11 -----
 test/resources/static/Pipeline-with-ws.yaml   | 15 -------
 5 files changed, 11 insertions(+), 62 deletions(-)

diff --git a/packages/e2e/cypress/e2e/write/create-pipeline-run-with-ws.cy.js b/packages/e2e/cypress/e2e/write/create-pipeline-run-with-ws.cy.js
index a65e6021ac..6c9be4045c 100644
--- a/packages/e2e/cypress/e2e/write/create-pipeline-run-with-ws.cy.js
+++ b/packages/e2e/cypress/e2e/write/create-pipeline-run-with-ws.cy.js
@@ -41,7 +41,7 @@ describe('Create Pipeline Run with Workspace', () => {
 
     cy.contains('pipeline-with-ws-run-').first().click();
 
-    cy.get('header[class="tkn--pipeline-run-header"]', { timeout: 6000 })
+    cy.get('header[class="tkn--pipeline-run-header"]', { timeout: 10000 })
       .find('span[class="tkn--status-label"]', { timeout: 15000 })
       .should('have.text', 'Succeeded');
   });
@@ -58,7 +58,7 @@ describe('Create Pipeline Run with Workspace', () => {
 
     cy.contains('pipeline-with-ws-pvc-run-').first().click();
 
-    cy.get('header[class="tkn--pipeline-run-header"]', { timeout: 6000 })
+    cy.get('header[class="tkn--pipeline-run-header"]', { timeout: 10000 })
       .find('span[class="tkn--status-label"]', { timeout: 15000 })
       .should('have.text', 'Succeeded');
   });
diff --git a/src/containers/CreatePipelineRun/CreatePipelineRun.js b/src/containers/CreatePipelineRun/CreatePipelineRun.js
index 27bd6dfca6..2d08cc0a41 100644
--- a/src/containers/CreatePipelineRun/CreatePipelineRun.js
+++ b/src/containers/CreatePipelineRun/CreatePipelineRun.js
@@ -194,6 +194,15 @@ function CreatePipelineRun({ intl }) {
         true
       );
     const workspacesSpecMap = keyBy(workspaceSpecs, 'name');
+    const validWorkspaces =
+      !workspaces ||
+      Object.keys(workspacesSpecMap).reduce(
+        (acc, name) =>
+          acc &&
+          (!!workspaces[name] || workspacesSpecMap[name]?.optional === true),
+        true
+      );
+    const workspacesSpecMap = keyBy(workspaceSpecs, 'name');
     const validWorkspaces =
       !workspaces ||
       Object.keys(workspacesSpecMap).reduce(
@@ -450,17 +459,6 @@ function CreatePipelineRun({ intl }) {
     });
   }
 
-  function handleWorkspacePVCSubPathChange(key, value) {
-    setState(state => {
-      const workspace = initWorkspace(key);
-      workspace['sub-path'] = value;
-
-      return {
-        ...state
-      };
-    });
-  }
-
   function handleSubmit(event) {
     event.preventDefault();
 
@@ -727,28 +725,6 @@ function CreatePipelineRun({ intl }) {
                     handleWorkspaceChange(workspaceSpec.name, id, kind);
                   }}
                 />
-                {workspaces[workspaceSpec.name] &&
-                workspaces[workspaceSpec.name].value &&
-                workspaces[workspaceSpec.name].kind ===
-                  'PersistentVolumeClaim' ? (
-                  <TextInput
-                    id={`create-pipelinerun--ws-sub-path-${workspaceSpec.name}`}
-                    key={`create-pipelinerun--ws-sub-path-${workspaceSpec.name}`}
-                    labelText="sub-path"
-                    helperText={intl.formatMessage({
-                      id: 'dashboard.createRun.subPathHelperText',
-                      defaultMessage: 'Specify Sub Path (optional)'
-                    })}
-                    value={
-                      (workspaces[workspaceSpec.name] &&
-                        workspaces[workspaceSpec.name]['sub-path']) ||
-                      ''
-                    }
-                    onChange={({ target: { value } }) =>
-                      handleWorkspacePVCSubPathChange(workspaceSpec.name, value)
-                    }
-                  />
-                ) : null}
               </>
             ))}
           </FormGroup>
diff --git a/test/e2e-tests.sh b/test/e2e-tests.sh
index 107adc47fa..416fd2fc97 100755
--- a/test/e2e-tests.sh
+++ b/test/e2e-tests.sh
@@ -145,7 +145,6 @@ test_dashboard() {
   else
       docker run --rm --network=host -v $VIDEO_PATH:/home/node/cypress/videos dashboard-e2e || fail_test "Browser E2E tests failed"
   fi
-
   # If we get here the tests passed, no need to upload artifacts
   rm -rf $VIDEO_PATH
   kill -9 $dashboardForwardPID
diff --git a/test/resources/envsubst/pipelinerun-with-ws.yaml b/test/resources/envsubst/pipelinerun-with-ws.yaml
index 02ac87598a..fb165f1888 100644
--- a/test/resources/envsubst/pipelinerun-with-ws.yaml
+++ b/test/resources/envsubst/pipelinerun-with-ws.yaml
@@ -19,17 +19,6 @@ spec:
     - name: optional-ws
       configMap:
         name: configmap-test
-#    - name: empDirSubPath-ws
-#      emptyDir: {}
-#      subPath: deep
-#    - name: volumeClaimTemplate-ws
-#      volumeClaimTemplate:
-#        spec:
-#          accessModes:
-#            - ReadWriteOnce
-#          resources:
-#            requests:
-#              storage: 10Mi
   pipelineRef:
     name: pipeline-with-ws
   serviceAccountName: e2e-tests
diff --git a/test/resources/static/Pipeline-with-ws.yaml b/test/resources/static/Pipeline-with-ws.yaml
index 00b07f843c..c905c915ae 100644
--- a/test/resources/static/Pipeline-with-ws.yaml
+++ b/test/resources/static/Pipeline-with-ws.yaml
@@ -14,8 +14,6 @@ spec:
       description: Pipeline Workspace Description
     - name: optional-unbound-ws
       optional: true
-#    - name: empDirSubPath-ws
-#    - name: volumeClaimTemplate-ws
   tasks:
     - name: hello
       workspaces:
@@ -29,10 +27,6 @@ spec:
           workspace: optional-ws
         - name: optional-unbound-ws
           workspace: optional-unbound-ws
-#        - name: empDirSubPath-ws
-#          workspace: empDirSubPath-ws
-#        - name: volumeClaimTemplate-ws
-#          workspace: volumeClaimTemplate-ws
       taskSpec:
         workspaces:
           - name: configmap-ws
@@ -43,8 +37,6 @@ spec:
             description: Task Workspace Description
           - name: optional-unbound-ws
             optional: true
-#          - name: empDirSubPath-ws
-#          - name: volumeClaimTemplate-ws
         steps:
           - name: write
             image: busybox
@@ -91,10 +83,3 @@ spec:
                 exit 1
               fi
               echo "Hello World!"
-#              echo "$(workspaces.empDirSubPath-ws.path)"
-#              actualVCT=$(cat $(workspaces.volumeClaimTemplate-ws.path)/test)
-#              if [ "$actualVCT" != "volumeClaimTemplate-value" ]; then
-#                echo "VolumeClaimTemplate. Actual: $actualVCT. Expected: volumeClaimTemplate-value"
-#                exit 1
-#              fi
-#              echo $actualVCT