2016-01-20 23:48:52 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-20 23:48:52 +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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2016-10-07 22:24:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
2016-01-20 23:48:52 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2017-01-26 06:16:44 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/i18n"
|
2016-01-20 23:48:52 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
var (
|
2016-10-07 22:24:42 +00:00
|
|
|
history_long = templates.LongDesc(`
|
2016-05-20 17:49:56 +00:00
|
|
|
View previous rollout revisions and configurations.`)
|
2016-10-07 22:24:42 +00:00
|
|
|
|
|
|
|
history_example = templates.Examples(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# View the rollout history of a deployment
|
|
|
|
kubectl rollout history deployment/abc
|
2016-03-21 21:00:43 +00:00
|
|
|
|
2017-05-18 22:46:20 +00:00
|
|
|
# View the details of daemonset revision 3
|
|
|
|
kubectl rollout history daemonset/abc --revision=3`)
|
2016-01-20 23:48:52 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func NewCmdRolloutHistory(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
2016-08-17 18:28:07 +00:00
|
|
|
options := &resource.FilenameOptions{}
|
2016-01-20 23:48:52 +00:00
|
|
|
|
2017-05-18 22:46:20 +00:00
|
|
|
validArgs := []string{"deployment", "daemonset"}
|
2016-08-22 02:46:50 +00:00
|
|
|
argAliases := kubectl.ResourceAliases(validArgs)
|
|
|
|
|
2016-01-20 23:48:52 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "history (TYPE NAME | TYPE/NAME) [flags]",
|
2017-01-26 06:16:44 +00:00
|
|
|
Short: i18n.T("View rollout history"),
|
2016-01-20 23:48:52 +00:00
|
|
|
Long: history_long,
|
|
|
|
Example: history_example,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cmdutil.CheckErr(RunHistory(f, cmd, out, args, options))
|
|
|
|
},
|
2016-08-22 02:46:50 +00:00
|
|
|
ValidArgs: validArgs,
|
|
|
|
ArgAliases: argAliases,
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().Int64("revision", 0, "See the details, including podTemplate of the revision specified")
|
2016-08-17 18:28:07 +00:00
|
|
|
usage := "identifying the resource to get from a server."
|
|
|
|
cmdutil.AddFilenameOptionFlags(cmd, options, usage)
|
2016-01-20 23:48:52 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func RunHistory(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error {
|
2017-06-14 21:14:42 +00:00
|
|
|
if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(options.Filenames) {
|
|
|
|
return cmdutil.UsageErrorf(cmd, "Required resource not specified.")
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
revision := cmdutil.GetFlagInt64(cmd, "revision")
|
2016-10-10 11:07:38 +00:00
|
|
|
if revision < 0 {
|
|
|
|
return fmt.Errorf("revision must be a positive integer: %v", revision)
|
|
|
|
}
|
2016-01-20 23:48:52 +00:00
|
|
|
|
|
|
|
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
r := f.NewBuilder(true).
|
2016-01-20 23:48:52 +00:00
|
|
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
2016-08-17 18:28:07 +00:00
|
|
|
FilenameParam(enforceNamespace, options).
|
2016-01-20 23:48:52 +00:00
|
|
|
ResourceTypeOrNameArgs(true, args...).
|
2016-05-03 23:41:43 +00:00
|
|
|
ContinueOnError().
|
2016-01-20 23:48:52 +00:00
|
|
|
Latest().
|
|
|
|
Flatten().
|
2016-05-03 23:41:43 +00:00
|
|
|
Do()
|
|
|
|
err = r.Err()
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:31:29 +00:00
|
|
|
return r.Visit(func(info *resource.Info, err error) error {
|
2016-05-03 23:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-20 23:48:52 +00:00
|
|
|
mapping := info.ResourceMapping()
|
|
|
|
historyViewer, err := f.HistoryViewer(mapping)
|
|
|
|
if err != nil {
|
2016-05-03 23:41:43 +00:00
|
|
|
return err
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
historyInfo, err := historyViewer.ViewHistory(info.Namespace, info.Name, revision)
|
2016-01-20 23:48:52 +00:00
|
|
|
if err != nil {
|
2016-05-03 23:41:43 +00:00
|
|
|
return err
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-10 16:31:29 +00:00
|
|
|
header := fmt.Sprintf("%s %q", mapping.Resource, info.Name)
|
|
|
|
if revision > 0 {
|
|
|
|
header = fmt.Sprintf("%s with revision #%d", header, revision)
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|
2016-06-10 16:31:29 +00:00
|
|
|
fmt.Fprintf(out, "%s\n", header)
|
|
|
|
fmt.Fprintf(out, "%s\n", historyInfo)
|
2016-05-03 23:41:43 +00:00
|
|
|
return nil
|
|
|
|
})
|
2016-01-20 23:48:52 +00:00
|
|
|
}
|