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.
This commit is contained in:
		@@ -101,7 +101,8 @@ func NewCmdApply(f *cmdutil.Factory, out io.Writer) *cobra.Command {
 | 
				
			|||||||
	cmdutil.AddValidateFlags(cmd)
 | 
						cmdutil.AddValidateFlags(cmd)
 | 
				
			||||||
	cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on")
 | 
						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.")
 | 
						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.AddRecordFlag(cmd)
 | 
				
			||||||
	cmdutil.AddInclude3rdPartyFlags(cmd)
 | 
						cmdutil.AddInclude3rdPartyFlags(cmd)
 | 
				
			||||||
	return cmd
 | 
						return cmd
 | 
				
			||||||
@@ -148,6 +149,8 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dryRun := cmdutil.GetFlagBool(cmd, "dry-run")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	encoder := f.JSONEncoder()
 | 
						encoder := f.JSONEncoder()
 | 
				
			||||||
	decoder := f.Decoder(false)
 | 
						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
 | 
									// Then create the resource and skip the three-way merge
 | 
				
			||||||
				if err := createAndRefresh(info); err != nil {
 | 
									if err := createAndRefresh(info); err != nil {
 | 
				
			||||||
					return cmdutil.AddSourceToErr("creating", info.Source, err)
 | 
										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 {
 | 
									} else {
 | 
				
			||||||
					visitedUids.Insert(string(uid))
 | 
										visitedUids.Insert(string(uid))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			count++
 | 
								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
 | 
								return nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if !dryRun {
 | 
				
			||||||
			overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
 | 
								overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
 | 
				
			||||||
			helper := resource.NewHelper(info.Client, info.Mapping)
 | 
								helper := resource.NewHelper(info.Client, info.Mapping)
 | 
				
			||||||
			patcher := NewPatcher(encoder, decoder, info.Mapping, helper, overwrite)
 | 
								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 {
 | 
								} else {
 | 
				
			||||||
				visitedUids.Insert(string(uid))
 | 
									visitedUids.Insert(string(uid))
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		count++
 | 
							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
 | 
							return nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -66,9 +66,10 @@ func NewCmdCreate(f *cmdutil.Factory, out io.Writer) *cobra.Command {
 | 
				
			|||||||
	cmdutil.AddFilenameOptionFlags(cmd, options, usage)
 | 
						cmdutil.AddFilenameOptionFlags(cmd, options, usage)
 | 
				
			||||||
	cmd.MarkFlagRequired("filename")
 | 
						cmd.MarkFlagRequired("filename")
 | 
				
			||||||
	cmdutil.AddValidateFlags(cmd)
 | 
						cmdutil.AddValidateFlags(cmd)
 | 
				
			||||||
	cmdutil.AddOutputFlagsForMutation(cmd)
 | 
						cmdutil.AddPrinterFlags(cmd)
 | 
				
			||||||
	cmdutil.AddApplyAnnotationFlags(cmd)
 | 
						cmdutil.AddApplyAnnotationFlags(cmd)
 | 
				
			||||||
	cmdutil.AddRecordFlag(cmd)
 | 
						cmdutil.AddRecordFlag(cmd)
 | 
				
			||||||
 | 
						cmdutil.AddDryRunFlag(cmd)
 | 
				
			||||||
	cmdutil.AddInclude3rdPartyFlags(cmd)
 | 
						cmdutil.AddInclude3rdPartyFlags(cmd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// create subcommands
 | 
						// create subcommands
 | 
				
			||||||
@@ -116,6 +117,8 @@ func RunCreate(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *r
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dryRun := cmdutil.GetFlagBool(cmd, "dry-run")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	count := 0
 | 
						count := 0
 | 
				
			||||||
	err = r.Visit(func(info *resource.Info, err error) error {
 | 
						err = r.Visit(func(info *resource.Info, err error) error {
 | 
				
			||||||
		if err != nil {
 | 
							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 {
 | 
								if err := createAndRefresh(info); err != nil {
 | 
				
			||||||
				return cmdutil.AddSourceToErr("creating", info.Source, err)
 | 
									return cmdutil.AddSourceToErr("creating", info.Source, err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		count++
 | 
							count++
 | 
				
			||||||
		shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
 | 
							shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
 | 
				
			||||||
		if !shortOutput {
 | 
							if !shortOutput {
 | 
				
			||||||
			f.PrintObjectSpecificMessage(info.Object, out)
 | 
								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
 | 
							return nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user