1
0

update demo

Signed-off-by: dragonflylee <dragonflylee@outlook.com>
This commit is contained in:
2023-11-07 10:40:16 +08:00
parent b2972195cb
commit 696f6ba425
12 changed files with 231 additions and 10 deletions

62
tests/ctrl/main.go Normal file
View File

@@ -0,0 +1,62 @@
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)
}
}