isEventValid checks if events belonging to a specific event type are valid ie; 1. event should map to a valid devfile command 2. preStart and postStop events should either map to an apply command or a composite command with apply commands 3. postStart and preStop events should either map to an exec
(eventNames []string, eventType string, commandMap map[string]v1alpha2.Command)
| 66 | // 2. preStart and postStop events should either map to an apply command or a composite command with apply commands |
| 67 | // 3. postStart and preStop events should either map to an exec command or a composite command with exec commands |
| 68 | func isEventValid(eventNames []string, eventType string, commandMap map[string]v1alpha2.Command) error { |
| 69 | var invalidCommand, invalidApplyEvents, invalidExecEvents []string |
| 70 | |
| 71 | for _, eventName := range eventNames { |
| 72 | command, ok := commandMap[strings.ToLower(eventName)] |
| 73 | if !ok { // check if event is in the list of devfile commands |
| 74 | invalidCommand = append(invalidCommand, eventName) |
| 75 | continue |
| 76 | } |
| 77 | |
| 78 | switch eventType { |
| 79 | case preStart, postStop: |
| 80 | // check if the event is either an apply command or a composite of apply commands |
| 81 | if command.Apply == nil && command.Composite == nil { |
| 82 | invalidApplyEvents = append(invalidApplyEvents, eventName) |
| 83 | } else if command.Composite != nil { |
| 84 | invalidApplyEvents = append(invalidApplyEvents, validateCompositeEvents(*command.Composite, eventName, eventType, commandMap)...) |
| 85 | } |
| 86 | case postStart, preStop: |
| 87 | // check if the event is either an exec command or a composite of exec commands |
| 88 | if command.Exec == nil && command.Composite == nil { |
| 89 | invalidExecEvents = append(invalidExecEvents, eventName) |
| 90 | } else if command.Composite != nil { |
| 91 | invalidExecEvents = append(invalidExecEvents, validateCompositeEvents(*command.Composite, eventName, eventType, commandMap)...) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | var err error |
| 97 | var eventErrorsList []string |
| 98 | |
| 99 | if len(invalidCommand) > 0 { |
| 100 | eventErrorsList = append(eventErrorsList, fmt.Sprintf("%s does not map to a valid devfile command", strings.Join(invalidCommand, ", "))) |
| 101 | } |
| 102 | |
| 103 | if len(invalidApplyEvents) > 0 { |
| 104 | eventErrorsList = append(eventErrorsList, fmt.Sprintf("%s should either map to an apply command or a composite command with apply commands", strings.Join(invalidApplyEvents, ", "))) |
| 105 | } |
| 106 | |
| 107 | if len(invalidExecEvents) > 0 { |
| 108 | eventErrorsList = append(eventErrorsList, fmt.Sprintf("%s should either map to an exec command or a composite command with exec commands", strings.Join(invalidExecEvents, ", "))) |
| 109 | } |
| 110 | |
| 111 | if len(eventErrorsList) != 0 { |
| 112 | eventErrors := fmt.Sprintf("\n%s", strings.Join(eventErrorsList, "\n")) |
| 113 | err = &InvalidEventError{eventType: eventType, errorMsg: eventErrors} |
| 114 | } |
| 115 | |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | // validateCompositeEvents checks if a composite subcommands are |
| 120 | // 1. apply commands for preStart and postStop |