From 9c163f0a0a97a9bcf10c3f91d2cee02567700d84 Mon Sep 17 00:00:00 2001 From: SataQiu <1527062125@qq.com> Date: Wed, 29 Jul 2020 10:39:59 +0800 Subject: [PATCH] kubectl: improve the warning message when doing kubectl apply to the resource without expected annotation Signed-off-by: SataQiu <1527062125@qq.com> --- staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go | 4 ++-- staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 7531f581af1..e1ae85b6e8e 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -132,7 +132,7 @@ var ( # Apply the configuration in manifest.yaml and delete all the other configmaps that are not in the file. kubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap`)) - warningNoLastAppliedConfigAnnotation = "Warning: %[1]s apply should be used on resource created by either %[1]s create --save-config or %[1]s apply\n" + 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" ) // NewApplyOptions creates new ApplyOptions for the `apply` command @@ -538,7 +538,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err) metadata, _ := meta.Accessor(info.Object) annotationMap := metadata.GetAnnotations() if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok { - fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, o.cmdBaseName) + fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, info.ObjectName(), corev1.LastAppliedConfigAnnotation, o.cmdBaseName) } patcher, err := newPatcher(o, info, helper) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go index 7fec5564bbd..1d199ebfcc2 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go @@ -504,7 +504,7 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) { // uses the name from the file, not the response expectRC := "replicationcontroller/" + nameRC + "\n" - expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "kubectl") + expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "replicationcontrollers/test-rc", corev1.LastAppliedConfigAnnotation, "kubectl") if errBuf.String() != expectWarning { t.Fatalf("unexpected non-warning: %s\nexpected: %s", errBuf.String(), expectWarning) }