|
| 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