add operator
This commit is contained in:
56
pkg/controllers/services.go
Normal file
56
pkg/controllers/services.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user