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

deployer controller: ensure phase direction #8417

Merged
merged 1 commit into from
Apr 11, 2016
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
7 changes: 3 additions & 4 deletions pkg/deploy/controller/deployerpod/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ func (c *DeployerPodController) Handle(pod *kapi.Pod) error {

switch pod.Status.Phase {
case kapi.PodRunning:
if !deployutil.IsTerminatedDeployment(deployment) {
nextStatus = deployapi.DeploymentStatusRunning
}
nextStatus = deployapi.DeploymentStatusRunning

case kapi.PodSucceeded:
nextStatus = deployapi.DeploymentStatusComplete

Expand Down Expand Up @@ -137,7 +136,7 @@ func (c *DeployerPodController) Handle(pod *kapi.Pod) error {
}
}

if currentStatus != nextStatus {
if deployutil.CanTransitionPhase(currentStatus, nextStatus) {
deployment.Annotations[deployapi.DeploymentStatusAnnotation] = string(nextStatus)
if _, err := c.kClient.ReplicationControllers(deployment.Namespace).Update(deployment); err != nil {
if kerrors.IsNotFound(err) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/deploy/controller/deployerpod/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ func TestHandle_podTerminatedFailNoContainerStatusTest(t *testing.T) {
// TestHandle_cleanupDesiredReplicasAnnotation ensures that the desired replicas annotation
// will be cleaned up in a complete deployment and stay around in a failed deployment
func TestHandle_cleanupDesiredReplicasAnnotation(t *testing.T) {
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codecs.LegacyCodec(deployapi.SchemeGroupVersion))
// shared fixtures shouldn't be used in unit tests
shared, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codecs.LegacyCodec(deployapi.SchemeGroupVersion))

tests := []struct {
name string
Expand All @@ -361,17 +362,18 @@ func TestHandle_cleanupDesiredReplicasAnnotation(t *testing.T) {
}{
{
name: "complete deployment - cleaned up annotation",
pod: succeededPod(deployment),
pod: succeededPod(shared),
expected: false,
},
{
name: "failed deployment - annotation stays",
pod: terminatedPod(deployment),
pod: terminatedPod(shared),
expected: true,
},
}

for _, test := range tests {
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codecs.LegacyCodec(deployapi.SchemeGroupVersion))
var updatedDeployment *kapi.ReplicationController
deployment.Annotations[deployapi.DesiredReplicasAnnotation] = "1"

Expand Down
2 changes: 1 addition & 1 deletion pkg/deploy/controller/deployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *DeploymentController) Handle(deployment *kapi.ReplicationController) er
}
}

if currentStatus != nextStatus || deploymentScaled {
if deployutil.CanTransitionPhase(currentStatus, nextStatus) || deploymentScaled {
deployment.Annotations[deployapi.DeploymentStatusAnnotation] = string(nextStatus)
if _, err := c.deploymentClient.updateDeployment(deployment.Namespace, deployment); err != nil {
if config, decodeErr := c.decodeConfig(deployment); decodeErr == nil {
Expand Down
27 changes: 27 additions & 0 deletions pkg/deploy/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,33 @@ func IsTerminatedDeployment(deployment *api.ReplicationController) bool {
return current == deployapi.DeploymentStatusComplete || current == deployapi.DeploymentStatusFailed
}

// CanTransitionPhase returns whether it is allowed to go from the current to the next phase.
func CanTransitionPhase(current, next deployapi.DeploymentStatus) bool {
switch current {
case deployapi.DeploymentStatusNew:
switch next {
case deployapi.DeploymentStatusPending,
deployapi.DeploymentStatusRunning,
deployapi.DeploymentStatusFailed,
deployapi.DeploymentStatusComplete:
return true
}
case deployapi.DeploymentStatusPending:
switch next {
case deployapi.DeploymentStatusRunning,
deployapi.DeploymentStatusFailed,
deployapi.DeploymentStatusComplete:
return true
}
case deployapi.DeploymentStatusRunning:
switch next {
case deployapi.DeploymentStatusFailed, deployapi.DeploymentStatusComplete:
return true
}
}
return false
}

// annotationFor returns the annotation with key for obj.
func annotationFor(obj runtime.Object, key string) string {
meta, err := api.ObjectMetaFor(obj)
Expand Down
166 changes: 166 additions & 0 deletions pkg/deploy/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,169 @@ func TestSort(t *testing.T) {
t.Errorf("Unexpected sort order")
}
}

func TestCanTransitionPhase(t *testing.T) {
tests := []struct {
name string
current, next deployapi.DeploymentStatus
expected bool
}{
{
name: "New->New",
current: deployapi.DeploymentStatusNew,
next: deployapi.DeploymentStatusNew,
expected: false,
},
{
name: "New->Pending",
current: deployapi.DeploymentStatusNew,
next: deployapi.DeploymentStatusPending,
expected: true,
},
{
name: "New->Running",
current: deployapi.DeploymentStatusNew,
next: deployapi.DeploymentStatusRunning,
expected: true,
},
{
name: "New->Complete",
current: deployapi.DeploymentStatusNew,
next: deployapi.DeploymentStatusComplete,
expected: true,
},
{
name: "New->Failed",
current: deployapi.DeploymentStatusNew,
next: deployapi.DeploymentStatusFailed,
expected: true,
},
{
name: "Pending->New",
current: deployapi.DeploymentStatusPending,
next: deployapi.DeploymentStatusNew,
expected: false,
},
{
name: "Pending->Pending",
current: deployapi.DeploymentStatusPending,
next: deployapi.DeploymentStatusPending,
expected: false,
},
{
name: "Pending->Running",
current: deployapi.DeploymentStatusPending,
next: deployapi.DeploymentStatusRunning,
expected: true,
},
{
name: "Pending->Failed",
current: deployapi.DeploymentStatusPending,
next: deployapi.DeploymentStatusFailed,
expected: true,
},
{
name: "Pending->Complete",
current: deployapi.DeploymentStatusPending,
next: deployapi.DeploymentStatusComplete,
expected: true,
},
{
name: "Running->New",
current: deployapi.DeploymentStatusRunning,
next: deployapi.DeploymentStatusNew,
expected: false,
},
{
name: "Running->Pending",
current: deployapi.DeploymentStatusRunning,
next: deployapi.DeploymentStatusPending,
expected: false,
},
{
name: "Running->Running",
current: deployapi.DeploymentStatusRunning,
next: deployapi.DeploymentStatusRunning,
expected: false,
},
{
name: "Running->Failed",
current: deployapi.DeploymentStatusRunning,
next: deployapi.DeploymentStatusFailed,
expected: true,
},
{
name: "Running->Complete",
current: deployapi.DeploymentStatusRunning,
next: deployapi.DeploymentStatusComplete,
expected: true,
},
{
name: "Complete->New",
current: deployapi.DeploymentStatusComplete,
next: deployapi.DeploymentStatusNew,
expected: false,
},
{
name: "Complete->Pending",
current: deployapi.DeploymentStatusComplete,
next: deployapi.DeploymentStatusPending,
expected: false,
},
{
name: "Complete->Running",
current: deployapi.DeploymentStatusComplete,
next: deployapi.DeploymentStatusRunning,
expected: false,
},
{
name: "Complete->Failed",
current: deployapi.DeploymentStatusComplete,
next: deployapi.DeploymentStatusFailed,
expected: false,
},
{
name: "Complete->Complete",
current: deployapi.DeploymentStatusComplete,
next: deployapi.DeploymentStatusComplete,
expected: false,
},
{
name: "Failed->New",
current: deployapi.DeploymentStatusFailed,
next: deployapi.DeploymentStatusNew,
expected: false,
},
{
name: "Failed->Pending",
current: deployapi.DeploymentStatusFailed,
next: deployapi.DeploymentStatusPending,
expected: false,
},
{
name: "Failed->Running",
current: deployapi.DeploymentStatusFailed,
next: deployapi.DeploymentStatusRunning,
expected: false,
},
{
name: "Failed->Complete",
current: deployapi.DeploymentStatusFailed,
next: deployapi.DeploymentStatusComplete,
expected: false,
},
{
name: "Failed->Failed",
current: deployapi.DeploymentStatusFailed,
next: deployapi.DeploymentStatusFailed,
expected: false,
},
}

for _, test := range tests {
got := CanTransitionPhase(test.current, test.next)
if got != test.expected {
t.Errorf("%s: expected %t, got %t", test.name, test.expected, got)
}
}
}