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"
|
|
|
|
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-01-16 20:13:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2017-01-11 14:09:48 +00:00
|
|
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
2016-01-25 11:34:48 +00:00
|
|
|
"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"
|
2017-01-26 06:16:44 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/i18n"
|
2016-01-25 11:34:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PauseConfig 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 PauseConfig struct {
|
2016-08-17 18:28:07 +00:00
|
|
|
resource.FilenameOptions
|
|
|
|
|
2016-11-14 11:53:27 +00:00
|
|
|
Pauser func(info *resource.Info) ([]byte, error)
|
2016-10-27 11:01:50 +00:00
|
|
|
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
|
|
|
pause_long = templates.LongDesc(`
|
2016-05-20 17:49:56 +00:00
|
|
|
Mark the provided resource as paused
|
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.
|
2017-05-18 22:46:20 +00:00
|
|
|
Use "kubectl rollout resume" to resume a paused resource.
|
2016-05-20 17:49:56 +00:00
|
|
|
Currently only deployments support being paused.`)
|
2016-01-25 11:34:48 +00:00
|
|
|
|
2016-10-07 22:24:42 +00:00
|
|
|
pause_example = templates.Examples(`
|
2016-05-20 17:49:56 +00:00
|
|
|
# Mark the nginx deployment as paused. Any current state of
|
|
|
|
# the deployment will continue its function, new updates to the deployment will not
|
|
|
|
# have an effect as long as the deployment is paused.
|
|
|
|
kubectl rollout pause deployment/nginx`)
|
2016-01-25 11:34:48 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 00:18:39 +00:00
|
|
|
func NewCmdRolloutPause(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
2016-08-17 18:28:07 +00:00
|
|
|
options := &PauseConfig{}
|
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: "pause RESOURCE",
|
2017-01-26 06:16:44 +00:00
|
|
|
Short: i18n.T("Mark the provided resource as paused"),
|
2016-01-25 11:34:48 +00:00
|
|
|
Long: pause_long,
|
|
|
|
Example: pause_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.CompletePause(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.RunPause()
|
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 *PauseConfig) CompletePause(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string) error {
|
2017-06-14 21:14:42 +00:00
|
|
|
if len(args) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames) {
|
|
|
|
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use)
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
|
|
|
|
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.Pauser = f.Pauser
|
2016-01-25 11:34:48 +00:00
|
|
|
o.Out = out
|
|
|
|
|
|
|
|
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-16 21:52:51 +00:00
|
|
|
r := f.NewBuilder(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
|
|
|
|
|
|
|
o.Infos, err = r.Infos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-25 11:34:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o PauseConfig) RunPause() 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.Pauser) {
|
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 paused")
|
2016-05-03 23:41:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-10-27 11:01:50 +00:00
|
|
|
|
2017-01-16 20:13:59 +00:00
|
|
|
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
|
2016-10-27 11:01:50 +00:00
|
|
|
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, "paused")
|
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
|
|
|
}
|