Skip to content

Commit

Permalink
fix: use fixed size int32 (#1155)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Perenesenko <[email protected]>
  • Loading branch information
perenesenko authored May 10, 2021
1 parent 92e2aab commit b6aaf4b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/rollouts/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ type RolloutPause struct {
func (p RolloutPause) DurationSeconds() int32 {
if p.Duration != nil {
if p.Duration.Type == intstr.String {
s, err := strconv.Atoi(p.Duration.StrVal)
s, err := strconv.ParseInt(p.Duration.StrVal, 10, 32)
if err != nil {
d, err := time.ParseDuration(p.Duration.StrVal)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/rollouts/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ func TestRolloutPauseDuration(t *testing.T) {
assert.Equal(t, int32(0), rp.DurationSeconds())
rp.Duration = DurationFromString("1z")
assert.Equal(t, int32(-1), rp.DurationSeconds())
rp.Duration = DurationFromString("20000000000") // out of int32
assert.Equal(t, int32(-1), rp.DurationSeconds())
}
4 changes: 2 additions & 2 deletions utils/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getIntFromAnnotation(rs *appsv1.ReplicaSet, annotationKey string) (int32, b
if !ok {
return int32(0), false
}
intValue, err := strconv.Atoi(annotationValue)
intValue, err := strconv.ParseInt(annotationValue, 10, 32)
if err != nil {
log.Warnf("Cannot convert the value %q with annotation key %q for the replica set %q", annotationValue, annotationKey, rs.Name)
return int32(0), false
Expand Down Expand Up @@ -186,7 +186,7 @@ func IsSaturated(rollout *v1alpha1.Rollout, rs *appsv1.ReplicaSet) bool {
return false
}
desiredString := rs.Annotations[DesiredReplicasAnnotation]
desired, err := strconv.Atoi(desiredString)
desired, err := strconv.ParseInt(desiredString, 10, 32)
if err != nil {
return false
}
Expand Down
7 changes: 7 additions & 0 deletions utils/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ func TestAnnotationUtils(t *testing.T) {
}
})

t.Run("GetDesiredReplicasAnnotationOutOfInt32Value", func(t *testing.T) {
cRS := tRS.DeepCopy()
cRS.Annotations[DesiredReplicasAnnotation] = "20000000000"
_, ok := GetDesiredReplicasAnnotation(cRS)
assert.Equal(t, false, ok, "Should be an error as 20M value does not fit into int32")
})

//Check if annotations reflect rollouts state
tRS.Annotations[DesiredReplicasAnnotation] = "1"
tRS.Status.AvailableReplicas = 1
Expand Down

0 comments on commit b6aaf4b

Please sign in to comment.