/
raptor.go
190 lines (159 loc) · 6.4 KB
/
raptor.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Copyright (c) 2022 RaptorML authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package setup
import (
"fmt"
"github.com/raptor-ml/raptor/api"
"github.com/raptor-ml/raptor/internal/accessor"
"github.com/raptor-ml/raptor/internal/engine"
corectrl "github.com/raptor-ml/raptor/internal/engine/controllers"
"github.com/raptor-ml/raptor/internal/historian"
opctrl "github.com/raptor-ml/raptor/internal/operator"
"github.com/raptor-ml/raptor/internal/stats"
"github.com/raptor-ml/raptor/pkg/plugins"
"github.com/raptor-ml/raptor/pkg/runtimemanager"
"github.com/spf13/viper"
"net/http"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
func setupStats(mgr manager.Manager) {
// Setup usage reports
stats.UID = viper.GetString("usage-reporting-uid")
OrFail(mgr.Add(stats.Run(
mgr.GetConfig(),
mgr.GetClient(),
viper.GetBool("usage-reporting"),
ctrl.Log.WithName("stats"),
)), "unable to add stats")
}
func historianClient(mgr manager.Manager) historian.Client {
// Create Notifiers
collectNotifier, err := plugins.NewCollectNotifier(viper.GetString("notifier-provider"), viper.GetViper())
OrFail(err, "failed to create collect notifier")
writeNotifier, err := plugins.NewWriteNotifier(viper.GetString("notifier-provider"), viper.GetViper())
OrFail(err, "failed to create collect notifier")
// Create a Historian Client
hsc := historian.NewClient(historian.ClientConfig{
CollectNotifier: collectNotifier,
WriteNotifier: writeNotifier,
Logger: ctrl.Log.WithName("historian"),
CollectNotificationWorkers: 5,
WriteNotificationWorkers: 5,
})
OrFail(hsc.WithManager(mgr), "failed to create historian client")
return hsc
}
func coreControllers(mgr manager.Manager, eng api.ManagerEngine) {
var err error
// Setup Core Controllers
err = (&corectrl.DataSourceReconciler{
Reader: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EngineManager: eng,
}).SetupWithManager(mgr)
OrFail(err, "unable to create core controller", "controller", "DataSource")
err = (&corectrl.FeatureReconciler{
Reader: mgr.GetClient(),
Scheme: mgr.GetScheme(),
UpdatesAllowed: updatesAllowed,
EngineManager: eng,
}).SetupWithManager(mgr)
OrFail(err, "unable to create core controller", "controller", "Feature")
err = (&corectrl.ModelReconciler{
Reader: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EngineManager: eng,
}).SetupWithManager(mgr)
OrFail(err, "unable to create core controller", "controller", "Model")
}
func operatorControllers(mgr manager.Manager, rm api.RuntimeManager) {
var err error
coreAddr := viper.GetString("accessor-service")
if coreAddr == "" {
ns, err := getInClusterNamespace()
OrFail(err, "unable to get in-cluster namespace. Please set the accessor-service flag")
coreAddr = fmt.Sprintf("raptor-core-service.%s.svc", ns)
}
err = (&opctrl.DataSourceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
CoreAddr: coreAddr,
RuntimeManager: rm,
EventRecorder: mgr.GetEventRecorderFor("DataSource-controller"),
}).SetupWithManager(mgr)
OrFail(err, "unable to create controller", "operator", "DataSource")
err = (&opctrl.ModelReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
EventRecorder: mgr.GetEventRecorderFor("Model-controller"),
}).SetupWithManager(mgr)
OrFail(err, "unable to create controller", "operator", "Model")
err = (&opctrl.FeatureReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
RuntimeManager: rm,
}).SetupWithManager(mgr)
OrFail(err, "unable to create controller", "operator", "FeaturePipeliner")
if !viper.GetBool("no-webhooks") {
opctrl.SetupFeatureWebhook(mgr, updatesAllowed, rm)
}
}
func Core(mgr manager.Manager, certsReady chan struct{}) {
// Setup usage reporting
setupStats(mgr)
// Create a Historian Client
hsc := historianClient(mgr)
// Create the state
state, err := plugins.NewState(viper.GetString("state-provider"), viper.GetViper())
OrFail(err, fmt.Sprintf("failed to create state for provider %s", viper.GetString("state-provider")))
err = mgr.AddHealthzCheck("state", func(req *http.Request) error {
return state.Ping(req.Context())
})
OrFail(err, "unable to add health check for state")
err = mgr.AddReadyzCheck("state", func(req *http.Request) error {
return state.Ping(req.Context())
})
OrFail(err, "unable to add ready check for state")
ns, err := getInClusterNamespace()
OrFail(err, "unable to get in-cluster namespace. Please set the system-namespace flag")
podname := viper.GetString("pod-name")
if podname == "" {
podname = os.Getenv("HOSTNAME")
}
rm, err := runtimemanager.New(mgr, ns, podname)
OrFail(err, "unable to create python runtime manager")
// Create a new Core engine
eng := engine.New(state, hsc, rm, ctrl.Log.WithName("engine"))
// Create a new Accessor
acc := accessor.New(eng, ctrl.Log.WithName("accessor"))
OrFail(mgr.Add(acc.GRPC(viper.GetString("accessor-grpc-address"))), "unable to start gRPC accessor")
OrFail(mgr.Add(acc.GrpcUds()), "unable to start gRPC UDS accessor")
OrFail(
mgr.Add(acc.HTTP(viper.GetString("accessor-http-address"), viper.GetString("accessor-http-prefix"))),
"unable to start HTTP accessor")
// The call to mgr.Start will never return, but the certs won't be ready until the manager starts
// and we can't set up the webhooks without them (the webhook server runnable will try to read the
// certs, and if those certs don't exist, the entire process will exit). So start a goroutine
// which will wait until the certs are ready, and then create the rest of the HNC controllers.
go func() {
// The controllers won't work until the webhooks are operating, and those won't work until the
// certs are all in place.
setupLog.Info("Waiting for certificate generation to complete")
<-certsReady
setupLog.Info("Certs ready")
coreControllers(mgr, eng)
operatorControllers(mgr, rm)
}()
}