Skip to content

Commit 690c15b

Browse files
committed
Disable support for Exec-type commands in the preStart lifecycle
As suggested by David, do not support preStart Exec-type commands in devfiles and instead return an error. Signed-off-by: Angel Misevski <[email protected]>
1 parent 7b2942a commit 690c15b

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

pkg/library/lifecycle.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
package library
1414

1515
import (
16+
"fmt"
17+
1618
"github.com/devfile/api/pkg/apis/workspaces/v1alpha1"
1719
)
1820

21+
// GetInitContainers partitions the components in a devfile's flattened spec into initContainer and non-initContainer lists
22+
// based off devfile lifecycle bindings and commands. Note that a component can appear in both lists, if e.g. it referred to
23+
// in a preStart command and in a regular command.
1924
func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initContainers, mainComponents []v1alpha1.Component, err error) {
2025
components := devfile.Components
2126
commands := devfile.Commands
@@ -29,6 +34,10 @@ func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initCo
2934
if err != nil {
3035
return nil, nil, err
3136
}
37+
// Check that commands in PreStart lifecycle binding are supported
38+
if err = checkEventCommandsValidity(initCommands); err != nil {
39+
return nil, nil, err
40+
}
3241
initComponentKeys, err := commandListToComponentKeys(initCommands)
3342
if err != nil {
3443
return nil, nil, err
@@ -64,3 +73,20 @@ func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initCo
6473

6574
return initContainers, mainComponents, nil
6675
}
76+
77+
func checkEventCommandsValidity(initCommands []v1alpha1.Command) error {
78+
for _, cmd := range initCommands {
79+
commandType, err := getCommandType(cmd)
80+
if err != nil {
81+
return err
82+
}
83+
switch commandType {
84+
case v1alpha1.ApplyCommandType:
85+
continue
86+
default:
87+
// How a prestart exec command should be implemented is undefined currently, so we reject it.
88+
return fmt.Errorf("only apply-type commands are supported in the prestart lifecycle binding")
89+
}
90+
}
91+
return nil
92+
}

pkg/library/lifecycle_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func loadTestCaseOrPanic(t *testing.T, testFilename string) testCase {
4848
if err := yaml.Unmarshal(bytes, &test); err != nil {
4949
t.Fatal(err)
5050
}
51-
t.Log(fmt.Printf("Read file:\n %+v \n\n", test))
51+
t.Log(fmt.Sprintf("Read file:\n %+v \n\n", test))
5252
return test
5353
}
5454

@@ -66,7 +66,7 @@ func TestGetInitContainers(t *testing.T) {
6666
assert.True(t, len(tt.Input.Components) > 0, "Input defines no components")
6767
gotInitContainers, gotMainComponents, err := GetInitContainers(tt.Input)
6868
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
69-
assert.Regexp(t, tt.Output.ErrRegexp, err.Error(), "Error message should match")
69+
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
7070
} else {
7171
assert.Equal(t, tt.Output.InitContainers, gotInitContainers, "Init containers should match expected")
7272
assert.Equal(t, tt.Output.MainContainers, gotMainComponents, "Main containers should match expected")

pkg/library/testdata/lifecycle/init_and_main_container.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ input:
99
name: test-container2
1010
image: my-image
1111
commands:
12-
- exec:
12+
- apply:
1313
id: test_preStart_command
1414
component: test-container1
15-
command: "test_command"
1615
- exec:
1716
id: test_regular_command
1817
component: test-container1

pkg/library/testdata/lifecycle/prestart_exec_command.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ output:
2626
- container:
2727
name: test-container2
2828
image: my-image
29-
errRegexp:
29+
errRegexp: "only apply-type commands are supported in the prestart lifecycle binding"

0 commit comments

Comments
 (0)