Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7be90715
Commit
7be90715
authored
Feb 09, 2016
by
mqliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create some integer min/max function
parent
b5c12d10
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
197 additions
and
7 deletions
+197
-7
deployment_controller.go
pkg/controller/deployment/deployment_controller.go
+4
-4
rolling_updater.go
pkg/kubectl/rolling_updater.go
+2
-1
backoff.go
pkg/util/backoff.go
+3
-2
integer.go
pkg/util/integer/integer.go
+45
-0
integer_test.go
pkg/util/integer/integer_test.go
+143
-0
No files found.
pkg/controller/deployment/deployment_controller.go
View file @
7be90715
...
...
@@ -18,7 +18,6 @@ package deployment
import
(
"fmt"
"math"
"reflect"
"sort"
"strconv"
...
...
@@ -39,6 +38,7 @@ import (
"k8s.io/kubernetes/pkg/util"
deploymentutil
"k8s.io/kubernetes/pkg/util/deployment"
utilerrors
"k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/util/integer"
labelsutil
"k8s.io/kubernetes/pkg/util/labels"
podutil
"k8s.io/kubernetes/pkg/util/pod"
utilruntime
"k8s.io/kubernetes/pkg/util/runtime"
...
...
@@ -805,7 +805,7 @@ func (dc *DeploymentController) reconcileNewReplicaSet(allRSs []*extensions.Repl
// Scale up.
scaleUpCount
:=
maxTotalPods
-
currentPodCount
// Do not exceed the number of desired replicas.
scaleUpCount
=
int
(
math
.
Min
(
float64
(
scaleUpCount
),
float64
(
deployment
.
Spec
.
Replicas
-
newRS
.
Spec
.
Replicas
))
)
scaleUpCount
=
int
eger
.
IntMin
(
scaleUpCount
,
deployment
.
Spec
.
Replicas
-
newRS
.
Spec
.
Replicas
)
newReplicasCount
:=
newRS
.
Spec
.
Replicas
+
scaleUpCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
newReplicasCount
,
deployment
)
return
true
,
err
...
...
@@ -926,7 +926,7 @@ func (dc *DeploymentController) cleanupUnhealthyReplicas(oldRSs []*extensions.Re
continue
}
scaledDownCount
:=
int
(
math
.
Min
(
float64
(
maxCleanupCount
-
totalScaledDown
),
float64
(
targetRS
.
Spec
.
Replicas
-
readyPodCount
))
)
scaledDownCount
:=
int
eger
.
IntMin
(
maxCleanupCount
-
totalScaledDown
,
targetRS
.
Spec
.
Replicas
-
readyPodCount
)
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaledDownCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
if
err
!=
nil
{
...
...
@@ -974,7 +974,7 @@ func (dc *DeploymentController) scaleDownOldReplicaSetsForRollingUpdate(allRSs [
continue
}
// Scale down.
scaleDownCount
:=
int
(
math
.
Min
(
float64
(
targetRS
.
Spec
.
Replicas
),
float64
(
totalScaleDownCount
-
totalScaledDown
))
)
scaleDownCount
:=
int
eger
.
IntMin
(
targetRS
.
Spec
.
Replicas
,
totalScaleDownCount
-
totalScaledDown
)
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaleDownCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
if
err
!=
nil
{
...
...
pkg/kubectl/rolling_updater.go
View file @
7be90715
...
...
@@ -30,6 +30,7 @@ import (
client
"k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/integer"
"k8s.io/kubernetes/pkg/util/intstr"
"k8s.io/kubernetes/pkg/util/wait"
)
...
...
@@ -214,7 +215,7 @@ func (r *RollingUpdater) Update(config *RollingUpdaterConfig) error {
}
// The minumum pods which must remain available througout the update
// calculated for internal convenience.
minAvailable
:=
int
(
math
.
Max
(
float64
(
0
),
float64
(
desired
-
maxUnavailable
))
)
minAvailable
:=
int
eger
.
IntMax
(
0
,
desired
-
maxUnavailable
)
// If the desired new scale is 0, then the max unavailable is necessarily
// the effective scale of the old RC regardless of the configuration
// (equivalent to 100% maxUnavailable).
...
...
pkg/util/backoff.go
View file @
7be90715
...
...
@@ -17,9 +17,10 @@ limitations under the License.
package
util
import
(
"math"
"sync"
"time"
"k8s.io/kubernetes/pkg/util/integer"
)
type
backoffEntry
struct
{
...
...
@@ -65,7 +66,7 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
entry
=
p
.
initEntryUnsafe
(
id
)
}
else
{
delay
:=
entry
.
backoff
*
2
// exponential
entry
.
backoff
=
time
.
Duration
(
math
.
Min
(
float64
(
delay
),
floa
t64
(
p
.
maxDuration
)))
entry
.
backoff
=
time
.
Duration
(
integer
.
Int64Min
(
int64
(
delay
),
in
t64
(
p
.
maxDuration
)))
}
entry
.
lastUpdate
=
p
.
Clock
.
Now
()
}
...
...
pkg/util/integer/integer.go
0 → 100644
View file @
7be90715
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
integer
func
IntMax
(
a
,
b
int
)
int
{
if
b
>
a
{
return
b
}
return
a
}
func
IntMin
(
a
,
b
int
)
int
{
if
b
<
a
{
return
b
}
return
a
}
func
Int64Max
(
a
,
b
int64
)
int64
{
if
b
>
a
{
return
b
}
return
a
}
func
Int64Min
(
a
,
b
int64
)
int64
{
if
b
<
a
{
return
b
}
return
a
}
pkg/util/integer/integer_test.go
0 → 100644
View file @
7be90715
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
integer
import
"testing"
func
TestIntMax
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
nums
[]
int
expectedMax
int
}{
{
nums
:
[]
int
{
-
1
,
0
},
expectedMax
:
0
,
},
{
nums
:
[]
int
{
-
1
,
-
2
},
expectedMax
:
-
1
,
},
{
nums
:
[]
int
{
0
,
1
},
expectedMax
:
1
,
},
{
nums
:
[]
int
{
1
,
2
},
expectedMax
:
2
,
},
}
for
i
,
test
:=
range
tests
{
t
.
Logf
(
"executing scenario %d"
,
i
)
if
max
:=
IntMax
(
test
.
nums
[
0
],
test
.
nums
[
1
]);
max
!=
test
.
expectedMax
{
t
.
Errorf
(
"expected %v, got %v"
,
test
.
expectedMax
,
max
)
}
}
}
func
TestIntMin
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
nums
[]
int
expectedMin
int
}{
{
nums
:
[]
int
{
-
1
,
0
},
expectedMin
:
-
1
,
},
{
nums
:
[]
int
{
-
1
,
-
2
},
expectedMin
:
-
2
,
},
{
nums
:
[]
int
{
0
,
1
},
expectedMin
:
0
,
},
{
nums
:
[]
int
{
1
,
2
},
expectedMin
:
1
,
},
}
for
i
,
test
:=
range
tests
{
t
.
Logf
(
"executing scenario %d"
,
i
)
if
min
:=
IntMin
(
test
.
nums
[
0
],
test
.
nums
[
1
]);
min
!=
test
.
expectedMin
{
t
.
Errorf
(
"expected %v, got %v"
,
test
.
expectedMin
,
min
)
}
}
}
func
TestInt64Max
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
nums
[]
int64
expectedMax
int64
}{
{
nums
:
[]
int64
{
-
1
,
0
},
expectedMax
:
0
,
},
{
nums
:
[]
int64
{
-
1
,
-
2
},
expectedMax
:
-
1
,
},
{
nums
:
[]
int64
{
0
,
1
},
expectedMax
:
1
,
},
{
nums
:
[]
int64
{
1
,
2
},
expectedMax
:
2
,
},
}
for
i
,
test
:=
range
tests
{
t
.
Logf
(
"executing scenario %d"
,
i
)
if
max
:=
Int64Max
(
test
.
nums
[
0
],
test
.
nums
[
1
]);
max
!=
test
.
expectedMax
{
t
.
Errorf
(
"expected %v, got %v"
,
test
.
expectedMax
,
max
)
}
}
}
func
TestInt64Min
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
nums
[]
int64
expectedMin
int64
}{
{
nums
:
[]
int64
{
-
1
,
0
},
expectedMin
:
-
1
,
},
{
nums
:
[]
int64
{
-
1
,
-
2
},
expectedMin
:
-
2
,
},
{
nums
:
[]
int64
{
0
,
1
},
expectedMin
:
0
,
},
{
nums
:
[]
int64
{
1
,
2
},
expectedMin
:
1
,
},
}
for
i
,
test
:=
range
tests
{
t
.
Logf
(
"executing scenario %d"
,
i
)
if
min
:=
Int64Min
(
test
.
nums
[
0
],
test
.
nums
[
1
]);
min
!=
test
.
expectedMin
{
t
.
Errorf
(
"expected %v, got %v"
,
test
.
expectedMin
,
min
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment