mirror of https://github.com/k3s-io/k3s
add --dry-run flag to `kubectl apply`
Related StackOverflow: http://stackoverflow.com/questions/38824409/validate-openshift-objects-defined-in-yaml-before-actually-applying-or-executing This patch adds a `--dry-run` flag to the `apply` command in order to allow validation of objects, without actually creating them. If a `--dry-run` flag is present and no validation errors are found, the command will exit before patching or creating any objects. It also adds a `--dry-run` option to the `kubectl create` root command.pull/6/head
parent
ead65fc25f
commit
63e0279d93
|
@ -101,7 +101,8 @@ func NewCmdApply(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|||
cmdutil.AddValidateFlags(cmd)
|
||||
cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on")
|
||||
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
|
||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||
cmdutil.AddDryRunFlag(cmd)
|
||||
cmdutil.AddPrinterFlags(cmd)
|
||||
cmdutil.AddRecordFlag(cmd)
|
||||
cmdutil.AddInclude3rdPartyFlags(cmd)
|
||||
return cmd
|
||||
|
@ -148,6 +149,8 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
|||
return err
|
||||
}
|
||||
|
||||
dryRun := cmdutil.GetFlagBool(cmd, "dry-run")
|
||||
|
||||
encoder := f.JSONEncoder()
|
||||
decoder := f.Decoder(false)
|
||||
|
||||
|
@ -195,6 +198,7 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
|||
}
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
// Then create the resource and skip the three-way merge
|
||||
if err := createAndRefresh(info); err != nil {
|
||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||
|
@ -204,11 +208,14 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
|||
} else {
|
||||
visitedUids.Insert(string(uid))
|
||||
}
|
||||
}
|
||||
|
||||
count++
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "created")
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
|
||||
helper := resource.NewHelper(info.Client, info.Mapping)
|
||||
patcher := NewPatcher(encoder, decoder, info.Mapping, helper, overwrite)
|
||||
|
@ -234,8 +241,9 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
|||
} else {
|
||||
visitedUids.Insert(string(uid))
|
||||
}
|
||||
}
|
||||
count++
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "configured")
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "configured")
|
||||
return nil
|
||||
})
|
||||
|
||||
|
|
|
@ -66,9 +66,10 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|||
cmdutil.AddFilenameOptionFlags(cmd, options, usage)
|
||||
cmd.MarkFlagRequired("filename")
|
||||
cmdutil.AddValidateFlags(cmd)
|
||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||
cmdutil.AddPrinterFlags(cmd)
|
||||
cmdutil.AddApplyAnnotationFlags(cmd)
|
||||
cmdutil.AddRecordFlag(cmd)
|
||||
cmdutil.AddDryRunFlag(cmd)
|
||||
cmdutil.AddInclude3rdPartyFlags(cmd)
|
||||
|
||||
// create subcommands
|
||||
|
@ -116,6 +117,8 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *r
|
|||
return err
|
||||
}
|
||||
|
||||
dryRun := cmdutil.GetFlagBool(cmd, "dry-run")
|
||||
|
||||
count := 0
|
||||
err = r.Visit(func(info *resource.Info, err error) error {
|
||||
if err != nil {
|
||||
|
@ -131,16 +134,19 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *r
|
|||
}
|
||||
}
|
||||
|
||||
if !dryRun {
|
||||
if err := createAndRefresh(info); err != nil {
|
||||
return cmdutil.AddSourceToErr("creating", info.Source, err)
|
||||
}
|
||||
}
|
||||
|
||||
count++
|
||||
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
|
||||
if !shortOutput {
|
||||
f.PrintObjectSpecificMessage(info.Object, out)
|
||||
}
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "created")
|
||||
|
||||
cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue