diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index 669f5f1e138..ba3192c37bd 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -56,6 +56,7 @@ type ApplyOptions struct { GracePeriod int PruneResources []pruneResource Timeout time.Duration + cmdBaseName string } const ( @@ -65,8 +66,6 @@ const ( backOffPeriod = 1 * time.Second // how many times we can retry before back off triesBeforeBackOff = 1 - - warningNoLastAppliedConfigAnnotation = "Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply\n" ) var ( @@ -92,11 +91,17 @@ 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" ) -func NewCmdApply(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { +func NewCmdApply(baseName string, f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { var options ApplyOptions + // Store baseName for use in printing warnings / messages involving the base command name. + // This is useful for downstream command that wrap this one. + options.cmdBaseName = baseName + cmd := &cobra.Command{ Use: "apply -f FILENAME", Short: i18n.T("Apply a configuration to a resource by filename or stdin"), @@ -299,7 +304,7 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti return err } if _, ok := annotationMap[api.LastAppliedConfigAnnotation]; !ok { - fmt.Fprintf(errOut, warningNoLastAppliedConfigAnnotation) + fmt.Fprintf(errOut, warningNoLastAppliedConfigAnnotation, options.cmdBaseName) } overwrite := cmdutil.GetFlagBool(cmd, "overwrite") helper := resource.NewHelper(info.Client, info.Mapping) diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index ef0f9c394da..e166edae0c8 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -47,7 +47,7 @@ func TestApplyExtraArgsFail(t *testing.T) { errBuf := bytes.NewBuffer([]byte{}) f, _, _, _ := cmdtesting.NewAPIFactory() - c := NewCmdApply(f, buf, errBuf) + c := NewCmdApply("kubectl", f, buf, errBuf) if validateApplyArgs(c, []string{"rc"}) == nil { t.Fatalf("unexpected non-error") } @@ -377,14 +377,14 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameRC) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) // uses the name from the file, not the response expectRC := "replicationcontroller/" + nameRC + "\n" - expectWarning := warningNoLastAppliedConfigAnnotation + expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "kubectl") if errBuf.String() != expectWarning { t.Fatalf("unexpected non-warning: %s\nexpected: %s", errBuf.String(), expectWarning) } @@ -422,7 +422,7 @@ func TestApplyObject(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameRC) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) @@ -479,7 +479,7 @@ func TestApplyObjectOutput(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameRC) cmd.Flags().Set("output", "yaml") cmd.Run(cmd, []string{}) @@ -533,7 +533,7 @@ func TestApplyRetry(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameRC) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) @@ -578,7 +578,7 @@ func TestApplyNonExistObject(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameRC) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) @@ -636,7 +636,7 @@ func testApplyMultipleObjects(t *testing.T, asList bool) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) if asList { cmd.Flags().Set("filename", filenameRCSVC) } else { @@ -729,7 +729,7 @@ func TestApplyNULLPreservation(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameDeployObjClientside) cmd.Flags().Set("output", "name") @@ -789,7 +789,7 @@ func TestUnstructuredApply(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameWidgetClientside) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) @@ -876,7 +876,7 @@ func TestUnstructuredIdempotentApply(t *testing.T) { buf := bytes.NewBuffer([]byte{}) errBuf := bytes.NewBuffer([]byte{}) - cmd := NewCmdApply(f, buf, errBuf) + cmd := NewCmdApply("kubectl", f, buf, errBuf) cmd.Flags().Set("filename", filenameWidgetClientside) cmd.Flags().Set("output", "name") cmd.Run(cmd, []string{}) diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 3d61edd280f..51004668c24 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -337,7 +337,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob { Message: "Advanced Commands:", Commands: []*cobra.Command{ - NewCmdApply(f, out, err), + NewCmdApply("kubectl", f, out, err), NewCmdPatch(f, out), NewCmdReplace(f, out), deprecatedAlias("update", NewCmdReplace(f, out)),