HAOJX

mgr.Add函数用法:在mgr启动的时候会一起把这个对象也启动了

字数统计: 293阅读时长: 1 min
2021/12/28 Share
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")
}

}

在上面中mgr.add会把里面那个类型的东西也启动了 只要他实现了runable这个方法

也就是只要实现了

1
func Start(ctx context.Context) error

即可

一般用在搞一个内置的dashboard

CATALOG