Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rollout status of partitioned update #68

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions pkg/internal/polymorphichelpers/rollout_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,22 @@ func (s *CloneSetStatusViewer) Status(c kubernetes.Interface, obj runtime.Unstru
if err != nil {
return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, cs, err)
}
partition, err := CalculatePartitionReplicas(cs.Spec.UpdateStrategy.Partition, cs.Spec.Replicas)
if err != nil {
return "", false, fmt.Errorf("failed calculate partion of cs %T: %v", cs, err)
}

// check InPlaceOnly and InPlacePossible UpdateStrategy
if cs.Spec.UpdateStrategy.Type == kruiseappsv1alpha1.InPlaceOnlyCloneSetUpdateStrategyType ||
cs.Spec.UpdateStrategy.Type == kruiseappsv1alpha1.InPlaceIfPossibleCloneSetUpdateStrategyType {
if cs.Spec.Replicas != nil && cs.Spec.UpdateStrategy.Partition != nil {
return fmt.Sprintf("Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...\n",
cs.Status.UpdatedReplicas, *cs.Spec.Replicas-cs.Spec.UpdateStrategy.Partition.IntVal), false, nil
if cs.Status.UpdatedReplicas < (*cs.Spec.Replicas - int32(partition)) {
return fmt.Sprintf("Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...\n",
cs.Status.UpdatedReplicas, *cs.Spec.Replicas-cs.Spec.UpdateStrategy.Partition.IntVal), false, nil
}
}
return fmt.Sprintf("partitioned roll out complete: %d new pods have been updated...\n",
cs.Status.UpdatedReplicas), true, nil
}

if cs.Status.ObservedGeneration == 0 || cs.Generation > cs.Status.ObservedGeneration {
Expand All @@ -296,15 +304,22 @@ func (s *CloneSetStatusViewer) DetailStatus(c kubernetes.Interface, obj runtime.
return "", false, fmt.Errorf("failed to convert %T to %T: %v", obj, cs, err)
}

partition, err := CalculatePartitionReplicas(cs.Spec.UpdateStrategy.Partition, cs.Spec.Replicas)
if err != nil {
return "", false, fmt.Errorf("failed calculate partion of cs %T: %v", cs, err)
}

// check InPlaceOnly and InPlacePossible UpdateStrategy
if cs.Spec.UpdateStrategy.Type == kruiseappsv1alpha1.InPlaceOnlyCloneSetUpdateStrategyType ||
cs.Spec.UpdateStrategy.Type == kruiseappsv1alpha1.InPlaceIfPossibleCloneSetUpdateStrategyType {
if cs.Spec.Replicas != nil && cs.Spec.UpdateStrategy.Partition != nil {
if cs.Status.UpdatedReplicas < (*cs.Spec.Replicas - cs.Spec.UpdateStrategy.Partition.IntVal) {
if cs.Status.UpdatedReplicas < (*cs.Spec.Replicas - int32(partition)) {
return fmt.Sprintf("CloneSet %s Waiting for partitioned roll out to finish: %d out of %d new pods have been updated...\n%s",
cs.Name, cs.Status.UpdatedReplicas, *cs.Spec.Replicas-cs.Spec.UpdateStrategy.Partition.IntVal, generatePodsInfoForCloneSet(c, cs)), false, nil
}
}
return fmt.Sprintf("partitioned roll out complete: %d new pods have been updated...\n",
cs.Status.UpdatedReplicas), true, nil
}

if cs.Status.ObservedGeneration == 0 || cs.Generation > cs.Status.ObservedGeneration {
Expand Down
31 changes: 31 additions & 0 deletions pkg/internal/polymorphichelpers/rollout_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/utils/integer"
)

func getPodsByLabelSelector(client kubernetes.Interface, ns string, labelSelector *metav1.LabelSelector) ([]*corev1.Pod, error) {
Expand Down Expand Up @@ -72,3 +74,32 @@ func generatePodsInfoForCloneSet(client kubernetes.Interface, clone *kruiseappsv

return fmt.Sprintf("Updated ready pods: %v\nUpdated not ready pods: %v\n", ReadyPodsSlice, notReadyPodsSlice)
}

// CalculatePartitionReplicas returns absolute value of partition for workload. This func can solve some
// corner cases about percentage-type partition, such as:
// - if partition > "0%" and replicas > 0, we will ensure at least 1 old pod is reserved.
// - if partition < "100%" and replicas > 1, we will ensure at least 1 pod is upgraded.
func CalculatePartitionReplicas(partition *intstrutil.IntOrString, replicasPointer *int32) (int, error) {
if partition == nil {
return 0, nil
}

replicas := 1
if replicasPointer != nil {
replicas = int(*replicasPointer)
}

// 'roundUp=true' will ensure at least 1 old pod is reserved if partition > "0%" and replicas > 0.
pValue, err := intstrutil.GetScaledValueFromIntOrPercent(partition, replicas, true)
if err != nil {
return pValue, err
}

// if partition < "100%" and replicas > 1, we will ensure at least 1 pod is upgraded.
if replicas > 1 && pValue == replicas && partition.Type == intstrutil.String && partition.StrVal != "100%" {
pValue = replicas - 1
}

pValue = integer.IntMax(integer.IntMin(pValue, replicas), 0)
return pValue, nil
}