1
0

add operator

This commit is contained in:
2022-08-15 13:43:04 +08:00
parent d2aee92357
commit 9ba33d64d3
15 changed files with 1400 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
package controllers
import (
"context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// ServiceReconciler reconciles Service
type ServiceReconciler struct {
client client.Client
}
//+kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;
func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := ctrl.LoggerFrom(ctx)
var svc corev1.Service
err := r.client.Get(ctx, req.NamespacedName, &svc)
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
l.Info("got svc")
return ctrl.Result{}, nil
}
func (r *ServiceReconciler) InjectClient(c client.Client) error {
r.client = c
return nil
}
func NewService(mgr ctrl.Manager) error {
r := &ServiceReconciler{}
c, err := controller.NewUnmanaged("Services", mgr, controller.Options{
Reconciler: r, RecoverPanic: true,
})
if err != nil {
return err
}
if err = c.Watch(&source.Kind{Type: &corev1.Service{}},
&handler.EnqueueRequestForObject{}); err != nil {
return err
}
return mgr.Add(c)
}