Skip to content

Commit 2761945

Browse files
committed
Review comments and other improvements
1 parent 5237b34 commit 2761945

File tree

3 files changed

+105
-67
lines changed

3 files changed

+105
-67
lines changed

hack/test-cmd.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,6 @@ osc describe deploymentConfigs test-deployment-config
414414
[ ! "$(echo "#OTHER=foo" | osc env dc/test-deployment-config -e - --list | grep OTHER=foo)" ]
415415
[ "$(osc env dc/test-deployment-config TEST=bar OTHER=baz BAR-)" ]
416416
osc deploy test-deployment-config
417-
osc deploy test-deployment-config -L
418-
osc deploy test-deployment-config -L 3
419417
osc delete deploymentConfigs test-deployment-config
420418
echo "deploymentConfigs: ok"
421419

pkg/cmd/cli/cmd/deploy.go

Lines changed: 99 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67

78
"github.com/spf13/cobra"
89

910
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
1011
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
12+
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
1113
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
1214

15+
"github.com/openshift/origin/pkg/client"
1316
"github.com/openshift/origin/pkg/cmd/cli/describe"
1417
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
1518
deployapi "github.com/openshift/origin/pkg/deploy/api"
1619
deployutil "github.com/openshift/origin/pkg/deploy/util"
1720
)
1821

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+
1934
const (
2035
deploy_long = `View, start and restart deployments.
2136
@@ -26,92 +41,115 @@ NOTE: This command is still under active development and is subject to change.`
2641
deploy_example = ` // Display the latest deployment for the 'database' deployment config
2742
$ %[1]s deploy database
2843
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
3146
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`
3749
)
3850

3951
// NewCmdDeploy creates a new `deploy` command.
4052
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+
}
4456

4557
cmd := &cobra.Command{
4658
Use: "deploy DEPLOYMENTCONFIG",
4759
Short: "View, start and restart deployments.",
4860
Long: deploy_long,
4961
Example: fmt.Sprintf(deploy_example, fullName),
5062
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)
5965
}
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()))
6369
}
64-
if !listSizeProvided {
65-
listSize = 1
70+
71+
if err := options.RunDeploy(); err != nil {
72+
cmdutil.CheckErr(err)
6673
}
74+
},
75+
}
6776

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.")
6979

70-
osClient, kubeClient, err := f.Clients()
71-
cmdutil.CheckErr(err)
80+
return cmd
81+
}
7282

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
7585

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+
}
7894

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
9096

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)
106132
},
107133
}
108134

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+
}
113151

114-
return cmd
152+
return err
115153
}
116154

117155
// deployCommandClient abstracts access to the API server.

pkg/cmd/cli/describe/deployments.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (d *DeploymentConfigDescriber) Describe(namespace, name string) (string, er
148148
sorted = append(sorted, deploymentsHistory.Items...)
149149
sort.Sort(sorted)
150150
for _, item := range sorted {
151-
if item.Name != deploymentName {
151+
if item.Name != deploymentName && deploymentConfig.Name == item.Annotations[deployapi.DeploymentConfigAnnotation] {
152152
header := fmt.Sprintf("Deployment #%v", item.Annotations[deployapi.DeploymentVersionAnnotation])
153153
printDeploymentRc(&item, d.client, out, header, false)
154154
}
@@ -244,15 +244,17 @@ func printDeploymentRc(deployment *kapi.ReplicationController, client deployment
244244
fmt.Fprintf(w, "%v:\n", header)
245245
}
246246

247-
fmt.Fprintf(w, "\tName:\t%s\n", deployment.Name)
247+
if verbose {
248+
fmt.Fprintf(w, "\tName:\t%s\n", deployment.Name)
249+
}
248250
timeAt := strings.ToLower(formatRelativeTime(deployment.CreationTimestamp.Time))
249251
fmt.Fprintf(w, "\tCreated:\t%s ago\n", timeAt)
250252
fmt.Fprintf(w, "\tStatus:\t%s\n", deployment.Annotations[deployapi.DeploymentStatusAnnotation])
251-
fmt.Fprintf(w, "\tSelector:\t%s\n", formatLabels(deployment.Spec.Selector))
253+
fmt.Fprintf(w, "\tReplicas:\t%d current / %d desired\n", deployment.Status.Replicas, deployment.Spec.Replicas)
252254

253255
if verbose {
256+
fmt.Fprintf(w, "\tSelector:\t%s\n", formatLabels(deployment.Spec.Selector))
254257
fmt.Fprintf(w, "\tLabels:\t%s\n", formatLabels(deployment.Labels))
255-
fmt.Fprintf(w, "\tReplicas:\t%d current / %d desired\n", deployment.Status.Replicas, deployment.Spec.Replicas)
256258
running, waiting, succeeded, failed, err := getPodStatusForDeployment(deployment, client)
257259
if err != nil {
258260
return err

0 commit comments

Comments
 (0)