HAOJX

初始化控制器controller-runtime中mgr的例子

字数统计: 246阅读时长: 1 min
2021/10/17 Share

example

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
package k8sconfig

import (
v1 "github.com/haojianxun/dbcore/pkg/apis/dbconfig/v1"
"github.com/haojianxun/dbcore/pkg/controllers"
"github.com/haojianxun/dbcore/pkg/dashboard"
appsv1 "k8s.io/api/apps/v1"
"os"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/source"
)

func InitManager() {
logf.SetLogger(zap.New())
mgr ,err:= manager.New(K8sRestConfig(),manager.Options{
Logger: logf.Log.WithName("dbcore"),
})
if err != nil {
mgr.GetLogger().Error(err,"could not create manager")
os.Exit(1)
}
if err = v1.SchemeBuilder.AddToScheme(mgr.GetScheme());err!=nil {
mgr.GetLogger().Error(err,"unable to schme")
os.Exit(1)
}
dbConfigController:=controllers.NewDbConfigController()
if err = builder.ControllerManagedBy(mgr).
For(&v1.DbConfig{}).
Watches(&source.Kind{Type: &appsv1.Deployment{}},
handler.Funcs{
DeleteFunc: dbConfigController.OnDelete,
UpdateFunc: dbConfigController.OnUpdate},).
Complete(controllers.NewDbConfigController());err!=nil{
mgr.GetLogger().Error(err,"unable to create manager")
os.Exit(1)
}

if err:=mgr.Add(dashboard.NewAdmiUi());err!=nil{
mgr.GetLogger().Error(err,"unable to create dashboard")
os.Exit(1)
}
if err= mgr.Start(signals.SetupSignalHandler());err!= nil {
mgr.GetLogger().Error(err,"unable to start manager")
}

}

和官方包给的例子差不多

CATALOG