|
| 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 | + "github.com/openkruise/kruise/pkg/features" |
| 14 | + "github.com/openkruise/kruise/pkg/util" |
| 15 | + utilfeature "github.com/openkruise/kruise/pkg/util/feature" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + AnnotationUsingEnhancedLiveness = "apps.kruise.io/using-enhanced-liveness" |
| 20 | + AnnotationNativeLivenessContext = "apps.kruise.io/livenessprobe-context" |
| 21 | +) |
| 22 | + |
| 23 | +type containerLivenessProbe struct { |
| 24 | + Name string `json:"name"` |
| 25 | + LivenessProbe v1.Probe `json:"livenessProbe"` |
| 26 | +} |
| 27 | + |
| 28 | +func (h *PodCreateHandler) enhancedLivenessProbeWhenPodCreate(ctx context.Context, req admission.Request, pod *v1.Pod) (skip bool, err error) { |
| 29 | + |
| 30 | + if len(req.AdmissionRequest.SubResource) > 0 || |
| 31 | + req.AdmissionRequest.Operation != admissionv1.Create || |
| 32 | + req.AdmissionRequest.Resource.Resource != "pods" { |
| 33 | + return true, nil |
| 34 | + } |
| 35 | + |
| 36 | + if !util.IsPodOwnedByKruise(pod) && !utilfeature.DefaultFeatureGate.Enabled(features.EnhancedLivenessProbe) { |
| 37 | + return true, nil |
| 38 | + } |
| 39 | + |
| 40 | + if !usingEnhancedLivenessProbe(pod) { |
| 41 | + return true, nil |
| 42 | + } |
| 43 | + |
| 44 | + context, err := removeAndBackUpPodContainerLivenessProbe(pod) |
| 45 | + if err != nil { |
| 46 | + klog.Errorf("Remove pod (%v/%v) container livenessProbe config and backup error: %v", pod.Namespace, pod.Name, err) |
| 47 | + return false, err |
| 48 | + } |
| 49 | + klog.V(3).Infof("Mutating add pod(%s/%s) annotation[%s]=%s", pod.Namespace, pod.Name, AnnotationNativeLivenessContext, context) |
| 50 | + return false, nil |
| 51 | +} |
| 52 | + |
| 53 | +func removeAndBackUpPodContainerLivenessProbe(pod *v1.Pod) (string, error) { |
| 54 | + if len(pod.Spec.Containers) == 0 { |
| 55 | + return "", nil |
| 56 | + } |
| 57 | + |
| 58 | + containersLivenessProbe := []containerLivenessProbe{} |
| 59 | + for index, _ := range pod.Spec.Containers { |
| 60 | + getContainer := &pod.Spec.Containers[index] |
| 61 | + if getContainer.LivenessProbe == nil { |
| 62 | + continue |
| 63 | + } |
| 64 | + containersLivenessProbe = append(containersLivenessProbe, containerLivenessProbe{ |
| 65 | + Name: getContainer.Name, |
| 66 | + LivenessProbe: *getContainer.LivenessProbe, |
| 67 | + }) |
| 68 | + getContainer.LivenessProbe = nil |
| 69 | + } |
| 70 | + |
| 71 | + if len(containersLivenessProbe) == 0 { |
| 72 | + return "", nil |
| 73 | + } |
| 74 | + containersLivenessProbeRaw, err := json.Marshal(containersLivenessProbe) |
| 75 | + if err != nil { |
| 76 | + klog.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v", |
| 77 | + containersLivenessProbe, pod.Namespace, pod.Name, err) |
| 78 | + return "", fmt.Errorf("Failed to json marshal %v for pod: %v/%v, err: %v", |
| 79 | + containersLivenessProbe, pod.Namespace, pod.Name, err) |
| 80 | + } |
| 81 | + if pod.Annotations == nil { |
| 82 | + pod.Annotations = map[string]string{} |
| 83 | + } |
| 84 | + pod.Annotations[AnnotationNativeLivenessContext] = string(containersLivenessProbeRaw) |
| 85 | + return pod.Annotations[AnnotationNativeLivenessContext], nil |
| 86 | +} |
| 87 | + |
| 88 | +func usingEnhancedLivenessProbe(pod *v1.Pod) bool { |
| 89 | + return pod.Annotations[AnnotationUsingEnhancedLiveness] == "true" |
| 90 | +} |
0 commit comments