Skip to content

Commit c03fc48

Browse files
committed
add_enhanced_livenessProbe_webhook
Signed-off-by: jicheng.sk <[email protected]>
1 parent c33088b commit c03fc48

File tree

5 files changed

+611
-8
lines changed

5 files changed

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

0 commit comments

Comments
 (0)