-
Notifications
You must be signed in to change notification settings - Fork 777
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
301 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package mutating | ||
|
||
import ( | ||
"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" | ||
|
||
"github.com/openkruise/kruise/pkg/features" | ||
"github.com/openkruise/kruise/pkg/util" | ||
utilfeature "github.com/openkruise/kruise/pkg/util/feature" | ||
) | ||
|
||
const ( | ||
AnnotationUsingEnhancedLiveness = "apps.kruise.io/using-enhanced-liveness" | ||
AnnotationNativeLivenessContext = "apps.kruise.io/livenessprobe-context" | ||
) | ||
|
||
type containerLivenessProbe struct { | ||
Name string `json:"name"` | ||
LivenessProbe v1.Probe `json:"livenessProbe"` | ||
} | ||
|
||
func (h *PodCreateHandler) enhancedLivenessProbeWhenPodCreate(req admission.Request, pod *v1.Pod) (skip bool, err error) { | ||
|
||
if len(req.AdmissionRequest.SubResource) > 0 || | ||
req.AdmissionRequest.Operation != admissionv1.Create || | ||
req.AdmissionRequest.Resource.Resource != "pods" { | ||
return true, nil | ||
} | ||
|
||
if !util.IsPodOwnedByKruise(pod) && !utilfeature.DefaultFeatureGate.Enabled(features.EnhancedLivenessProbe) { | ||
return true, nil | ||
} | ||
|
||
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 | ||
} | ||
klog.V(3).Infof("Mutating add pod(%s/%s) annotation[%s]=%s", pod.Namespace, pod.Name, AnnotationNativeLivenessContext, context) | ||
return false, nil | ||
} | ||
|
||
func removeAndBackUpPodContainerLivenessProbe(pod *v1.Pod) (string, error) { | ||
if len(pod.Spec.Containers) == 0 { | ||
return "", nil | ||
} | ||
|
||
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) | ||
} | ||
if pod.Annotations == nil { | ||
pod.Annotations = map[string]string{} | ||
} | ||
pod.Annotations[AnnotationNativeLivenessContext] = string(containersLivenessProbeRaw) | ||
return pod.Annotations[AnnotationNativeLivenessContext], nil | ||
} | ||
|
||
func usingEnhancedLivenessProbe(pod *v1.Pod) bool { | ||
return pod.Annotations[AnnotationUsingEnhancedLiveness] == "true" | ||
} |
188 changes: 188 additions & 0 deletions
188
pkg/webhook/pod/mutating/enhancedlivenessprobe_handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package mutating | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
func TestRemoveAndBackUpPodContainerLivenessProbe(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
pod *corev1.Pod | ||
expectResult string | ||
}{ | ||
{ | ||
name: "livenessProbe configuration for standard container", | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod1", | ||
Namespace: "namespace1", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "c1", | ||
LivenessProbe: &corev1.Probe{ | ||
FailureThreshold: 3, | ||
InitialDelaySeconds: 3000, | ||
PeriodSeconds: 100, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 5, | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "/health", | ||
Port: intstr.IntOrString{ | ||
Type: intstr.Int, | ||
IntVal: 7001, | ||
}, | ||
Scheme: corev1.URISchemeHTTP, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectResult: `[{"name":"c1","livenessProbe":{"httpGet":{"path":"/health","port":7001,"scheme":"HTTP"},"initialDelaySeconds":3000,"timeoutSeconds":5,"periodSeconds":100,"successThreshold":1,"failureThreshold":3}}]`, | ||
}, | ||
{ | ||
name: "livenessProbe configuration for multi-standard containers", | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod2", | ||
Namespace: "sp1", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "c1", | ||
LivenessProbe: &corev1.Probe{ | ||
FailureThreshold: 3, | ||
InitialDelaySeconds: 3000, | ||
PeriodSeconds: 100, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 5, | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "/health", | ||
Port: intstr.IntOrString{ | ||
Type: intstr.Int, | ||
IntVal: 7001, | ||
}, | ||
Scheme: corev1.URISchemeHTTP, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "c2", | ||
LivenessProbe: &corev1.Probe{ | ||
FailureThreshold: 3, | ||
InitialDelaySeconds: 3000, | ||
PeriodSeconds: 100, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 5, | ||
ProbeHandler: corev1.ProbeHandler{ | ||
Exec: &corev1.ExecAction{ | ||
Command: []string{ | ||
"/home/admin/liveness.sh", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectResult: `[{"name":"c1","livenessProbe":{"httpGet":{"path":"/health","port":7001,"scheme":"HTTP"},"initialDelaySeconds":3000,"timeoutSeconds":5,"periodSeconds":100,"successThreshold":1,"failureThreshold":3}},{"name":"c2","livenessProbe":{"exec":{"command":["/home/admin/liveness.sh"]},"initialDelaySeconds":3000,"timeoutSeconds":5,"periodSeconds":100,"successThreshold":1,"failureThreshold":3}}]`, | ||
}, | ||
{ | ||
name: "different livenssProbe configuration for multi-standard containers", | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod3", | ||
Namespace: "sp1", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "c1", | ||
LivenessProbe: &corev1.Probe{ | ||
FailureThreshold: 3, | ||
InitialDelaySeconds: 3000, | ||
PeriodSeconds: 100, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 5, | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "/health", | ||
Port: intstr.IntOrString{ | ||
Type: intstr.Int, | ||
IntVal: 7001, | ||
}, | ||
Scheme: corev1.URISchemeHTTP, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "c2", | ||
LivenessProbe: &corev1.Probe{ | ||
FailureThreshold: 3, | ||
InitialDelaySeconds: 3000, | ||
PeriodSeconds: 100, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 5, | ||
ProbeHandler: corev1.ProbeHandler{ | ||
Exec: &corev1.ExecAction{ | ||
Command: []string{ | ||
"/home/admin/liveness.sh", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "c3", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectResult: `[{"name":"c1","livenessProbe":{"httpGet":{"path":"/health","port":7001,"scheme":"HTTP"},"initialDelaySeconds":3000,"timeoutSeconds":5,"periodSeconds":100,"successThreshold":1,"failureThreshold":3}},{"name":"c2","livenessProbe":{"exec":{"command":["/home/admin/liveness.sh"]},"initialDelaySeconds":3000,"timeoutSeconds":5,"periodSeconds":100,"successThreshold":1,"failureThreshold":3}}]`, | ||
}, | ||
{ | ||
name: "no livenessProbe configuration for standard container", | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod3", | ||
Namespace: "sp1", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "c1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
get, err := removeAndBackUpPodContainerLivenessProbe(tc.pod) | ||
if err != nil { | ||
t.Errorf("Test case: %v failed, err: %v", tc.name, err) | ||
} | ||
if !reflect.DeepEqual(get, tc.expectResult) { | ||
t.Errorf("Test case: %v failed, expect: %v, but: %v", | ||
tc.name, tc.expectResult, get) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters