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

cloneset partition support float percent #1124

Merged
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
35 changes: 34 additions & 1 deletion pkg/util/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ limitations under the License.
package util

import (
"fmt"
"math"
"strconv"
"strings"
"sync"

"github.com/docker/distribution/reference"
Expand Down Expand Up @@ -163,7 +166,7 @@ func CalculatePartitionReplicas(partition *intstrutil.IntOrString, replicasPoint
}

// 'roundUp=true' will ensure at least 1 old pod is reserved if partition > "0%" and replicas > 0.
pValue, err := intstrutil.GetScaledValueFromIntOrPercent(partition, replicas, true)
pValue, err := GetScaledValueFromIntOrPercent(partition, replicas, true)
if err != nil {
return pValue, err
}
Expand All @@ -189,3 +192,33 @@ func IsReferenceEqual(ref1, ref2 appsv1alpha1.TargetReference) bool {
}
return gv1.Group == gv2.Group && ref1.Kind == ref2.Kind && ref1.Name == ref2.Name
}

func GetScaledValueFromIntOrPercent(intOrPercent *intstrutil.IntOrString, total int, roundUp bool) (int, error) {
if intOrPercent == nil {
return 0, fmt.Errorf("nil value for IntOrString")
}

switch intOrPercent.Type {
case intstrutil.Int:
return intOrPercent.IntValue(), nil
case intstrutil.String:
s := intOrPercent.StrVal
if strings.HasSuffix(s, "%") {
s = strings.TrimSuffix(intOrPercent.StrVal, "%")
} else {
return 0, fmt.Errorf("invalid type: string is not a percentage")
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, err
}
var value int
if roundUp {
value = int(math.Ceil(v * (float64(total)) / 100))
} else {
value = int(math.Floor(v * (float64(total)) / 100))
}
return value, nil
}
return 0, fmt.Errorf("invalid type: neither int nor percentage")
}
111 changes: 111 additions & 0 deletions pkg/util/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ func TestCalculatePartitionReplicas(t *testing.T) {
expectedValue: 0,
succeeded: false,
},
{
name: `replicas=9, partition=0.01%, expected=1, err==nil`,
replicas: pointer.Int32(9),
partition: &intstr.IntOrString{Type: intstr.String, StrVal: "0.01%"},
expectedValue: 1,
succeeded: true,
},
{
name: `replicas=999, partition=99.99%, expected=998, err==nil`,
replicas: pointer.Int32(999),
partition: &intstr.IntOrString{Type: intstr.String, StrVal: "99.99%"},
expectedValue: 998,
succeeded: true,
},
}

for _, cs := range cases {
Expand All @@ -299,3 +313,100 @@ func TestCalculatePartitionReplicas(t *testing.T) {
})
}
}

func TestGetScaledValueFromIntOrPercent(t *testing.T) {
tests := []struct {
input intstr.IntOrString
total int
roundUp bool
expectErr bool
expectVal int
}{
{
input: intstr.FromInt(123),
expectErr: false,
expectVal: 123,
},
{
input: intstr.FromString("90%"),
total: 100,
roundUp: true,
expectErr: false,
expectVal: 90,
},
{
input: intstr.FromString("90%"),
total: 95,
roundUp: true,
expectErr: false,
expectVal: 86,
},
{
input: intstr.FromString("90%"),
total: 95,
roundUp: false,
expectErr: false,
expectVal: 85,
},
{
input: intstr.FromString("99.99%"),
total: 999,
roundUp: false,
expectErr: false,
expectVal: 998,
},
{
input: intstr.FromString("99.99%"),
total: 999,
roundUp: true,
expectErr: false,
expectVal: 999,
},
{
input: intstr.FromString("0.01%"),
total: 95,
roundUp: false,
expectErr: false,
expectVal: 0,
},
{
input: intstr.FromString("0.01%"),
total: 95,
roundUp: true,
expectErr: false,
expectVal: 1,
},
{
input: intstr.FromString("%"),
expectErr: true,
},
{
input: intstr.FromString("90#"),
expectErr: true,
},
{
input: intstr.FromString("#%"),
expectErr: true,
},
{
input: intstr.FromString("90"),
expectErr: true,
},
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add 0.01% partition UT.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


for i, test := range tests {
t.Logf("test case %d", i)
value, err := GetScaledValueFromIntOrPercent(&test.input, test.total, test.roundUp)
if test.expectErr && err == nil {
t.Errorf("expected error, but got none")
continue
}
if !test.expectErr && err != nil {
t.Errorf("unexpected err: %v", err)
continue
}
if test.expectVal != value {
t.Errorf("expected %v, but got %v", test.expectVal, value)
}
}
}
2 changes: 1 addition & 1 deletion pkg/webhook/cloneset/validating/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (h *CloneSetCreateUpdateHandler) validateUpdateStrategy(strategy *appsv1alp
appsv1alpha1.InPlaceOnlyCloneSetUpdateStrategyType)))
}

partition, err := intstrutil.GetValueFromIntOrPercent(strategy.Partition, replicas, true)
partition, err := util.GetScaledValueFromIntOrPercent(strategy.Partition, replicas, true)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("partition"), strategy.Partition.String(),
fmt.Sprintf("failed getValueFromIntOrPercent for partition: %v", err)))
Expand Down