Merge pull request #95544 from SaiHarshaK/warn-race

warn user about resource being deleted
This commit is contained in:
Kubernetes Prow Robot 2020-11-05 09:25:21 -08:00 committed by GitHub
commit 374989e461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -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())
}
}

View File

@ -564,6 +564,9 @@ func (o *DiffOptions) Run() error {
break
}
}
apply.WarnIfDeleting(info.Object, o.Diff.ErrOut)
return err
})
if err != nil {