Skip to content

Commit

Permalink
Add handling of working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
othomann authored and tekton-robot committed Feb 27, 2020
1 parent 6877e66 commit 4c26ba5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
10 changes: 10 additions & 0 deletions config/config-feature-flags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 19 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
42 changes: 42 additions & 0 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 4c26ba5

Please sign in to comment.