mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Merge pull request #48894 from juanvallejo/jvallejo/remove-hardcoded-kubectl-in-apply-warn-msg
Automatic merge from submit-queue (batch tested with PRs 47066, 48892, 48933, 48854, 48894) replace hardcoded use of "kubectl" in apply warning msg **Release note**: ```release-note NONE ``` Removes use of hardcoded "kubectl" in the `kubectl apply ...` warning message that is printed when the `last-applied-configuration` annotation is missing on a resource. This is useful for downstream wrappers around the `apply` command. cc @stevekuznetsov @fabianofranz
This commit is contained in:
commit
2610b9cf52
@ -56,6 +56,7 @@ type ApplyOptions struct {
|
|||||||
GracePeriod int
|
GracePeriod int
|
||||||
PruneResources []pruneResource
|
PruneResources []pruneResource
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
cmdBaseName string
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -65,8 +66,6 @@ const (
|
|||||||
backOffPeriod = 1 * time.Second
|
backOffPeriod = 1 * time.Second
|
||||||
// how many times we can retry before back off
|
// how many times we can retry before back off
|
||||||
triesBeforeBackOff = 1
|
triesBeforeBackOff = 1
|
||||||
|
|
||||||
warningNoLastAppliedConfigAnnotation = "Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply\n"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -92,11 +91,17 @@ var (
|
|||||||
|
|
||||||
# Apply the configuration in manifest.yaml and delete all the other configmaps that are not in the file.
|
# 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`))
|
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
|
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{
|
cmd := &cobra.Command{
|
||||||
Use: "apply -f FILENAME",
|
Use: "apply -f FILENAME",
|
||||||
Short: i18n.T("Apply a configuration to a resource by filename or stdin"),
|
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
|
return err
|
||||||
}
|
}
|
||||||
if _, ok := annotationMap[api.LastAppliedConfigAnnotation]; !ok {
|
if _, ok := annotationMap[api.LastAppliedConfigAnnotation]; !ok {
|
||||||
fmt.Fprintf(errOut, warningNoLastAppliedConfigAnnotation)
|
fmt.Fprintf(errOut, warningNoLastAppliedConfigAnnotation, options.cmdBaseName)
|
||||||
}
|
}
|
||||||
overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
|
overwrite := cmdutil.GetFlagBool(cmd, "overwrite")
|
||||||
helper := resource.NewHelper(info.Client, info.Mapping)
|
helper := resource.NewHelper(info.Client, info.Mapping)
|
||||||
|
@ -47,7 +47,7 @@ func TestApplyExtraArgsFail(t *testing.T) {
|
|||||||
errBuf := bytes.NewBuffer([]byte{})
|
errBuf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
f, _, _, _ := cmdtesting.NewAPIFactory()
|
f, _, _, _ := cmdtesting.NewAPIFactory()
|
||||||
c := NewCmdApply(f, buf, errBuf)
|
c := NewCmdApply("kubectl", f, buf, errBuf)
|
||||||
if validateApplyArgs(c, []string{"rc"}) == nil {
|
if validateApplyArgs(c, []string{"rc"}) == nil {
|
||||||
t.Fatalf("unexpected non-error")
|
t.Fatalf("unexpected non-error")
|
||||||
}
|
}
|
||||||
@ -377,14 +377,14 @@ func TestApplyObjectWithoutAnnotation(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameRC)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
|
|
||||||
// uses the name from the file, not the response
|
// uses the name from the file, not the response
|
||||||
expectRC := "replicationcontroller/" + nameRC + "\n"
|
expectRC := "replicationcontroller/" + nameRC + "\n"
|
||||||
expectWarning := warningNoLastAppliedConfigAnnotation
|
expectWarning := fmt.Sprintf(warningNoLastAppliedConfigAnnotation, "kubectl")
|
||||||
if errBuf.String() != expectWarning {
|
if errBuf.String() != expectWarning {
|
||||||
t.Fatalf("unexpected non-warning: %s\nexpected: %s", 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{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameRC)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
@ -479,7 +479,7 @@ func TestApplyObjectOutput(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameRC)
|
||||||
cmd.Flags().Set("output", "yaml")
|
cmd.Flags().Set("output", "yaml")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
@ -533,7 +533,7 @@ func TestApplyRetry(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameRC)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
@ -578,7 +578,7 @@ func TestApplyNonExistObject(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameRC)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
@ -636,7 +636,7 @@ func testApplyMultipleObjects(t *testing.T, asList bool) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := bytes.NewBuffer([]byte{})
|
errBuf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
cmd := NewCmdApply(f, buf, errBuf)
|
cmd := NewCmdApply("kubectl", f, buf, errBuf)
|
||||||
if asList {
|
if asList {
|
||||||
cmd.Flags().Set("filename", filenameRCSVC)
|
cmd.Flags().Set("filename", filenameRCSVC)
|
||||||
} else {
|
} else {
|
||||||
@ -729,7 +729,7 @@ func TestApplyNULLPreservation(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameDeployObjClientside)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
|
|
||||||
@ -789,7 +789,7 @@ func TestUnstructuredApply(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameWidgetClientside)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
@ -876,7 +876,7 @@ func TestUnstructuredIdempotentApply(t *testing.T) {
|
|||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
errBuf := 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("filename", filenameWidgetClientside)
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Run(cmd, []string{})
|
cmd.Run(cmd, []string{})
|
||||||
|
@ -337,7 +337,7 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
|
|||||||
{
|
{
|
||||||
Message: "Advanced Commands:",
|
Message: "Advanced Commands:",
|
||||||
Commands: []*cobra.Command{
|
Commands: []*cobra.Command{
|
||||||
NewCmdApply(f, out, err),
|
NewCmdApply("kubectl", f, out, err),
|
||||||
NewCmdPatch(f, out),
|
NewCmdPatch(f, out),
|
||||||
NewCmdReplace(f, out),
|
NewCmdReplace(f, out),
|
||||||
deprecatedAlias("update", NewCmdReplace(f, out)),
|
deprecatedAlias("update", NewCmdReplace(f, out)),
|
||||||
|
Loading…
Reference in New Issue
Block a user