mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor update for CSI 0.3.0
This commit is contained in:
226
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/replace.go
generated
vendored
226
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/replace.go
generated
vendored
@ -18,21 +18,25 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"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/resource"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
||||
"k8s.io/kubernetes/pkg/kubectl/validation"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -60,8 +64,41 @@ var (
|
||||
kubectl replace --force -f ./pod.json`))
|
||||
)
|
||||
|
||||
func NewCmdReplace(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
options := &resource.FilenameOptions{}
|
||||
type ReplaceOptions struct {
|
||||
PrintFlags *genericclioptions.PrintFlags
|
||||
DeleteFlags *DeleteFlags
|
||||
RecordFlags *genericclioptions.RecordFlags
|
||||
|
||||
DeleteOptions *DeleteOptions
|
||||
|
||||
PrintObj func(obj runtime.Object) error
|
||||
|
||||
createAnnotation bool
|
||||
validate bool
|
||||
|
||||
Schema validation.Schema
|
||||
Builder func() *resource.Builder
|
||||
BuilderArgs []string
|
||||
|
||||
Namespace string
|
||||
EnforceNamespace bool
|
||||
|
||||
Recorder genericclioptions.Recorder
|
||||
|
||||
genericclioptions.IOStreams
|
||||
}
|
||||
|
||||
func NewReplaceOptions(streams genericclioptions.IOStreams) *ReplaceOptions {
|
||||
return &ReplaceOptions{
|
||||
PrintFlags: genericclioptions.NewPrintFlags("replaced"),
|
||||
DeleteFlags: NewDeleteFlags("to use to replace the resource."),
|
||||
|
||||
IOStreams: streams,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCmdReplace(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
|
||||
o := NewReplaceOptions(streams)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "replace -f FILENAME",
|
||||
@ -70,62 +107,106 @@ func NewCmdReplace(f cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
Long: replaceLong,
|
||||
Example: replaceExample,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
|
||||
err := RunReplace(f, out, cmd, args, options)
|
||||
cmdutil.CheckErr(err)
|
||||
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
||||
cmdutil.CheckErr(o.Validate(cmd))
|
||||
cmdutil.CheckErr(o.Run())
|
||||
},
|
||||
}
|
||||
usage := "to use to replace the resource."
|
||||
cmdutil.AddFilenameOptionFlags(cmd, options, usage)
|
||||
|
||||
o.PrintFlags.AddFlags(cmd)
|
||||
o.DeleteFlags.AddFlags(cmd)
|
||||
o.RecordFlags.AddFlags(cmd)
|
||||
|
||||
cmd.MarkFlagRequired("filename")
|
||||
cmd.Flags().Bool("force", false, "Delete and re-create the specified resource")
|
||||
cmd.Flags().Bool("cascade", false, "Only relevant during a force replace. If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController).")
|
||||
cmd.Flags().Int("grace-period", -1, "Only relevant during a force replace. Period of time in seconds given to the old resource to terminate gracefully. Ignored if negative.")
|
||||
cmd.Flags().Duration("timeout", 0, "Only relevant during a force replace. The length of time to wait before giving up on a delete of the old resource, zero means determine a timeout from the size of the object. Any other values should contain a corresponding time unit (e.g. 1s, 2m, 3h).")
|
||||
cmdutil.AddValidateFlags(cmd)
|
||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||
cmdutil.AddApplyAnnotationFlags(cmd)
|
||||
cmdutil.AddRecordFlag(cmd)
|
||||
cmdutil.AddInclude3rdPartyFlags(cmd)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error {
|
||||
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
||||
func (o *ReplaceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
||||
var err error
|
||||
|
||||
o.RecordFlags.Complete(cmd)
|
||||
o.Recorder, err = o.RecordFlags.ToRecorder()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
||||
o.validate = cmdutil.GetFlagBool(cmd, "validate")
|
||||
o.createAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
||||
|
||||
printer, err := o.PrintFlags.ToPrinter()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
o.PrintObj = func(obj runtime.Object) error {
|
||||
return printer.PrintObj(obj, o.Out)
|
||||
}
|
||||
|
||||
dynamicClient, err := f.DynamicClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
deleteOpts := o.DeleteFlags.ToOptions(dynamicClient, o.IOStreams)
|
||||
|
||||
//Replace will create a resource if it doesn't exist already, so ignore not found error
|
||||
deleteOpts.IgnoreNotFound = true
|
||||
if o.PrintFlags.OutputFormat != nil {
|
||||
deleteOpts.Output = *o.PrintFlags.OutputFormat
|
||||
}
|
||||
if deleteOpts.GracePeriod == 0 {
|
||||
// To preserve backwards compatibility, but prevent accidental data loss, we convert --grace-period=0
|
||||
// into --grace-period=1 and wait until the object is successfully deleted.
|
||||
deleteOpts.GracePeriod = 1
|
||||
deleteOpts.WaitForDeletion = true
|
||||
}
|
||||
o.DeleteOptions = deleteOpts
|
||||
|
||||
schema, err := f.Validator(o.validate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
force := cmdutil.GetFlagBool(cmd, "force")
|
||||
if cmdutil.IsFilenameSliceEmpty(options.Filenames) {
|
||||
return cmdutil.UsageErrorf(cmd, "Must specify --filename to replace")
|
||||
o.Schema = schema
|
||||
o.Builder = f.NewBuilder
|
||||
o.BuilderArgs = args
|
||||
|
||||
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
if force {
|
||||
return forceReplace(f, out, cmd, args, shortOutput, options)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if cmdutil.GetFlagInt(cmd, "grace-period") >= 0 {
|
||||
func (o *ReplaceOptions) Validate(cmd *cobra.Command) error {
|
||||
if o.DeleteOptions.GracePeriod >= 0 && !o.DeleteOptions.ForceDeletion {
|
||||
return fmt.Errorf("--grace-period must have --force specified")
|
||||
}
|
||||
|
||||
if cmdutil.GetFlagDuration(cmd, "timeout") != 0 {
|
||||
if o.DeleteOptions.Timeout != 0 && !o.DeleteOptions.ForceDeletion {
|
||||
return fmt.Errorf("--timeout must have --force specified")
|
||||
}
|
||||
|
||||
r := f.NewBuilder().
|
||||
if cmdutil.IsFilenameSliceEmpty(o.DeleteOptions.FilenameOptions.Filenames) {
|
||||
return cmdutil.UsageErrorf(cmd, "Must specify --filename to replace")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *ReplaceOptions) Run() error {
|
||||
if o.DeleteOptions.ForceDeletion {
|
||||
return o.forceReplace()
|
||||
}
|
||||
|
||||
r := o.Builder().
|
||||
Unstructured().
|
||||
Schema(schema).
|
||||
Schema(o.Schema).
|
||||
ContinueOnError().
|
||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, options).
|
||||
NamespaceParam(o.Namespace).DefaultNamespace().
|
||||
FilenameParam(o.EnforceNamespace, &o.DeleteOptions.FilenameOptions).
|
||||
Flatten().
|
||||
Do()
|
||||
if err := r.Err(); err != nil {
|
||||
@ -137,14 +218,12 @@ func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
||||
return err
|
||||
}
|
||||
|
||||
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, cmdutil.InternalVersionJSONEncoder()); err != nil {
|
||||
if err := kubectl.CreateOrUpdateAnnotation(o.createAnnotation, info.Object, cmdutil.InternalVersionJSONEncoder()); err != nil {
|
||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||
}
|
||||
|
||||
if cmdutil.ShouldRecord(cmd, info) {
|
||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||
}
|
||||
if err := o.Recorder.Record(info.Object); err != nil {
|
||||
glog.V(4).Infof("error recording current command: %v", err)
|
||||
}
|
||||
|
||||
// Serialize the object with the annotation applied.
|
||||
@ -154,23 +233,12 @@ func RunReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
||||
}
|
||||
|
||||
info.Refresh(obj, true)
|
||||
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "replaced")
|
||||
return nil
|
||||
return o.PrintObj(info.Object)
|
||||
})
|
||||
}
|
||||
|
||||
func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *resource.FilenameOptions) error {
|
||||
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, filename := range options.Filenames {
|
||||
func (o *ReplaceOptions) forceReplace() error {
|
||||
for i, filename := range o.DeleteOptions.FilenameOptions.Filenames {
|
||||
if filename == "-" {
|
||||
tempDir, err := ioutil.TempDir("", "kubectl_replace_")
|
||||
if err != nil {
|
||||
@ -182,53 +250,36 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options.Filenames[i] = tempFilename
|
||||
o.DeleteOptions.FilenameOptions.Filenames[i] = tempFilename
|
||||
}
|
||||
}
|
||||
|
||||
r := f.NewBuilder().
|
||||
r := o.Builder().
|
||||
Unstructured().
|
||||
ContinueOnError().
|
||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, options).
|
||||
ResourceTypeOrNameArgs(false, args...).RequireObject(false).
|
||||
NamespaceParam(o.Namespace).DefaultNamespace().
|
||||
ResourceTypeOrNameArgs(false, o.BuilderArgs...).RequireObject(false).
|
||||
FilenameParam(o.EnforceNamespace, &o.DeleteOptions.FilenameOptions).
|
||||
Flatten().
|
||||
Do()
|
||||
if err := r.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//Replace will create a resource if it doesn't exist already, so ignore not found error
|
||||
ignoreNotFound := true
|
||||
timeout := cmdutil.GetFlagDuration(cmd, "timeout")
|
||||
gracePeriod := cmdutil.GetFlagInt(cmd, "grace-period")
|
||||
waitForDeletion := false
|
||||
if gracePeriod == 0 {
|
||||
// To preserve backwards compatibility, but prevent accidental data loss, we convert --grace-period=0
|
||||
// into --grace-period=1 and wait until the object is successfully deleted.
|
||||
gracePeriod = 1
|
||||
waitForDeletion = true
|
||||
}
|
||||
// By default use a reaper to delete all related resources.
|
||||
if cmdutil.GetFlagBool(cmd, "cascade") {
|
||||
glog.Warningf("\"cascade\" is set, kubectl will delete and re-create all resources managed by this resource (e.g. Pods created by a ReplicationController). Consider using \"kubectl rolling-update\" if you want to update a ReplicationController together with its Pods.")
|
||||
err = ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, timeout, gracePeriod, waitForDeletion, shortOutput, false)
|
||||
} else {
|
||||
err = DeleteResult(r, out, ignoreNotFound, gracePeriod, shortOutput)
|
||||
}
|
||||
if err != nil {
|
||||
if err := o.DeleteOptions.DeleteResult(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
timeout := o.DeleteOptions.Timeout
|
||||
if timeout == 0 {
|
||||
timeout = kubectl.Timeout
|
||||
timeout = 5 * time.Minute
|
||||
}
|
||||
err = r.Visit(func(info *resource.Info, err error) error {
|
||||
err := r.Visit(func(info *resource.Info, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wait.PollImmediate(kubectl.Interval, timeout, func() (bool, error) {
|
||||
return wait.PollImmediate(1*time.Second, timeout, func() (bool, error) {
|
||||
if err := info.Get(); !errors.IsNotFound(err) {
|
||||
return false, err
|
||||
}
|
||||
@ -239,12 +290,12 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
return err
|
||||
}
|
||||
|
||||
r = f.NewBuilder().
|
||||
r = o.Builder().
|
||||
Unstructured().
|
||||
Schema(schema).
|
||||
Schema(o.Schema).
|
||||
ContinueOnError().
|
||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, options).
|
||||
NamespaceParam(o.Namespace).DefaultNamespace().
|
||||
FilenameParam(o.EnforceNamespace, &o.DeleteOptions.FilenameOptions).
|
||||
Flatten().
|
||||
Do()
|
||||
err = r.Err()
|
||||
@ -258,14 +309,12 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
return err
|
||||
}
|
||||
|
||||
if err := kubectl.CreateOrUpdateAnnotation(cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag), info, cmdutil.InternalVersionJSONEncoder()); err != nil {
|
||||
if err := kubectl.CreateOrUpdateAnnotation(o.createAnnotation, info.Object, cmdutil.InternalVersionJSONEncoder()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cmdutil.ShouldRecord(cmd, info) {
|
||||
if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil {
|
||||
return cmdutil.AddSourceToErr("replacing", info.Source, err)
|
||||
}
|
||||
if err := o.Recorder.Record(info.Object); err != nil {
|
||||
glog.V(4).Infof("error recording current command: %v", err)
|
||||
}
|
||||
|
||||
obj, err := resource.NewHelper(info.Client, info.Mapping).Create(info.Namespace, true, info.Object)
|
||||
@ -275,8 +324,7 @@ func forceReplace(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
|
||||
count++
|
||||
info.Refresh(obj, true)
|
||||
cmdutil.PrintSuccess(shortOutput, out, info.Object, false, "replaced")
|
||||
return nil
|
||||
return o.PrintObj(info.Object)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user