From b87ee3dff7336bd7c5ac112c3ad50d321fe35b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Py=C5=BCalski?= Date: Wed, 15 Jun 2016 13:35:56 +0200 Subject: [PATCH] Validation logic applied to edited file The file that is submitted via edit is now subject to validation logic as any other file. The validation flags were added to the edit command. Fixes: #17542 --- pkg/kubectl/cmd/edit.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/kubectl/cmd/edit.go b/pkg/kubectl/cmd/edit.go index 8c7f5a449a4..cf35a499c9a 100644 --- a/pkg/kubectl/cmd/edit.go +++ b/pkg/kubectl/cmd/edit.go @@ -120,6 +120,7 @@ func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command { usage := "Filename, directory, or URL to file to use to edit the resource" kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage) cmdutil.AddRecursiveFlag(cmd, &options.Recursive) + cmdutil.AddValidateFlags(cmd) cmd.Flags().StringP("output", "o", "yaml", "Output format. One of: yaml|json.") cmd.Flags().String("output-version", "", "Output the formatted object with the given group version (for ex: 'extensions/v1beta1').") cmd.Flags().Bool("windows-line-endings", gruntime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)") @@ -253,6 +254,16 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args } glog.V(4).Infof("User edited:\n%s", string(edited)) + // Apply validation + schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir")) + if err != nil { + return preservedFile(err, file, errOut) + } + err = schema.ValidateBytes(stripComments(edited)) + if err != nil { + return preservedFile(err, file, errOut) + } + // Compare content without comments if bytes.Equal(stripComments(original), stripComments(edited)) { os.Remove(file)