mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #26136 from AdoHe/kubectl_apply_overwrite
Automatic merge from submit-queue kubectl apply add --overwrite flag ```release-note Add "kubectl --overwrite" flag to automatically resolve conflicts between the modified and live configuration using values from the modified configuration. ``` fixes #17238 This PR just add the `--overwrite` flag, pkg/strategicpatch has already support overwrite. @bgrant0607 @jackgr ptal.
This commit is contained in:
commit
f50bb73ed6
@ -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"
|
||||
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
||||
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.AddRecursiveFlag(cmd, &options.Recursive)
|
||||
cmdutil.AddOutputFlagsForMutation(cmd)
|
||||
@ -170,8 +171,9 @@ func RunApply(f *cmdutil.Factory, cmd *cobra.Command, out io.Writer, options *Ap
|
||||
return nil
|
||||
}
|
||||
|
||||
overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
|
||||
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)
|
||||
if err != nil {
|
||||
@ -212,15 +214,17 @@ type patcher struct {
|
||||
mapping *meta.RESTMapping
|
||||
helper *resource.Helper
|
||||
|
||||
overwrite bool
|
||||
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{
|
||||
encoder: encoder,
|
||||
decoder: decoder,
|
||||
mapping: mapping,
|
||||
helper: helper,
|
||||
overwrite: overwrite,
|
||||
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.
|
||||
patch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, true)
|
||||
patch, err := strategicpatch.CreateThreeWayMergePatch(original, modified, current, versionedObject, p.overwrite)
|
||||
if err != nil {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user