forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment_config_controller.go
92 lines (78 loc) · 3.38 KB
/
deployment_config_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package controller
import (
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// DeploymentConfigController is responsible for creating a deployment when a DeploymentConfig is
// updated with a new LatestVersion. Any deployment created is correlated to a DeploymentConfig
// by setting the DeploymentConfigLabel on the deployment.
//
// Deployments are represented by ReplicationControllers. The DeploymentConfig used to create the
// ReplicationController is encoded and stored in an annotation on the ReplicationController.
type DeploymentConfigController struct {
// DeploymentInterface provides access to Deployments.
DeploymentInterface dccDeploymentInterface
// NextDeploymentConfig blocks until the next DeploymentConfig is available.
NextDeploymentConfig func() *deployapi.DeploymentConfig
// Codec is used to encode DeploymentConfigs which are stored on deployments.
Codec runtime.Codec
// Stop is an optional channel that controls when the controller exits.
Stop <-chan struct{}
}
// dccDeploymentInterface is a small private interface for dealing with Deployments.
type dccDeploymentInterface interface {
GetDeployment(namespace, name string) (*kapi.ReplicationController, error)
CreateDeployment(namespace string, deployment *kapi.ReplicationController) (*kapi.ReplicationController, error)
}
// Process DeploymentConfig events one at a time.
func (c *DeploymentConfigController) Run() {
go util.Until(c.HandleDeploymentConfig, 0, c.Stop)
}
// Process a single DeploymentConfig event.
func (c *DeploymentConfigController) HandleDeploymentConfig() {
config := c.NextDeploymentConfig()
deploy, err := c.shouldDeploy(config)
if err != nil {
util.HandleError(fmt.Errorf("unable to decide whether to redeploy %s: %v", labelFor(config), err))
return
}
if !deploy {
return
}
deployment, err := deployutil.MakeDeployment(config, c.Codec)
if err != nil {
util.HandleError(fmt.Errorf("unable to create deployment for %s: %v", labelFor(config), err))
return
}
glog.V(4).Infof("Deploying %s", labelFor(config))
if _, deployErr := c.DeploymentInterface.CreateDeployment(config.Namespace, deployment); deployErr != nil {
util.HandleError(fmt.Errorf("unable to create deployment %s: %v", labelFor(config), err))
return
}
}
// shouldDeploy returns true if the DeploymentConfig should have a new Deployment created.
func (c *DeploymentConfigController) shouldDeploy(config *deployapi.DeploymentConfig) (bool, error) {
if config.LatestVersion == 0 {
glog.V(5).Infof("Waiting for first version of %s", labelFor(config))
return false, nil
}
latestDeploymentID := deployutil.LatestDeploymentNameForConfig(config)
deployment, err := c.DeploymentInterface.GetDeployment(config.Namespace, latestDeploymentID)
if err != nil {
if errors.IsNotFound(err) {
return true, nil
}
return false, err
}
glog.V(5).Infof("Found deployment for %s - %s:%s", labelFor(config), deployment.UID, deployment.ResourceVersion)
return false, nil
}
func labelFor(config *deployapi.DeploymentConfig) string {
return fmt.Sprintf("%s/%s:%d", config.Namespace, config.Name, config.LatestVersion)
}