This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
controller.go
111 lines (92 loc) · 2.21 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package features
import (
"context"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/stack"
"github.com/rancher/rio/types"
"github.com/rancher/wrangler/pkg/merr"
)
type ControllerRegister func(ctx context.Context, rContext *types.Context) error
type FeatureSpec struct {
Enabled bool `json:"enabled,omitempty"`
Description string `json:"description,omitempty"`
Questions []riov1.Question `json:"questions,omitempty"`
Answers map[string]string `json:"answers,omitempty"`
Requires []string `json:"features,omitempty"`
}
type FeatureController struct {
FeatureName string
System bool
FeatureSpec FeatureSpec
Controllers []ControllerRegister
OnStop func() error
OnStart func() error
SystemStacks []*stack.SystemStack
FixedAnswers map[string]string
registered bool
}
func (f *FeatureController) Register() error {
if f.registered {
return nil
}
for _, ss := range f.SystemStacks {
qs, err := ss.Questions()
if err != nil {
return err
}
f.FeatureSpec.Questions = append(f.FeatureSpec.Questions, qs...)
}
Register(f)
return nil
}
func (f *FeatureController) Name() string {
return f.FeatureName
}
func (f *FeatureController) IsSystem() bool {
return f.System
}
func (f *FeatureController) Spec() FeatureSpec {
return f.FeatureSpec
}
func (f *FeatureController) Stop() error {
if f.OnStop != nil {
return f.OnStop()
}
var errs []error
for _, ss := range f.SystemStacks {
if err := ss.Remove(); err != nil {
errs = append(errs, err)
}
}
return merr.NewErrors(errs...)
}
func (f *FeatureController) Start(ctx context.Context) error {
var errs []error
for _, ss := range f.SystemStacks {
ans := map[string]string{}
for k, v := range f.FeatureSpec.Answers {
ans[k] = v
}
for k, v := range f.FixedAnswers {
ans[k] = v
}
if err := ss.Deploy(ans); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
return merr.NewErrors(errs...)
}
rContext := types.From(ctx)
for _, reg := range f.Controllers {
if err := reg(ctx, rContext); err != nil {
return err
}
}
if f.OnStart != nil {
if err := f.OnStart(); err != nil {
return err
}
}
return nil
}