forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
latest.go
191 lines (158 loc) · 5.52 KB
/
latest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package rollout
import (
"errors"
"fmt"
"io"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsclientinternal "github.com/openshift/origin/pkg/apps/generated/internalclientset/typed/apps/internalversion"
deployutil "github.com/openshift/origin/pkg/apps/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
var (
rolloutLatestLong = templates.LongDesc(`
Start a new rollout for a deployment config with the latest state from its triggers
This command is appropriate for running manual rollouts. If you want full control over
running new rollouts, use "oc set triggers --manual" to disable all triggers in your
deployment config and then whenever you want to run a new deployment process, use this
command in order to pick up the latest images found in the cluster that are pointed by
your image change triggers.`)
rolloutLatestExample = templates.Examples(`
# Start a new rollout based on the latest images defined in the image change triggers.
%[1]s rollout latest dc/nginx`)
)
// RolloutLatestOptions holds all the options for the `rollout latest` command.
type RolloutLatestOptions struct {
mapper meta.RESTMapper
typer runtime.ObjectTyper
infos []*resource.Info
DryRun bool
out io.Writer
output string
again bool
appsClient appsclientinternal.DeploymentConfigsGetter
kc kclientset.Interface
baseCommandName string
}
// NewCmdRolloutLatest implements the oc rollout latest subcommand.
func NewCmdRolloutLatest(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := &RolloutLatestOptions{
baseCommandName: fullName,
}
cmd := &cobra.Command{
Use: "latest DEPLOYMENTCONFIG",
Short: "Start a new rollout for a deployment config with the latest state from its triggers",
Long: rolloutLatestLong,
Example: fmt.Sprintf(rolloutLatestExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := opts.Complete(f, cmd, args, out)
kcmdutil.CheckErr(err)
if err := opts.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
err = opts.RunRolloutLatest()
kcmdutil.CheckErr(err)
},
ValidArgs: []string{"deploymentconfig"},
}
kcmdutil.AddPrinterFlags(cmd)
kcmdutil.AddDryRunFlag(cmd)
cmd.Flags().Bool("again", false, "If true, deploy the current pod template without updating state from triggers")
return cmd
}
func (o *RolloutLatestOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error {
if len(args) != 1 {
return errors.New("one deployment config name is needed as argument.")
}
namespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
o.DryRun = kcmdutil.GetFlagBool(cmd, "dry-run")
o.kc, err = f.ClientSet()
if err != nil {
return err
}
appsClient, err := f.OpenshiftInternalAppsClient()
if err != nil {
return err
}
o.appsClient = appsClient.Apps()
o.mapper, o.typer = f.Object()
o.infos, err = f.NewBuilder(true).
ContinueOnError().
NamespaceParam(namespace).
ResourceNames("deploymentconfigs", args[0]).
SingleResourceType().
Do().Infos()
if err != nil {
return err
}
o.out = out
o.output = kcmdutil.GetFlagString(cmd, "output")
o.again = kcmdutil.GetFlagBool(cmd, "again")
return nil
}
func (o RolloutLatestOptions) Validate() error {
if len(o.infos) != 1 {
return errors.New("a deployment config name is required.")
}
return nil
}
func (o RolloutLatestOptions) RunRolloutLatest() error {
info := o.infos[0]
config, ok := info.Object.(*deployapi.DeploymentConfig)
if !ok {
return fmt.Errorf("%s is not a deployment config", info.Name)
}
// TODO: Consider allowing one-off deployments for paused configs
// See https://github.com/openshift/origin/issues/9903
if config.Spec.Paused {
return fmt.Errorf("cannot deploy a paused deployment config")
}
deploymentName := deployutil.LatestDeploymentNameForConfig(config)
deployment, err := o.kc.Core().ReplicationControllers(config.Namespace).Get(deploymentName, metav1.GetOptions{})
switch {
case err == nil:
// Reject attempts to start a concurrent deployment.
if !deployutil.IsTerminatedDeployment(deployment) {
status := deployutil.DeploymentStatusFor(deployment)
return fmt.Errorf("#%d is already in progress (%s).", config.Status.LatestVersion, status)
}
case !kerrors.IsNotFound(err):
return err
}
dc := config
if !o.DryRun {
request := &deployapi.DeploymentRequest{
Name: config.Name,
Latest: !o.again,
Force: true,
}
dc, err = o.appsClient.DeploymentConfigs(config.Namespace).Instantiate(config.Name, request)
// Pre 1.4 servers don't support the instantiate endpoint. Fallback to incrementing
// latestVersion on them.
if kerrors.IsNotFound(err) || kerrors.IsForbidden(err) {
config.Status.LatestVersion++
dc, err = o.appsClient.DeploymentConfigs(config.Namespace).Update(config)
}
if err != nil {
return err
}
info.Refresh(dc, true)
}
if o.output == "revision" {
fmt.Fprintf(o.out, fmt.Sprintf("%d", dc.Status.LatestVersion))
return nil
}
kcmdutil.PrintSuccess(o.mapper, o.output == "name", o.out, info.Mapping.Resource, info.Name, o.DryRun, "rolled out")
return nil
}