forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollout_status.go
208 lines (175 loc) · 5.73 KB
/
rollout_status.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rollout
import (
"fmt"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
"k8s.io/kubernetes/pkg/util/interrupt"
)
var (
status_long = templates.LongDesc(`
Show the status of the rollout.
By default 'rollout status' will watch the status of the latest rollout
until it's done. If you don't want to wait for the rollout to finish then
you can use --watch=false. Note that if a new rollout starts in-between, then
'rollout status' will continue watching the latest revision. If you want to
pin to a specific revision and abort if it is rolled over by another revision,
use --revision=N where N is the revision you need to watch for.`)
status_example = templates.Examples(`
# Watch the rollout status of a deployment
kubectl rollout status deployment/nginx`)
)
type RolloutStatusOptions struct {
FilenameOptions *resource.FilenameOptions
genericclioptions.IOStreams
Namespace string
EnforceNamespace bool
BuilderArgs []string
Watch bool
Revision int64
StatusViewer func(*meta.RESTMapping) (kubectl.StatusViewer, error)
Builder *resource.Builder
}
func NewRolloutStatusOptions(streams genericclioptions.IOStreams) *RolloutStatusOptions {
return &RolloutStatusOptions{
FilenameOptions: &resource.FilenameOptions{},
IOStreams: streams,
Watch: true,
}
}
func NewCmdRolloutStatus(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
o := NewRolloutStatusOptions(streams)
validArgs := []string{"deployment", "daemonset", "statefulset"}
cmd := &cobra.Command{
Use: "status (TYPE NAME | TYPE/NAME) [flags]",
DisableFlagsInUseLine: true,
Short: i18n.T("Show the status of the rollout"),
Long: status_long,
Example: status_example,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(f, args))
cmdutil.CheckErr(o.Validate(cmd, args))
cmdutil.CheckErr(o.Run())
},
ValidArgs: validArgs,
}
usage := "identifying the resource to get from a server."
cmdutil.AddFilenameOptionFlags(cmd, o.FilenameOptions, usage)
cmd.Flags().BoolVarP(&o.Watch, "watch", "w", o.Watch, "Watch the status of the rollout until it's done.")
cmd.Flags().Int64Var(&o.Revision, "revision", o.Revision, "Pin to a specific revision for showing its status. Defaults to 0 (last revision).")
return cmd
}
func (o *RolloutStatusOptions) Complete(f cmdutil.Factory, args []string) error {
o.Builder = f.NewBuilder()
var err error
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
o.BuilderArgs = args
o.StatusViewer = func(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) {
return polymorphichelpers.StatusViewerFn(f, mapping)
}
return nil
}
func (o *RolloutStatusOptions) Validate(cmd *cobra.Command, args []string) error {
if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames) {
return cmdutil.UsageErrorf(cmd, "Required resource not specified.")
}
return nil
}
func (o *RolloutStatusOptions) Run() error {
r := o.Builder.
WithScheme(legacyscheme.Scheme).
NamespaceParam(o.Namespace).DefaultNamespace().
FilenameParam(o.EnforceNamespace, o.FilenameOptions).
ResourceTypeOrNameArgs(true, o.BuilderArgs...).
SingleResourceType().
Latest().
Do()
err := r.Err()
if err != nil {
return err
}
infos, err := r.Infos()
if err != nil {
return err
}
if len(infos) != 1 {
return fmt.Errorf("rollout status is only supported on individual resources and resource collections - %d resources were found", len(infos))
}
info := infos[0]
mapping := info.ResourceMapping()
obj, err := r.Object()
if err != nil {
return err
}
rv, err := meta.NewAccessor().ResourceVersion(obj)
if err != nil {
return err
}
statusViewer, err := o.StatusViewer(mapping)
if err != nil {
return err
}
revision := o.Revision
if revision < 0 {
return fmt.Errorf("revision must be a positive integer: %v", revision)
}
// check if deployment's has finished the rollout
status, done, err := statusViewer.Status(info.Namespace, info.Name, revision)
if err != nil {
return err
}
fmt.Fprintf(o.Out, "%s", status)
if done {
return nil
}
shouldWatch := o.Watch
if !shouldWatch {
return nil
}
// watch for changes to the deployment
w, err := r.Watch(rv)
if err != nil {
return err
}
// if the rollout isn't done yet, keep watching deployment status
intr := interrupt.New(nil, w.Stop)
return intr.Run(func() error {
_, err := watch.Until(0, w, func(e watch.Event) (bool, error) {
// print deployment's status
status, done, err := statusViewer.Status(info.Namespace, info.Name, revision)
if err != nil {
return false, err
}
fmt.Fprintf(o.Out, "%s", status)
// Quit waiting if the rollout is done
if done {
return true, nil
}
return false, nil
})
return err
})
}