2016-01-21 19:07:23 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-21 19:07:23 +00:00
|
|
|
|
|
|
|
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 (
|
2018-08-02 12:11:59 +00:00
|
|
|
"context"
|
2016-01-21 19:07:23 +00:00
|
|
|
"fmt"
|
2018-08-02 12:11:59 +00:00
|
|
|
"time"
|
2016-01-21 19:07:23 +00:00
|
|
|
|
2018-05-17 17:04:35 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-04-23 14:23:01 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2018-08-02 12:11:59 +00:00
|
|
|
watchtools "k8s.io/client-go/tools/watch"
|
2016-01-21 19:07:23 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2016-10-07 22:24:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
2016-01-21 19:07:23 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
2018-04-24 04:40:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2018-05-10 12:20:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
2018-05-17 17:04:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
|
2018-07-05 20:13:20 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2017-07-07 04:04:11 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
2016-08-08 17:56:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/interrupt"
|
2016-01-21 19:07:23 +00:00
|
|
|
)
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
var (
|
2016-10-07 22:24:42 +00:00
|
|
|
status_long = templates.LongDesc(`
|
2016-10-10 11:07:38 +00:00
|
|
|
Show the status of the rollout.
|
2016-10-05 14:33:06 +00:00
|
|
|
|
2016-10-10 11:07:38 +00:00
|
|
|
By default 'rollout status' will watch the status of the latest rollout
|
2016-10-05 14:33:06 +00:00
|
|
|
until it's done. If you don't want to wait for the rollout to finish then
|
2016-10-10 11:07:38 +00:00
|
|
|
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.`)
|
2016-10-07 22:24:42 +00:00
|
|
|
|
|
|
|
status_example = templates.Examples(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# Watch the rollout status of a deployment
|
|
|
|
kubectl rollout status deployment/nginx`)
|
2016-01-21 19:07:23 +00:00
|
|
|
)
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
type RolloutStatusOptions struct {
|
2018-07-05 20:13:20 +00:00
|
|
|
PrintFlags *genericclioptions.PrintFlags
|
2018-04-24 04:40:35 +00:00
|
|
|
|
|
|
|
Namespace string
|
|
|
|
EnforceNamespace bool
|
|
|
|
BuilderArgs []string
|
|
|
|
|
|
|
|
Watch bool
|
|
|
|
Revision int64
|
|
|
|
|
|
|
|
StatusViewer func(*meta.RESTMapping) (kubectl.StatusViewer, error)
|
2018-07-05 20:13:20 +00:00
|
|
|
Builder func() *resource.Builder
|
2018-04-24 04:40:35 +00:00
|
|
|
|
2018-07-05 20:13:20 +00:00
|
|
|
FilenameOptions *resource.FilenameOptions
|
|
|
|
genericclioptions.IOStreams
|
2018-04-24 04:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRolloutStatusOptions(streams genericclioptions.IOStreams) *RolloutStatusOptions {
|
|
|
|
return &RolloutStatusOptions{
|
2018-07-05 20:13:20 +00:00
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("").WithTypeSetter(scheme.Scheme),
|
2018-04-24 04:40:35 +00:00
|
|
|
FilenameOptions: &resource.FilenameOptions{},
|
|
|
|
IOStreams: streams,
|
|
|
|
Watch: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCmdRolloutStatus(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
|
|
|
|
o := NewRolloutStatusOptions(streams)
|
2016-01-21 19:07:23 +00:00
|
|
|
|
2017-08-07 21:49:46 +00:00
|
|
|
validArgs := []string{"deployment", "daemonset", "statefulset"}
|
2016-08-22 02:46:50 +00:00
|
|
|
|
2016-01-21 19:07:23 +00:00
|
|
|
cmd := &cobra.Command{
|
2017-10-11 06:26:02 +00:00
|
|
|
Use: "status (TYPE NAME | TYPE/NAME) [flags]",
|
|
|
|
DisableFlagsInUseLine: true,
|
2017-01-26 06:16:44 +00:00
|
|
|
Short: i18n.T("Show the status of the rollout"),
|
2016-01-21 19:07:23 +00:00
|
|
|
Long: status_long,
|
|
|
|
Example: status_example,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2018-04-24 04:40:35 +00:00
|
|
|
cmdutil.CheckErr(o.Complete(f, args))
|
|
|
|
cmdutil.CheckErr(o.Validate(cmd, args))
|
|
|
|
cmdutil.CheckErr(o.Run())
|
2016-01-21 19:07:23 +00:00
|
|
|
},
|
2018-05-07 19:25:40 +00:00
|
|
|
ValidArgs: validArgs,
|
2016-01-21 19:07:23 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 18:28:07 +00:00
|
|
|
usage := "identifying the resource to get from a server."
|
2018-04-24 04:40:35 +00:00
|
|
|
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).")
|
2018-07-05 20:13:20 +00:00
|
|
|
|
2016-01-21 19:07:23 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
func (o *RolloutStatusOptions) Complete(f cmdutil.Factory, args []string) error {
|
2018-07-05 20:13:20 +00:00
|
|
|
o.Builder = f.NewBuilder
|
2016-01-21 19:07:23 +00:00
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
var err error
|
2018-05-24 13:33:36 +00:00
|
|
|
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
|
2016-01-21 19:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
o.BuilderArgs = args
|
2018-05-17 17:04:35 +00:00
|
|
|
o.StatusViewer = func(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) {
|
|
|
|
return polymorphichelpers.StatusViewerFn(f, mapping)
|
|
|
|
}
|
2018-04-24 04:40:35 +00:00
|
|
|
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.")
|
|
|
|
}
|
2018-07-05 20:13:20 +00:00
|
|
|
|
|
|
|
if o.Revision < 0 {
|
|
|
|
return fmt.Errorf("revision must be a positive integer: %v", o.Revision)
|
|
|
|
}
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *RolloutStatusOptions) Run() error {
|
2018-07-05 20:13:20 +00:00
|
|
|
r := o.Builder().
|
|
|
|
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
|
2018-04-24 04:40:35 +00:00
|
|
|
NamespaceParam(o.Namespace).DefaultNamespace().
|
|
|
|
FilenameParam(o.EnforceNamespace, o.FilenameOptions).
|
|
|
|
ResourceTypeOrNameArgs(true, o.BuilderArgs...).
|
2016-01-21 19:07:23 +00:00
|
|
|
SingleResourceType().
|
|
|
|
Latest().
|
|
|
|
Do()
|
2018-04-24 04:40:35 +00:00
|
|
|
err := r.Err()
|
2016-01-21 19:07:23 +00:00
|
|
|
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
|
|
|
|
}
|
2018-04-23 14:23:01 +00:00
|
|
|
rv, err := meta.NewAccessor().ResourceVersion(obj)
|
2016-01-21 19:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
statusViewer, err := o.StatusViewer(mapping)
|
2016-01-21 19:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if deployment's has finished the rollout
|
2018-07-05 20:13:20 +00:00
|
|
|
status, done, err := statusViewer.Status(info.Namespace, info.Name, o.Revision)
|
2016-01-21 19:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-24 04:40:35 +00:00
|
|
|
fmt.Fprintf(o.Out, "%s", status)
|
2016-01-21 19:07:23 +00:00
|
|
|
if done {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-24 04:40:35 +00:00
|
|
|
shouldWatch := o.Watch
|
2016-10-05 14:33:06 +00:00
|
|
|
if !shouldWatch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-21 19:07:23 +00:00
|
|
|
// 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
|
2018-08-02 12:11:59 +00:00
|
|
|
// TODO: expose timeout
|
|
|
|
timeout := 0 * time.Second
|
|
|
|
ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
intr := interrupt.New(nil, cancel)
|
2016-10-03 15:41:49 +00:00
|
|
|
return intr.Run(func() error {
|
2018-08-02 12:11:59 +00:00
|
|
|
_, err := watchtools.UntilWithoutRetry(ctx, w, func(e watch.Event) (bool, error) {
|
2016-08-08 17:56:19 +00:00
|
|
|
// print deployment's status
|
2018-07-05 20:13:20 +00:00
|
|
|
status, done, err := statusViewer.Status(info.Namespace, info.Name, o.Revision)
|
2016-08-08 17:56:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-04-24 04:40:35 +00:00
|
|
|
fmt.Fprintf(o.Out, "%s", status)
|
2016-08-08 17:56:19 +00:00
|
|
|
// Quit waiting if the rollout is done
|
|
|
|
if done {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
return err
|
2016-01-21 19:07:23 +00:00
|
|
|
})
|
|
|
|
}
|