Skip to content

Commit b076f60

Browse files
authored
Refactor(cli): move default values (#581)
1 parent 60baa8b commit b076f60

40 files changed

+179
-126
lines changed

internal/cli/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
func NewCliCmd() *cobra.Command {
2828
cmd := &cobra.Command{
2929
Use: "obocli",
30-
Short: "OceanBase Operator CLI",
31-
Long: "OceanBase Operator CLI tool to manage OceanBase clusters, tenants, and backups.",
30+
Short: "OceanBase Operator Cli",
31+
Long: "OceanBase Operator Cli tool to manage OceanBase clusters, tenants, and backups.",
3232
Run: func(cmd *cobra.Command, args []string) {
3333
if cmd.Flags().Changed("version") {
3434
versionCmd := version.NewCmd()

internal/cli/cluster/create.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -309,47 +309,47 @@ func (o *CreateOptions) AddZoneFlags(cmd *cobra.Command) {
309309
func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
310310
baseFlags := cmd.Flags()
311311
baseFlags.StringVarP(&o.ClusterName, FLAG_CLUSTER_NAME, "n", "", "Cluster name, if not specified, use resource name in k8s instead")
312-
baseFlags.StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "The namespace of the cluster")
313-
baseFlags.Int64Var(&o.ClusterId, FLAG_CLUSTER_ID, 0, "The id of the cluster")
312+
baseFlags.StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The namespace of the cluster")
313+
baseFlags.Int64Var(&o.ClusterId, FLAG_CLUSTER_ID, DEFAULT_ID, "The id of the cluster")
314314
baseFlags.StringVarP(&o.RootPassword, FLAG_ROOTPASSWD, "p", "", "The root password of the cluster")
315315
baseFlags.StringVar(&o.Mode, FLAG_MODE, "", "The mode of the cluster")
316316
}
317317

318318
// AddObserverFlags adds the observer-related flags to the command.
319319
func (o *CreateOptions) AddObserverFlags(cmd *cobra.Command) {
320320
observerFlags := pflag.NewFlagSet(FLAGSET_OBSERVER, pflag.ContinueOnError)
321-
observerFlags.StringVar(&o.OBServer.Image, FLAG_OBSERVER_IMAGE, "oceanbase/oceanbase-cloud-native:4.2.1.6-106000012024042515", "The image of the observer")
322-
observerFlags.Int64Var(&o.OBServer.Resource.Cpu, FLAG_OBSERVER_CPU, 2, "The cpu of the observer")
323-
observerFlags.Int64Var(&o.OBServer.Resource.MemoryGB, FLAG_MONITOR_MEMORY, 10, "The memory of the observer")
324-
observerFlags.StringVar(&o.OBServer.Storage.Data.StorageClass, FLAG_DATA_STORAGE_CLASS, "local-path", "The storage class of the data storage")
325-
observerFlags.StringVar(&o.OBServer.Storage.RedoLog.StorageClass, FLAG_REDO_LOG_STORAGE_CLASS, "local-path", "The storage class of the redo log storage")
326-
observerFlags.StringVar(&o.OBServer.Storage.Log.StorageClass, FLAG_LOG_STORAGE_CLASS, "local-path", "The storage class of the log storage")
327-
observerFlags.Int64Var(&o.OBServer.Storage.Data.SizeGB, FLAG_DATA_STORAGE_SIZE, 50, "The size of the data storage")
328-
observerFlags.Int64Var(&o.OBServer.Storage.RedoLog.SizeGB, FLAG_REDO_LOG_STORAGE_SIZE, 50, "The size of the redo log storage")
329-
observerFlags.Int64Var(&o.OBServer.Storage.Log.SizeGB, FLAG_LOG_STORAGE_SIZE, 20, "The size of the log storage")
321+
observerFlags.StringVar(&o.OBServer.Image, FLAG_OBSERVER_IMAGE, DEFAULT_OBSERVER_IMAGE, "The image of the observer")
322+
observerFlags.Int64Var(&o.OBServer.Resource.Cpu, FLAG_OBSERVER_CPU, DEFAULT_CPU_NUM, "The cpu of the observer")
323+
observerFlags.Int64Var(&o.OBServer.Resource.MemoryGB, FLAG_MONITOR_MEMORY, DEFAULT_MONITOR_MEMORY, "The memory of the observer")
324+
observerFlags.StringVar(&o.OBServer.Storage.Data.StorageClass, FLAG_DATA_STORAGE_CLASS, DEFAULT_DATA_STORAGE_CLASS, "The storage class of the data storage")
325+
observerFlags.StringVar(&o.OBServer.Storage.RedoLog.StorageClass, FLAG_REDO_LOG_STORAGE_CLASS, DEFAULT_REDO_LOG_STORAGE_CLASS, "The storage class of the redo log storage")
326+
observerFlags.StringVar(&o.OBServer.Storage.Log.StorageClass, FLAG_LOG_STORAGE_CLASS, DEFAULT_LOG_STORAGE_CLASS, "The storage class of the log storage")
327+
observerFlags.Int64Var(&o.OBServer.Storage.Data.SizeGB, FLAG_DATA_STORAGE_SIZE, DEFAULT_DATA_STORAGE_SIZE, "The size of the data storage")
328+
observerFlags.Int64Var(&o.OBServer.Storage.RedoLog.SizeGB, FLAG_REDO_LOG_STORAGE_SIZE, DEFAULT_REDO_LOG_STORAGE_SIZE, "The size of the redo log storage")
329+
observerFlags.Int64Var(&o.OBServer.Storage.Log.SizeGB, FLAG_LOG_STORAGE_SIZE, DEFAULT_LOG_STORAGE_SIZE, "The size of the log storage")
330330
cmd.Flags().AddFlagSet(observerFlags)
331331
}
332332

333333
// AddMonitorFlags adds the monitor-related flags to the command.
334334
func (o *CreateOptions) AddMonitorFlags(cmd *cobra.Command) {
335335
monitorFlags := pflag.NewFlagSet(FLAGSET_MONITOR, pflag.ContinueOnError)
336-
monitorFlags.StringVar(&o.Monitor.Image, FLAG_MONITOR_IMAGE, "oceanbase/obagent:4.2.1-100000092023101717", "The image of the monitor")
337-
monitorFlags.Int64Var(&o.Monitor.Resource.Cpu, FLAG_MONITOR_CPU, 1, "The cpu of the monitor")
338-
monitorFlags.Int64Var(&o.Monitor.Resource.MemoryGB, FLAG_MONITOR_MEMORY, 1, "The memory of the monitor")
336+
monitorFlags.StringVar(&o.Monitor.Image, FLAG_MONITOR_IMAGE, DEFAULT_MONITOR_IMAGE, "The image of the monitor")
337+
monitorFlags.Int64Var(&o.Monitor.Resource.Cpu, FLAG_MONITOR_CPU, DEFAULT_MONITOR_CPU, "The cpu of the monitor")
338+
monitorFlags.Int64Var(&o.Monitor.Resource.MemoryGB, FLAG_MONITOR_MEMORY, DEFAULT_MONITOR_MEMORY, "The memory of the monitor")
339339
cmd.Flags().AddFlagSet(monitorFlags)
340340
}
341341

342342
// AddBackupVolumeFlags adds the backup-volume-related flags to the command.
343343
func (o *CreateOptions) AddBackupVolumeFlags(cmd *cobra.Command) {
344344
backupVolumeFlags := pflag.NewFlagSet(FLAGSET_BACKUP_VOLUME, pflag.ContinueOnError)
345-
backupVolumeFlags.StringVar(&o.BackupVolume.Address, FLAG_BACKUP_ADDRESS, "local-path", "The storage class of the backup storage")
346-
backupVolumeFlags.StringVar(&o.BackupVolume.Path, FLAG_BACKUP_PATH, "/opt/nfs", "The size of the backup storage")
345+
backupVolumeFlags.StringVar(&o.BackupVolume.Address, FLAG_BACKUP_ADDRESS, DEFAULT_BACKUP_ADDRESS, "The storage class of the backup storage")
346+
backupVolumeFlags.StringVar(&o.BackupVolume.Path, FLAG_BACKUP_PATH, DEFAULT_BACKUP_PATH, "The size of the backup storage")
347347
cmd.Flags().AddFlagSet(backupVolumeFlags)
348348
}
349349

350350
// AddParameterFlags adds the parameter-related flags, e.g. __min_full_resource_pool_memory, to the command
351351
func (o *CreateOptions) AddParameterFlags(cmd *cobra.Command) {
352352
parameterFlags := pflag.NewFlagSet(FLAGSET_PARAMETERS, pflag.ContinueOnError)
353-
parameterFlags.StringToStringVar(&o.KvParameters, FLAG_PARAMETERS, map[string]string{"__min_full_resource_pool_memory": "2147483648", "system_memory": "1G"}, "Other parameter settings in obcluster, e.g., __min_full_resource_pool_memory")
353+
parameterFlags.StringToStringVar(&o.KvParameters, FLAG_PARAMETERS, map[string]string{"__min_full_resource_pool_memory": DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY, "system_memory": DEFAULT_SYSTEM_MEMORY}, "Other parameter settings in OBCluster, e.g., __min_full_resource_pool_memory")
354354
cmd.Flags().AddFlagSet(parameterFlags)
355355
}

internal/cli/cluster/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ func NewDeleteOptions() *DeleteOptions {
2929

3030
// AddFlags add basic flags for cluster management
3131
func (o *DeleteOptions) AddFlags(cmd *cobra.Command) {
32-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "namespace of ob cluster")
32+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
3333
}

internal/cli/cluster/enter.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ See the Mulan PSL v2 for more details.
1313
*/
1414
package cluster
1515

16+
// Flags and FlagSets for cluster management
1617
const (
17-
// Flagsets for cluster
18+
// FlagSets for cluster
1819
FLAGSET_ZONE = "zone"
1920
FLAGSET_OBSERVER = "observer"
2021
FLAGSET_MONITOR = "monitor"
@@ -54,3 +55,29 @@ const (
5455
// Flags for parameter-related options
5556
FLAG_PARAMETERS = "parameters"
5657
)
58+
59+
// Default values for cluster management
60+
const (
61+
// Default values for int and string flags
62+
DEFAULT_NAMESPACE = "default"
63+
DEFAULT_ID = 0
64+
DEFAULT_CPU_NUM = 2
65+
DEFAULT_OBSERVER_IMAGE = "oceanbase/oceanbase-cloud-native:4.2.1.6-106000012024042515"
66+
DEFAULT_OBSERVER_CPU = 2
67+
DEFAULT_OBSERVER_MEMORY = 10
68+
DEFAULT_DATA_STORAGE_CLASS = "local-path"
69+
DEFAULT_REDO_LOG_STORAGE_CLASS = "local-path"
70+
DEFAULT_LOG_STORAGE_CLASS = "local-path"
71+
DEFAULT_DATA_STORAGE_SIZE = 50
72+
DEFAULT_REDO_LOG_STORAGE_SIZE = 50
73+
DEFAULT_LOG_STORAGE_SIZE = 20
74+
DEFAULT_MONITOR_IMAGE = "oceanbase/obagent:4.2.1-100000092023101717"
75+
DEFAULT_MONITOR_CPU = 1
76+
DEFAULT_MONITOR_MEMORY = 1
77+
DEFAULT_BACKUP_ADDRESS = "local-path"
78+
DEFAULT_BACKUP_PATH = "/opt/nfs"
79+
80+
// Default values for Parameter flag
81+
DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY = "2147483648"
82+
DEFAULT_SYSTEM_MEMORY = "1G"
83+
)

internal/cli/cluster/scale.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (o *ScaleOptions) Validate() error {
140140
typeAdd = true
141141
}
142142
if typeDelete && deleteNum > maxDeleteNum {
143-
return fmt.Errorf("Obcluster has %d Zones, can only delete %d zones", zoneNum, maxDeleteNum)
143+
return fmt.Errorf("Ob cluster has %d Zones, can only delete %d zones", zoneNum, maxDeleteNum)
144144
}
145145
}
146146
trueCount := 0
@@ -167,6 +167,6 @@ func (o *ScaleOptions) Validate() error {
167167

168168
// Add Flags for scale options
169169
func (o *ScaleOptions) AddFlags(cmd *cobra.Command) {
170-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "namespace of ob cluster")
170+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
171171
cmd.Flags().StringToStringVar(&o.Zones, FLAG_ZONES, nil, "zone of ob cluster")
172172
}

internal/cli/cluster/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ func NewShowOptions() *ShowOptions {
2929

3030
// AddFlags add basic flags for cluster management
3131
func (o *ShowOptions) AddFlags(cmd *cobra.Command) {
32-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "namespace of ob cluster")
32+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
3333
}

internal/cli/cluster/update.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func GetUpdateOperation(o *UpdateOptions) *v1alpha1.OBClusterOperation {
6464

6565
func (o *UpdateOptions) Validate() error {
6666
updateTypeCount := 0
67-
if o.Resource.Cpu != 0 || o.Resource.MemoryGB != 0 {
67+
if o.CheckIfFlagChanged(FLAG_OBSERVER_CPU, FLAG_OBSERVER_MEMORY) {
6868
updateTypeCount++
6969
o.UpdateType = "resource"
7070
}
71-
if o.Storage.Data.StorageClass != "" || o.Storage.Log.StorageClass != "" || o.Storage.RedoLog.StorageClass != "" {
71+
if o.CheckIfFlagChanged(FLAG_DATA_STORAGE_CLASS, FLAG_LOG_STORAGE_CLASS, FLAG_REDO_LOG_STORAGE_CLASS) {
7272
updateTypeCount++
7373
o.UpdateType = "modifyStorageClass"
7474
}
75-
if o.Storage.Data.SizeGB != 0 || o.Storage.Log.SizeGB != 0 || o.Storage.RedoLog.SizeGB != 0 {
75+
if o.CheckIfFlagChanged(FLAG_DATA_STORAGE_SIZE, FLAG_LOG_STORAGE_SIZE, FLAG_REDO_LOG_STORAGE_SIZE) {
7676
updateTypeCount++
7777
o.UpdateType = "expandStorageSize"
7878
}
@@ -128,13 +128,13 @@ func (o *UpdateOptions) Complete() error {
128128

129129
// AddFlags for update options
130130
func (o *UpdateOptions) AddFlags(cmd *cobra.Command) {
131-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "namespace of ob cluster")
132-
cmd.Flags().Int64Var(&o.Resource.Cpu, FLAG_OBSERVER_CPU, 0, "The cpu of the observer")
133-
cmd.Flags().Int64Var(&o.Resource.MemoryGB, FLAG_MONITOR_MEMORY, 0, "The memory of the observer")
131+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
132+
cmd.Flags().Int64Var(&o.Resource.Cpu, FLAG_OBSERVER_CPU, DEFAULT_OBSERVER_CPU, "The cpu of the observer")
133+
cmd.Flags().Int64Var(&o.Resource.MemoryGB, FLAG_OBSERVER_MEMORY, DEFAULT_OBSERVER_MEMORY, "The memory of the observer")
134134
cmd.Flags().StringVar(&o.Storage.Data.StorageClass, FLAG_DATA_STORAGE_CLASS, "", "The storage class of the data storage")
135135
cmd.Flags().StringVar(&o.Storage.RedoLog.StorageClass, FLAG_REDO_LOG_STORAGE_CLASS, "", "The storage class of the redo log storage")
136136
cmd.Flags().StringVar(&o.Storage.Log.StorageClass, FLAG_LOG_STORAGE_CLASS, "", "The storage class of the log storage")
137-
cmd.Flags().Int64Var(&o.Storage.Data.SizeGB, FLAG_DATA_STORAGE_SIZE, 0, "The size of the data storage")
138-
cmd.Flags().Int64Var(&o.Storage.RedoLog.SizeGB, FLAG_REDO_LOG_STORAGE_SIZE, 0, "The size of the redo log storage")
139-
cmd.Flags().Int64Var(&o.Storage.Log.SizeGB, FLAG_LOG_STORAGE_SIZE, 0, "The size of the log storage")
137+
cmd.Flags().Int64Var(&o.Storage.Data.SizeGB, FLAG_DATA_STORAGE_SIZE, DEFAULT_DATA_STORAGE_SIZE, "The size of the data storage")
138+
cmd.Flags().Int64Var(&o.Storage.RedoLog.SizeGB, FLAG_REDO_LOG_STORAGE_SIZE, DEFAULT_REDO_LOG_STORAGE_SIZE, "The size of the redo log storage")
139+
cmd.Flags().Int64Var(&o.Storage.Log.SizeGB, FLAG_LOG_STORAGE_SIZE, DEFAULT_LOG_STORAGE_SIZE, "The size of the log storage")
140140
}

internal/cli/cluster/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ func (o *UpgradeOptions) Validate() error {
6161

6262
// AddFlags for upgrade options
6363
func (o *UpgradeOptions) AddFlags(cmd *cobra.Command) {
64-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, "default", "namespace of ob cluster")
64+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
6565
cmd.Flags().StringVar(&o.Image, FLAG_OBSERVER_IMAGE, "", "The image of observer") // set image to null, avoid image downgrade
6666
}

internal/cli/cmd/cluster/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func NewCmd() *cobra.Command {
2222
cmd := &cobra.Command{
2323
Use: "cluster <subcommand>",
2424
Aliases: []string{"c"},
25-
Short: "Command for cluster management",
26-
Long: `Command for cluster management, such as Create, UpGrade, Delete, Scale, Show.`,
25+
Short: "Command for OBCluster's management",
26+
Long: `Command for OBCluster's management, such as Create, UpGrade, Delete, Scale, Show.`,
2727
}
2828
cmd.AddCommand(NewCreateCmd())
2929
cmd.AddCommand(NewDeleteCmd())

internal/cli/cmd/cluster/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewCreateCmd() *cobra.Command {
2828
logger := cmdUtil.GetDefaultLoggerInstance()
2929
cmd := &cobra.Command{
3030
Use: "create <cluster_name>",
31-
Short: "Create ob cluster",
31+
Short: "Create an ob cluster",
3232
Aliases: []string{"c"},
3333
Args: cobra.ExactArgs(1),
3434
PreRunE: o.Parse,
@@ -47,7 +47,7 @@ func NewCreateCmd() *cobra.Command {
4747
if err != nil {
4848
logger.Fatalln(err)
4949
}
50-
logger.Printf("Create obcluster instance: %s", o.ClusterName)
50+
logger.Printf("Create OBCluster instance: %s", o.ClusterName)
5151
logger.Printf("Run `echo $(kubectl get secret %s -o jsonpath='{.data.password}'|base64 --decode)` to get the secrets", obcluster.Spec.UserSecrets.Root)
5252
},
5353
}

0 commit comments

Comments
 (0)