Inverted error handling to ensure server-side apply does not fall back on client-side apply when there is an error

This commit is contained in:
Josh Samuels 2019-06-10 20:33:59 -07:00
parent 8de1569dda
commit 4b29036db9

View File

@ -398,6 +398,7 @@ func (o *ApplyOptions) Run() error {
if err != nil { if err != nil {
return cmdutil.AddSourceToErr("serverside-apply", info.Source, err) return cmdutil.AddSourceToErr("serverside-apply", info.Source, err)
} }
options := metav1.PatchOptions{ options := metav1.PatchOptions{
Force: &o.ForceConflicts, Force: &o.ForceConflicts,
FieldManager: o.FieldManager, FieldManager: o.FieldManager,
@ -405,6 +406,7 @@ func (o *ApplyOptions) Run() error {
if o.ServerDryRun { if o.ServerDryRun {
options.DryRun = []string{metav1.DryRunAll} options.DryRun = []string{metav1.DryRunAll}
} }
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch( obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(
info.Namespace, info.Namespace,
info.Name, info.Name,
@ -412,29 +414,33 @@ func (o *ApplyOptions) Run() error {
data, data,
&options, &options,
) )
if err == nil { if err != nil {
info.Refresh(obj, true) if isIncompatibleServerError(err) {
metadata, err := meta.Accessor(info.Object) klog.Warningf("serverside-apply incompatible server: %v", err)
if err != nil {
return err
} }
visitedUids.Insert(string(metadata.GetUID()))
count++
if len(output) > 0 && !shortOutput {
objs = append(objs, info.Object)
return nil
}
printer, err := o.ToPrinter("serverside-applied")
if err != nil {
return err
}
return printer.PrintObj(info.Object, o.Out)
} else if !isIncompatibleServerError(err) {
return err return err
} }
// If we're talking to a server which does not implement server-side apply,
// continue with the client side apply after this block. info.Refresh(obj, true)
klog.Warningf("serverside-apply incompatible server: %v", err) metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
visitedUids.Insert(string(metadata.GetUID()))
count++
if len(output) > 0 && !shortOutput {
objs = append(objs, info.Object)
return nil
}
printer, err := o.ToPrinter("serverside-applied")
if err != nil {
return err
}
return printer.PrintObj(info.Object, o.Out)
} }
// Get the modified configuration of the object. Embed the result // Get the modified configuration of the object. Embed the result