diff --git a/pkg/cluster/manager/cleanup.go b/pkg/cluster/manager/cleanup.go index 8e4690226b..5ceaa3a682 100644 --- a/pkg/cluster/manager/cleanup.go +++ b/pkg/cluster/manager/cleanup.go @@ -23,6 +23,7 @@ import ( "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -32,6 +33,10 @@ import ( // CleanCluster cleans the cluster without destroying it func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt operator.Options, skipConfirm bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil { return err diff --git a/pkg/cluster/manager/destroy.go b/pkg/cluster/manager/destroy.go index a78366df2d..d1b648bfe1 100644 --- a/pkg/cluster/manager/destroy.go +++ b/pkg/cluster/manager/destroy.go @@ -22,6 +22,7 @@ import ( "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -31,6 +32,10 @@ import ( // DestroyCluster destroy the cluster. func (m *Manager) DestroyCluster(name string, gOpt operator.Options, destroyOpt operator.Options, skipConfirm bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) && !errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) && diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index 7bad340777..53fb208832 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -25,6 +25,7 @@ import ( perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/api" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" "github.com/pingcap/tiup/pkg/cluster/executor" operator "github.com/pingcap/tiup/pkg/cluster/operation" @@ -52,6 +53,10 @@ type InstInfo struct { // Display cluster meta and topology. func (m *Manager) Display(name string, opt operator.Options) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + clusterInstInfos, err := m.GetClusterTopology(name, opt) if err != nil { return err diff --git a/pkg/cluster/manager/edit_config.go b/pkg/cluster/manager/edit_config.go index 961c8156c5..96c25d044d 100644 --- a/pkg/cluster/manager/edit_config.go +++ b/pkg/cluster/manager/edit_config.go @@ -23,6 +23,7 @@ import ( "github.com/fatih/color" perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/spec" "github.com/pingcap/tiup/pkg/logger/log" "github.com/pingcap/tiup/pkg/meta" @@ -32,6 +33,10 @@ import ( // EditConfig lets the user edit the cluster's config. func (m *Manager) EditConfig(name string, skipConfirm bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) { return err diff --git a/pkg/cluster/manager/exec.go b/pkg/cluster/manager/exec.go index 1fc655f0f0..870c854367 100644 --- a/pkg/cluster/manager/exec.go +++ b/pkg/cluster/manager/exec.go @@ -21,6 +21,7 @@ import ( "github.com/fatih/color" "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -37,6 +38,10 @@ type ExecOptions struct { // Exec shell command on host in the tidb cluster. func (m *Manager) Exec(name string, opt ExecOptions, gOpt operator.Options) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil { return err diff --git a/pkg/cluster/manager/patch.go b/pkg/cluster/manager/patch.go index 9eef66f12b..78feabe43f 100644 --- a/pkg/cluster/manager/patch.go +++ b/pkg/cluster/manager/patch.go @@ -34,6 +34,10 @@ import ( // Patch the cluster. func (m *Manager) Patch(name string, packagePath string, opt operator.Options, overwrite, offline bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil { return err diff --git a/pkg/cluster/manager/reload.go b/pkg/cluster/manager/reload.go index bb0af9c264..22867346bb 100644 --- a/pkg/cluster/manager/reload.go +++ b/pkg/cluster/manager/reload.go @@ -18,6 +18,7 @@ import ( "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -26,6 +27,10 @@ import ( // Reload the cluster. func (m *Manager) Reload(name string, opt operator.Options, skipRestart bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + sshTimeout := opt.SSHTimeout metadata, err := m.meta(name) diff --git a/pkg/cluster/manager/rename.go b/pkg/cluster/manager/rename.go index 94fe4fe240..0be9f465ed 100644 --- a/pkg/cluster/manager/rename.go +++ b/pkg/cluster/manager/rename.go @@ -17,6 +17,7 @@ import ( "os" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" "github.com/pingcap/tiup/pkg/logger/log" @@ -25,11 +26,18 @@ import ( // Rename the cluster func (m *Manager) Rename(name string, opt operator.Options, newName string) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } if !utils.IsExist(m.specManager.Path(name)) { return errorRenameNameNotExist. New("Cluster name '%s' not exist", name). WithProperty(cliutil.SuggestionFromFormat("Please double check your cluster name")) } + + if err := clusterutil.ValidateClusterNameOrError(newName); err != nil { + return err + } if utils.IsExist(m.specManager.Path(newName)) { return errorRenameNameDuplicate. New("Cluster name '%s' is duplicated", newName). diff --git a/pkg/cluster/manager/scale_in.go b/pkg/cluster/manager/scale_in.go index aeb40fd863..972eae07d7 100644 --- a/pkg/cluster/manager/scale_in.go +++ b/pkg/cluster/manager/scale_in.go @@ -23,6 +23,7 @@ import ( "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -38,6 +39,10 @@ func (m *Manager) ScaleIn( gOpt operator.Options, scale func(builer *task.Builder, metadata spec.Metadata, tlsCfg *tls.Config), ) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + var ( force bool = gOpt.Force nodes []string = gOpt.Nodes diff --git a/pkg/cluster/manager/scale_out.go b/pkg/cluster/manager/scale_out.go index a6e7ce3921..cd5d5b781b 100644 --- a/pkg/cluster/manager/scale_out.go +++ b/pkg/cluster/manager/scale_out.go @@ -22,6 +22,7 @@ import ( perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/api" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" "github.com/pingcap/tiup/pkg/cluster/executor" operator "github.com/pingcap/tiup/pkg/cluster/operation" @@ -51,6 +52,10 @@ func (m *Manager) ScaleOut( skipConfirm bool, gOpt operator.Options, ) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) // allow specific validation errors so that user can recover a broken // cluster if it is somehow in a bad state. diff --git a/pkg/cluster/manager/transfer.go b/pkg/cluster/manager/transfer.go index 35a78e9c82..1c4169864c 100644 --- a/pkg/cluster/manager/transfer.go +++ b/pkg/cluster/manager/transfer.go @@ -24,6 +24,7 @@ import ( "github.com/google/uuid" "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -41,6 +42,10 @@ type TransferOptions struct { // Transfer copies files from or to host in the tidb cluster. func (m *Manager) Transfer(name string, opt TransferOptions, gOpt operator.Options) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil { return err diff --git a/pkg/cluster/manager/upgrade.go b/pkg/cluster/manager/upgrade.go index 5e50e6a12e..efc161388e 100644 --- a/pkg/cluster/manager/upgrade.go +++ b/pkg/cluster/manager/upgrade.go @@ -22,6 +22,7 @@ import ( "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/cliutil" + "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -35,6 +36,10 @@ import ( // Upgrade the cluster. func (m *Manager) Upgrade(name string, clusterVersion string, opt operator.Options, skipConfirm, offline bool) error { + if err := clusterutil.ValidateClusterNameOrError(name); err != nil { + return err + } + metadata, err := m.meta(name) if err != nil { return err