1
1
package cmd
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"io"
6
7
7
8
"github.com/spf13/cobra"
8
9
9
10
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
10
11
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
12
+ kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
11
13
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
12
14
15
+ "github.com/openshift/origin/pkg/client"
13
16
"github.com/openshift/origin/pkg/cmd/cli/describe"
14
17
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
15
18
deployapi "github.com/openshift/origin/pkg/deploy/api"
16
19
deployutil "github.com/openshift/origin/pkg/deploy/util"
17
20
)
18
21
22
+ type DeployOptions struct {
23
+ out io.Writer
24
+ osClient * client.Client
25
+ kubeClient * kclient.Client
26
+ namespace string
27
+ baseCommandName string
28
+
29
+ deploymentConfigName string
30
+ deployLatest bool
31
+ retryDeploy bool
32
+ }
33
+
19
34
const (
20
35
deploy_long = `View, start and restart deployments.
21
36
@@ -26,74 +41,114 @@ NOTE: This command is still under active development and is subject to change.`
26
41
deploy_example = ` // Display the latest deployment for the 'database' deployment config
27
42
$ %[1]s deploy database
28
43
29
- // Start a new deployment based on the 'frontend' deployment config
30
- $ %[1]s deploy frontend --latest`
44
+ // Start a new deployment based on the 'database' deployment config
45
+ $ %[1]s deploy frontend --latest
46
+
47
+ // Retry the latest failed deployment based on the 'frontend' deployment config
48
+ $ %[1]s deploy frontend --retry`
31
49
)
32
50
33
51
// NewCmdDeploy creates a new `deploy` command.
34
52
func NewCmdDeploy (fullName string , f * clientcmd.Factory , out io.Writer ) * cobra.Command {
35
- var deployLatest bool
36
- var retryDeploy bool
53
+ options := & DeployOptions {
54
+ baseCommandName : fullName ,
55
+ }
37
56
38
57
cmd := & cobra.Command {
39
58
Use : "deploy DEPLOYMENTCONFIG" ,
40
59
Short : "View, start and restart deployments." ,
41
60
Long : deploy_long ,
42
61
Example : fmt .Sprintf (deploy_example , fullName ),
43
62
Run : func (cmd * cobra.Command , args []string ) {
44
- if len (args ) == 0 || len (args [0 ]) == 0 {
45
- fmt .Println (cmdutil .UsageError (cmd , "A deploymentConfig name is required." ))
46
- return
63
+ if err := options .Complete (f , args , out ); err != nil {
64
+ cmdutil .CheckErr (err )
47
65
}
48
- if deployLatest && retryDeploy {
49
- fmt . Println ( cmdutil . UsageError ( cmd , "Only one of --latest or --retry is allowed." ))
50
- return
66
+
67
+ if err := options . Validate ( args ); err != nil {
68
+ cmdutil . CheckErr ( cmdutil . UsageError ( cmd , err . Error ()))
51
69
}
52
70
53
- configName := args [0 ]
71
+ if err := options .RunDeploy (); err != nil {
72
+ cmdutil .CheckErr (err )
73
+ }
74
+ },
75
+ }
54
76
55
- osClient , kubeClient , err := f . Clients ( )
56
- cmdutil . CheckErr ( err )
77
+ cmd . Flags (). BoolVar ( & options . deployLatest , "latest" , false , "Start a new deployment now." )
78
+ cmd . Flags (). BoolVar ( & options . retryDeploy , "retry" , false , "Retry the latest failed deployment." )
57
79
58
- namespace , err := f . DefaultNamespace ()
59
- cmdutil . CheckErr ( err )
80
+ return cmd
81
+ }
60
82
61
- config , err := osClient . DeploymentConfigs ( namespace ). Get ( configName )
62
- cmdutil . CheckErr ( err )
83
+ func ( o * DeployOptions ) Complete ( f * clientcmd. Factory , args [] string , out io. Writer ) error {
84
+ var err error
63
85
64
- commandClient := & deployCommandClientImpl {
65
- GetDeploymentFn : func (namespace , name string ) (* kapi.ReplicationController , error ) {
66
- return kubeClient .ReplicationControllers (namespace ).Get (name )
67
- },
68
- UpdateDeploymentConfigFn : func (config * deployapi.DeploymentConfig ) (* deployapi.DeploymentConfig , error ) {
69
- return osClient .DeploymentConfigs (config .Namespace ).Update (config )
70
- },
71
- UpdateDeploymentFn : func (deployment * kapi.ReplicationController ) (* kapi.ReplicationController , error ) {
72
- return kubeClient .ReplicationControllers (deployment .Namespace ).Update (deployment )
73
- },
74
- }
86
+ o .osClient , o .kubeClient , err = f .Clients ()
87
+ if err != nil {
88
+ return err
89
+ }
90
+ o .namespace , err = f .DefaultNamespace ()
91
+ if err != nil {
92
+ return err
93
+ }
75
94
76
- switch {
77
- case deployLatest :
78
- c := & deployLatestCommand {client : commandClient }
79
- err = c .deploy (config , out )
80
- case retryDeploy :
81
- c := & retryDeploymentCommand {client : commandClient }
82
- err = c .retry (config , out )
83
- default :
84
- describer := describe .NewLatestDeploymentDescriber (osClient , kubeClient )
85
- desc , err := describer .Describe (config .Namespace , config .Name )
86
- cmdutil .CheckErr (err )
87
- fmt .Fprintln (out , desc )
88
- }
89
- cmdutil .CheckErr (err )
95
+ o .out = out
96
+
97
+ if len (args ) > 0 {
98
+ o .deploymentConfigName = args [0 ]
99
+ }
100
+
101
+ return nil
102
+ }
103
+
104
+ func (o DeployOptions ) Validate (args []string ) error {
105
+ if len (args ) == 0 || len (args [0 ]) == 0 {
106
+ return errors .New ("A deploymentConfig name is required." )
107
+ }
108
+ if len (args ) > 1 {
109
+ return errors .New ("Only one deploymentConfig name is supported as argument." )
110
+ }
111
+ if o .deployLatest && o .retryDeploy {
112
+ return errors .New ("Only one of --latest or --retry is allowed." )
113
+ }
114
+ return nil
115
+ }
116
+
117
+ func (o DeployOptions ) RunDeploy () error {
118
+ config , err := o .osClient .DeploymentConfigs (o .namespace ).Get (o .deploymentConfigName )
119
+ if err != nil {
120
+ return err
121
+ }
122
+
123
+ commandClient := & deployCommandClientImpl {
124
+ GetDeploymentFn : func (namespace , name string ) (* kapi.ReplicationController , error ) {
125
+ return o .kubeClient .ReplicationControllers (namespace ).Get (name )
126
+ },
127
+ UpdateDeploymentConfigFn : func (config * deployapi.DeploymentConfig ) (* deployapi.DeploymentConfig , error ) {
128
+ return o .osClient .DeploymentConfigs (config .Namespace ).Update (config )
129
+ },
130
+ UpdateDeploymentFn : func (deployment * kapi.ReplicationController ) (* kapi.ReplicationController , error ) {
131
+ return o .kubeClient .ReplicationControllers (deployment .Namespace ).Update (deployment )
90
132
},
91
133
}
92
134
93
- cmd .Flags ().BoolVar (& deployLatest , "latest" , false , "Start a new deployment now." )
94
- cmd .Flags ().BoolVar (& retryDeploy , "retry" , false , "Retry the latest failed deployment." )
135
+ switch {
136
+ case o .deployLatest :
137
+ c := & deployLatestCommand {client : commandClient }
138
+ err = c .deploy (config , o .out )
139
+ case o .retryDeploy :
140
+ c := & retryDeploymentCommand {client : commandClient }
141
+ err = c .retry (config , o .out )
142
+ default :
143
+ describer := describe .NewLatestDeploymentsDescriber (o .osClient , o .kubeClient , - 1 )
144
+ desc , err := describer .Describe (config .Namespace , config .Name )
145
+ if err != nil {
146
+ return err
147
+ }
148
+ fmt .Fprintln (o .out , desc )
149
+ }
95
150
96
- return cmd
151
+ return err
97
152
}
98
153
99
154
// deployCommandClient abstracts access to the API server.
0 commit comments