Fixes problem where kubectl apply stops after first error

Kubernetes-commit: b75990cc7bccc5693df34e29745eea0c98d095d1
This commit is contained in:
Sean R. Sullivan 2020-03-26 13:24:33 -07:00 committed by Kubernetes Publisher
parent 6b2a72dcaa
commit 26ca5b3402
1 changed files with 150 additions and 132 deletions

View File

@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
@ -371,15 +372,40 @@ func (o *ApplyOptions) Run() error {
// Generates the objects using the resource builder if they have not
// already been stored by calling "SetObjects()" in the pre-processor.
errs := []error{}
infos, err := o.GetObjects()
if err != nil {
return err
errs = append(errs, err)
}
if len(infos) == 0 {
if len(infos) == 0 && len(errs) == 0 {
return fmt.Errorf("no objects passed to apply")
}
// Iterate through all objects, applying each one.
for _, info := range infos {
if err := o.applyOneObject(info); err != nil {
errs = append(errs, err)
}
}
// If any errors occurred during apply, then return error (or
// aggregate of errors).
if len(errs) == 1 {
return errs[0]
}
if len(errs) > 1 {
return utilerrors.NewAggregate(errs)
}
if o.PostProcessorFn != nil {
klog.V(4).Infof("Running apply post-processor function")
if err := o.PostProcessorFn(); err != nil {
return err
}
}
return nil
}
func (o *ApplyOptions) applyOneObject(info *resource.Info) error {
o.MarkNamespaceVisited(info)
if err := o.Recorder.Record(info.Object); err != nil {
@ -440,7 +466,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
}
if o.shouldPrintObject() {
continue
return nil
}
printer, err := o.ToPrinter("serverside-applied")
@ -451,7 +477,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
if err = printer.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
return nil
}
// Get the modified configuration of the object. Embed the result
@ -494,7 +520,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
}
if o.shouldPrintObject() {
continue
return nil
}
printer, err := o.ToPrinter("created")
@ -504,7 +530,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
if err = printer.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
return nil
}
if err := o.MarkObjectVisited(info); err != nil {
@ -537,12 +563,12 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
if err = printer.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
return nil
}
}
if o.shouldPrintObject() {
continue
return nil
}
printer, err := o.ToPrinter("configured")
@ -552,14 +578,6 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
if err = printer.PrintObj(info.Object, o.Out); err != nil {
return err
}
}
if o.PostProcessorFn != nil {
klog.V(4).Infof("Running apply post-processor function")
if err := o.PostProcessorFn(); err != nil {
return err
}
}
return nil
}