Skip to content

Commit

Permalink
add enhanced livenessprobe controller
Browse files Browse the repository at this point in the history
Signed-off-by: jicheng.sk <jicheng.sk@alibaba-inc.com>
  • Loading branch information
BH4AWS committed Mar 21, 2024
1 parent d3cae3d commit 05ebc64
Show file tree
Hide file tree
Showing 13 changed files with 3,190 additions and 21 deletions.
9 changes: 6 additions & 3 deletions pkg/controller/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package controller

import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/openkruise/kruise/pkg/controller/advancedcronjob"
"github.com/openkruise/kruise/pkg/controller/broadcastjob"
"github.com/openkruise/kruise/pkg/controller/cloneset"
Expand All @@ -26,6 +30,7 @@ import (
"github.com/openkruise/kruise/pkg/controller/ephemeraljob"
"github.com/openkruise/kruise/pkg/controller/imagelistpulljob"
"github.com/openkruise/kruise/pkg/controller/imagepulljob"
enhancedlivenessprobe2nodeprobe "github.com/openkruise/kruise/pkg/controller/livenessprobemapnodeprobe"
"github.com/openkruise/kruise/pkg/controller/nodeimage"
"github.com/openkruise/kruise/pkg/controller/nodepodprobe"
"github.com/openkruise/kruise/pkg/controller/persistentpodstate"
Expand All @@ -38,9 +43,6 @@ import (
"github.com/openkruise/kruise/pkg/controller/statefulset"
"github.com/openkruise/kruise/pkg/controller/uniteddeployment"
"github.com/openkruise/kruise/pkg/controller/workloadspread"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

var controllerAddFuncs []func(manager.Manager) error
Expand All @@ -67,6 +69,7 @@ func init() {
controllerAddFuncs = append(controllerAddFuncs, podprobemarker.Add)
controllerAddFuncs = append(controllerAddFuncs, nodepodprobe.Add)
controllerAddFuncs = append(controllerAddFuncs, imagelistpulljob.Add)
controllerAddFuncs = append(controllerAddFuncs, enhancedlivenessprobe2nodeprobe.Add)
}

func SetupWithManager(m manager.Manager) error {
Expand Down
139 changes: 139 additions & 0 deletions pkg/controller/livenessprobemapnodeprobe/livenessprobemapnodeprobe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package enhancedlivenessprobe2nodeprobe

import (
"context"
"fmt"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/openkruise/kruise/pkg/util"
utilclient "github.com/openkruise/kruise/pkg/util/client"
)

const (
concurrentReconciles = 10
controllerName = "livenessprobemapnodeprobe-controller"

FinalizerPodEnhancedLivenessProbe = "pod.kruise.io/enhanced-liveness-probe-cleanup"

AddNodeProbeConfigOpType = "addNodeProbe"
DelNodeProbeConfigOpType = "delNodeProbe"
)

func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}

var _ reconcile.Reconciler = &ReconcileEnhancedLivenessProbe2NodeProbe{}

// ReconcileEnhancedLivenessProbe reconciles a Pod object
type ReconcileEnhancedLivenessProbe2NodeProbe struct {
client.Client
recorder record.EventRecorder
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) *ReconcileEnhancedLivenessProbe2NodeProbe {
return &ReconcileEnhancedLivenessProbe2NodeProbe{
Client: utilclient.NewClientFromManager(mgr, controllerName),
recorder: mgr.GetEventRecorderFor(controllerName),
}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r *ReconcileEnhancedLivenessProbe2NodeProbe) error {
// Create a new controller
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r, MaxConcurrentReconciles: concurrentReconciles})
if err != nil {
return err
}

// watch events of pod
if err = c.Watch(&source.Kind{Type: &v1.Pod{}}, &enqueueRequestForPod{reader: mgr.GetClient()}); err != nil {
return err
}
return nil
}

// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=apps.kruise.io,resources=nodepodprobes,verbs=get;list;watch;create;update;patch:delete
func (r *ReconcileEnhancedLivenessProbe2NodeProbe) Reconcile(_ context.Context, request reconcile.Request) (res reconcile.Result, err error) {
start := time.Now()
klog.V(3).Infof("Starting to process Pod %v", request.NamespacedName)
defer func() {
if err != nil {
klog.Warningf("Failed to process Pod %v, elapsedTime %v, error: %v", request.NamespacedName, time.Since(start), err)
} else {
klog.Infof("Finish to process Pod %v, elapsedTime %v", request.NamespacedName, time.Since(start))
}
}()

err = r.syncPodContainersLivenessProbe(request.Namespace, request.Name)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}

func (r *ReconcileEnhancedLivenessProbe2NodeProbe) syncPodContainersLivenessProbe(namespace, name string) error {
getPod := &v1.Pod{}
var err error
if err = r.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, getPod); err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to get pod %s/%s: %v", namespace, name, err)
}

if getPod.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(getPod, FinalizerPodEnhancedLivenessProbe) {
err = util.UpdateFinalizer(r.Client, getPod, util.AddFinalizerOpType, FinalizerPodEnhancedLivenessProbe)
if err != nil {
klog.Errorf("Failed to update pod %s/%s finalizer %v, err: %v", getPod.Namespace, getPod.Name, FinalizerPodEnhancedLivenessProbe, err)
return err
}
}
err = r.addOrRemoveNodePodProbeConfig(getPod, AddNodeProbeConfigOpType)
if err != nil {
klog.Errorf("Failed to add or remove node pod probe config in %s process for pod: %s/%s, err: %v", AddNodeProbeConfigOpType, getPod.Namespace, getPod.Name, err)
return err
}
} else {
// pod in deleting process
err = r.addOrRemoveNodePodProbeConfig(getPod, DelNodeProbeConfigOpType)
if err != nil {
klog.Errorf("Failed to add or remove node pod probe config in %s process for pod: %s/%s, "+
"err: %v", DelNodeProbeConfigOpType, getPod.Namespace, getPod.Name, err)
return err
}
if controllerutil.ContainsFinalizer(getPod, FinalizerPodEnhancedLivenessProbe) {
err = util.UpdateFinalizer(r.Client, getPod, util.RemoveFinalizerOpType, FinalizerPodEnhancedLivenessProbe)
if err != nil {
klog.Errorf("Failed to update pod %s/%s finalizer %v, err: %v", getPod.Namespace, getPod.Name, FinalizerPodEnhancedLivenessProbe, err)
return err
}
}
}
return nil
}

func (r *ReconcileEnhancedLivenessProbe2NodeProbe) addOrRemoveNodePodProbeConfig(pod *v1.Pod, op string) error {
if op == DelNodeProbeConfigOpType {
return r.delNodeProbeConfig(pod)
}
if op == AddNodeProbeConfigOpType {
return r.addNodeProbeConfig(pod)
}
return fmt.Errorf("No found op %s(just support %s and %s)", op, AddNodeProbeConfigOpType, DelNodeProbeConfigOpType)
}
Loading

0 comments on commit 05ebc64

Please sign in to comment.