Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_enhanced_livenessProbe_webhook #1467

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)
18 changes: 13 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
EnhancedLivenessProbeGate 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},

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

func init() {
Expand Down Expand Up @@ -167,6 +173,7 @@ func SetDefaultFeatureGates() {
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", PodUnavailableBudgetUpdateGate))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", WorkloadSpread))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", SidecarSetPatchPodMetadataDefaultsAllowed))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", EnhancedLivenessProbeGate))
}
if !utilfeature.DefaultFeatureGate.Enabled(KruiseDaemon) {
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", PreDownloadImageForInPlaceUpdate))
Expand All @@ -176,6 +183,7 @@ func SetDefaultFeatureGates() {
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", PodProbeMarkerGate))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", SidecarTerminator))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", ImagePullJobGate))
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=false", EnhancedLivenessProbeGate))
}
if utilfeature.DefaultFeatureGate.Enabled(PreDownloadImageForInPlaceUpdate) || utilfeature.DefaultFeatureGate.Enabled(PreDownloadImageForDaemonSetUpdate) {
_ = utilfeature.DefaultMutableFeatureGate.Set(fmt.Sprintf("%s=true", ImagePullJobGate))
Expand Down
88 changes: 88 additions & 0 deletions pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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/util"
)

type containerLivenessProbe struct {
furykerry marked this conversation as resolved.
Show resolved Hide resolved
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 ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if users modify LivenessProbe when updating pods?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1 version, the native version just supports the creation process.

req.AdmissionRequest.Resource.Resource != "pods" {
return true, nil
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L27-L28

Added lines #L27 - L28 were not covered by tests

if !util.IsPodOwnedByKruise(pod) {
return true, nil
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L31-L32

Added lines #L31 - L32 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 42 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L40 - L42 were not covered by tests
furykerry marked this conversation as resolved.
Show resolved Hide resolved
if context == "" {
return true, nil
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L44 - L45 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) {
furykerry marked this conversation as resolved.
Show resolved Hide resolved
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 76 in pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/webhook/pod/mutating/enhancedlivenessprobe_handler.go#L72-L76

Added lines #L72 - L76 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
Loading