Skip to content

Commit

Permalink
kubectl apply add --overwrite flag
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Jul 18, 2016
1 parent 4196be0 commit ddbd539
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pkg/kubectl/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -212,16 +214,18 @@ type patcher struct {
mapping *meta.RESTMapping
helper *resource.Helper

backOff clockwork.Clock
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,
backOff: clockwork.NewRealClock(),
encoder: encoder,
decoder: decoder,
mapping: mapping,
helper: helper,
overwrite: overwrite,
backOff: clockwork.NewRealClock(),
}
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ddbd539

Please sign in to comment.