diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go index f85247e3912..2722af720c7 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -18,6 +18,7 @@ package apply import ( "fmt" + "io" "net/http" "github.com/spf13/cobra" @@ -133,6 +134,7 @@ var ( kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`)) warningNoLastAppliedConfigAnnotation = "Warning: resource %[1]s is missing the %[2]s annotation which is required by %[3]s apply. %[3]s apply should only be used on resources created declaratively by either %[3]s create --save-config or %[3]s apply. The missing annotation will be patched automatically.\n" + warningChangesOnDeletingResource = "Warning: Detected changes to resource %[1]s which is currently being deleted.\n" ) // NewApplyOptions creates new ApplyOptions for the `apply` command @@ -469,6 +471,8 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err) info.Refresh(obj, true) + WarnIfDeleting(info.Object, o.ErrOut) + if err := o.MarkObjectVisited(info); err != nil { return err } @@ -556,6 +560,8 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err) info.Refresh(patchedObject, true) + WarnIfDeleting(info.Object, o.ErrOut) + if string(patchBytes) == "{}" && !o.shouldPrintObject() { printer, err := o.ToPrinter("unchanged") if err != nil { @@ -708,3 +714,12 @@ func GetApplyFieldManagerFlag(cmd *cobra.Command, serverSide bool) string { return FieldManagerClientSideApply } + +// WarnIfDeleting prints a warning if a resource is being deleted +func WarnIfDeleting(obj runtime.Object, stderr io.Writer) { + metadata, _ := meta.Accessor(obj) + if metadata != nil && metadata.GetDeletionTimestamp() != nil { + // just warn the user about the conflict + fmt.Fprintf(stderr, warningChangesOnDeletingResource, metadata.GetName()) + } +} diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go index 75ee84b7d6b..74984350648 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/diff/diff.go @@ -564,6 +564,9 @@ func (o *DiffOptions) Run() error { break } } + + apply.WarnIfDeleting(info.Object, o.Diff.ErrOut) + return err }) if err != nil {