Skip to content

Commit

Permalink
Merge pull request #3296 from neteric/add-interpretPodHealth
Browse files Browse the repository at this point in the history
add interpretPodHealth to DefaultHealthInterpreter
  • Loading branch information
karmada-bot authored Mar 21, 2023
2 parents ad4eade + 7dd581f commit a14d64f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/resourceinterpreter/defaultinterpreter/healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func getAllDefaultHealthInterpreter() map[schema.GroupVersionKind]healthInterpre
s[corev1.SchemeGroupVersion.WithKind(util.ServiceKind)] = interpretServiceHealth
s[networkingv1.SchemeGroupVersion.WithKind(util.IngressKind)] = interpretIngressHealth
s[corev1.SchemeGroupVersion.WithKind(util.PersistentVolumeClaimKind)] = interpretPersistentVolumeClaimHealth
s[corev1.SchemeGroupVersion.WithKind(util.PodKind)] = interpretPodHealth
s[policyv1.SchemeGroupVersion.WithKind(util.PodDisruptionBudgetKind)] = interpretPodDisruptionBudgetHealth
return s
}
Expand Down Expand Up @@ -147,6 +148,25 @@ func interpretPersistentVolumeClaimHealth(object *unstructured.Unstructured) (bo
return pvc.Status.Phase == corev1.ClaimBound, nil
}

func interpretPodHealth(object *unstructured.Unstructured) (bool, error) {
pod := &corev1.Pod{}
err := helper.ConvertToTypedObject(object, pod)
if err != nil {
return false, err
}

if pod.Status.Phase == corev1.PodSucceeded {
return true, nil
}

_, condition := helper.GetPodCondition(&pod.Status, corev1.PodReady)
if pod.Status.Phase == corev1.PodRunning && condition != nil && condition.Status == corev1.ConditionTrue {
return true, nil
}

return false, nil
}

func interpretPodDisruptionBudgetHealth(object *unstructured.Unstructured) (bool, error) {
pdb := &policyv1.PodDisruptionBudget{}
err := helper.ConvertToTypedObject(object, pdb)
Expand Down
117 changes: 117 additions & 0 deletions pkg/resourceinterpreter/defaultinterpreter/healthy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,123 @@ func Test_interpretPersistentVolumeClaimHealth(t *testing.T) {
}
}

func Test_interpretPodHealth(t *testing.T) {
tests := []struct {
name string
object *unstructured.Unstructured
want bool
wantErr bool
}{
{
name: "service type pod healthy",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "fake-pod",
},
"status": map[string]interface{}{
"conditions": []map[string]string{
{
"type": "Ready",
"status": "True",
},
},
"phase": "Running",
},
},
},
want: true,
wantErr: false,
},
{
name: "job type pod healthy",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "fake-pod",
},
"status": map[string]interface{}{
"phase": "Succeeded",
},
},
},
want: true,
wantErr: false,
},
{
name: "pod condition ready false",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "fake-pod",
},
"status": map[string]interface{}{
"conditions": []map[string]string{
{
"type": "Ready",
"status": "Unknown",
},
},
"phase": "Running",
},
},
},
want: false,
wantErr: false,
},
{
name: "pod phase not running and not succeeded",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "fake-pod",
},
"status": map[string]interface{}{
"phase": "Failed",
},
},
},
want: false,
wantErr: false,
},
{
name: "condition or phase nil",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "Pod",
"metadata": map[string]interface{}{
"name": "fake-pod",
},
},
},
want: false,
wantErr: false,
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := interpretPodHealth(tt.object)
if (err != nil) != tt.wantErr {
t.Errorf("interpretPodHealth() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("interpretPodHealth() = %v, want %v", got, tt.want)
}
})
}
}
func Test_interpretPodDisruptionBudgetHealth(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit a14d64f

Please sign in to comment.