2016-01-25 11:34:48 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-25 11:34:48 +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 (
|
2016-10-27 11:01:50 +00:00
|
|
|
"fmt"
|
2016-01-25 11:34:48 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2016-10-27 11:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-01-25 11:34:48 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2016-10-27 11:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
|
2016-10-07 22:24:42 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
2016-01-25 11:34:48 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-05-03 23:41:43 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2016-01-25 11:34:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResumeConfig is the start of the data required to perform the operation. As new fields are added, add them here instead of
|
|
|
|
// referencing the cmd.Flags()
|
|
|
|
type ResumeConfig struct {
|
2016-08-17 18:28:07 +00:00
|
|
|
resource.FilenameOptions
|
|
|
|
|
2016-10-27 11:01:50 +00:00
|
|
|
Resumer func(object *resource.Info) (bool, error)
|
|
|
|
Mapper meta.RESTMapper
|
|
|
|
Typer runtime.ObjectTyper
|
|
|
|
Encoder runtime.Encoder
|
|
|
|
Infos []*resource.Info
|
2016-01-25 11:34:48 +00:00
|
|
|
|
2016-08-17 18:28:07 +00:00
|
|
|
Out io.Writer
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
var (
|
2016-10-07 22:24:42 +00:00
|
|
|
resume_long = templates.LongDesc(`
|
2016-05-20 17:49:56 +00:00
|
|
|
Resume a paused resource
|
2016-01-25 11:34:48 +00:00
|
|
|
|
2016-05-20 17:49:56 +00:00
|
|
|
Paused resources will not be reconciled by a controller. By resuming a
|
|
|
|
resource, we allow it to be reconciled again.
|
|
|
|
Currently only deployments support being resumed.`)
|
2016-01-25 11:34:48 +00:00
|
|
|
|
2016-10-07 22:24:42 +00:00
|
|
|
resume_example = templates.Examples(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# Resume an already paused deployment
|
|
|
|
kubectl rollout resume deployment/nginx`)
|
2016-01-25 11:34:48 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func NewCmdRolloutResume(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
2016-08-17 18:28:07 +00:00
|
|
|
options := &ResumeConfig{}
|
2016-01-25 11:34:48 +00:00
|
|
|
|
2016-08-22 02:46:50 +00:00
|
|
|
validArgs := []string{"deployment"}
|
|
|
|
argAliases := kubectl.ResourceAliases(validArgs)
|
|
|
|
|
2016-01-25 11:34:48 +00:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "resume RESOURCE",
|
|
|
|
Short: "Resume a paused resource",
|
|
|
|
Long: resume_long,
|
|
|
|
Example: resume_example,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-05-03 23:41:43 +00:00
|
|
|
allErrs := []error{}
|
2016-08-17 18:28:07 +00:00
|
|
|
err := options.CompleteResume(f, cmd, out, args)
|
2016-05-03 23:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
allErrs = append(allErrs, err)
|
|
|
|
}
|
2016-08-17 18:28:07 +00:00
|
|
|
err = options.RunResume()
|
2016-05-03 23:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
allErrs = append(allErrs, err)
|
|
|
|
}
|
|
|
|
cmdutil.CheckErr(utilerrors.Flatten(utilerrors.NewAggregate(allErrs)))
|
2016-01-25 11:34:48 +00:00
|
|
|
},
|
2016-08-22 02:46:50 +00:00
|
|
|
ValidArgs: validArgs,
|
|
|
|
ArgAliases: argAliases,
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 18:28:07 +00:00
|
|
|
usage := "identifying the resource to get from a server."
|
|
|
|
cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage)
|
2016-01-25 11:34:48 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func (o *ResumeConfig) CompleteResume(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error {
|
2016-08-17 18:28:07 +00:00
|
|
|
if len(args) == 0 && cmdutil.IsFilenameEmpty(o.Filenames) {
|
2016-01-25 11:34:48 +00:00
|
|
|
return cmdutil.UsageError(cmd, cmd.Use)
|
|
|
|
}
|
|
|
|
|
2016-09-16 19:50:34 +00:00
|
|
|
o.Mapper, o.Typer = f.Object()
|
2016-10-27 11:01:50 +00:00
|
|
|
o.Encoder = f.JSONEncoder()
|
|
|
|
|
|
|
|
o.Resumer = f.Resumer
|
2016-01-25 11:34:48 +00:00
|
|
|
o.Out = out
|
|
|
|
|
|
|
|
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:41:43 +00:00
|
|
|
r := resource.NewBuilder(o.Mapper, o.Typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
|
2016-01-25 11:34:48 +00:00
|
|
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
2016-08-17 18:28:07 +00:00
|
|
|
FilenameParam(enforceNamespace, &o.FilenameOptions).
|
2016-01-25 11:34:48 +00:00
|
|
|
ResourceTypeOrNameArgs(true, args...).
|
2016-05-03 23:41:43 +00:00
|
|
|
ContinueOnError().
|
2016-01-25 11:34:48 +00:00
|
|
|
Latest().
|
2016-05-03 23:41:43 +00:00
|
|
|
Flatten().
|
|
|
|
Do()
|
|
|
|
err = r.Err()
|
2016-01-25 11:34:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-03 23:41:43 +00:00
|
|
|
|
|
|
|
err = r.Visit(func(info *resource.Info, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Infos = append(o.Infos, info)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o ResumeConfig) RunResume() error {
|
2016-05-03 23:41:43 +00:00
|
|
|
allErrs := []error{}
|
2016-11-23 05:06:36 +00:00
|
|
|
for _, patch := range set.CalculatePatches(o.Infos, o.Encoder, o.Resumer) {
|
2016-10-27 11:01:50 +00:00
|
|
|
info := patch.Info
|
|
|
|
|
|
|
|
if patch.Err != nil {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("error: %s %q %v", info.Mapping.Resource, info.Name, patch.Err))
|
2016-05-03 23:41:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-10-27 11:01:50 +00:00
|
|
|
|
|
|
|
if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {
|
2016-08-23 18:11:39 +00:00
|
|
|
cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, false, "already resumed")
|
2016-05-03 23:41:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-10-27 11:01:50 +00:00
|
|
|
|
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, api.StrategicMergePatchType, patch.Patch)
|
|
|
|
if err != nil {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("failed to patch: %v", err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Refresh(obj, true)
|
2016-08-23 18:11:39 +00:00
|
|
|
cmdutil.PrintSuccess(o.Mapper, false, o.Out, info.Mapping.Resource, info.Name, false, "resumed")
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
2016-10-27 11:01:50 +00:00
|
|
|
|
2016-05-03 23:41:43 +00:00
|
|
|
return utilerrors.NewAggregate(allErrs)
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|