Skip to content

Commit 1211cd6

Browse files
committed
[core] Add support for enabling/disabling roles at deployment time
1 parent 8009437 commit 1211cd6

5 files changed

Lines changed: 52 additions & 2 deletions

File tree

core/workflow/aggregatorrole.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package workflow
2626

2727
import (
2828
"errors"
29+
"strings"
2930
texttemplate "text/template"
3031

3132
"github.com/AliceO2Group/Control/core/repos"
@@ -91,9 +92,10 @@ func (r *aggregatorRole) ProcessTemplates(workflowRepo *repos.Repo) (err error)
9192
template.STAGE3: template.Fields{
9293
template.WrapPointer(&r.Name),
9394
},
94-
template.STAGE4: append(
95+
template.STAGE4: append(append(
9596
template.WrapConstraints(r.Constraints),
9697
r.wrapConnectFields()...),
98+
template.WrapPointer(&r.Enabled)),
9799
}
98100

99101
// TODO: push cached templates here
@@ -116,13 +118,27 @@ func (r *aggregatorRole) ProcessTemplates(workflowRepo *repos.Repo) (err error)
116118
r.Vars.Set(k, v)
117119
}
118120

121+
r.Enabled = strings.TrimSpace(r.Enabled)
122+
123+
// Process templates for child roles
119124
for _, role := range r.Roles {
120125
role.setParent(r)
121126
err = role.ProcessTemplates(workflowRepo)
122127
if err != nil {
123128
return
124129
}
125130
}
131+
132+
// If any child is not Enabled after template resolution,
133+
// we filter it out of existence
134+
enabledRoles := make([]Role, 0)
135+
for _, role := range r.Roles {
136+
if role.IsEnabled() {
137+
enabledRoles = append(enabledRoles, role)
138+
}
139+
}
140+
r.Roles = enabledRoles
141+
126142
return
127143
}
128144

core/workflow/iteratorrole.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,24 @@ func (i *iteratorRole) ProcessTemplates(workflowRepo *repos.Repo) (err error) {
139139
return
140140
}
141141

142+
// Process templates for child roles
142143
for _, role := range i.Roles {
143144
err = role.ProcessTemplates(workflowRepo)
144145
if err != nil {
145146
return
146147
}
147148
}
149+
150+
// If any child is not Enabled after template resolution,
151+
// we filter it out of existence
152+
enabledRoles := make([]Role, 0)
153+
for _, role := range i.Roles {
154+
if role.IsEnabled() {
155+
enabledRoles = append(enabledRoles, role)
156+
}
157+
}
158+
i.Roles = enabledRoles
159+
148160
return
149161
}
150162

@@ -220,6 +232,13 @@ func (i *iteratorRole) GetState() task.State {
220232
panic("implement me")
221233
}
222234

235+
func (i *iteratorRole) IsEnabled() bool {
236+
if i == nil || i.template == nil {
237+
return false
238+
}
239+
return i.template.IsEnabled()
240+
}
241+
223242
func (i *iteratorRole) setParent(role Updatable) {
224243
i.template.setParent(role)
225244
for _, v := range i.Roles {

core/workflow/role.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Role interface {
5858
SetRuntimeVar(key string, value string)
5959
SetRuntimeVars(kv map[string]string)
6060
GetHooksForTrigger(trigger string) task.Tasks
61+
IsEnabled() bool
6162
}
6263

6364
type Updatable interface {

core/workflow/rolebase.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package workflow
2626

2727
import (
2828
"fmt"
29+
"strings"
2930

3031
"github.com/AliceO2Group/Control/common/gera"
3132
"github.com/AliceO2Group/Control/common/logger"
@@ -59,6 +60,13 @@ type roleBase struct {
5960
UserVars *gera.StringWrapMap `yaml:"-"`
6061
Locals map[string]string `yaml:"-"` // only used for passing iterator from template to new role
6162
Bind []channel.Inbound `yaml:"bind,omitempty"`
63+
Enabled string `yaml:"enabled,omitempty"`
64+
}
65+
66+
func (r *roleBase) IsEnabled() bool {
67+
// Only valid after ProcessTemplates
68+
trimmed := strings.ToLower(strings.TrimSpace(r.Enabled))
69+
return trimmed == "true" || trimmed == "1"
6270
}
6371

6472
func (r *roleBase) SetRuntimeVar(key string, value string) {
@@ -171,6 +179,7 @@ func (r *roleBase) UnmarshalYAML(unmarshal func(interface{}) error) (err error)
171179
Locals: make(map[string]string),
172180
status: SafeStatus{status:task.INACTIVE},
173181
state: SafeState{state:task.STANDBY},
182+
Enabled: "true",
174183
}
175184
err = unmarshal(&role)
176185
if err == nil {
@@ -208,6 +217,7 @@ func (r *roleBase) copy() copyable {
208217
status: r.status,
209218
state: r.state,
210219
Bind: make([]channel.Inbound, len(r.Bind)),
220+
Enabled: r.Enabled,
211221
}
212222

213223
copied := copy(rCopy.Connect, r.Connect)

core/workflow/taskrole.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package workflow
2626

2727
import (
2828
"errors"
29+
"strings"
2930
texttemplate "text/template"
3031
"time"
3132

@@ -115,9 +116,10 @@ func (t *taskRole) ProcessTemplates(workflowRepo *repos.Repo) (err error) {
115116
template.WrapPointer(&t.Timeout),
116117
template.WrapPointer(&t.Trigger),
117118
},
118-
template.STAGE4: append(
119+
template.STAGE4: append(append(
119120
template.WrapConstraints(t.Constraints),
120121
t.wrapConnectFields()...),
122+
template.WrapPointer(&t.Enabled)),
121123
}
122124

123125
// FIXME: push cached templates here
@@ -140,6 +142,8 @@ func (t *taskRole) ProcessTemplates(workflowRepo *repos.Repo) (err error) {
140142
t.Vars.Set(k, v)
141143
}
142144

145+
t.Enabled = strings.TrimSpace(t.Enabled)
146+
143147
t.resolveTaskClassIdentifier(workflowRepo)
144148

145149
return

0 commit comments

Comments
 (0)