Skip to content
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
6 changes: 1 addition & 5 deletions pkg/cmd/infra/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func deploy(kClient kclient.Interface, namespace, deploymentName string) error {
}

// TODO: Choose a strategy based on some input
strategy := &strategy.DeploymentStrategy{
ReplicationController: strategy.RealReplicationController{KubeClient: kClient},
Codec: latest.Codec,
}

strategy := strategy.NewRecreateDeploymentStrategy(kClient, latest.Codec)
return strategy.Deploy(newDeployment, oldDeployments)
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/deploy/api/test/ok.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func OkImageChangeTrigger() deployapi.DeploymentTriggerPolicy {

func OkDeploymentConfig(version int) *deployapi.DeploymentConfig {
return &deployapi.DeploymentConfig{
ObjectMeta: kapi.ObjectMeta{Name: "config"},
ObjectMeta: kapi.ObjectMeta{
Namespace: kapi.NamespaceDefault,
Name: "config",
},
LatestVersion: version,
Triggers: []deployapi.DeploymentTriggerPolicy{
OkImageChangeTrigger(),
Expand Down
97 changes: 64 additions & 33 deletions pkg/deploy/strategy/recreate/recreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,63 @@ package recreate

import (
"fmt"
"time"

"github.com/golang/glog"

kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"

deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)

// DeploymentStrategy is a simple strategy appropriate as a default. Its behavior is to increase the
// RecreateDeploymentStrategy is a simple strategy appropriate as a default. Its behavior is to increase the
// replica count of the new deployment to 1, and to decrease the replica count of previous deployments
// to zero.
//
// A failure to disable any existing deployments will be considered a deployment failure.
type DeploymentStrategy struct {
// ReplicationController is used to interact with ReplicatonControllers.
ReplicationController ReplicationControllerInterface
// Codec is used to decode DeploymentConfigs contained in deployments.
Codec runtime.Codec
type RecreateDeploymentStrategy struct {
// client is used to interact with ReplicatonControllers.
client replicationControllerClient
// codec is used to decode DeploymentConfigs contained in deployments.
codec runtime.Codec

retryTimeout time.Duration
retryPeriod time.Duration
}

func NewRecreateDeploymentStrategy(client kclient.Interface, codec runtime.Codec) *RecreateDeploymentStrategy {
return &RecreateDeploymentStrategy{
client: &realReplicationController{client},
codec: codec,
retryTimeout: 10 * time.Second,
retryPeriod: 1 * time.Second,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you get a conflict you should read and retry immediately. You don't need timeouts and waits for that type of error (for the others, retry is appropriate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I now made the retry logic continue immediately when IsConflict. Timeout is still effective, though. Seems like if there's enough thrashing on an RC that you can't get an update in with tight-loop attempts over 10 seconds, something somewhere's probably very wrong and we should fail anyway. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm fine with that for now. We just don't need a retry period on a conflict.

----- Original Message -----

+type RecreateDeploymentStrategy struct {

  • // client is used to interact with ReplicatonControllers.
  • client replicationControllerClient
  • // codec is used to decode DeploymentConfigs contained in deployments.
  • codec runtime.Codec
  • retryTimeout time.Duration
  • retryPeriod time.Duration
    +}

+func NewRecreateDeploymentStrategy(client kclient.Interface, codec
runtime.Codec) *RecreateDeploymentStrategy {

  • return &RecreateDeploymentStrategy{
  •   client:       &realReplicationController{client},
    
  •   codec:        codec,
    
  •   retryTimeout: 10 \* time.Second,
    
  •   retryPeriod:  1 \* time.Second,
    

I made the retry logic continue immediately when IsConflict. Timeout is
still effective, though. Seems like if there's enough thrashing on an RC
that you can't get an update in with tight-loop attempts over 10 seconds,
something somewhere's probably very wrong and we should fail anyway. What do
you think?


Reply to this email directly or view it on GitHub:
https://github.com/openshift/origin/pull/846/files#r24012945

}
}

// Deploy makes deployment active and disables oldDeployments.
func (s *DeploymentStrategy) Deploy(deployment *kapi.ReplicationController, oldDeployments []kapi.ObjectReference) error {
func (s *RecreateDeploymentStrategy) Deploy(deployment *kapi.ReplicationController, oldDeployments []kapi.ObjectReference) error {
var err error
var deploymentConfig *deployapi.DeploymentConfig

if deploymentConfig, err = deployutil.DecodeDeploymentConfig(deployment, s.Codec); err != nil {
if deploymentConfig, err = deployutil.DecodeDeploymentConfig(deployment, s.codec); err != nil {
return fmt.Errorf("Couldn't decode DeploymentConfig from deployment %s: %v", deployment.Name, err)
}

deployment.Spec.Replicas = deploymentConfig.Template.ControllerTemplate.Replicas
glog.Infof("Updating deployment %s replica count to %d", deployment.Name, deployment.Spec.Replicas)
if _, err := s.ReplicationController.updateReplicationController(deployment.Namespace, deployment); err != nil {
return fmt.Errorf("Error updating deployment %s replica count to %d: %v", deployment.Name, deployment.Spec.Replicas, err)
if err = s.updateReplicas(deployment.Namespace, deployment.Name, deploymentConfig.Template.ControllerTemplate.Replicas); err != nil {
return err
}

// For this simple deploy, disable previous replication controllers.
// TODO: This isn't transactional, and we don't actually wait for the replica count to
// become zero before deleting them.
glog.Infof("Found %d prior deployments to disable", len(oldDeployments))
allProcessed := true
for _, oldDeployment := range oldDeployments {
oldController, oldErr := s.ReplicationController.getReplicationController(oldDeployment.Namespace, oldDeployment.Name)
if oldErr != nil {
glog.Errorf("Error getting old deployment %s for disabling: %v", oldDeployment.Name, oldErr)
allProcessed = false
continue
}

glog.Infof("Setting replicas to zero for old deployment %s", oldController.Name)
oldController.Spec.Replicas = 0
if _, err := s.ReplicationController.updateReplicationController(oldController.Namespace, oldController); err != nil {
glog.Errorf("Error updating replicas to zero for old deployment %s: %#v", oldController.Name, err)
if err = s.updateReplicas(oldDeployment.Namespace, oldDeployment.Name, 0); err != nil {
glog.Errorf("%v", err)
allProcessed = false
continue
}
}

Expand All @@ -70,19 +70,50 @@ func (s *DeploymentStrategy) Deploy(deployment *kapi.ReplicationController, oldD
return nil
}

type ReplicationControllerInterface interface {
// updateReplicas attempts to set the given deployment's replicaCount using retry logic.
func (s *RecreateDeploymentStrategy) updateReplicas(namespace, name string, replicaCount int) error {
var err error
var deployment *kapi.ReplicationController

timeout := time.After(s.retryTimeout)
for {
select {
case <-timeout:
return fmt.Errorf("Couldn't successfully update deployment %s replica count to %d (timeout exceeded)", deployment.Name, replicaCount)
default:
if deployment, err = s.client.getReplicationController(namespace, name); err != nil {
glog.Errorf("Couldn't get deployment %s/%s: %v", namespace, name, err)
} else {
deployment.Spec.Replicas = replicaCount
glog.Infof("Updating deployment %s/%s replica count to %d", namespace, name, replicaCount)
if _, err = s.client.updateReplicationController(namespace, deployment); err == nil {
return nil
}
// For conflict errors, retry immediately
if kerrors.IsConflict(err) {
continue
}
glog.Errorf("Error updating deployment %s/%s replica count to %d: %v", namespace, name, replicaCount, err)
}

time.Sleep(s.retryPeriod)
}
}
}

type replicationControllerClient interface {
getReplicationController(namespace, name string) (*kapi.ReplicationController, error)
updateReplicationController(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error)
}

type RealReplicationController struct {
KubeClient kclient.Interface
type realReplicationController struct {
client kclient.Interface
}

func (r RealReplicationController) getReplicationController(namespace string, name string) (*kapi.ReplicationController, error) {
return r.KubeClient.ReplicationControllers(namespace).Get(name)
func (r realReplicationController) getReplicationController(namespace string, name string) (*kapi.ReplicationController, error) {
return r.client.ReplicationControllers(namespace).Get(name)
}

func (r RealReplicationController) updateReplicationController(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
return r.KubeClient.ReplicationControllers(namespace).Update(ctrl)
func (r realReplicationController) updateReplicationController(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
return r.client.ReplicationControllers(namespace).Update(ctrl)
}
145 changes: 126 additions & 19 deletions pkg/deploy/strategy/recreate/recreate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package recreate

import (
"fmt"
"testing"
"time"

kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"

Expand All @@ -14,12 +16,13 @@ func TestFirstDeployment(t *testing.T) {
var updatedController *kapi.ReplicationController
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)

strategy := &DeploymentStrategy{
Codec: api.Codec,
ReplicationController: &testControllerClient{
strategy := &RecreateDeploymentStrategy{
codec: api.Codec,
retryTimeout: 1 * time.Second,
retryPeriod: 1 * time.Millisecond,
client: &testControllerClient{
getReplicationControllerFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
t.Fatalf("unexpected call to getReplicationController")
return nil, nil
return deployment, nil
},
updateReplicationControllerFunc: func(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
updatedController = ctrl
Expand All @@ -43,33 +46,41 @@ func TestFirstDeployment(t *testing.T) {
}
}

func TestSecondDeployment(t *testing.T) {
func TestSecondDeploymentSuccessfulRetries(t *testing.T) {
updatedControllers := make(map[string]*kapi.ReplicationController)
oldDeployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
newConfig := deploytest.OkDeploymentConfig(2)
newDeployment, _ := deployutil.MakeDeployment(newConfig, kapi.Codec)

strategy := &DeploymentStrategy{
Codec: api.Codec,
ReplicationController: &testControllerClient{
getReplicationControllerFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
if e, a := oldDeployment.Namespace, namespace; e != a {
t.Fatalf("expected getReplicationController call with %s, got %s", e, a)
}
errorCounts := map[string]int{}

if e, a := oldDeployment.Name, name; e != a {
t.Fatalf("expected getReplicationController call with %s, got %s", e, a)
strategy := &RecreateDeploymentStrategy{
codec: api.Codec,
retryTimeout: 1 * time.Second,
retryPeriod: 1 * time.Millisecond,
client: &testControllerClient{
getReplicationControllerFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
switch name {
case oldDeployment.Name:
return oldDeployment, nil
case newDeployment.Name:
return newDeployment, nil
default:
t.Fatalf("unexpected call to getReplicationController: %s/%s", namespace, name)
return nil, nil
}
return oldDeployment, nil
},
updateReplicationControllerFunc: func(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
if errorCounts[ctrl.Name] < 3 {
errorCounts[ctrl.Name] = errorCounts[ctrl.Name] + 1
return nil, fmt.Errorf("test error %d", errorCounts[ctrl.Name])
}
updatedControllers[ctrl.Name] = ctrl
return ctrl, nil
},
},
}

newConfig := deploytest.OkDeploymentConfig(2)
newDeployment, _ := deployutil.MakeDeployment(newConfig, kapi.Codec)

err := strategy.Deploy(newDeployment, []kapi.ObjectReference{
{
Namespace: oldDeployment.Namespace,
Expand All @@ -90,6 +101,102 @@ func TestSecondDeployment(t *testing.T) {
}
}

func TestSecondDeploymentFailedInitialRetries(t *testing.T) {
updatedControllers := make(map[string]*kapi.ReplicationController)
oldDeployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
newConfig := deploytest.OkDeploymentConfig(2)
newDeployment, _ := deployutil.MakeDeployment(newConfig, kapi.Codec)

strategy := &RecreateDeploymentStrategy{
codec: api.Codec,
retryTimeout: 1 * time.Millisecond,
retryPeriod: 1 * time.Millisecond,
client: &testControllerClient{
getReplicationControllerFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
switch name {
case oldDeployment.Name:
return oldDeployment, nil
case newDeployment.Name:
return newDeployment, nil
default:
t.Fatalf("unexpected call to getReplicationController: %s/%s", namespace, name)
return nil, nil
}
},
updateReplicationControllerFunc: func(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
return nil, fmt.Errorf("update failure")
},
},
}

err := strategy.Deploy(newDeployment, []kapi.ObjectReference{
{
Namespace: oldDeployment.Namespace,
Name: oldDeployment.Name,
},
})

if err == nil {
t.Fatalf("expected a deploy error: %#v", err)
}

if len(updatedControllers) > 0 {
t.Fatalf("unexpected controller updates: %v", updatedControllers)
}
}

func TestSecondDeploymentFailedDisableRetries(t *testing.T) {
updatedControllers := make(map[string]*kapi.ReplicationController)
oldDeployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
newConfig := deploytest.OkDeploymentConfig(2)
newDeployment, _ := deployutil.MakeDeployment(newConfig, kapi.Codec)

strategy := &RecreateDeploymentStrategy{
codec: api.Codec,
retryTimeout: 1 * time.Millisecond,
retryPeriod: 1 * time.Millisecond,
client: &testControllerClient{
getReplicationControllerFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
switch name {
case oldDeployment.Name:
return oldDeployment, nil
case newDeployment.Name:
return newDeployment, nil
default:
t.Fatalf("unexpected call to getReplicationController: %s/%s", namespace, name)
return nil, nil
}
},
updateReplicationControllerFunc: func(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error) {
switch ctrl.Name {
case newDeployment.Name:
return newDeployment, nil
case oldDeployment.Name:
return nil, fmt.Errorf("update error")
default:
t.Fatalf("unexpected call to getReplicationController: %s/%s", namespace, ctrl.Name)
return nil, nil
}
},
},
}

err := strategy.Deploy(newDeployment, []kapi.ObjectReference{
{
Namespace: oldDeployment.Namespace,
Name: oldDeployment.Name,
},
})

if err == nil {
t.Fatalf("expcted a deploy error: %#v", err)
}

if len(updatedControllers) > 0 {
t.Fatalf("unexpected controller updates: %v", updatedControllers)
}
}

type testControllerClient struct {
getReplicationControllerFunc func(namespace, name string) (*kapi.ReplicationController, error)
updateReplicationControllerFunc func(namespace string, ctrl *kapi.ReplicationController) (*kapi.ReplicationController, error)
Expand Down