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
e9262df4
Commit
e9262df4
authored
Feb 04, 2016
by
Janet Kuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Scale up early when deployment creates new Replica set
parent
3616b4bf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
76 additions
and
40 deletions
+76
-40
deployment_controller.go
pkg/controller/deployment/deployment_controller.go
+37
-38
deployment.go
pkg/util/deployment/deployment.go
+37
-0
deployment.go
test/e2e/deployment.go
+2
-2
No files found.
pkg/controller/deployment/deployment_controller.go
View file @
e9262df4
...
@@ -586,7 +586,7 @@ func (dc *DeploymentController) getAllReplicaSets(deployment extensions.Deployme
...
@@ -586,7 +586,7 @@ func (dc *DeploymentController) getAllReplicaSets(deployment extensions.Deployme
maxOldV
:=
maxRevision
(
allOldRSs
)
maxOldV
:=
maxRevision
(
allOldRSs
)
// Get new replica set with the updated revision number
// Get new replica set with the updated revision number
newRS
,
err
:=
dc
.
getNewReplicaSet
(
deployment
,
maxOldV
)
newRS
,
err
:=
dc
.
getNewReplicaSet
(
deployment
,
maxOldV
,
oldRSs
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
nil
,
err
return
nil
,
nil
,
err
}
}
...
@@ -648,7 +648,7 @@ func (dc *DeploymentController) getOldReplicaSets(deployment extensions.Deployme
...
@@ -648,7 +648,7 @@ func (dc *DeploymentController) getOldReplicaSets(deployment extensions.Deployme
// Returns a replica set that matches the intent of the given deployment.
// Returns a replica set that matches the intent of the given deployment.
// It creates a new replica set if required.
// It creates a new replica set if required.
// The revision of the new replica set will be updated to maxOldRevision + 1
// The revision of the new replica set will be updated to maxOldRevision + 1
func
(
dc
*
DeploymentController
)
getNewReplicaSet
(
deployment
extensions
.
Deployment
,
maxOldRevision
int64
)
(
*
extensions
.
ReplicaSet
,
error
)
{
func
(
dc
*
DeploymentController
)
getNewReplicaSet
(
deployment
extensions
.
Deployment
,
maxOldRevision
int64
,
oldRSs
[]
*
extensions
.
ReplicaSet
)
(
*
extensions
.
ReplicaSet
,
error
)
{
// Calculate revision number for this new replica set
// Calculate revision number for this new replica set
newRevision
:=
strconv
.
FormatInt
(
maxOldRevision
+
1
,
10
)
newRevision
:=
strconv
.
FormatInt
(
maxOldRevision
+
1
,
10
)
...
@@ -701,14 +701,22 @@ func (dc *DeploymentController) getNewReplicaSet(deployment extensions.Deploymen
...
@@ -701,14 +701,22 @@ func (dc *DeploymentController) getNewReplicaSet(deployment extensions.Deploymen
}
}
// Set new replica set's annotation
// Set new replica set's annotation
setNewReplicaSetAnnotations
(
&
deployment
,
&
newRS
,
newRevision
)
setNewReplicaSetAnnotations
(
&
deployment
,
&
newRS
,
newRevision
)
allRSs
:=
append
(
oldRSs
,
&
newRS
)
newReplicasCount
,
err
:=
deploymentutil
.
NewRSNewReplicas
(
&
deployment
,
allRSs
,
&
newRS
)
if
err
!=
nil
{
return
nil
,
err
}
newRS
.
Spec
.
Replicas
=
newReplicasCount
createdRS
,
err
:=
dc
.
client
.
Extensions
()
.
ReplicaSets
(
namespace
)
.
Create
(
&
newRS
)
createdRS
,
err
:=
dc
.
client
.
Extensions
()
.
ReplicaSets
(
namespace
)
.
Create
(
&
newRS
)
if
err
!=
nil
{
if
err
!=
nil
{
dc
.
rsExpectations
.
DeleteExpectations
(
dKey
)
dc
.
rsExpectations
.
DeleteExpectations
(
dKey
)
return
nil
,
fmt
.
Errorf
(
"error creating replica set: %v"
,
err
)
return
nil
,
fmt
.
Errorf
(
"error creating replica set: %v"
,
err
)
}
}
if
newReplicasCount
>
0
{
dc
.
eventRecorder
.
Eventf
(
&
deployment
,
api
.
EventTypeNormal
,
"ScalingReplicaSet"
,
"Scaled %s replica set %s to %d"
,
"up"
,
createdRS
.
Name
,
newReplicasCount
)
}
err
=
dc
.
updateDeploymentRevision
(
deployment
,
newRevision
)
return
createdRS
,
dc
.
updateDeploymentRevision
(
deployment
,
newRevision
)
return
createdRS
,
err
}
}
// setNewReplicaSetAnnotations sets new replica set's annotations appropriately by updating its revision and
// setNewReplicaSetAnnotations sets new replica set's annotations appropriately by updating its revision and
...
@@ -752,9 +760,12 @@ func (dc *DeploymentController) updateDeploymentRevision(deployment extensions.D
...
@@ -752,9 +760,12 @@ func (dc *DeploymentController) updateDeploymentRevision(deployment extensions.D
if
deployment
.
Annotations
==
nil
{
if
deployment
.
Annotations
==
nil
{
deployment
.
Annotations
=
make
(
map
[
string
]
string
)
deployment
.
Annotations
=
make
(
map
[
string
]
string
)
}
}
deployment
.
Annotations
[
deploymentutil
.
RevisionAnnotation
]
=
revision
if
deployment
.
Annotations
[
deploymentutil
.
RevisionAnnotation
]
!=
revision
{
_
,
err
:=
dc
.
updateDeployment
(
&
deployment
)
deployment
.
Annotations
[
deploymentutil
.
RevisionAnnotation
]
=
revision
return
err
_
,
err
:=
dc
.
updateDeployment
(
&
deployment
)
return
err
}
return
nil
}
}
func
(
dc
*
DeploymentController
)
reconcileNewReplicaSet
(
allRSs
[]
*
extensions
.
ReplicaSet
,
newRS
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
(
bool
,
error
)
{
func
(
dc
*
DeploymentController
)
reconcileNewReplicaSet
(
allRSs
[]
*
extensions
.
ReplicaSet
,
newRS
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
(
bool
,
error
)
{
...
@@ -764,29 +775,15 @@ func (dc *DeploymentController) reconcileNewReplicaSet(allRSs []*extensions.Repl
...
@@ -764,29 +775,15 @@ func (dc *DeploymentController) reconcileNewReplicaSet(allRSs []*extensions.Repl
}
}
if
newRS
.
Spec
.
Replicas
>
deployment
.
Spec
.
Replicas
{
if
newRS
.
Spec
.
Replicas
>
deployment
.
Spec
.
Replicas
{
// Scale down.
// Scale down.
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
deployment
.
Spec
.
Replicas
,
deployment
)
scaled
,
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
deployment
.
Spec
.
Replicas
,
deployment
)
return
true
,
err
return
scaled
,
err
}
}
// Check if we can scale up.
newReplicasCount
,
err
:=
deploymentutil
.
NewRSNewReplicas
(
&
deployment
,
allRSs
,
newRS
)
maxSurge
,
err
:=
intstrutil
.
GetValueFromIntOrPercent
(
&
deployment
.
Spec
.
Strategy
.
RollingUpdate
.
MaxSurge
,
deployment
.
Spec
.
Replicas
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
}
}
scaled
,
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
newReplicasCount
,
deployment
)
// Find the total number of pods
return
scaled
,
err
currentPodCount
:=
deploymentutil
.
GetReplicaCountForReplicaSets
(
allRSs
)
maxTotalPods
:=
deployment
.
Spec
.
Replicas
+
maxSurge
if
currentPodCount
>=
maxTotalPods
{
// Cannot scale up.
return
false
,
nil
}
// Scale up.
scaleUpCount
:=
maxTotalPods
-
currentPodCount
// Do not exceed the number of desired replicas.
scaleUpCount
=
integer
.
IntMin
(
scaleUpCount
,
deployment
.
Spec
.
Replicas
-
newRS
.
Spec
.
Replicas
)
newReplicasCount
:=
newRS
.
Spec
.
Replicas
+
scaleUpCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
newReplicasCount
,
deployment
)
return
true
,
err
}
}
// Set expectationsCheck to false to bypass expectations check when testing
// Set expectationsCheck to false to bypass expectations check when testing
...
@@ -903,7 +900,7 @@ func (dc *DeploymentController) cleanupUnhealthyReplicas(oldRSs []*extensions.Re
...
@@ -903,7 +900,7 @@ func (dc *DeploymentController) cleanupUnhealthyReplicas(oldRSs []*extensions.Re
scaledDownCount
:=
integer
.
IntMin
(
maxCleanupCount
-
totalScaledDown
,
targetRS
.
Spec
.
Replicas
-
readyPodCount
)
scaledDownCount
:=
integer
.
IntMin
(
maxCleanupCount
-
totalScaledDown
,
targetRS
.
Spec
.
Replicas
-
readyPodCount
)
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaledDownCount
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaledDownCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
_
,
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
if
err
!=
nil
{
if
err
!=
nil
{
return
totalScaledDown
,
err
return
totalScaledDown
,
err
}
}
...
@@ -949,7 +946,7 @@ func (dc *DeploymentController) scaleDownOldReplicaSetsForRollingUpdate(allRSs [
...
@@ -949,7 +946,7 @@ func (dc *DeploymentController) scaleDownOldReplicaSetsForRollingUpdate(allRSs [
// Scale down.
// Scale down.
scaleDownCount
:=
integer
.
IntMin
(
targetRS
.
Spec
.
Replicas
,
totalScaleDownCount
-
totalScaledDown
)
scaleDownCount
:=
integer
.
IntMin
(
targetRS
.
Spec
.
Replicas
,
totalScaleDownCount
-
totalScaledDown
)
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaleDownCount
newReplicasCount
:=
targetRS
.
Spec
.
Replicas
-
scaleDownCount
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
_
,
_
,
err
=
dc
.
scaleReplicaSetAndRecordEvent
(
targetRS
,
newReplicasCount
,
deployment
)
if
err
!=
nil
{
if
err
!=
nil
{
return
totalScaledDown
,
err
return
totalScaledDown
,
err
}
}
...
@@ -968,23 +965,21 @@ func (dc *DeploymentController) scaleDownOldReplicaSetsForRecreate(oldRSs []*ext
...
@@ -968,23 +965,21 @@ func (dc *DeploymentController) scaleDownOldReplicaSetsForRecreate(oldRSs []*ext
if
rs
.
Spec
.
Replicas
==
0
{
if
rs
.
Spec
.
Replicas
==
0
{
continue
continue
}
}
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
rs
,
0
,
deployment
)
scaledRS
,
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
rs
,
0
,
deployment
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
}
}
scaled
=
true
if
scaledRS
{
scaled
=
true
}
}
}
return
scaled
,
nil
return
scaled
,
nil
}
}
// scaleUpNewReplicaSetForRecreate scales up new replica set when deployment strategy is "Recreate"
// scaleUpNewReplicaSetForRecreate scales up new replica set when deployment strategy is "Recreate"
func
(
dc
*
DeploymentController
)
scaleUpNewReplicaSetForRecreate
(
newRS
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
(
bool
,
error
)
{
func
(
dc
*
DeploymentController
)
scaleUpNewReplicaSetForRecreate
(
newRS
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
(
bool
,
error
)
{
if
newRS
.
Spec
.
Replicas
==
deployment
.
Spec
.
Replicas
{
scaled
,
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
deployment
.
Spec
.
Replicas
,
deployment
)
// Scaling not required.
return
scaled
,
err
return
false
,
nil
}
_
,
err
:=
dc
.
scaleReplicaSetAndRecordEvent
(
newRS
,
deployment
.
Spec
.
Replicas
,
deployment
)
return
true
,
err
}
}
func
(
dc
*
DeploymentController
)
cleanupOldReplicaSets
(
oldRSs
[]
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
error
{
func
(
dc
*
DeploymentController
)
cleanupOldReplicaSets
(
oldRSs
[]
*
extensions
.
ReplicaSet
,
deployment
extensions
.
Deployment
)
error
{
...
@@ -1042,7 +1037,11 @@ func (dc *DeploymentController) calculateStatus(allRSs []*extensions.ReplicaSet,
...
@@ -1042,7 +1037,11 @@ func (dc *DeploymentController) calculateStatus(allRSs []*extensions.ReplicaSet,
return
return
}
}
func
(
dc
*
DeploymentController
)
scaleReplicaSetAndRecordEvent
(
rs
*
extensions
.
ReplicaSet
,
newScale
int
,
deployment
extensions
.
Deployment
)
(
*
extensions
.
ReplicaSet
,
error
)
{
func
(
dc
*
DeploymentController
)
scaleReplicaSetAndRecordEvent
(
rs
*
extensions
.
ReplicaSet
,
newScale
int
,
deployment
extensions
.
Deployment
)
(
bool
,
*
extensions
.
ReplicaSet
,
error
)
{
// No need to scale
if
rs
.
Spec
.
Replicas
==
newScale
{
return
false
,
rs
,
nil
}
scalingOperation
:=
"down"
scalingOperation
:=
"down"
if
rs
.
Spec
.
Replicas
<
newScale
{
if
rs
.
Spec
.
Replicas
<
newScale
{
scalingOperation
=
"up"
scalingOperation
=
"up"
...
@@ -1051,7 +1050,7 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(rs *extensions.Rep
...
@@ -1051,7 +1050,7 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(rs *extensions.Rep
if
err
==
nil
{
if
err
==
nil
{
dc
.
eventRecorder
.
Eventf
(
&
deployment
,
api
.
EventTypeNormal
,
"ScalingReplicaSet"
,
"Scaled %s replica set %s to %d"
,
scalingOperation
,
rs
.
Name
,
newScale
)
dc
.
eventRecorder
.
Eventf
(
&
deployment
,
api
.
EventTypeNormal
,
"ScalingReplicaSet"
,
"Scaled %s replica set %s to %d"
,
scalingOperation
,
rs
.
Name
,
newScale
)
}
}
return
newRS
,
err
return
true
,
newRS
,
err
}
}
func
(
dc
*
DeploymentController
)
scaleReplicaSet
(
rs
*
extensions
.
ReplicaSet
,
newScale
int
)
(
*
extensions
.
ReplicaSet
,
error
)
{
func
(
dc
*
DeploymentController
)
scaleReplicaSet
(
rs
*
extensions
.
ReplicaSet
,
newScale
int
)
(
*
extensions
.
ReplicaSet
,
error
)
{
...
...
pkg/util/deployment/deployment.go
View file @
e9262df4
...
@@ -26,6 +26,8 @@ import (
...
@@ -26,6 +26,8 @@ import (
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/apis/extensions"
clientset
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
clientset
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/integer"
intstrutil
"k8s.io/kubernetes/pkg/util/intstr"
labelsutil
"k8s.io/kubernetes/pkg/util/labels"
labelsutil
"k8s.io/kubernetes/pkg/util/labels"
podutil
"k8s.io/kubernetes/pkg/util/pod"
podutil
"k8s.io/kubernetes/pkg/util/pod"
)
)
...
@@ -233,3 +235,38 @@ func Revision(rs *extensions.ReplicaSet) (int64, error) {
...
@@ -233,3 +235,38 @@ func Revision(rs *extensions.ReplicaSet) (int64, error) {
}
}
return
strconv
.
ParseInt
(
v
,
10
,
64
)
return
strconv
.
ParseInt
(
v
,
10
,
64
)
}
}
func
IsRollingUpdate
(
deployment
*
extensions
.
Deployment
)
bool
{
return
deployment
.
Spec
.
Strategy
.
Type
==
extensions
.
RollingUpdateDeploymentStrategyType
}
// NewRSNewReplicas calculates the number of replicas a deployment's new RS should have.
// When one of the followings is true, we're rolling out the deployment; otherwise, we're scaling it.
// 1) The new RS is saturated: newRS's replicas == deployment's replicas
// 2) Max number of pods allowed is reached: deployment's replicas + maxSurge == all RSs' replicas
func
NewRSNewReplicas
(
deployment
*
extensions
.
Deployment
,
allRSs
[]
*
extensions
.
ReplicaSet
,
newRS
*
extensions
.
ReplicaSet
)
(
int
,
error
)
{
switch
deployment
.
Spec
.
Strategy
.
Type
{
case
extensions
.
RollingUpdateDeploymentStrategyType
:
// Check if we can scale up.
maxSurge
,
err
:=
intstrutil
.
GetValueFromIntOrPercent
(
&
deployment
.
Spec
.
Strategy
.
RollingUpdate
.
MaxSurge
,
deployment
.
Spec
.
Replicas
)
if
err
!=
nil
{
return
0
,
err
}
// Find the total number of pods
currentPodCount
:=
GetReplicaCountForReplicaSets
(
allRSs
)
maxTotalPods
:=
deployment
.
Spec
.
Replicas
+
maxSurge
if
currentPodCount
>=
maxTotalPods
{
// Cannot scale up.
return
newRS
.
Spec
.
Replicas
,
nil
}
// Scale up.
scaleUpCount
:=
maxTotalPods
-
currentPodCount
// Do not exceed the number of desired replicas.
scaleUpCount
=
integer
.
IntMin
(
scaleUpCount
,
deployment
.
Spec
.
Replicas
-
newRS
.
Spec
.
Replicas
)
return
newRS
.
Spec
.
Replicas
+
scaleUpCount
,
nil
case
extensions
.
RecreateDeploymentStrategyType
:
return
deployment
.
Spec
.
Replicas
,
nil
default
:
return
0
,
fmt
.
Errorf
(
"deployment type %v isn't supported"
,
deployment
.
Spec
.
Strategy
.
Type
)
}
}
test/e2e/deployment.go
View file @
e9262df4
...
@@ -386,8 +386,8 @@ func testRecreateDeployment(f *Framework) {
...
@@ -386,8 +386,8 @@ func testRecreateDeployment(f *Framework) {
newRS
,
err
:=
deploymentutil
.
GetNewReplicaSet
(
*
deployment
,
c
)
newRS
,
err
:=
deploymentutil
.
GetNewReplicaSet
(
*
deployment
,
c
)
Expect
(
err
)
.
NotTo
(
HaveOccurred
())
Expect
(
err
)
.
NotTo
(
HaveOccurred
())
Expect
(
newRS
)
.
NotTo
(
Equal
(
nil
))
Expect
(
newRS
)
.
NotTo
(
Equal
(
nil
))
Expect
(
events
.
Items
[
0
]
.
Message
)
.
Should
(
Equal
(
fmt
.
Sprintf
(
"Scaled
down replica set %s to 0"
,
rs
Name
)))
Expect
(
events
.
Items
[
0
]
.
Message
)
.
Should
(
Equal
(
fmt
.
Sprintf
(
"Scaled
up replica set %s to 3"
,
newRS
.
Name
)))
Expect
(
events
.
Items
[
1
]
.
Message
)
.
Should
(
Equal
(
fmt
.
Sprintf
(
"Scaled
up replica set %s to 3"
,
newRS
.
Name
)))
Expect
(
events
.
Items
[
1
]
.
Message
)
.
Should
(
Equal
(
fmt
.
Sprintf
(
"Scaled
down replica set %s to 0"
,
rs
Name
)))
// Check if it's updated to revision 1 correctly
// Check if it's updated to revision 1 correctly
checkDeploymentRevision
(
c
,
ns
,
deploymentName
,
"1"
,
"redis"
,
"redis"
)
checkDeploymentRevision
(
c
,
ns
,
deploymentName
,
"1"
,
"redis"
,
"redis"
)
...
...
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