Skip to content

Commit e19bee7

Browse files
committed
add_enhanced_livenessProbe_webhook
1 parent c33088b commit e19bee7

File tree

5 files changed

+614
-8
lines changed

5 files changed

+614
-8
lines changed

Diff for: apis/apps/v1alpha1/well_know_annotations.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package v1alpha1
2+
3+
const (
4+
// AnnotationUsingEnhancedLiveness indicates that the enhanced liveness probe of pod is enabled.
5+
AnnotationUsingEnhancedLiveness = "apps.kruise.io/using-enhanced-liveness"
6+
// AnnotationUsingEnhancedLiveness indicates the backup probe (json types) of the pod native container livnessprobe configuration.
7+
AnnotationNativeContainerProbeContext = "apps.kruise.io/container-probe-context"
8+
)

Diff for: pkg/features/kruise_features.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ const (
113113

114114
// DeletionProtectionForCRDCascadingGate enable deletionProtection for crd Cascading
115115
DeletionProtectionForCRDCascadingGate featuregate.Feature = "DeletionProtectionForCRDCascadingGate"
116+
117+
// Enables a enhanced livenessProbe solution
118+
EnhancedLivenessProbe featuregate.Feature = "EnhancedLivenessProbe"
116119
)
117120

118121
var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
@@ -135,11 +138,14 @@ var defaultFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
135138
SidecarTerminator: {Default: false, PreRelease: featuregate.Alpha},
136139
PodProbeMarkerGate: {Default: true, PreRelease: featuregate.Alpha},
137140
PreDownloadImageForDaemonSetUpdate: {Default: false, PreRelease: featuregate.Alpha},
138-
CloneSetEventHandlerOptimization: {Default: false, PreRelease: featuregate.Alpha},
139-
PreparingUpdateAsUpdate: {Default: false, PreRelease: featuregate.Alpha},
140-
ImagePullJobGate: {Default: false, PreRelease: featuregate.Alpha},
141-
ResourceDistributionGate: {Default: false, PreRelease: featuregate.Alpha},
142-
DeletionProtectionForCRDCascadingGate: {Default: false, PreRelease: featuregate.Alpha},
141+
142+
CloneSetEventHandlerOptimization: {Default: false, PreRelease: featuregate.Alpha},
143+
PreparingUpdateAsUpdate: {Default: false, PreRelease: featuregate.Alpha},
144+
ImagePullJobGate: {Default: false, PreRelease: featuregate.Alpha},
145+
ResourceDistributionGate: {Default: false, PreRelease: featuregate.Alpha},
146+
DeletionProtectionForCRDCascadingGate: {Default: false, PreRelease: featuregate.Alpha},
147+
148+
EnhancedLivenessProbe: {Default: false, PreRelease: featuregate.Alpha},
143149
}
144150

145151
func init() {
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package mutating
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
admissionv1 "k8s.io/api/admission/v1"
9+
v1 "k8s.io/api/core/v1"
10+
"k8s.io/klog/v2"
11+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
12+
13+
alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
14+
"github.com/openkruise/kruise/pkg/features"
15+
"github.com/openkruise/kruise/pkg/util"
16+
utilfeature "github.com/openkruise/kruise/pkg/util/feature"
17+
)
18+
19+
type containerLivenessProbe struct {
20+
Name string `json:"name"`
21+
LivenessProbe v1.Probe `json:"livenessProbe"`
22+
}
23+
24+
func (h *PodCreateHandler) enhancedLivenessProbeWhenPodCreate(ctx context.Context, req admission.Request, pod *v1.Pod) (skip bool, err error) {
25+
26+
if len(req.AdmissionRequest.SubResource) > 0 ||
27+
req.AdmissionRequest.Operation != admissionv1.Create ||
28+
req.AdmissionRequest.Resource.Resource != "pods" {
29+
return true, nil
30+
}
31+
32+
if !util.IsPodOwnedByKruise(pod) && !utilfeature.DefaultFeatureGate.Enabled(features.EnhancedLivenessProbe) {
33+
return true, nil
34+
}
35+
36+
if !usingEnhancedLivenessProbe(pod) {
37+
return true, nil
38+
}
39+
40+
context, err := removeAndBackUpPodContainerLivenessProbe(pod)
41+
if err != nil {
42+
klog.Errorf("Remove pod (%v/%v) container livenessProbe config and backup error: %v", pod.Namespace, pod.Name, err)
43+
return false, err
44+
}
45+
if context == "" {
46+
klog.Warningf("No found the native container livenessProbe config for pod: %s/%s", pod.Namespace, pod.Name)
47+
return true, nil
48+
}
49+
klog.V(3).Infof("Mutating add pod(%s/%s) annotation[%s]=%s", pod.Namespace, pod.Name, alpha1.AnnotationNativeContainerProbeContext, context)
50+
return false, nil
51+
}
52+
53+
// return two parameters:
54+
// 1. the json string of the pod containers native livenessProbe configurations.
55+
// 2. the error reason of the function.
56+
func removeAndBackUpPodContainerLivenessProbe(pod *v1.Pod) (string, error) {
57+
containersLivenessProbe := []containerLivenessProbe{}
58+
for index := range pod.Spec.Containers {
59+
getContainer := &pod.Spec.Containers[index]
60+
if getContainer.LivenessProbe == nil {
61+
continue
62+
}
63+
containersLivenessProbe = append(containersLivenessProbe, containerLivenessProbe{
64+
Name: getContainer.Name,
65+
LivenessProbe: *getContainer.LivenessProbe,
66+
})
67+
getContainer.LivenessProbe = nil
68+
}
69+
70+
if len(containersLivenessProbe) == 0 {
71+
return "", nil
72+
}
73+
containersLivenessProbeRaw, err := json.Marshal(containersLivenessProbe)
74+
if err != nil {
75+
klog.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v",
76+
containersLivenessProbe, pod.Namespace, pod.Name, err)
77+
return "", fmt.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v",
78+
containersLivenessProbe, pod.Namespace, pod.Name, err)
79+
}
80+
if pod.Annotations == nil {
81+
pod.Annotations = map[string]string{}
82+
}
83+
pod.Annotations[alpha1.AnnotationNativeContainerProbeContext] = string(containersLivenessProbeRaw)
84+
return pod.Annotations[alpha1.AnnotationNativeContainerProbeContext], nil
85+
}
86+
87+
// return one parameter:
88+
// 1. the native container livenessprobe is enabled when the alpha1.AnnotationUsingEnhancedLiveness is true.
89+
func usingEnhancedLivenessProbe(pod *v1.Pod) bool {
90+
return pod.Annotations[alpha1.AnnotationUsingEnhancedLiveness] == "true"
91+
}

0 commit comments

Comments
 (0)