Merge pull request #89539 from seans3/kubectl-apply-fix

Fixes problem where kubectl apply stops after first error
This commit is contained in:
Kubernetes Prow Robot 2020-03-27 17:11:53 -07:00 committed by GitHub
commit 2d4253077d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 132 deletions

19
hack/testdata/multi-resource.yaml vendored Normal file
View File

@ -0,0 +1,19 @@
# Tests that initial failures to not block subsequent applies.
# Pod must be before namespace, so it initially fails. Second
# apply of pod should succeed, since namespace finally exists.
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: multi-resource-ns
labels:
name: test-pod-label
spec:
containers:
- name: kubernetes-pause
image: k8s.gcr.io/pause:2.0
---
apiVersion: v1
kind: Namespace
metadata:
name: multi-resource-ns

View File

@ -22,6 +22,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/jsonmergepatch:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/mergepatch:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",

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
}

View File

@ -274,6 +274,19 @@ __EOF__
# cleanup
kubectl delete --kustomize hack/testdata/kustomize
## kubectl apply multiple resources with initial failure.
# Pre-Condition: no POD exists
kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
# First pass, namespace is created, but pod is not (since namespace does not exist yet).
kubectl apply -f hack/testdata/multi-resource.yaml "${kube_flags[@]:?}"
output_message=$(! kubectl get pods test-pod 2>&1 "${kube_flags[@]:?}")
kube::test::if_has_string "${output_message}" 'pods "test-pod" not found'
# Second pass, pod is created (now that namespace exists).
kubectl apply -f hack/testdata/multi-resource.yaml "${kube_flags[@]:?}"
kube::test::get_object_assert 'pod test-pod' "{{${id_field}}}" 'test-pod'
# cleanup
kubectl delete -f hack/testdata/multi-resource.yaml
set +o nounset
set +o errexit
}