-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
342 lines (302 loc) · 13 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2021 IBM Corporation
//
// 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.
/*
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 main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"github.com/operator-framework/operator-lib/leader"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
servingv1alpha1 "github.com/kserve/modelmesh-serving/apis/serving/v1alpha1"
"github.com/kserve/modelmesh-serving/controllers"
"github.com/kserve/modelmesh-serving/controllers/modelmesh"
"github.com/kserve/modelmesh-serving/pkg/mmesh"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
kfsv1alpha1 "github.com/kserve/modelmesh-serving/apis/kfserving/v1alpha1"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
ControllerNamespace string
)
const (
ControllerNamespaceEnvVar = "NAMESPACE"
DefaultControllerNamespace = "model-serving"
KubeNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
ControllerPodNameEnvVar = "POD_NAME"
ControllerDeploymentNameEnvVar = "CONTROLLER_DEPLOYMENT"
DefaultControllerName = "modelmesh-controller"
UserConfigMapName = "model-serving-config"
DevModeLoggingEnvVar = "DEV_MODE_LOGGING"
serviceMonitorCRDName = "servicemonitors.monitoring.coreos.com"
LeaderLockName = "modelmesh-controller-leader-lock"
LeaderForLifeLockName = "modelmesh-controller-leader-for-life-lock"
EnableTrainedModelEnvVar = "ENABLE_KSTM_WATCH"
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
err := servingv1alpha1.AddToScheme(scheme)
if err != nil {
log.Fatalf("cannot add model serving v1 scheme, %v", err)
}
_ = batchv1.AddToScheme(scheme)
_ = servingv1alpha1.AddToScheme(scheme)
_ = kfsv1alpha1.AddToScheme(scheme)
_ = monitoringv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
_, devLoggingSetting := os.LookupEnv(DevModeLoggingEnvVar)
ctrl.SetLogger(zap.New(zap.UseDevMode(devLoggingSetting)))
// ----- mmesh related envar setup -----
controllerNamespace := os.Getenv(ControllerNamespaceEnvVar)
if controllerNamespace == "" {
bytes, err := ioutil.ReadFile(KubeNamespaceFile)
if err != nil {
//TODO check kube context and retrieve namespace from there
setupLog.Info("Error reading Kube-mounted namespace file, reverting to default namespace",
"file", KubeNamespaceFile, "err", err, "default", DefaultControllerNamespace)
controllerNamespace = DefaultControllerNamespace
} else {
controllerNamespace = string(bytes)
}
}
ControllerNamespace = controllerNamespace
controllerDeploymentName := os.Getenv(ControllerDeploymentNameEnvVar)
if controllerDeploymentName == "" {
podName := os.Getenv(ControllerPodNameEnvVar)
if podName != "" {
if matches := regexp.MustCompile("(.*)-.*-.*").FindStringSubmatch(podName); len(matches) == 2 {
deployment := matches[1]
setupLog.Info("Use controller deployment from POD_NAME", "Deployment", deployment)
controllerDeploymentName = deployment
}
}
if controllerDeploymentName == "" {
setupLog.Info("Skip empty Controller deployment from Env Var, use default",
"name", DefaultControllerName)
controllerDeploymentName = DefaultControllerName
}
}
// TODO: use the manager client instead. This will require restructuring the dependency
// relationship with the manager so that this code runs after mgr.Start()
cfg := config.GetConfigOrDie()
cl, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "unable to create an api server client")
os.Exit(1)
}
cp, err := controllers.NewConfigProvider(context.Background(), cl, types.NamespacedName{Name: UserConfigMapName, Namespace: ControllerNamespace})
if err != nil {
setupLog.Error(err, "Error loading user config from configmap", "ConfigMapName", UserConfigMapName)
os.Exit(1)
}
conf := cp.GetConfig()
setupLog.Info("Using adapter", "image", conf.StorageHelperImage.TaggedImage())
setupLog.Info("Using model-mesh", "image", conf.ModelMeshImage.TaggedImage())
setupLog.Info("Using inference service", "name", conf.InferenceServiceName, "port", conf.InferenceServicePort)
// mmesh service kubedns or hostname
mmeshEndpoint := conf.ModelMeshEndpoint
setupLog.Info("MMesh Configuration", "serviceName", conf.InferenceServiceName, "port", conf.InferenceServicePort,
"mmeshEndpoint", mmeshEndpoint)
//TODO: this should be moved out of package globals
modelmesh.StorageSecretName = conf.StorageSecretName
// ----- end of mmesh related envar setup -----
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var leaderElectionType string
var leaseDuration time.Duration
var leaseRenewDeadline time.Duration
var leaseRetryPeriod time.Duration
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&leaderElectionType, "leader-elect-type", "lease",
"Leader election type. Values: ['lease'=leader with lease]; ['life'=leader for life].")
flag.DurationVar(&leaseDuration, "leader-elect-lease-duration-sec", 15*time.Second,
"Duration that non-leader candidates will wait to force acquire leadership.")
flag.DurationVar(&leaseRenewDeadline, "leader-elect-lease-renew-deadline-sec", 10*time.Second,
"Duration the leader will retry refreshing leadership before giving up.")
flag.DurationVar(&leaseRetryPeriod, "leader-elect-retry-period-sec", 2*time.Second,
"Duration the Leader elector clients should wait between tries of actions.")
flag.Parse()
mgrOpts := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Namespace: ControllerNamespace,
Port: 9443,
HealthProbeBindAddress: probeAddr,
}
if enableLeaderElection {
if leaderElectionType == "lease" {
setupLog.Info("using leader-with-lease election method")
mgrOpts.LeaderElectionNamespace = ControllerNamespace
mgrOpts.LeaderElection = enableLeaderElection
mgrOpts.LeaderElectionID = LeaderLockName
mgrOpts.LeaseDuration = &leaseDuration
mgrOpts.RenewDeadline = &leaseRenewDeadline
mgrOpts.RetryPeriod = &leaseRetryPeriod
} else if leaderElectionType == "life" {
setupLog.Info("using leader-for-life election method")
// try to become leader using leader-for-life
err = leader.Become(context.TODO(), LeaderForLifeLockName)
if err != nil {
setupLog.Error(err, "Failed to obtain leader-for-life lock")
os.Exit(1)
}
} else {
err = fmt.Errorf("Invalid value for leader-elect-type: '%s'. Use 'lease' or 'life'", leaderElectionType)
setupLog.Error(err, "Error processing command-line flags.")
os.Exit(1)
}
} else {
setupLog.Info("leader election is disabled")
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), mgrOpts)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
_, err = mmesh.InitGrpcResolver(ControllerNamespace, mgr)
if err != nil {
setupLog.Error(err, "Failed to Initialize Grpc Resolver, exit")
os.Exit(1)
}
mmService := mmesh.NewMMService()
modelEventStream, err := mmesh.NewModelEventStream(ctrl.Log.WithName("ModelMeshEventStream"),
mgr.GetClient(), ControllerNamespace)
if err != nil {
setupLog.Error(err, "Failed to Initialize Model Event Stream, exit")
os.Exit(1)
}
// Check if the ServiceMonitor CRD exists in the cluster
sm := &monitoringv1.ServiceMonitor{}
serviceMonitorCRDExists := true
err = cl.Get(context.Background(), client.ObjectKey{Name: "foo", Namespace: controllerNamespace}, sm)
if meta.IsNoMatchError(err) {
serviceMonitorCRDExists = false
setupLog.Info("Service Monitor CRD is not found in the cluster")
} else if err != nil && !errors.IsNotFound(err) {
serviceMonitorCRDExists = false
setupLog.Error(err, "Unable to access Service Monitor CRD", "CRDName", serviceMonitorCRDName)
}
if err = (&controllers.ServiceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Service"),
Scheme: mgr.GetScheme(),
ControllerDeployment: types.NamespacedName{Namespace: ControllerNamespace, Name: controllerDeploymentName},
ModelMeshService: mmService,
ModelEventStream: modelEventStream,
ConfigProvider: cp,
ConfigMapName: types.NamespacedName{Namespace: ControllerNamespace, Name: UserConfigMapName},
ServiceMonitorCRDExists: serviceMonitorCRDExists,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Service")
os.Exit(1)
}
registryMap := map[string]servingv1alpha1.PredictorRegistry{
controllers.PredictorCRSourceId: servingv1alpha1.PredictorCRRegistry{Client: mgr.GetClient()},
}
// Watch TrainedModels only if env var is explicitly set to "true" or if
// env var is unset/empty and TrainedModel CRD is present and accessible.
enableTMWatch := false
envVarVal, _ := os.LookupEnv(EnableTrainedModelEnvVar)
if envVarVal != "false" {
tm := &kfsv1alpha1.TrainedModel{}
err = cl.Get(context.Background(), client.ObjectKey{Name: "foo", Namespace: ControllerNamespace}, tm)
if err == nil || errors.IsNotFound(err) {
enableTMWatch = true
registryMap[controllers.TrainedModelCRSourceId] = kfsv1alpha1.TrainedModelRegistry{Client: mgr.GetClient()}
setupLog.Info("Reconciliation of TrainedModels is enabled")
} else if envVarVal == "true" {
// If env var is explicitly true, require that TrainedModel CRD is present
setupLog.Error(err, "Unable to access TrainedModel Custom Resource")
os.Exit(1)
} else if meta.IsNoMatchError(err) {
setupLog.Info("TrainedModel CRD not found, will not reconcile")
} else {
setupLog.Error(err, "TrainedModel CRD not accessible, will not reconcile")
}
}
if err = (&controllers.PredictorReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Predictor"),
MMService: mmService,
RegistryLookup: registryMap,
}).SetupWithManager(mgr, modelEventStream, enableTMWatch); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Predictor")
os.Exit(1)
}
if err = (&controllers.ServingRuntimeReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ServingRuntime"),
Scheme: mgr.GetScheme(),
ConfigProvider: cp,
ConfigMapName: types.NamespacedName{Namespace: ControllerNamespace, Name: UserConfigMapName},
DeploymentNamespace: ControllerNamespace,
DeploymentName: controllerDeploymentName,
EnableTrainedModelWatch: enableTMWatch,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ServingRuntime")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
// Add Healthz Endpoint
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
// Add Readyz Endpoint
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}