forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy.go
445 lines (398 loc) · 16.6 KB
/
deploy.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package cmd
import (
"errors"
"fmt"
"io"
"sort"
"strconv"
"strings"
"time"
units "github.com/docker/go-units"
"github.com/spf13/cobra"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/api"
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"
appsinternalclient "github.com/openshift/origin/pkg/apps/client/internalversion"
appsclient "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"
"github.com/openshift/origin/pkg/oc/cli/describe"
)
// DeployOptions holds all the options for the `deploy` command
type DeployOptions struct {
out io.Writer
appsClient appsclient.AppsInterface
kubeClient kclientset.Interface
builder *resource.Builder
namespace string
baseCommandName string
deploymentConfigName string
deployLatest bool
retryDeploy bool
cancelDeploy bool
enableTriggers bool
follow bool
}
var (
deployLong = templates.LongDesc(`
View, start, cancel, or retry a deployment
This command allows you to control a deployment config. Each individual deployment is exposed
as a new replication controller, and the deployment process manages scaling down old deployments
and scaling up new ones. Use '%[1]s rollback' to rollback to any previous deployment.
There are several deployment strategies defined:
* Rolling (default) - scales up the new deployment in stages, gradually reducing the number
of old deployments. If one of the new deployed pods never becomes "ready", the new deployment
will be rolled back (scaled down to zero). Use when your application can tolerate two versions
of code running at the same time (many web applications, scalable databases)
* Recreate - scales the old deployment down to zero, then scales the new deployment up to full.
Use when your application cannot tolerate two versions of code running at the same time
* Custom - run your own deployment process inside a Docker container using your own scripts.
If a deployment fails, you may opt to retry it (if the error was transient). Some deployments may
never successfully complete - in which case you can use the '--latest' flag to force a redeployment.
If a deployment config has completed deploying successfully at least once in the past, it would be
automatically rolled back in the event of a new failed deployment. Note that you would still need
to update the erroneous deployment config in order to have its template persisted across your
application.
If you want to cancel a running deployment, use '--cancel' but keep in mind that this is a best-effort
operation and may take some time to complete. It’s possible the deployment will partially or totally
complete before the cancellation is effective. In such a case an appropriate event will be emitted.
If no options are given, shows information about the latest deployment.`)
deployExample = templates.Examples(`
# Display the latest deployment for the 'database' deployment config
%[1]s deploy database
# Start a new deployment based on the 'database'
%[1]s deploy database --latest
# Start a new deployment and follow its log
%[1]s deploy database --latest --follow
# Retry the latest failed deployment based on 'frontend'
# The deployer pod and any hook pods are deleted for the latest failed deployment
%[1]s deploy frontend --retry
# Cancel the in-progress deployment based on 'frontend'
%[1]s deploy frontend --cancel`)
)
// NewCmdDeploy creates a new `deploy` command.
func NewCmdDeploy(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &DeployOptions{
baseCommandName: fullName,
}
cmd := &cobra.Command{
Use: "deploy DEPLOYMENTCONFIG [--latest|--retry|--cancel|--enable-triggers]",
Short: "View, start, cancel, or retry a deployment",
Long: fmt.Sprintf(deployLong, fullName),
Example: fmt.Sprintf(deployExample, fullName),
SuggestFor: []string{"deployment"},
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args, out); err != nil {
kcmdutil.CheckErr(err)
}
if err := options.Validate(); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
if err := options.RunDeploy(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
cmd.Deprecated = "Use the `rollout latest` and `rollout cancel` commands instead."
cmd.Flags().BoolVar(&options.deployLatest, "latest", false, "If true, start a new deployment now.")
cmd.Flags().MarkDeprecated("latest", fmt.Sprintf("use '%s rollout latest' instead", fullName))
cmd.Flags().BoolVar(&options.retryDeploy, "retry", false, "If true, retry the latest failed deployment.")
cmd.Flags().BoolVar(&options.cancelDeploy, "cancel", false, "If true, cancel the in-progress deployment.")
cmd.Flags().MarkDeprecated("cancel", fmt.Sprintf("use '%s rollout cancel' instead", fullName))
cmd.Flags().BoolVar(&options.enableTriggers, "enable-triggers", false, "If true, enables all image triggers for the deployment config.")
cmd.Flags().MarkDeprecated("enable-triggers", fmt.Sprintf("use '%s set triggers' instead", fullName))
cmd.Flags().BoolVar(&options.follow, "follow", false, "If true, follow the logs of a deployment")
return cmd
}
func (o *DeployOptions) Complete(f *clientcmd.Factory, args []string, out io.Writer) error {
if len(args) > 1 {
return errors.New("only one deployment config name is supported as argument.")
}
var err error
o.kubeClient, err = f.ClientSet()
if err != nil {
return err
}
client, err := f.OpenshiftInternalAppsClient()
if err != nil {
return err
}
o.appsClient = client.Apps()
o.namespace, _, err = f.DefaultNamespace()
if err != nil {
return err
}
o.builder = f.NewBuilder(true)
o.out = out
if len(args) > 0 {
o.deploymentConfigName = args[0]
}
return nil
}
func (o DeployOptions) Validate() error {
if len(o.deploymentConfigName) == 0 {
msg := fmt.Sprintf("a deployment config name is required.\nUse \"%s get dc\" for a list of available deployment configs.", o.baseCommandName)
return errors.New(msg)
}
numOptions := 0
if o.deployLatest {
numOptions++
}
if o.retryDeploy {
numOptions++
}
if o.cancelDeploy {
if o.follow {
return errors.New("cannot follow the logs while canceling a deployment")
}
numOptions++
}
if o.enableTriggers {
if o.follow {
return errors.New("cannot follow the logs while enabling triggers for a deployment")
}
numOptions++
}
if numOptions > 1 {
return errors.New("only one of --latest, --retry, --cancel, or --enable-triggers is allowed.")
}
return nil
}
func (o DeployOptions) RunDeploy() error {
r := o.builder.
NamespaceParam(o.namespace).
ResourceNames("deploymentconfigs", o.deploymentConfigName).
SingleResourceType().
Do()
resultObj, err := r.Object()
if err != nil {
return err
}
config, ok := resultObj.(*deployapi.DeploymentConfig)
if !ok {
return fmt.Errorf("%s is not a valid deployment config", o.deploymentConfigName)
}
switch {
case o.deployLatest:
err = o.deploy(config)
case o.retryDeploy:
err = o.retry(config)
case o.cancelDeploy:
err = o.cancel(config)
case o.enableTriggers:
err = o.reenableTriggers(config)
default:
if o.follow {
return o.getLogs(config)
}
describer := describe.NewLatestDeploymentsDescriber(o.appsClient, o.kubeClient, -1)
desc, err := describer.Describe(config.Namespace, config.Name)
if err != nil {
return err
}
fmt.Fprint(o.out, desc)
}
return err
}
// deploy launches a new deployment unless there's already a deployment
// process in progress for config.
func (o DeployOptions) deploy(config *deployapi.DeploymentConfig) error {
if config.Spec.Paused {
return fmt.Errorf("cannot deploy a paused deployment config")
}
// TODO: This implies that deploymentconfig.status.latestVersion is always synced. Currently,
// that's the case because clients (oc, trigger controllers) are updating the status directly.
// Clients should be acting either on spec or on annotations and status updates should be a
// responsibility of the main controller. We need to start by unplugging this assumption from
// our client tools.
deploymentName := deployutil.LatestDeploymentNameForConfig(config)
deployment, err := o.kubeClient.Core().ReplicationControllers(config.Namespace).Get(deploymentName, metav1.GetOptions{})
if err == nil && !deployutil.IsTerminatedDeployment(deployment) {
// Reject attempts to start a concurrent deployment.
return fmt.Errorf("#%d is already in progress (%s).\nOptionally, you can cancel this deployment using 'oc rollout cancel dc/%s'.",
config.Status.LatestVersion, deployutil.DeploymentStatusFor(deployment), config.Name)
}
if err != nil && !kerrors.IsNotFound(err) {
return err
}
request := &deployapi.DeploymentRequest{
Name: config.Name,
Latest: false,
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 {
if kerrors.IsBadRequest(err) {
err = fmt.Errorf("%v - try 'oc rollout latest dc/%s'", err, config.Name)
}
return err
}
fmt.Fprintf(o.out, "Started deployment #%d\n", dc.Status.LatestVersion)
if o.follow {
return o.getLogs(dc)
}
fmt.Fprintf(o.out, "Use '%s logs -f dc/%s' to track its progress.\n", o.baseCommandName, dc.Name)
return nil
}
// retry resets the status of the latest deployment to New, which will cause
// the deployment to be retried. An error is returned if the deployment is not
// currently in a failed state.
func (o DeployOptions) retry(config *deployapi.DeploymentConfig) error {
if config.Spec.Paused {
return fmt.Errorf("cannot retry a paused deployment config")
}
if config.Status.LatestVersion == 0 {
return fmt.Errorf("no deployments found for %s/%s", config.Namespace, config.Name)
}
// TODO: This implies that deploymentconfig.status.latestVersion is always synced. Currently,
// that's the case because clients (oc, trigger controllers) are updating the status directly.
// Clients should be acting either on spec or on annotations and status updates should be a
// responsibility of the main controller. We need to start by unplugging this assumption from
// our client tools.
deploymentName := deployutil.LatestDeploymentNameForConfig(config)
deployment, err := o.kubeClient.Core().ReplicationControllers(config.Namespace).Get(deploymentName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return fmt.Errorf("unable to find the latest deployment (#%d).\nYou can start a new deployment with 'oc deploy --latest dc/%s'.", config.Status.LatestVersion, config.Name)
}
return err
}
if !deployutil.IsFailedDeployment(deployment) {
message := fmt.Sprintf("#%d is %s; only failed deployments can be retried.\n", config.Status.LatestVersion, deployutil.DeploymentStatusFor(deployment))
if deployutil.IsCompleteDeployment(deployment) {
message += fmt.Sprintf("You can start a new deployment with 'oc deploy --latest dc/%s'.", config.Name)
} else {
message += fmt.Sprintf("Optionally, you can cancel this deployment with 'oc rollout cancel dc/%s'.", config.Name)
}
return fmt.Errorf(message)
}
// Delete the deployer pod as well as the deployment hooks pods, if any
pods, err := o.kubeClient.Core().Pods(config.Namespace).List(metav1.ListOptions{LabelSelector: deployutil.DeployerPodSelector(deploymentName).String()})
if err != nil {
return fmt.Errorf("failed to list deployer/hook pods for deployment #%d: %v", config.Status.LatestVersion, err)
}
for _, pod := range pods.Items {
err := o.kubeClient.Core().Pods(pod.Namespace).Delete(pod.Name, metav1.NewDeleteOptions(0))
if err != nil {
return fmt.Errorf("failed to delete deployer/hook pod %s for deployment #%d: %v", pod.Name, config.Status.LatestVersion, err)
}
}
deployment.Annotations[deployapi.DeploymentStatusAnnotation] = string(deployapi.DeploymentStatusNew)
// clear out the cancellation flag as well as any previous status-reason annotation
delete(deployment.Annotations, deployapi.DeploymentStatusReasonAnnotation)
delete(deployment.Annotations, deployapi.DeploymentCancelledAnnotation)
_, err = o.kubeClient.Core().ReplicationControllers(deployment.Namespace).Update(deployment)
if err != nil {
return err
}
fmt.Fprintf(o.out, "Retried #%d\n", config.Status.LatestVersion)
if o.follow {
return o.getLogs(config)
}
fmt.Fprintf(o.out, "Use '%s logs -f dc/%s' to track its progress.\n", o.baseCommandName, config.Name)
return nil
}
// cancel cancels any deployment process in progress for config.
// TODO: this code will be deprecated
func (o DeployOptions) cancel(config *deployapi.DeploymentConfig) error {
if config.Spec.Paused {
return fmt.Errorf("cannot cancel a paused deployment config")
}
deploymentList, err := o.kubeClient.Core().ReplicationControllers(config.Namespace).List(metav1.ListOptions{LabelSelector: deployutil.ConfigSelector(config.Name).String()})
if err != nil {
return err
}
if len(deploymentList.Items) == 0 {
fmt.Fprintf(o.out, "There have been no deployments for %s/%s\n", config.Namespace, config.Name)
return nil
}
deployments := make([]*kapi.ReplicationController, 0, len(deploymentList.Items))
for i := range deploymentList.Items {
deployments = append(deployments, &deploymentList.Items[i])
}
sort.Sort(deployutil.ByLatestVersionDesc(deployments))
failedCancellations := []string{}
anyCancelled := false
for _, deployment := range deployments {
status := deployutil.DeploymentStatusFor(deployment)
switch status {
case deployapi.DeploymentStatusNew,
deployapi.DeploymentStatusPending,
deployapi.DeploymentStatusRunning:
if deployutil.IsDeploymentCancelled(deployment) {
continue
}
deployment.Annotations[deployapi.DeploymentCancelledAnnotation] = deployapi.DeploymentCancelledAnnotationValue
deployment.Annotations[deployapi.DeploymentStatusReasonAnnotation] = deployapi.DeploymentCancelledByUser
_, err := o.kubeClient.Core().ReplicationControllers(deployment.Namespace).Update(deployment)
if err == nil {
fmt.Fprintf(o.out, "Cancelled deployment #%d\n", config.Status.LatestVersion)
anyCancelled = true
} else {
fmt.Fprintf(o.out, "Couldn't cancel deployment #%d (status: %s): %v\n", deployutil.DeploymentVersionFor(deployment), status, err)
failedCancellations = append(failedCancellations, strconv.FormatInt(deployutil.DeploymentVersionFor(deployment), 10))
}
}
}
if len(failedCancellations) > 0 {
return fmt.Errorf("couldn't cancel deployment %s", strings.Join(failedCancellations, ", "))
}
if !anyCancelled {
latest := deployments[0]
maybeCancelling := ""
if deployutil.IsDeploymentCancelled(latest) && !deployutil.IsTerminatedDeployment(latest) {
maybeCancelling = " (cancelling)"
}
timeAt := strings.ToLower(units.HumanDuration(time.Now().Sub(latest.CreationTimestamp.Time)))
fmt.Fprintf(o.out, "No deployments are in progress (latest deployment #%d %s%s %s ago)\n",
deployutil.DeploymentVersionFor(latest),
strings.ToLower(string(deployutil.DeploymentStatusFor(latest))),
maybeCancelling,
timeAt)
}
return nil
}
// reenableTriggers enables all image triggers and then persists config.
func (o DeployOptions) reenableTriggers(config *deployapi.DeploymentConfig) error {
enabled := []string{}
for _, trigger := range config.Spec.Triggers {
if trigger.Type == deployapi.DeploymentTriggerOnImageChange {
trigger.ImageChangeParams.Automatic = true
enabled = append(enabled, trigger.ImageChangeParams.From.Name)
}
}
if len(enabled) == 0 {
fmt.Fprintln(o.out, "No image triggers found to enable")
return nil
}
_, err := o.appsClient.DeploymentConfigs(config.Namespace).Update(config)
if err != nil {
return err
}
fmt.Fprintf(o.out, "Enabled image triggers: %s\n", strings.Join(enabled, ","))
return nil
}
func (o DeployOptions) getLogs(config *deployapi.DeploymentConfig) error {
opts := deployapi.DeploymentLogOptions{
Follow: true,
}
logClient := appsinternalclient.NewRolloutLogClient(o.appsClient.RESTClient(), config.Namespace)
readCloser, err := logClient.Logs(config.Name, opts).Stream()
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(o.out, readCloser)
return err
}