Skip to content

Commit f985c57

Browse files
committed
TMP Prop
1 parent 52854ef commit f985c57

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

pkg/library/command.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ func getCommandType(command v1alpha1.Command) (v1alpha1.CommandType, error) {
2626
return command.CommandType, nil
2727
}
2828

29-
func getCommandsForIds(ids []string, commands []v1alpha1.Command) ([]v1alpha1.Command, error) {
29+
func getCommandsForIDs(ids []string, commands []v1alpha1.Command) ([]v1alpha1.Command, error) {
3030
var resolvedCommands []v1alpha1.Command
3131

3232
for _, id := range ids {
33-
resolvedCommand, err := getCommandById(id, commands)
33+
resolvedCommand, err := getCommandByID(id, commands)
3434
if err != nil {
3535
return nil, err
3636
}
@@ -40,48 +40,49 @@ func getCommandsForIds(ids []string, commands []v1alpha1.Command) ([]v1alpha1.Co
4040
return resolvedCommands, nil
4141
}
4242

43-
func getCommandById(id string, commands []v1alpha1.Command) (*v1alpha1.Command, error) {
43+
func getCommandByID(id string, commands []v1alpha1.Command) (*v1alpha1.Command, error) {
4444
for _, command := range commands {
45-
commandKey, err := command.Key()
45+
commandID, err := command.Key()
4646
if err != nil {
4747
return nil, err
4848
}
49-
if commandKey == id {
49+
if commandID == id {
5050
return &command, nil
5151
}
5252
}
5353
return nil, fmt.Errorf("no command with key %s is defined", id)
5454
}
5555

56-
func commandListToComponentKeys(commands []v1alpha1.Command) (map[string]bool, error) {
57-
componentKeys := map[string]bool{}
56+
func commandListToComponentID(commands []v1alpha1.Command) (map[string]bool, error) {
57+
componentIDs := map[string]bool{}
5858
for _, command := range commands {
5959
commandType, err := getCommandType(command)
6060
if err != nil {
6161
return nil, err
6262
}
6363
switch commandType {
6464
case v1alpha1.ApplyCommandType:
65-
componentKeys[command.Apply.Component] = true
65+
componentIDs[command.Apply.Component] = true
6666
case v1alpha1.ExecCommandType:
6767
// TODO: This will require special handling (how do we handle prestart exec?)
68-
componentKeys[command.Exec.Component] = true
68+
componentIDs[command.Exec.Component] = true
6969
case v1alpha1.CompositeCommandType:
7070
// TODO: Handle composite commands: what if an init command is composite and refers to other commands
7171
default: // Ignore
7272
}
7373
}
74-
return componentKeys, nil
74+
return componentIDs, nil
7575
}
7676

77-
func removeCommandsByKeys(keys []string, commands []v1alpha1.Command) ([]v1alpha1.Command, error) {
77+
func removeCommandsByIDs(IDs []string, commands []v1alpha1.Command) ([]v1alpha1.Command, error) {
78+
toRemove := stringListToMap(IDs)
7879
var filtered []v1alpha1.Command
7980
for _, command := range commands {
8081
key, err := command.Key()
8182
if err != nil {
8283
return nil, err
8384
}
84-
if !listContains(key, keys) {
85+
if !toRemove[key] {
8586
filtered = append(filtered, command)
8687
}
8788
}

pkg/library/common.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
package library
1414

15+
func stringListToMap(list []string) map[string]bool {
16+
res := map[string]bool{}
17+
for _, item := range list {
18+
res[item] = true
19+
}
20+
return res
21+
}
22+
1523
func listContains(query string, list []string) bool {
1624
for _, elem := range list {
1725
if query == elem {

pkg/library/lifecycle.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initCo
3030
return nil, components, nil
3131
}
3232

33-
initCommands, err := getCommandsForIds(events.PreStart, commands)
33+
initCommands, err := getCommandsForIDs(events.PreStart, commands)
3434
if err != nil {
3535
return nil, nil, err
3636
}
3737
// Check that commands in PreStart lifecycle binding are supported
3838
if err = checkEventCommandsValidity(initCommands); err != nil {
3939
return nil, nil, err
4040
}
41-
initComponentKeys, err := commandListToComponentKeys(initCommands)
41+
initComponentIDs, err := commandListToComponentID(initCommands)
4242
if err != nil {
4343
return nil, nil, err
4444
}
4545

4646
// Need to also consider components that are *both* init containers and in the main deployment
4747
// Example: component is referenced in both a prestart event and a regular, non-prestart command
4848
// TODO: Figure out details of handling postStop commands, since they should not be included in main deployment
49-
nonInitCommands, err := removeCommandsByKeys(events.PreStart, commands)
49+
nonInitCommands, err := removeCommandsByIDs(events.PreStart, commands)
5050
if err != nil {
5151
return nil, nil, err
5252
}
53-
mainComponentKeys, err := commandListToComponentKeys(nonInitCommands)
53+
mainComponentIDs, err := commandListToComponentID(nonInitCommands)
5454
if err != nil {
5555
return nil, nil, err
5656
}
@@ -60,9 +60,9 @@ func GetInitContainers(devfile v1alpha1.DevWorkspaceTemplateSpecContent) (initCo
6060
if err != nil {
6161
return nil, nil, err
6262
}
63-
if initComponentKeys[componentID] {
63+
if initComponentIDs[componentID] {
6464
initContainers = append(initContainers, component)
65-
if mainComponentKeys[componentID] {
65+
if mainComponentIDs[componentID] {
6666
// Component is *also* a main component.
6767
mainComponents = append(mainComponents, component)
6868
}

pkg/library/lifecycle_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func TestGetInitContainers(t *testing.T) {
6161
t.Run(tt.Name, func(t *testing.T) {
6262
// sanity check that file reads correctly.
6363
assert.True(t, len(tt.Input.Components) > 0, "Input defines no components")
64+
if tt.Output.ErrRegexp != nil && len(*tt.Output.ErrRegexp) > 0 &&
65+
(len(tt.Output.InitContainers) > 0 || len(tt.Output.MainContainers) > 0) {
66+
t.Fatal("No init containers or main containers could be configured when error is expected")
67+
}
68+
6469
gotInitContainers, gotMainComponents, err := GetInitContainers(tt.Input)
6570
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
6671
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")

pkg/library/testdata/lifecycle/prestart_exec_command.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,4 @@ input:
1818
- "test_command"
1919

2020
output:
21-
initContainers:
22-
- container:
23-
name: test-container1
24-
image: my-image
25-
mainContainers:
26-
- container:
27-
name: test-container2
28-
image: my-image
2921
errRegexp: "only apply-type commands are supported in the prestart lifecycle binding"

0 commit comments

Comments
 (0)