Skip to content

Commit 8ddc6ad

Browse files
authored
feat(cli): add cmd completion on tenant creation and fix some bugs (#643)
1 parent b79187d commit 8ddc6ad

File tree

18 files changed

+164
-55
lines changed

18 files changed

+164
-55
lines changed

internal/cli/cluster/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (o *CreateOptions) AddFlags(cmd *cobra.Command) {
340340
// AddZoneFlags adds the zone-related flags to the command.
341341
func (o *CreateOptions) AddZoneFlags(cmd *cobra.Command) {
342342
zoneFlags := pflag.NewFlagSet(FLAGSET_ZONE, pflag.ContinueOnError)
343-
zoneFlags.StringToStringVarP(&o.Zones, FLAG_ZONES, "z", map[string]string{"z1": "1"}, "The zones of the cluster in the format 'Zone=Replica', multiple values can be provided separated by commas")
343+
zoneFlags.StringToStringVarP(&o.Zones, FLAG_ZONES, "z", map[string]string{"z1": "1"}, "The zones of the cluster, e.g. '--zones=<zone>=<replica>'")
344344
cmd.Flags().AddFlagSet(zoneFlags)
345345
}
346346

@@ -351,7 +351,7 @@ func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
351351
baseFlags.StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The namespace of the cluster")
352352
baseFlags.Int64Var(&o.ClusterId, FLAG_CLUSTER_ID, DEFAULT_ID, "The id of the cluster")
353353
baseFlags.StringVarP(&o.RootPassword, FLAG_ROOT_PASSWORD, "p", "", "The root password of the cluster")
354-
baseFlags.StringVar(&o.Mode, FLAG_MODE, "", "The mode of the cluster")
354+
baseFlags.StringVar(&o.Mode, FLAG_MODE, DEFAULT_MODE, "The mode of the cluster")
355355
}
356356

357357
// AddObserverFlags adds the observer-related flags to the command.

internal/cli/cluster/enter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const (
8080
DEFAULT_MONITOR_CPU = 1
8181
DEFAULT_MONITOR_MEMORY = 1
8282
DEFAULT_NAME = "test"
83+
DEFAULT_MODE = "service"
8384

8485
// Default values for Parameter flag
8586
DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY = "2147483648"

internal/cli/cluster/scale.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ func (o *ScaleOptions) Validate() error {
165165
return nil
166166
}
167167

168-
// Add Flags for scale options
168+
// AddFlags for scale options
169169
func (o *ScaleOptions) AddFlags(cmd *cobra.Command) {
170170
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob cluster")
171-
cmd.Flags().StringToStringVar(&o.Zones, FLAG_ZONES, nil, "zone of ob cluster")
171+
cmd.Flags().StringToStringVar(&o.Zones, FLAG_ZONES, nil, "The zone of the cluster, e.g. '--zones=<zone>=<replica>', set replicas to 0 to delete the zone, only one operation of adding, deleting or modifying is allowd at a time.")
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, "namespace of ob cluster")
32+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of the ob cluster")
3333
}

internal/cli/cluster/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ func (o *UpdateOptions) Complete() error {
114114
expandStorageSize.DataStorage = apiresource.NewQuantity(o.Storage.Data.SizeGB*constant.GB, apiresource.BinarySI)
115115
}
116116
if o.Storage.RedoLog.SizeGB != 0 {
117-
expandStorageSize.DataStorage = apiresource.NewQuantity(o.Storage.RedoLog.SizeGB*constant.GB, apiresource.BinarySI)
117+
expandStorageSize.RedoLogStorage = apiresource.NewQuantity(o.Storage.RedoLog.SizeGB*constant.GB, apiresource.BinarySI)
118118
}
119119
if o.Storage.Log.SizeGB != 0 {
120-
expandStorageSize.DataStorage = apiresource.NewQuantity(o.Storage.Log.SizeGB*constant.GB, apiresource.BinarySI)
120+
expandStorageSize.LogStorage = apiresource.NewQuantity(o.Storage.Log.SizeGB*constant.GB, apiresource.BinarySI)
121121
}
122122
o.ModifyConfig = &v1alpha1.ModifyOBServersConfig{ExpandStorageSize: expandStorageSize}
123123
default:

internal/cli/cmd/cluster/show.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ func NewShowCmd() *cobra.Command {
4747
if len(obcluster.Status.OBZoneStatus) > 0 {
4848
tbLog.Println("ZONE \t STATUS")
4949
for _, zone := range obcluster.Status.OBZoneStatus {
50-
tbLog.Printf("%s \t %s \n\n", zone.Zone, zone.Status)
50+
tbLog.Printf("%s \t %s \n", zone.Zone, zone.Status)
5151
}
52+
tbLog.Println()
5253
}
5354
if len(obcluster.Status.Parameters) > 0 {
5455
tbLog.Println("KEY \t VALUE")
5556
for _, Parameter := range obcluster.Status.Parameters {
56-
tbLog.Printf("%s \t %s \n\n", Parameter.Name, Parameter.Value)
57+
tbLog.Printf("%s \t %s \n", Parameter.Name, Parameter.Value)
5758
}
59+
tbLog.Println()
5860
}
5961

6062
if len(obclusterOperationList.Items) > 0 {
@@ -63,7 +65,7 @@ func NewShowCmd() *cobra.Command {
6365
})
6466
tbLog.Println("OPERATION TYPE \t TTLDAYS \t STATUS \t CREATETIME")
6567
for _, op := range obclusterOperationList.Items {
66-
tbLog.Printf("%s \t %d \t %s \t %s\n", op.Spec.Type, op.Spec.TTLDays, op.Status.Status, op.CreationTimestamp)
68+
tbLog.Printf("%s \t %d \t %s \t %s \n", op.Spec.Type, op.Spec.TTLDays, op.Status.Status, op.CreationTimestamp)
6769
}
6870
} else {
6971
logger.Printf("No OBClusterOperations found in %s", obcluster.Spec.ClusterName)

internal/cli/cmd/tenant/show.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ func NewShowCmd() *cobra.Command {
5151
if len(obtenant.Status.Pools) > 0 {
5252
tbLog.Println("ZONELIST \t UNITNUM \t PRIORITY")
5353
for _, pool := range obtenant.Status.Pools {
54-
tbLog.Printf("%s \t %d \t %d\n\n", pool.ZoneList, pool.UnitNumber, pool.Priority)
54+
tbLog.Printf("%s \t %d \t %d \n", pool.ZoneList, pool.UnitNumber, pool.Priority)
5555
}
56+
tbLog.Println()
5657
}
5758
if len(obtenantOperationList.Items) > 0 {
5859
sort.Slice(obtenantOperationList.Items, func(i, j int) bool {
5960
return obtenantOperationList.Items[i].Name < obtenantOperationList.Items[j].Name
6061
})
6162
tbLog.Println("OPERATION TYPE \t STATUS \t CREATETIME")
6263
for _, op := range obtenantOperationList.Items {
63-
tbLog.Printf("%s \t %s \t %s\n", op.Spec.Type, op.Status.Status, op.CreationTimestamp)
64+
tbLog.Printf("%s \t %s \t %s \n", op.Spec.Type, op.Status.Status, op.CreationTimestamp)
6465
}
6566
} else {
6667
logger.Printf("No OBTenantOperations found in %s", obtenant.Spec.TenantName)

internal/cli/demo/prompt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (pf *PromptFactory) CreatePrompt(promptType string) any {
130130
}
131131
case tenant.FLAG_TENANT_NAME_IN_K8S:
132132
return &promptui.Prompt{
133-
Label: "Please input the tenant resource name, press `enter` to use the default name `demo-tenant`: ",
133+
Label: "Please input the tenant resource name, press `enter` to use the default name `t1`: ",
134134
Templates: pf.promptTepl,
135135
Validate: func(input string) error {
136136
if !utils.CheckResourceName(input) {
@@ -143,7 +143,7 @@ func (pf *PromptFactory) CreatePrompt(promptType string) any {
143143
}
144144
case tenant.FLAG_TENANT_NAME:
145145
return &promptui.Prompt{
146-
Label: "Please input the tenant name, press `enter` to use the default name `demo_tenant`: ",
146+
Label: "Please input the tenant name, press `enter` to use the default name `t1`: ",
147147
Templates: pf.promptTepl,
148148
Validate: func(input string) error {
149149
if !utils.CheckTenantName(input) {

internal/cli/tenant/activate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ func GetActivateOperation(o *ActivateOptions) *v1alpha1.OBTenantOperation {
5454
// AddFlags add basic flags for tenant management
5555
func (o *ActivateOptions) AddFlags(cmd *cobra.Command) {
5656
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "namespace of ob tenant")
57-
cmd.Flags().BoolVarP(&o.force, FLAG_FORCE, "f", DEFAULT_FORCE_FLAG, "force operation")
57+
cmd.Flags().BoolVarP(&o.force, FLAG_FORCE, "f", DEFAULT_FORCE_FLAG, "If the operation is a force operation")
5858
}

internal/cli/tenant/create.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,21 @@ func (o *CreateOptions) Complete() error {
107107
if o.RootPassword == "" {
108108
o.RootPassword = utils.GenerateRandomPassword(8, 32)
109109
}
110+
if o.TenantName == "" {
111+
o.TenantName = o.Name
112+
}
110113
if o.Timestamp != "" {
111114
o.Source.Restore.Until.Timestamp = &o.Timestamp
112115
}
116+
if o.RestoreType != "" {
117+
o.RestoreType = strings.ToUpper(o.RestoreType)
118+
}
119+
if o.RestoreType == "NFS" && o.Source.Restore.BakDataSource == "" {
120+
o.Source.Restore.BakDataSource = fmt.Sprintf("%s/%s", "backup", o.From)
121+
}
122+
if o.RestoreType == "NFS" && o.Source.Restore.ArchiveSource == "" {
123+
o.Source.Restore.ArchiveSource = fmt.Sprintf("%s/%s", "archive", o.From)
124+
}
113125
return nil
114126
}
115127

@@ -118,7 +130,7 @@ func (o *CreateOptions) Validate() error {
118130
return errors.New("namespace is not specified")
119131
}
120132
if o.ClusterName == "" {
121-
return errors.New("cluster name is not specified")
133+
return errors.New("cluster is not specified")
122134
}
123135
if o.TenantName == "" {
124136
return errors.New("tenant name is not specified")
@@ -129,9 +141,6 @@ func (o *CreateOptions) Validate() error {
129141
if !utils.CheckTenantName(o.TenantName) {
130142
return fmt.Errorf("invalid tenant name: %s, the first letter must be a letter or an underscore and cannot contain -", o.TenantName)
131143
}
132-
if o.Source != nil && o.Source.Tenant != nil && o.TenantRole == "PRIMARY" {
133-
return fmt.Errorf("invalid tenant role")
134-
}
135144
if o.Restore && o.RestoreType != "OSS" && o.RestoreType != "NFS" {
136145
return errors.New("Restore Type not supported")
137146
}
@@ -179,15 +188,15 @@ func CreateOBTenant(ctx context.Context, p *CreateOptions) (*v1alpha1.OBTenant,
179188
if kubeerrors.IsNotFound(err) {
180189
return nil, fmt.Errorf("primary tenant not found")
181190
}
182-
return nil, fmt.Errorf(err.Error())
191+
return nil, err
183192
}
184193
if existing.Status.TenantRole != apiconst.TenantRolePrimary {
185194
return nil, fmt.Errorf("the target tenant is not primary tenant")
186195
}
187196
// Match root password
188197
rootSecret, err := k8sclient.ClientSet.CoreV1().Secrets(existing.Namespace).Get(ctx, existing.Status.Credentials.Root, v1.GetOptions{})
189198
if err != nil {
190-
return nil, fmt.Errorf(err.Error())
199+
return nil, err
191200
}
192201
if pwd, ok := rootSecret.Data["password"]; ok {
193202
if p.RootPassword != string(pwd) {
@@ -196,7 +205,7 @@ func CreateOBTenant(ctx context.Context, p *CreateOptions) (*v1alpha1.OBTenant,
196205
if t.Spec.Credentials.Root != "" {
197206
err = utils.CreatePasswordSecret(ctx, nn.Namespace, t.Spec.Credentials.Root, p.RootPassword)
198207
if err != nil {
199-
return nil, fmt.Errorf(err.Error())
208+
return nil, err
200209
}
201210
}
202211
}
@@ -396,7 +405,7 @@ func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
396405
baseFlags.StringVarP(&o.RootPassword, FLAG_ROOTPASSWD, "p", "", "The root password of the primary tenant, if not specified, generate a random password")
397406
baseFlags.StringVarP(&o.Charset, FLAG_CHARSET, "c", DEFAULT_CHARSET, "The charset used in ob tenant")
398407
baseFlags.StringVar(&o.ConnectWhiteList, FLAG_CONNECT_WHITE_LIST, DEFAULT_CONNECT_WHITE_LIST, "The connect white list used in ob tenant")
399-
baseFlags.StringVar(&o.From, FLAG_FROM, "", "restore from data source")
408+
baseFlags.StringVar(&o.From, FLAG_FROM, "", "The source tenant to create a standby tenant or restore the tenant")
400409
}
401410

402411
// AddPoolFlags add pool-related flags
@@ -422,14 +431,14 @@ func (o *CreateOptions) AddUnitFlags(cmd *cobra.Command) {
422431
// AddRestoreFlags add restore flags
423432
func (o *CreateOptions) AddRestoreFlags(cmd *cobra.Command) {
424433
restoreFlags := pflag.NewFlagSet(FLAGSET_RESTORE, pflag.ContinueOnError)
425-
restoreFlags.BoolVarP(&o.Restore, FLAG_RESTORE, "r", DEFAULT_RESTORE_FLAG, "Restore from backup files")
434+
restoreFlags.BoolVarP(&o.Restore, FLAG_RESTORE, "r", DEFAULT_RESTORE_FLAG, "Restore from backup files, set to true to restore a tenant, also need the `from` flag to specify the source tenant")
426435
restoreFlags.StringVar(&o.RestoreType, FLAG_RESTORE_TYPE, DEFAULT_RESTORE_TYPE, "The type of restore source, support OSS or NFS")
427-
restoreFlags.StringVar(&o.Source.Restore.ArchiveSource, FLAG_ARCHIVE_SOURCE, DEFAULT_ARCHIVE_SOURCE, "The archive source of restore")
436+
restoreFlags.StringVar(&o.Source.Restore.ArchiveSource, FLAG_ARCHIVE_SOURCE, "", "The archive source of restore")
428437
restoreFlags.StringVar(&o.Source.Restore.BakEncryptionPassword, FLAG_BAK_ENCRYPTION_PASS, "", "The backup encryption password of obtenant")
429-
restoreFlags.StringVar(&o.Source.Restore.BakDataSource, FLAG_BAK_DATA_SOURCE, DEFAULT_BAK_DATA_SOURCE, "The bak data source of restore")
438+
restoreFlags.StringVar(&o.Source.Restore.BakDataSource, FLAG_BAK_DATA_SOURCE, "", "The bak data source of restore")
430439
restoreFlags.StringVar(&o.Source.Restore.OSSAccessID, FLAG_OSS_ACCESS_ID, "", "The oss access id of restore")
431440
restoreFlags.StringVar(&o.Source.Restore.OSSAccessKey, FLAG_OSS_ACCESS_KEY, "", "The oss access key of restore")
432441
restoreFlags.BoolVar(&o.Source.Restore.Until.Unlimited, FLAG_UNLIMITED, DEFAULT_UNLIMITED_FLAG, "time limited for restore")
433-
restoreFlags.StringVar(&o.Timestamp, FLAG_UNTIL_TIMESTAMP, "", "timestamp for obtenant restore")
442+
restoreFlags.StringVar(&o.Timestamp, FLAG_UNTIL_TIMESTAMP, "", "Timestamp for obtenant restore")
434443
cmd.Flags().AddFlagSet(restoreFlags)
435444
}

0 commit comments

Comments
 (0)