Skip to content

Commit

Permalink
add_enhanced_livenessProbe_webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
BH4AWS committed Mar 18, 2024
1 parent c33088b commit e19bee7
Show file tree
Hide file tree
Showing 5 changed files with 614 additions and 8 deletions.
8 changes: 8 additions & 0 deletions apis/apps/v1alpha1/well_know_annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package v1alpha1

const (
// AnnotationUsingEnhancedLiveness indicates that the enhanced liveness probe of pod is enabled.
AnnotationUsingEnhancedLiveness = "apps.kruise.io/using-enhanced-liveness"
// AnnotationUsingEnhancedLiveness indicates the backup probe (json types) of the pod native container livnessprobe configuration.
AnnotationNativeContainerProbeContext = "apps.kruise.io/container-probe-context"
)
16 changes: 11 additions & 5 deletions pkg/features/kruise_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ const (

// DeletionProtectionForCRDCascadingGate enable deletionProtection for crd Cascading
DeletionProtectionForCRDCascadingGate featuregate.Feature = "DeletionProtectionForCRDCascadingGate"

// Enables a enhanced livenessProbe solution
EnhancedLivenessProbe featuregate.Feature = "EnhancedLivenessProbe"
)

var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
Expand All @@ -135,11 +138,14 @@ var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
SidecarTerminator: {Default: false, PreRelease: featuregate.Alpha},
PodProbeMarkerGate: {Default: true, PreRelease: featuregate.Alpha},
PreDownloadImageForDaemonSetUpdate: {Default: false, PreRelease: featuregate.Alpha},
CloneSetEventHandlerOptimization: {Default: false, PreRelease: featuregate.Alpha},
PreparingUpdateAsUpdate: {Default: false, PreRelease: featuregate.Alpha},
ImagePullJobGate: {Default: false, PreRelease: featuregate.Alpha},
ResourceDistributionGate: {Default: false, PreRelease: featuregate.Alpha},
DeletionProtectionForCRDCascadingGate: {Default: false, PreRelease: featuregate.Alpha},

CloneSetEventHandlerOptimization: {Default: false, PreRelease: featuregate.Alpha},
PreparingUpdateAsUpdate: {Default: false, PreRelease: featuregate.Alpha},
ImagePullJobGate: {Default: false, PreRelease: featuregate.Alpha},
ResourceDistributionGate: {Default: false, PreRelease: featuregate.Alpha},
DeletionProtectionForCRDCascadingGate: {Default: false, PreRelease: featuregate.Alpha},

EnhancedLivenessProbe: {Default: false, PreRelease: featuregate.Alpha},
}

func init() {
Expand Down
91 changes: 91 additions & 0 deletions pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package mutating

import (
"context"
"encoding/json"
"fmt"

admissionv1 "k8s.io/api/admission/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
"github.com/openkruise/kruise/pkg/features"
"github.com/openkruise/kruise/pkg/util"
utilfeature "github.com/openkruise/kruise/pkg/util/feature"
)

type containerLivenessProbe struct {
Name string `json:"name"`
LivenessProbe v1.Probe `json:"livenessProbe"`
}

func (h *PodCreateHandler) enhancedLivenessProbeWhenPodCreate(ctx context.Context, req admission.Request, pod *v1.Pod) (skip bool, err error) {

if len(req.AdmissionRequest.SubResource) > 0 ||
req.AdmissionRequest.Operation != admissionv1.Create ||
req.AdmissionRequest.Resource.Resource != "pods" {
return true, nil
}

Check warning on line 30 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L29-L30

Added lines #L29 - L30 were not covered by tests

if !util.IsPodOwnedByKruise(pod) && !utilfeature.DefaultFeatureGate.Enabled(features.EnhancedLivenessProbe) {
return true, nil
}

Check warning on line 34 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L33-L34

Added lines #L33 - L34 were not covered by tests

if !usingEnhancedLivenessProbe(pod) {
return true, nil
}

context, err := removeAndBackUpPodContainerLivenessProbe(pod)
if err != nil {
klog.Errorf("Remove pod (%v/%v) container livenessProbe config and backup error: %v", pod.Namespace, pod.Name, err)
return false, err
}

Check warning on line 44 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L42-L44

Added lines #L42 - L44 were not covered by tests
if context == "" {
klog.Warningf("No found the native container livenessProbe config for pod: %s/%s", pod.Namespace, pod.Name)
return true, nil
}

Check warning on line 48 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L46-L48

Added lines #L46 - L48 were not covered by tests
klog.V(3).Infof("Mutating add pod(%s/%s) annotation[%s]=%s", pod.Namespace, pod.Name, alpha1.AnnotationNativeContainerProbeContext, context)
return false, nil
}

// return two parameters:
// 1. the json string of the pod containers native livenessProbe configurations.
// 2. the error reason of the function.
func removeAndBackUpPodContainerLivenessProbe(pod *v1.Pod) (string, error) {
containersLivenessProbe := []containerLivenessProbe{}
for index := range pod.Spec.Containers {
getContainer := &pod.Spec.Containers[index]
if getContainer.LivenessProbe == nil {
continue
}
containersLivenessProbe = append(containersLivenessProbe, containerLivenessProbe{
Name: getContainer.Name,
LivenessProbe: *getContainer.LivenessProbe,
})
getContainer.LivenessProbe = nil
}

if len(containersLivenessProbe) == 0 {
return "", nil
}
containersLivenessProbeRaw, err := json.Marshal(containersLivenessProbe)
if err != nil {
klog.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v",
containersLivenessProbe, pod.Namespace, pod.Name, err)
return "", fmt.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v",
containersLivenessProbe, pod.Namespace, pod.Name, err)
}

Check warning on line 79 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L75-L79

Added lines #L75 - L79 were not covered by tests
if pod.Annotations == nil {
pod.Annotations = map[string]string{}
}
pod.Annotations[alpha1.AnnotationNativeContainerProbeContext] = string(containersLivenessProbeRaw)
return pod.Annotations[alpha1.AnnotationNativeContainerProbeContext], nil
}

// return one parameter:
// 1. the native container livenessprobe is enabled when the alpha1.AnnotationUsingEnhancedLiveness is true.
func usingEnhancedLivenessProbe(pod *v1.Pod) bool {
return pod.Annotations[alpha1.AnnotationUsingEnhancedLiveness] == "true"
}
Loading

0 comments on commit e19bee7

Please sign in to comment.