@@ -2,16 +2,18 @@ package deployer
2
2
3
3
import (
4
4
"fmt"
5
+ "io"
6
+ "os"
5
7
"sort"
6
8
"time"
7
9
8
- "github.com/golang/glog"
9
10
"github.com/spf13/cobra"
10
11
11
12
kapi "k8s.io/kubernetes/pkg/api"
12
13
"k8s.io/kubernetes/pkg/client/restclient"
13
14
kclient "k8s.io/kubernetes/pkg/client/unversioned"
14
15
"k8s.io/kubernetes/pkg/kubectl"
16
+ kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
15
17
16
18
"github.com/openshift/origin/pkg/client"
17
19
"github.com/openshift/origin/pkg/cmd/util"
@@ -32,8 +34,10 @@ This command launches a deployment as described by a deployment configuration.`
32
34
)
33
35
34
36
type config struct {
35
- DeploymentName string
36
- Namespace string
37
+ Out , ErrOut io.Writer
38
+
39
+ rcName string
40
+ Namespace string
37
41
}
38
42
39
43
// NewCommandDeployer provides a CLI handler for deploy.
@@ -45,46 +49,53 @@ func NewCommandDeployer(name string) *cobra.Command {
45
49
Short : "Run the deployer" ,
46
50
Long : deployerLong ,
47
51
Run : func (c * cobra.Command , args []string ) {
48
- if len (cfg .DeploymentName ) == 0 {
49
- glog .Fatal ("deployment is required" )
50
- }
51
- if len (cfg .Namespace ) == 0 {
52
- glog .Fatal ("namespace is required" )
53
- }
54
-
55
- kcfg , err := restclient .InClusterConfig ()
56
- if err != nil {
57
- glog .Fatal (err )
58
- }
59
- kc , err := kclient .New (kcfg )
60
- if err != nil {
61
- glog .Fatal (err )
62
- }
63
- oc , err := client .New (kcfg )
64
- if err != nil {
65
- glog .Fatal (err )
66
- }
67
-
68
- deployer := NewDeployer (kc , oc )
69
- if err = deployer .Deploy (cfg .Namespace , cfg .DeploymentName ); err != nil {
70
- glog .Fatal (err )
71
- }
52
+ cfg .Out = os .Stdout
53
+ cfg .ErrOut = c .Out ()
54
+ err := cfg .RunDeployer ()
55
+ kcmdutil .CheckErr (err )
72
56
},
73
57
}
74
58
75
59
cmd .AddCommand (version .NewVersionCommand (name , false ))
76
60
77
61
flag := cmd .Flags ()
78
- flag .StringVar (& cfg .DeploymentName , "deployment" , util .Env ("OPENSHIFT_DEPLOYMENT_NAME" , "" ), "The deployment name to start" )
62
+ flag .StringVar (& cfg .rcName , "deployment" , util .Env ("OPENSHIFT_DEPLOYMENT_NAME" , "" ), "The deployment name to start" )
79
63
flag .StringVar (& cfg .Namespace , "namespace" , util .Env ("OPENSHIFT_DEPLOYMENT_NAMESPACE" , "" ), "The deployment namespace" )
80
64
81
65
return cmd
82
66
}
83
67
68
+ func (cfg * config ) RunDeployer () error {
69
+ if len (cfg .rcName ) == 0 {
70
+ return fmt .Errorf ("--deployment or OPENSHIFT_DEPLOYMENT_NAME is required" )
71
+ }
72
+ if len (cfg .Namespace ) == 0 {
73
+ return fmt .Errorf ("--namespace or OPENSHIFT_DEPLOYMENT_NAMESPACE is required" )
74
+ }
75
+
76
+ kcfg , err := restclient .InClusterConfig ()
77
+ if err != nil {
78
+ return err
79
+ }
80
+ kc , err := kclient .New (kcfg )
81
+ if err != nil {
82
+ return err
83
+ }
84
+ oc , err := client .New (kcfg )
85
+ if err != nil {
86
+ return err
87
+ }
88
+
89
+ deployer := NewDeployer (kc , oc , cfg .Out , cfg .ErrOut )
90
+ return deployer .Deploy (cfg .Namespace , cfg .rcName )
91
+ }
92
+
84
93
// NewDeployer makes a new Deployer from a kube client.
85
- func NewDeployer (client kclient.Interface , oclient client.Interface ) * Deployer {
94
+ func NewDeployer (client kclient.Interface , oclient client.Interface , out , errOut io. Writer ) * Deployer {
86
95
scaler , _ := kubectl .ScalerFor (kapi .Kind ("ReplicationController" ), client )
87
96
return & Deployer {
97
+ out : out ,
98
+ errOut : errOut ,
88
99
getDeployment : func (namespace , name string ) (* kapi.ReplicationController , error ) {
89
100
return client .ReplicationControllers (namespace ).Get (name )
90
101
},
@@ -95,10 +106,10 @@ func NewDeployer(client kclient.Interface, oclient client.Interface) *Deployer {
95
106
strategyFor : func (config * deployapi.DeploymentConfig ) (strategy.DeploymentStrategy , error ) {
96
107
switch config .Spec .Strategy .Type {
97
108
case deployapi .DeploymentStrategyTypeRecreate :
98
- return recreate .NewRecreateDeploymentStrategy (client , oclient , kapi .Codecs .UniversalDecoder ()), nil
109
+ return recreate .NewRecreateDeploymentStrategy (client , oclient , kapi .Codecs .UniversalDecoder (), out , errOut ), nil
99
110
case deployapi .DeploymentStrategyTypeRolling :
100
- recreate := recreate .NewRecreateDeploymentStrategy (client , oclient , kapi .Codecs .UniversalDecoder ())
101
- return rolling .NewRollingDeploymentStrategy (config .Namespace , client , oclient , kapi .Codecs .UniversalDecoder (), recreate ), nil
111
+ recreate := recreate .NewRecreateDeploymentStrategy (client , oclient , kapi .Codecs .UniversalDecoder (), out , errOut )
112
+ return rolling .NewRollingDeploymentStrategy (config .Namespace , client , oclient , kapi .Codecs .UniversalDecoder (), recreate , out , errOut ), nil
102
113
default :
103
114
return nil , fmt .Errorf ("unsupported strategy type: %s" , config .Spec .Strategy .Type )
104
115
}
@@ -115,6 +126,8 @@ func NewDeployer(client kclient.Interface, oclient client.Interface) *Deployer {
115
126
// 4. Pass the last completed deployment and the new deployment to a strategy
116
127
// to perform the deployment.
117
128
type Deployer struct {
129
+ // out and errOut control display when deploy is invoked
130
+ out , errOut io.Writer
118
131
// strategyFor returns a DeploymentStrategy for config.
119
132
strategyFor func (config * deployapi.DeploymentConfig ) (strategy.DeploymentStrategy , error )
120
133
// getDeployment finds the named deployment.
@@ -125,18 +138,18 @@ type Deployer struct {
125
138
scaler kubectl.Scaler
126
139
}
127
140
128
- // Deploy starts the deployment process for deploymentName .
129
- func (d * Deployer ) Deploy (namespace , deploymentName string ) error {
141
+ // Deploy starts the deployment process for rcName .
142
+ func (d * Deployer ) Deploy (namespace , rcName string ) error {
130
143
// Look up the new deployment.
131
- to , err := d .getDeployment (namespace , deploymentName )
144
+ to , err := d .getDeployment (namespace , rcName )
132
145
if err != nil {
133
- return fmt .Errorf ("couldn't get deployment %s/%s : %v" , namespace , deploymentName , err )
146
+ return fmt .Errorf ("couldn't get deployment %s: %v" , rcName , err )
134
147
}
135
148
136
149
// Decode the config from the deployment.
137
150
config , err := deployutil .DecodeDeploymentConfig (to , kapi .Codecs .UniversalDecoder ())
138
151
if err != nil {
139
- return fmt .Errorf ("couldn't decode deployment config from deployment %s/%s : %v" , to . Namespace , to .Name , err )
152
+ return fmt .Errorf ("couldn't decode deployment config from deployment %s: %v" , to .Name , err )
140
153
}
141
154
142
155
// Get a strategy for the deployment.
@@ -148,7 +161,7 @@ func (d *Deployer) Deploy(namespace, deploymentName string) error {
148
161
// New deployments must have a desired replica count.
149
162
desiredReplicas , hasDesired := deployutil .DeploymentDesiredReplicas (to )
150
163
if ! hasDesired {
151
- return fmt .Errorf ("deployment %s has no desired replica count " , deployutil . LabelForDeployment ( to ) )
164
+ return fmt .Errorf ("deployment %s has already run to completion " , to . Name )
152
165
}
153
166
154
167
// Find all deployments for the config.
@@ -189,17 +202,16 @@ func (d *Deployer) Deploy(namespace, deploymentName string) error {
189
202
// Scale the deployment down to zero.
190
203
retryWaitParams := kubectl .NewRetryParams (1 * time .Second , 120 * time .Second )
191
204
if err := d .scaler .Scale (candidate .Namespace , candidate .Name , uint (0 ), & kubectl.ScalePrecondition {Size : - 1 , ResourceVersion : "" }, retryWaitParams , retryWaitParams ); err != nil {
192
- glog . Errorf ( " Couldn't scale down prior deployment %s: %v" , deployutil .LabelForDeployment (& candidate ), err )
205
+ fmt . Fprintf ( d . errOut , "error: Couldn't scale down prior deployment %s: %v\n " , deployutil .LabelForDeployment (& candidate ), err )
193
206
} else {
194
- glog . Infof ( " Scaled down prior deployment %s" , deployutil . LabelForDeployment ( & candidate ) )
207
+ fmt . Fprintf ( d . out , "--> Scaled older deployment %s down \n " , candidate . Name )
195
208
}
196
209
}
197
210
198
211
// Perform the deployment.
199
- if from == nil {
200
- glog .Infof ("Deploying %s for the first time (replicas: %d)" , deployutil .LabelForDeployment (to ), desiredReplicas )
201
- } else {
202
- glog .Infof ("Deploying from %s to %s (replicas: %d)" , deployutil .LabelForDeployment (from ), deployutil .LabelForDeployment (to ), desiredReplicas )
212
+ if err := strategy .Deploy (from , to , desiredReplicas ); err != nil {
213
+ return err
203
214
}
204
- return strategy .Deploy (from , to , desiredReplicas )
215
+ fmt .Fprintf (d .out , "--> Success\n " )
216
+ return nil
205
217
}
0 commit comments