63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package tests
|
||
|
||
import (
|
||
"context"
|
||
"os"
|
||
|
||
appsv1 "k8s.io/api/apps/v1"
|
||
corev1 "k8s.io/api/core/v1"
|
||
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/handler"
|
||
"sigs.k8s.io/controller-runtime/pkg/source"
|
||
)
|
||
|
||
type podReconcile struct {
|
||
client.Client
|
||
}
|
||
|
||
func (r *podReconcile) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||
rs := &appsv1.ReplicaSet{}
|
||
err := r.Get(ctx, req.NamespacedName, rs)
|
||
if err != nil {
|
||
return ctrl.Result{}, err
|
||
}
|
||
// DoSometing
|
||
return ctrl.Result{}, nil
|
||
}
|
||
|
||
func (r *podReconcile) InjectClient(c client.Client) error {
|
||
r.Client = c
|
||
return nil
|
||
}
|
||
|
||
func main() {
|
||
log := ctrl.Log.WithName("entrypoint")
|
||
|
||
// 初始化 manager,同时生成一个默认配置的 Cache
|
||
mgr, err := ctrl.NewManager(config.GetConfigOrDie(), ctrl.Options{})
|
||
if err != nil {
|
||
log.Error(err, "unable to set up overall controller manager")
|
||
}
|
||
// 注册 scheme
|
||
if err = corev1.AddToScheme(mgr.GetScheme()); err != nil {
|
||
log.Error(err, "unable to add scheme")
|
||
}
|
||
|
||
// 创建新的 controller
|
||
err = ctrl.NewControllerManagedBy(mgr).For(&appsv1.ReplicaSet{}).
|
||
// 当 Pod 变更时,也触发 ReplicaSet
|
||
Watches(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{}).
|
||
Complete(&podReconcile{})
|
||
if err != nil {
|
||
log.Error(err, "unable to add controller")
|
||
}
|
||
|
||
// 启动 manager
|
||
if err = mgr.Start(ctrl.SetupSignalHandler()); err != nil {
|
||
log.Error(err, "unable to run manager")
|
||
os.Exit(1)
|
||
}
|
||
}
|