Skip to content

Commit 7a92fd0

Browse files
authored
Fix: bugs in cluster and tenant creating (#590)
1 parent 4c56b70 commit 7a92fd0

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

internal/cli/cluster/create.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func NewCreateOptions() *CreateOptions {
5555
OBServer: &param.OBServerSpec{
5656
Storage: &param.OBServerStorageSpec{},
5757
},
58-
Parameters: make([]modelcommon.KVPair, 0),
59-
Zones: make(map[string]string),
60-
Topology: make([]param.ZoneTopology, 0),
58+
BackupVolume: &param.NFSVolumeSpec{},
59+
Parameters: make([]modelcommon.KVPair, 0),
60+
Zones: make(map[string]string),
61+
Topology: make([]param.ZoneTopology, 0),
6162
}
6263
}
6364

@@ -75,14 +76,20 @@ func (o *CreateOptions) Validate() error {
7576
}
7677

7778
func (o *CreateOptions) Parse(_ *cobra.Command, args []string) error {
79+
// Parse the zone topology
7880
topology, err := utils.MapZonesToTopology(o.Zones)
7981
if err != nil {
8082
return err
8183
}
84+
// Parse the parameters
8285
parameters, err := utils.MapParameters(o.KvParameters)
8386
if err != nil {
8487
return err
8588
}
89+
// Parse the BackupVolume related flags
90+
if o.BackupVolume.Address == "" || o.BackupVolume.Path == "" {
91+
o.BackupVolume = nil
92+
}
8693
o.Parameters = parameters
8794
o.Topology = topology
8895
o.Name = args[0]
@@ -256,7 +263,7 @@ func buildMonitorTemplate(monitorSpec *param.MonitorSpec) *apitypes.MonitorTempl
256263
return monitorTemplate
257264
}
258265

259-
// Create an OBClusterInstance
266+
// CreateOBClusterInstance creates an OBClusterInstance
260267
func CreateOBClusterInstance(param *CreateOptions) *v1alpha1.OBCluster {
261268
observerTemplate := buildOBServerTemplate(param.OBServer)
262269
monitorTemplate := buildMonitorTemplate(param.Monitor)
@@ -296,6 +303,7 @@ func (o *CreateOptions) AddFlags(cmd *cobra.Command) {
296303
o.AddObserverFlags(cmd)
297304
o.AddZoneFlags(cmd)
298305
o.AddParameterFlags(cmd)
306+
o.AddBackupVolumeFlags(cmd)
299307
}
300308

301309
// AddZoneFlags adds the zone-related flags to the command.
@@ -319,8 +327,8 @@ func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
319327
func (o *CreateOptions) AddObserverFlags(cmd *cobra.Command) {
320328
observerFlags := pflag.NewFlagSet(FLAGSET_OBSERVER, pflag.ContinueOnError)
321329
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")
330+
observerFlags.Int64Var(&o.OBServer.Resource.Cpu, FLAG_OBSERVER_CPU, DEFAULT_OBSERVER_CPU, "The cpu of the observer")
331+
observerFlags.Int64Var(&o.OBServer.Resource.MemoryGB, FLAG_OBSERVER_MEMORY, DEFAULT_OBSERVER_MEMORY, "The memory of the observer")
324332
observerFlags.StringVar(&o.OBServer.Storage.Data.StorageClass, FLAG_DATA_STORAGE_CLASS, DEFAULT_DATA_STORAGE_CLASS, "The storage class of the data storage")
325333
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")
326334
observerFlags.StringVar(&o.OBServer.Storage.Log.StorageClass, FLAG_LOG_STORAGE_CLASS, DEFAULT_LOG_STORAGE_CLASS, "The storage class of the log storage")
@@ -342,14 +350,14 @@ func (o *CreateOptions) AddMonitorFlags(cmd *cobra.Command) {
342350
// AddBackupVolumeFlags adds the backup-volume-related flags to the command.
343351
func (o *CreateOptions) AddBackupVolumeFlags(cmd *cobra.Command) {
344352
backupVolumeFlags := pflag.NewFlagSet(FLAGSET_BACKUP_VOLUME, pflag.ContinueOnError)
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")
353+
backupVolumeFlags.StringVar(&o.BackupVolume.Address, FLAG_BACKUP_ADDRESS, "", "The storage class of the backup storage")
354+
backupVolumeFlags.StringVar(&o.BackupVolume.Path, FLAG_BACKUP_PATH, "", "The size of the backup storage")
347355
cmd.Flags().AddFlagSet(backupVolumeFlags)
348356
}
349357

350358
// AddParameterFlags adds the parameter-related flags, e.g. __min_full_resource_pool_memory, to the command
351359
func (o *CreateOptions) AddParameterFlags(cmd *cobra.Command) {
352360
parameterFlags := pflag.NewFlagSet(FLAGSET_PARAMETERS, pflag.ContinueOnError)
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")
361+
parameterFlags.StringToStringVar(&o.KvParameters, FLAG_PARAMETERS, map[string]string{"__min_full_resource_pool_memory": DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY, "system_memory": DEFAULT_MIN_SYSTEM_MEMORY}, "Other parameter settings in OBCluster, e.g., __min_full_resource_pool_memory")
354362
cmd.Flags().AddFlagSet(parameterFlags)
355363
}

internal/cli/cluster/enter.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ const (
6161
// Default values for int and string flags
6262
DEFAULT_NAMESPACE = "default"
6363
DEFAULT_ID = 0
64-
DEFAULT_CPU_NUM = 2
65-
DEFAULT_OBSERVER_IMAGE = "oceanbase/oceanbase-cloud-native:4.2.1.6-106000012024042515"
64+
DEFAULT_OBSERVER_IMAGE = "quay.io/oceanbase/oceanbase-cloud-native:4.2.1.6-106000012024042515"
6665
DEFAULT_OBSERVER_CPU = 2
6766
DEFAULT_OBSERVER_MEMORY = 10
6867
DEFAULT_DATA_STORAGE_CLASS = "local-path"
@@ -74,10 +73,8 @@ const (
7473
DEFAULT_MONITOR_IMAGE = "oceanbase/obagent:4.2.1-100000092023101717"
7574
DEFAULT_MONITOR_CPU = 1
7675
DEFAULT_MONITOR_MEMORY = 1
77-
DEFAULT_BACKUP_ADDRESS = "local-path"
78-
DEFAULT_BACKUP_PATH = "/opt/nfs"
7976

8077
// Default values for Parameter flag
8178
DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY = "2147483648"
82-
DEFAULT_SYSTEM_MEMORY = "1G"
79+
DEFAULT_MIN_SYSTEM_MEMORY = "1G"
8380
)

internal/cli/tenant/create.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ type CreateOptions struct {
7878
Timestamp string `json:"timestamp"`
7979
}
8080

81-
func (o *CreateOptions) Parse(_ *cobra.Command, args []string) error {
81+
func (o *CreateOptions) Parse(cmd *cobra.Command, args []string) error {
8282
pools, err := utils.MapZonesToPools(o.ZonePriority)
8383
if err != nil {
8484
return err
8585
}
8686
o.Pools = pools
8787
o.Name = args[0]
88+
o.Cmd = cmd
89+
o.TenantRole = string(apiconst.TenantRolePrimary)
8890
if o.CheckIfFlagChanged("from") {
8991
o.Source.Tenant = &o.From
90-
o.TenantRole = string(apiconst.TenantRolePrimary)
91-
} else {
92-
o.TenantRole = string(apiconst.TenantRoleStandby)
93-
}
94-
// create empty standby tenant
95-
if !o.Restore {
96-
o.Source.Restore = nil
92+
// If flag `restore` is specified, restore a tenant, otherwise create an empty standby tenant.
93+
if !o.Restore {
94+
o.TenantRole = string(apiconst.TenantRoleStandby)
95+
o.Source.Restore = nil
96+
}
9797
}
9898
return nil
9999
}
@@ -162,7 +162,7 @@ func CreateOBTenant(ctx context.Context, p *CreateOptions) (*v1alpha1.OBTenant,
162162
if strings.Contains(*p.Source.Tenant, "/") {
163163
splits := strings.Split(*p.Source.Tenant, "/")
164164
if len(splits) != 2 {
165-
return nil, oberr.NewBadRequest("invalid tenant name")
165+
return nil, fmt.Errorf("invalid tenant name")
166166
}
167167
ns, tenantCR = splits[0], splits[1]
168168
}
@@ -172,29 +172,29 @@ func CreateOBTenant(ctx context.Context, p *CreateOptions) (*v1alpha1.OBTenant,
172172
})
173173
if err != nil {
174174
if kubeerrors.IsNotFound(err) {
175-
return nil, oberr.NewBadRequest("primary tenant not found")
175+
return nil, fmt.Errorf("primary tenant not found")
176176
}
177-
return nil, oberr.NewInternal(err.Error())
177+
return nil, fmt.Errorf(err.Error())
178178
}
179179
if existing.Status.TenantRole != apiconst.TenantRolePrimary {
180-
return nil, oberr.NewBadRequest("the target tenant is not primary tenant")
180+
return nil, fmt.Errorf("the target tenant is not primary tenant")
181181
}
182182
// Match root password
183183
rootSecret, err := k8sclient.ClientSet.CoreV1().Secrets(existing.Namespace).Get(ctx, existing.Status.Credentials.Root, v1.GetOptions{})
184184
if err != nil {
185-
return nil, oberr.NewInternal(err.Error())
185+
return nil, fmt.Errorf(err.Error())
186186
}
187187
if pwd, ok := rootSecret.Data["password"]; ok {
188188
if p.RootPassword != string(pwd) {
189-
return nil, oberr.NewBadRequest("root password not match")
189+
return nil, fmt.Errorf("root password not match")
190190
}
191191
if t.Spec.Credentials.Root != "" {
192192
err = createPasswordSecret(ctx, types.NamespacedName{
193193
Namespace: nn.Namespace,
194194
Name: t.Spec.Credentials.Root,
195195
}, p.RootPassword)
196196
if err != nil {
197-
return nil, oberr.NewInternal(err.Error())
197+
return nil, fmt.Errorf(err.Error())
198198
}
199199
}
200200
}
@@ -414,8 +414,8 @@ func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
414414
baseFlags.StringVarP(&o.TenantName, FLAG_TENANT_NAME, "n", "", "Tenant name, if not specified, use name in k8s instead")
415415
baseFlags.StringVar(&o.ClusterName, FLAG_CLUSTER_NAME, "", "The cluster name tenant belonged to in k8s")
416416
baseFlags.StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The namespace of the tenant")
417-
baseFlags.StringVarP(&o.RootPassword, FLAG_ROOTPASSWD, "p", "", "The root password of the cluster")
418-
baseFlags.StringVar(&o.Charset, FLAG_CHARSET, DEFAULT_CHARSET, "The charset using in ob tenant")
417+
baseFlags.StringVarP(&o.RootPassword, FLAG_ROOTPASSWD, "p", "", "The root password of the primary tenant, if not specified, generate a random password")
418+
baseFlags.StringVarP(&o.Charset, FLAG_CHARSET, "c", DEFAULT_CHARSET, "The charset using in ob tenant")
419419
baseFlags.StringVar(&o.ConnectWhiteList, FLAG_CONNECT_WHITE_LIST, DEFAULT_CONNECT_WHITE_LIST, "The connect white list using in ob tenant")
420420
baseFlags.StringVar(&o.From, FLAG_FROM, "", "restore from data source")
421421
}

internal/cli/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
const (
38-
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~#%^&*_-+|(){}[]:;,.?/\""
38+
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~#%^&*_-+|(){}[]:,.?/"
3939
factor = 4294901759
4040
)
4141

0 commit comments

Comments
 (0)