forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
124 lines (115 loc) · 4.78 KB
/
factory.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package deployment
import (
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
kutil "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
controller "github.com/openshift/origin/pkg/controller"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
// DeploymentControllerFactory can create a DeploymentController that creates
// deployer pods in a configurable way.
type DeploymentControllerFactory struct {
// KubeClient is a Kubernetes client.
KubeClient kclient.Interface
// Codec is used for encoding/decoding.
Codec runtime.Codec
// Environment is a set of environment which should be injected into all deployer pod containers.
Environment []kapi.EnvVar
// RecreateStrategyImage specifies which Docker image which should implement the Recreate strategy.
RecreateStrategyImage string
}
// Create creates a DeploymentController.
func (factory *DeploymentControllerFactory) Create() controller.RunnableController {
deploymentLW := &deployutil.ListWatcherImpl{
ListFunc: func() (runtime.Object, error) {
return factory.KubeClient.ReplicationControllers(kapi.NamespaceAll).List(labels.Everything())
},
WatchFunc: func(resourceVersion string) (watch.Interface, error) {
return factory.KubeClient.ReplicationControllers(kapi.NamespaceAll).Watch(labels.Everything(), labels.Everything(), resourceVersion)
},
}
deploymentQueue := cache.NewFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(deploymentLW, &kapi.ReplicationController{}, deploymentQueue).Run()
deployController := &DeploymentController{
deploymentClient: &deploymentClientImpl{
getDeploymentFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
return factory.KubeClient.ReplicationControllers(namespace).Get(name)
},
updateDeploymentFunc: func(namespace string, deployment *kapi.ReplicationController) (*kapi.ReplicationController, error) {
return factory.KubeClient.ReplicationControllers(namespace).Update(deployment)
},
},
podClient: &podClientImpl{
createPodFunc: func(namespace string, pod *kapi.Pod) (*kapi.Pod, error) {
return factory.KubeClient.Pods(namespace).Create(pod)
},
deletePodFunc: func(namespace, name string) error {
return factory.KubeClient.Pods(namespace).Delete(name)
},
},
makeContainer: func(strategy *deployapi.DeploymentStrategy) (*kapi.Container, error) {
return factory.makeContainer(strategy)
},
decodeConfig: func(deployment *kapi.ReplicationController) (*deployapi.DeploymentConfig, error) {
return deployutil.DecodeDeploymentConfig(deployment, factory.Codec)
},
}
return &controller.RetryController{
Queue: deploymentQueue,
RetryManager: controller.NewQueueRetryManager(deploymentQueue, cache.MetaNamespaceKeyFunc, 1),
ShouldRetry: func(obj interface{}, err error) bool {
if _, isFatal := err.(fatalError); isFatal {
kutil.HandleError(err)
return false
}
return true
},
Handle: func(obj interface{}) error {
deployment := obj.(*kapi.ReplicationController)
return deployController.Handle(deployment)
},
}
}
// makeContainer creates containers in the following way:
//
// 1. For the Recreate strategy, use the factory's RecreateStrategyImage as
// the container image, and the factory's Environment as the container
// environment.
// 2. For all Custom strategy, use the strategy's image for the container
// image, and use the combination of the factory's Environment and the
// strategy's environment as the container environment.
//
// An error is returned if the deployment strategy type is not supported.
func (factory *DeploymentControllerFactory) makeContainer(strategy *deployapi.DeploymentStrategy) (*kapi.Container, error) {
// Set default environment values
environment := []kapi.EnvVar{}
for _, env := range factory.Environment {
environment = append(environment, env)
}
// Every strategy type should be handled here.
switch strategy.Type {
case deployapi.DeploymentStrategyTypeRecreate:
// Use the factory-configured image.
return &kapi.Container{
Image: factory.RecreateStrategyImage,
Env: environment,
}, nil
case deployapi.DeploymentStrategyTypeCustom:
// Use user-defined values from the strategy input.
for _, env := range strategy.CustomParams.Environment {
environment = append(environment, env)
}
return &kapi.Container{
Image: strategy.CustomParams.Image,
Env: environment,
}, nil
default:
return nil, fmt.Errorf("unsupported deployment strategy type: %s", strategy.Type)
}
}