-
Notifications
You must be signed in to change notification settings - Fork 47
/
setup.go
134 lines (110 loc) · 4.24 KB
/
setup.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package controllers
import (
"context"
"fmt"
"path/filepath"
"github.com/go-logr/logr"
v1 "github.com/openshift/api/config/v1"
"kubevirt.io/ssp-operator/internal/common"
crd_watch "kubevirt.io/ssp-operator/internal/crd-watch"
"kubevirt.io/ssp-operator/internal/operands"
common_instancetypes "kubevirt.io/ssp-operator/internal/operands/common-instancetypes"
common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates"
data_sources "kubevirt.io/ssp-operator/internal/operands/data-sources"
"kubevirt.io/ssp-operator/internal/operands/metrics"
node_labeller "kubevirt.io/ssp-operator/internal/operands/node-labeller"
template_validator "kubevirt.io/ssp-operator/internal/operands/template-validator"
template_bundle "kubevirt.io/ssp-operator/internal/template-bundle"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
// Need to watch CRDs
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
func CreateAndStartReconciler(ctx context.Context, mgr controllerruntime.Manager) error {
mgrCtx, cancel := context.WithCancel(ctx)
defer cancel()
mgrCtx = logr.NewContext(mgrCtx, mgr.GetLogger())
if err := setupManager(mgrCtx, cancel, mgr); err != nil {
return err
}
mgr.GetLogger().Info("starting manager")
if err := mgr.Start(mgrCtx); err != nil {
mgr.GetLogger().Error(err, "problem running manager")
return err
}
return nil
}
func setupManager(ctx context.Context, cancel context.CancelFunc, mgr controllerruntime.Manager) error {
templatesFile := filepath.Join(templateBundleDir, "common-templates-"+common_templates.Version+".yaml")
templatesBundle, err := template_bundle.ReadBundle(templatesFile)
if err != nil {
return fmt.Errorf("failed to read template bundle: %w", err)
}
instancetypePath := common_instancetypes.BundleDir + common_instancetypes.ClusterInstancetypesBundlePrefix + ".yaml"
preferencePath := common_instancetypes.BundleDir + common_instancetypes.ClusterPreferencesBundlePrefix + ".yaml"
common_instancetypes_operand, err := common_instancetypes.New(instancetypePath, preferencePath)
if err != nil {
return err
}
sspOperands := []operands.Operand{
common_instancetypes_operand,
data_sources.New(templatesBundle.DataSources),
}
runningOnOpenShift, err := common.RunningOnOpenshift(ctx, mgr.GetAPIReader())
if err != nil {
return err
}
if runningOnOpenShift {
sspOperands = append(sspOperands,
metrics.New(),
template_validator.New(),
common_templates.New(templatesBundle.Templates),
node_labeller.New(),
)
}
var requiredCrds []string
for i := range sspOperands {
requiredCrds = append(requiredCrds, sspOperands[i].RequiredCrds()...)
}
crdWatch := crd_watch.New(requiredCrds...)
// Cleanly stops the manager and exit. The pod will be restarted.
crdWatch.AllCrdsAddedHandler = cancel
crdWatch.SomeCrdRemovedHandler = cancel
if err = crdWatch.Init(ctx, mgr.GetAPIReader()); err != nil {
return err
}
if missingCrds := crdWatch.MissingCrds(); len(missingCrds) > 0 {
mgr.GetLogger().Error(nil, "Some required crds are missing. The operator will not create any new resources.",
"missingCrds", missingCrds,
)
}
infrastructureTopology := v1.HighlyAvailableTopologyMode
if runningOnOpenShift {
infrastructureTopology, err = common.GetInfrastructureTopology(ctx, mgr.GetAPIReader())
if err != nil {
return err
}
}
if err = mgr.Add(crdWatch); err != nil {
return err
}
serviceController, err := CreateServiceController(ctx, mgr)
if err != nil {
return fmt.Errorf("failed to create service controller: %w", err)
}
if err = mgr.Add(getRunnable(mgr, serviceController)); err != nil {
return fmt.Errorf("error adding service controller: %w", err)
}
reconciler := NewSspReconciler(mgr.GetClient(), mgr.GetAPIReader(), infrastructureTopology, sspOperands, crdWatch)
return reconciler.setupController(mgr)
}
func getRunnable(mgr controllerruntime.Manager, serviceController *serviceReconciler) manager.Runnable {
return manager.RunnableFunc(func(ctx context.Context) error {
err := serviceController.Start(ctx, mgr)
if err != nil {
return fmt.Errorf("error starting serviceController: %w", err)
}
mgr.GetLogger().Info("Services Controller started")
return nil
})
}