-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (90 loc) · 3.2 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
package main
import (
"flag"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/klog"
clientset "github.com/Sion-L/controller-demo/pkg/client/clientset/versioned"
informers "github.com/Sion-L/controller-demo/pkg/client/informers/externalversions"
)
var (
onlyOneSignalHandler = make(chan struct{})
shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
)
// SetupSignalHandler 注册 SIGTERM 和 SIGINT 信号
// 返回一个 stop channel,该通道在捕获到第一个信号时被关闭
// 如果捕捉到第二个信号,程序将直接退出
func setupSignalHandler() (stopCh <-chan struct{}) {
// 当调用两次的时候 panics
close(onlyOneSignalHandler)
stop := make(chan struct{})
c := make(chan os.Signal, 2)
// Notify 函数让 signal 包将输入信号转发到 c
// 如果没有列出要传递的信号,会将所有输入信号传递到 c;否则只传递列出的输入信号
// 参考文档:https://cloud.tencent.com/developer/article/1645996
signal.Notify(c, shutdownSignals...)
go func() {
<-c
close(stop)
<-c
os.Exit(1) // 第二个信号,直接退出
}()
return stop
}
func initClient() (*kubernetes.Clientset, *rest.Config, error) {
var err error
var config *rest.Config
// inCluster(Pod)、KubeConfig(kubectl)
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(可选) kubeconfig 文件的绝对路径")
} else {
kubeconfig = flag.String("kubeconfig", "", "kubeconfig 文件的绝对路径")
}
flag.Parse()
// 首先使用 inCluster 模式(需要去配置对应的 RBAC 权限,默认的sa是default->是没有获取deployments的List权限)
if config, err = rest.InClusterConfig(); err != nil {
// 使用 KubeConfig 文件创建集群配置 Config 对象
if config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig); err != nil {
panic(err.Error())
}
}
// 已经获得了 rest.Config 对象
// 创建 Clientset 对象
kubeclient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, config, err
}
return kubeclient, config, nil
}
func main() {
flag.Parse()
//设置一个信号处理,应用于优雅关闭
stopCh := setupSignalHandler()
_, cfg, err := initClient()
if err != nil {
klog.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
// 实例化一个 CronTab 的 ClientSet
crontabClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building crontab clientset: %s", err.Error())
}
// informerFactory 工厂类, 这里注入我们通过代码生成的 client
// clent 主要用于和 API Server 进行通信,实现 ListAndWatch
crontabInformerFactory := informers.NewSharedInformerFactory(crontabClient, time.Second*30)
// 实例化自定义控制器
controller := NewController(crontabInformerFactory.Stable().V1beta1().CronTabs())
// 启动 informer,开始List & Watch
go crontabInformerFactory.Start(stopCh)
if err = controller.Run(2, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
}