diff --git a/config/config-feature-flags.yaml b/config/config-feature-flags.yaml index 68cbedcf1b8..c375577f27d 100644 --- a/config/config-feature-flags.yaml +++ b/config/config-feature-flags.yaml @@ -28,3 +28,13 @@ data: # See https://github.com/tektoncd/pipeline/issues/2013 for more # info. disable-home-env-overwrite: "false" + # Setting this flag to "true" will prevent Tekton overriding your + # Task container's working directory. + # + # The default behaviour currently is for Tekton to override the + # working directory if not set by the user but this will change + # in an upcoming release. + # + # See https://github.com/tektoncd/pipeline/issues/1836 for more + # info. + disable-working-directory-overwrite: "false" diff --git a/docs/install.md b/docs/install.md index 565da21a295..a9f6e7a34e1 100644 --- a/docs/install.md +++ b/docs/install.md @@ -244,6 +244,25 @@ data: disable-home-env-overwrite: "true" # Tekton will not overwrite $HOME in Steps. ``` +- `disable-working-directory-overwrite` - Setting this flag to "true" will prevent Tekton +from overwriting Step containers' working directory. The default +value is "false" and so the default behaviour is for the working directory to be +overwritten by Tekton with `/workspace` if the working directory is not specified explicitly +for the step container. This default is very likely to change in an upcoming +release. For further reference see https://github.com/tektoncd/pipeline/issues/1836. + +Here is an example of the `feature-flags` ConfigMap with `disable-working-directory-overwrite` +flipped on: + +```yaml +apiVersion: v1 +kind: ConfigMap +metadata: + name: feature-flags +data: + disable-working-directory-overwrite: "true" # Tekton will not overwrite the working directory in Steps. +``` + ## Custom Releases The [release Task](./../tekton/README.md) can be used for creating a custom diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index b36457e41d5..1cb6ca15d35 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -34,8 +34,9 @@ import ( const ( homeDir = "/tekton/home" - featureFlagConfigMapName = "feature-flags" - featureFlagDisableHomeEnvKey = "disable-home-env-overwrite" + featureFlagConfigMapName = "feature-flags" + featureFlagDisableHomeEnvKey = "disable-home-env-overwrite" + featureFlagDisableWorkingDirKey = "disable-working-directory-overwrite" taskRunLabelKey = pipeline.GroupName + pipeline.TaskRunLabelKey ) @@ -166,8 +167,9 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha // - sets container name to add "step-" prefix or "step-unnamed-#" if not specified. // TODO(#1605): Remove this loop and make each transformation in // isolation. + shouldOverrideWorkingDir := shouldOverrideWorkingDir(kubeclient) for i, s := range stepContainers { - if s.WorkingDir == "" { + if s.WorkingDir == "" && shouldOverrideWorkingDir { stepContainers[i].WorkingDir = pipeline.WorkspaceDir } if s.Name == "" { @@ -311,3 +313,17 @@ func shouldOverrideHomeEnv(kubeclient kubernetes.Interface) bool { } return true } + +// shouldOverrideWorkingDir returns a bool indicating whether a Pod should have its +// working directory overwritten with /workspace or if it should be +// left unmodified. The default behaviour is to overwrite the working directory with '/workspace' +// if not specified by the user, but this is planned to change in an upcoming release. +// +// For further reference see https://github.com/tektoncd/pipeline/issues/1836 +func shouldOverrideWorkingDir(kubeclient kubernetes.Interface) bool { + configMap, err := kubeclient.CoreV1().ConfigMaps(system.GetNamespace()).Get(featureFlagConfigMapName, metav1.GetOptions{}) + if err == nil && configMap != nil && configMap.Data != nil && configMap.Data[featureFlagDisableWorkingDirKey] == "true" { + return false + } + return true +} diff --git a/pkg/pod/pod_test.go b/pkg/pod/pod_test.go index 4c75af0d7a5..5ee55acb30c 100644 --- a/pkg/pod/pod_test.go +++ b/pkg/pod/pod_test.go @@ -842,3 +842,45 @@ func TestShouldOverrideHomeEnv(t *testing.T) { }) } } + +func TestShouldOverrideWorkingDir(t *testing.T) { + for _, tc := range []struct { + description string + configMap *corev1.ConfigMap + expected bool + }{{ + description: "Default behaviour: A missing disable-working-directory-overwrite flag should result in true", + configMap: &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: featureFlagConfigMapName, Namespace: system.GetNamespace()}, + Data: map[string]string{}, + }, + expected: true, + }, { + description: "Setting disable-working-directory-overwrite to false should result in true", + configMap: &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: featureFlagConfigMapName, Namespace: system.GetNamespace()}, + Data: map[string]string{ + featureFlagDisableWorkingDirKey: "false", + }, + }, + expected: true, + }, { + description: "Setting disable-working-directory-overwrite to true should result in false", + configMap: &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: featureFlagConfigMapName, Namespace: system.GetNamespace()}, + Data: map[string]string{ + featureFlagDisableWorkingDirKey: "true", + }, + }, + expected: false, + }} { + t.Run(tc.description, func(t *testing.T) { + kubeclient := fakek8s.NewSimpleClientset( + tc.configMap, + ) + if result := shouldOverrideWorkingDir(kubeclient); result != tc.expected { + t.Errorf("Expected %t Received %t", tc.expected, result) + } + }) + } +}