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,92 +41,115 @@ 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
- // List all deployments for the 'database' deployment config
30
- $ %[1]s deploy database -L
44
+ // Start a new deployment based on the 'database' deployment config
45
+ $ %[1]s deploy frontend --latest
31
46
32
- // List the most recent deployments for the 'database' deployment config, limited to three
33
- $ %[1]s deploy database -L 3
34
-
35
- // Start a new deployment based on the 'frontend' deployment config
36
- $ %[1]s deploy frontend --latest`
47
+ // Retry the latest failed deployment based on the 'frontend' deployment config
48
+ $ %[1]s deploy frontend --retry`
37
49
)
38
50
39
51
// NewCmdDeploy creates a new `deploy` command.
40
52
func NewCmdDeploy (fullName string , f * clientcmd.Factory , out io.Writer ) * cobra.Command {
41
- var deployLatest bool
42
- var retryDeploy bool
43
- var listSize int
53
+ options := & DeployOptions {
54
+ baseCommandName : fullName ,
55
+ }
44
56
45
57
cmd := & cobra.Command {
46
58
Use : "deploy DEPLOYMENTCONFIG" ,
47
59
Short : "View, start and restart deployments." ,
48
60
Long : deploy_long ,
49
61
Example : fmt .Sprintf (deploy_example , fullName ),
50
62
Run : func (cmd * cobra.Command , args []string ) {
51
- if len (args ) == 0 || len (args [0 ]) == 0 {
52
- fmt .Println (cmdutil .UsageError (cmd , "A deploymentConfig name is required." ))
53
- return
54
- }
55
- listSizeProvided := cmd .Flags ().Changed ("list" )
56
- if listSizeProvided && (deployLatest || retryDeploy ) {
57
- fmt .Println (cmdutil .UsageError (cmd , "The -L|--list flag can't be used with --latest or --retry." ))
58
- return
63
+ if err := options .Complete (f , args , out ); err != nil {
64
+ cmdutil .CheckErr (err )
59
65
}
60
- if deployLatest && retryDeploy {
61
- fmt . Println ( cmdutil . UsageError ( cmd , "Only one of --latest or --retry is allowed." ))
62
- return
66
+
67
+ if err := options . Validate ( args ); err != nil {
68
+ cmdutil . CheckErr ( cmdutil . UsageError ( cmd , err . Error ()))
63
69
}
64
- if ! listSizeProvided {
65
- listSize = 1
70
+
71
+ if err := options .RunDeploy (); err != nil {
72
+ cmdutil .CheckErr (err )
66
73
}
74
+ },
75
+ }
67
76
68
- configName := args [0 ]
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." )
69
79
70
- osClient , kubeClient , err := f . Clients ()
71
- cmdutil . CheckErr ( err )
80
+ return cmd
81
+ }
72
82
73
- namespace , err := f . DefaultNamespace ()
74
- cmdutil . CheckErr ( err )
83
+ func ( o * DeployOptions ) Complete ( f * clientcmd. Factory , args [] string , out io. Writer ) error {
84
+ var err error
75
85
76
- config , err := osClient .DeploymentConfigs (namespace ).Get (configName )
77
- cmdutil .CheckErr (err )
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
+ }
78
94
79
- commandClient := & deployCommandClientImpl {
80
- GetDeploymentFn : func (namespace , name string ) (* kapi.ReplicationController , error ) {
81
- return kubeClient .ReplicationControllers (namespace ).Get (name )
82
- },
83
- UpdateDeploymentConfigFn : func (config * deployapi.DeploymentConfig ) (* deployapi.DeploymentConfig , error ) {
84
- return osClient .DeploymentConfigs (config .Namespace ).Update (config )
85
- },
86
- UpdateDeploymentFn : func (deployment * kapi.ReplicationController ) (* kapi.ReplicationController , error ) {
87
- return kubeClient .ReplicationControllers (deployment .Namespace ).Update (deployment )
88
- },
89
- }
95
+ o .out = out
90
96
91
- switch {
92
- case deployLatest :
93
- c := & deployLatestCommand {client : commandClient }
94
- err = c .deploy (config , out )
95
- case retryDeploy :
96
- c := & retryDeploymentCommand {client : commandClient }
97
- err = c .retry (config , out )
98
- default :
99
- describer := describe .NewLatestDeploymentsDescriber (osClient , kubeClient , listSize )
100
- desc , err := describer .Describe (config .Namespace , config .Name )
101
- cmdutil .CheckErr (err )
102
- fmt .Fprintln (out , desc )
103
- fmt .Fprintf (out , "For more details run '%v describe dc/%v'.\n " , fullName , config .Name )
104
- }
105
- cmdutil .CheckErr (err )
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 )
106
132
},
107
133
}
108
134
109
- cmd .Flags ().BoolVar (& deployLatest , "latest" , false , "Start a new deployment now." )
110
- cmd .Flags ().BoolVar (& retryDeploy , "retry" , false , "Retry the latest failed deployment." )
111
- cmd .Flags ().IntVarP (& listSize , "list" , "L" , - 1 , "Print a list of the latest deployments, limited to the amount provided." )
112
- cmd .Flags ().MarkOptional ("list" )
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
+ fmt .Fprintf (o .out , "For more details run '%v describe dc/%v'.\n " , o .baseCommandName , config .Name )
150
+ }
113
151
114
- return cmd
152
+ return err
115
153
}
116
154
117
155
// deployCommandClient abstracts access to the API server.
0 commit comments