Skip to content

Commit

Permalink
Merge pull request #2944 from Fish-pro/automated-cherry-pick-of-#2928…
Browse files Browse the repository at this point in the history
…-upstream-release-1.4

Automated cherry pick of #2928: Fix misjudgment of deployment and statefuleset health status
  • Loading branch information
karmada-bot authored Dec 12, 2022
2 parents 1a61b30 + 8a4f5c7 commit 9ecdeb8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 9 deletions.
32 changes: 24 additions & 8 deletions pkg/resourceinterpreter/defaultinterpreter/healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ func interpretDeploymentHealth(object *unstructured.Unstructured) (bool, error)
return false, err
}

healthy := (deploy.Status.UpdatedReplicas == *deploy.Spec.Replicas) && (deploy.Generation == deploy.Status.ObservedGeneration)
return healthy, nil
if deploy.Status.ObservedGeneration != deploy.Generation {
return false, nil
}
if (deploy.Spec.Replicas != nil) && deploy.Status.UpdatedReplicas < *deploy.Spec.Replicas {
return false, nil
}
if deploy.Status.AvailableReplicas < deploy.Status.UpdatedReplicas {
return false, nil
}
return true, nil
}

func interpretStatefulSetHealth(object *unstructured.Unstructured) (bool, error) {
Expand All @@ -43,8 +51,16 @@ func interpretStatefulSetHealth(object *unstructured.Unstructured) (bool, error)
return false, err
}

healthy := (statefulSet.Status.UpdatedReplicas == *statefulSet.Spec.Replicas) && (statefulSet.Generation == statefulSet.Status.ObservedGeneration)
return healthy, nil
if statefulSet.Status.ObservedGeneration != statefulSet.Generation {
return false, nil
}
if (statefulSet.Spec.Replicas != nil) && statefulSet.Status.UpdatedReplicas < *statefulSet.Spec.Replicas {
return false, nil
}
if statefulSet.Status.AvailableReplicas < statefulSet.Status.UpdatedReplicas {
return false, nil
}
return true, nil
}

func interpretReplicaSetHealth(object *unstructured.Unstructured) (bool, error) {
Expand All @@ -54,10 +70,10 @@ func interpretReplicaSetHealth(object *unstructured.Unstructured) (bool, error)
return false, err
}

if replicaSet.Generation != replicaSet.Status.ObservedGeneration {
if replicaSet.Status.ObservedGeneration != replicaSet.Generation {
return false, nil
}
if replicaSet.Spec.Replicas != nil && replicaSet.Status.AvailableReplicas < *replicaSet.Spec.Replicas {
if (replicaSet.Spec.Replicas != nil) && replicaSet.Status.AvailableReplicas < *replicaSet.Spec.Replicas {
return false, nil
}
return true, nil
Expand All @@ -70,13 +86,13 @@ func interpretDaemonSetHealth(object *unstructured.Unstructured) (bool, error) {
return false, err
}

if daemonSet.Generation != daemonSet.Status.ObservedGeneration {
if daemonSet.Status.ObservedGeneration != daemonSet.Generation {
return false, nil
}
if daemonSet.Status.UpdatedNumberScheduled < daemonSet.Status.DesiredNumberScheduled {
return false, nil
}
if daemonSet.Status.NumberAvailable < daemonSet.Status.DesiredNumberScheduled {
if daemonSet.Status.NumberAvailable < daemonSet.Status.UpdatedNumberScheduled {
return false, nil
}

Expand Down
77 changes: 76 additions & 1 deletion pkg/resourceinterpreter/defaultinterpreter/healthy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Test_interpretDeploymentHealth(t *testing.T) {
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"updatedReplicas": 3,
"observedGeneration": 1,
},
Expand All @@ -50,6 +51,7 @@ func Test_interpretDeploymentHealth(t *testing.T) {
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"updatedReplicas": 3,
"observedGeneration": 2,
},
Expand Down Expand Up @@ -80,6 +82,29 @@ func Test_interpretDeploymentHealth(t *testing.T) {
want: false,
wantErr: false,
},
{
name: "availableReplicas not equal to updatedReplicas",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "fake-deployment",
"generation": 1,
},
"spec": map[string]interface{}{
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 0,
"updatedReplicas": 3,
"observedGeneration": 1,
},
},
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -115,6 +140,7 @@ func Test_interpretStatefulSetHealth(t *testing.T) {
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"updatedReplicas": 3,
"observedGeneration": 1,
},
Expand All @@ -137,6 +163,7 @@ func Test_interpretStatefulSetHealth(t *testing.T) {
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"updatedReplicas": 3,
"observedGeneration": 2,
},
Expand Down Expand Up @@ -167,6 +194,29 @@ func Test_interpretStatefulSetHealth(t *testing.T) {
want: false,
wantErr: false,
},
{
name: "availableReplicas not equal to updatedReplicas",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "StatefulSet",
"metadata": map[string]interface{}{
"name": "fake-statefulSet",
"generation": 1,
},
"spec": map[string]interface{}{
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 1,
"updatedReplicas": 3,
"observedGeneration": 1,
},
},
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -195,7 +245,11 @@ func Test_interpretReplicaSetHealth(t *testing.T) {
"metadata": map[string]interface{}{
"generation": 1,
},
"spec": map[string]interface{}{
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"observedGeneration": 1,
},
},
Expand All @@ -210,7 +264,11 @@ func Test_interpretReplicaSetHealth(t *testing.T) {
"metadata": map[string]interface{}{
"generation": 1,
},
"spec": map[string]interface{}{
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 3,
"observedGeneration": 2,
},
},
Expand All @@ -222,11 +280,15 @@ func Test_interpretReplicaSetHealth(t *testing.T) {
name: "replicas not equal to availableReplicas",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"generation": 1,
},
"spec": map[string]interface{}{
"replicas": 3,
},
"status": map[string]interface{}{
"availableReplicas": 2,
"availableReplicas": 2,
"observedGeneration": 2,
},
},
},
Expand Down Expand Up @@ -296,6 +358,19 @@ func Test_interpretDaemonSetHealth(t *testing.T) {
want: false,
wantErr: false,
},
{
name: "numberAvailable < updatedNumberScheduled",
object: &unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"updatedNumberScheduled": 5,
"numberAvailable": 3,
},
},
},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 9ecdeb8

Please sign in to comment.