mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
kubectl apply add --overwrite flag
This commit is contained in:
parent
4196be053b
commit
ddbd539d1e
@ -85,6 +85,7 @@ func NewCmdApply(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
|||||||
usage := "Filename, directory, or URL to file that contains the configuration to apply"
|
usage := "Filename, directory, or URL to file that contains the configuration to apply"
|
||||||
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
||||||
cmd.MarkFlagRequired("filename")
|
cmd.MarkFlagRequired("filename")
|
||||||
|
cmd.Flags().Bool("overwrite", true, "Automatically resolve conflicts between the modified and live configuration by using values from the modified configuration")
|
||||||
cmdutil.AddValidateFlags(cmd)
|
cmdutil.AddValidateFlags(cmd)
|
||||||
cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
|
cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
|
||||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||||
@ -170,8 +171,9 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
patcher := NewPatcher(encoder, decoder, info.Mapping, helper, overwrite)
|
||||||
|
|
||||||
patchBytes, err := patcher.patch(info.Object, modified, info.Source, info.Namespace, info.Name)
|
patchBytes, err := patcher.patch(info.Object, modified, info.Source, info.Namespace, info.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -212,15 +214,17 @@ type patcher struct {
|
|||||||
mapping *meta.RESTMapping
|
mapping *meta.RESTMapping
|
||||||
helper *resource.Helper
|
helper *resource.Helper
|
||||||
|
|
||||||
|
overwrite bool
|
||||||
backOff clockwork.Clock
|
backOff clockwork.Clock
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPatcher(encoder runtime.Encoder, decoder runtime.Decoder, mapping *meta.RESTMapping, helper *resource.Helper) *patcher {
|
func NewPatcher(encoder runtime.Encoder, decoder runtime.Decoder, mapping *meta.RESTMapping, helper *resource.Helper, overwrite bool) *patcher {
|
||||||
return &patcher{
|
return &patcher{
|
||||||
encoder: encoder,
|
encoder: encoder,
|
||||||
decoder: decoder,
|
decoder: decoder,
|
||||||
mapping: mapping,
|
mapping: mapping,
|
||||||
helper: helper,
|
helper: helper,
|
||||||
|
overwrite: overwrite,
|
||||||
backOff: clockwork.NewRealClock(),
|
backOff: clockwork.NewRealClock(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,7 +253,7 @@ func (p *patcher) patchSimple(obj runtime.Object, modified []byte, source, names
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compute a three way strategic merge patch to send to server.
|
// Compute a three way strategic merge patch to send to server.
|
||||||
patch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, true)
|
patch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, p.overwrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
format := "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor:"
|
format := "creating patch with:\noriginal:\n%s\nmodified:\n%s\ncurrent:\n%s\nfor:"
|
||||||
return nil, cmdutil.AddSourceToErr(fmt.Sprintf(format, original, modified, current), source, err)
|
return nil, cmdutil.AddSourceToErr(fmt.Sprintf(format, original, modified, current), source, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user