@@ -14,21 +14,23 @@ See the Mulan PSL v2 for more details.
1414package cluster
1515
1616import (
17- "errors "
17+ "context "
1818 "fmt"
1919
20- apitypes "github.com/oceanbase/ob-operator/api/types"
21- "github.com/oceanbase/ob-operator/api/v1alpha1"
22- oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase"
23- "github.com/oceanbase/ob-operator/internal/dashboard/business/common"
24- "github.com/oceanbase/ob-operator/internal/dashboard/business/constant"
25-
20+ "github.com/pkg/errors"
2621 "github.com/spf13/cobra"
2722 "github.com/spf13/pflag"
2823 corev1 "k8s.io/api/core/v1"
2924 apiresource "k8s.io/apimachinery/pkg/api/resource"
3025 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3126
27+ apitypes "github.com/oceanbase/ob-operator/api/types"
28+ "github.com/oceanbase/ob-operator/api/v1alpha1"
29+ "github.com/oceanbase/ob-operator/internal/clients"
30+ oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase"
31+ "github.com/oceanbase/ob-operator/internal/dashboard/business/common"
32+ "github.com/oceanbase/ob-operator/internal/dashboard/business/constant"
33+
3234 "github.com/oceanbase/ob-operator/internal/cli/generic"
3335 utils "github.com/oceanbase/ob-operator/internal/cli/utils"
3436 modelcommon "github.com/oceanbase/ob-operator/internal/dashboard/model/common"
@@ -75,7 +77,7 @@ func (o *CreateOptions) Validate() error {
7577 return nil
7678}
7779
78- func (o * CreateOptions ) Parse (_ * cobra.Command , args []string ) error {
80+ func (o * CreateOptions ) Parse (cmd * cobra.Command , args []string ) error {
7981 // Parse the zone topology
8082 topology , err := utils .MapZonesToTopology (o .Zones )
8183 if err != nil {
@@ -93,6 +95,7 @@ func (o *CreateOptions) Parse(_ *cobra.Command, args []string) error {
9395 o .Parameters = parameters
9496 o .Topology = topology
9597 o .Name = args [0 ]
98+ o .Cmd = cmd
9699 return nil
97100}
98101
@@ -263,38 +266,66 @@ func buildMonitorTemplate(monitorSpec *param.MonitorSpec) *apitypes.MonitorTempl
263266 return monitorTemplate
264267}
265268
266- // CreateOBClusterInstance creates an OBClusterInstance
267- func CreateOBClusterInstance ( param * CreateOptions ) * v1alpha1.OBCluster {
268- observerTemplate := buildOBServerTemplate (param .OBServer )
269- monitorTemplate := buildMonitorTemplate (param .Monitor )
270- backupVolume := buildBackupVolume (param .BackupVolume )
271- parameters := buildOBClusterParameters (param .Parameters )
272- topology := buildOBClusterTopology (param .Topology )
269+ // CreateOBCluster creates an OBCluster
270+ func CreateOBCluster ( ctx context. Context , o * CreateOptions ) ( * v1alpha1.OBCluster , error ) {
271+ observerTemplate := buildOBServerTemplate (o .OBServer )
272+ monitorTemplate := buildMonitorTemplate (o .Monitor )
273+ backupVolume := buildBackupVolume (o .BackupVolume )
274+ parameters := buildOBClusterParameters (o .Parameters )
275+ topology := buildOBClusterTopology (o .Topology )
273276 obcluster := & v1alpha1.OBCluster {
274277 ObjectMeta : metav1.ObjectMeta {
275- Namespace : param .Namespace ,
276- Name : param .Name ,
278+ Namespace : o .Namespace ,
279+ Name : o .Name ,
277280 Annotations : map [string ]string {},
278281 },
279282 Spec : v1alpha1.OBClusterSpec {
280- ClusterName : param .ClusterName ,
281- ClusterId : param .ClusterId ,
283+ ClusterName : o .ClusterName ,
284+ ClusterId : o .ClusterId ,
282285 OBServerTemplate : observerTemplate ,
283286 MonitorTemplate : monitorTemplate ,
284287 BackupVolume : backupVolume ,
285288 Parameters : parameters ,
286289 Topology : topology ,
287- UserSecrets : utils .GenerateUserSecrets (param .Name , param .ClusterId ),
290+ UserSecrets : utils .GenerateUserSecrets (o .Name , o .ClusterId ),
288291 },
289292 }
290- switch param .Mode {
293+ switch o .Mode {
291294 case string (modelcommon .ClusterModeStandalone ):
292295 obcluster .Annotations [oceanbaseconst .AnnotationsMode ] = oceanbaseconst .ModeStandalone
293296 case string (modelcommon .ClusterModeService ):
294297 obcluster .Annotations [oceanbaseconst .AnnotationsMode ] = oceanbaseconst .ModeService
295298 default :
296299 }
297- return obcluster
300+ if err := CreateSecretsForOBCluster (ctx , obcluster , o .RootPassword ); err != nil {
301+ return nil , err
302+ }
303+ obcluster , err := clients .CreateOBCluster (ctx , obcluster )
304+ if err != nil {
305+ return nil , err
306+ }
307+ return obcluster , nil
308+ }
309+
310+ // CreateSecretsForOBCluster creates secrets for OBCluster
311+ func CreateSecretsForOBCluster (ctx context.Context , obcluster * v1alpha1.OBCluster , rootPass string ) error {
312+ err := utils .CreatePasswordSecret (ctx , obcluster .Namespace , obcluster .Spec .UserSecrets .Root , rootPass )
313+ if err != nil {
314+ return errors .Wrap (err , "Create secret for user root" )
315+ }
316+ err = utils .CreatePasswordSecret (ctx , obcluster .Namespace , obcluster .Spec .UserSecrets .Monitor , utils .GenerateNaivePassword ())
317+ if err != nil {
318+ return errors .Wrap (err , "Create secret for user monitor" )
319+ }
320+ err = utils .CreatePasswordSecret (ctx , obcluster .Namespace , obcluster .Spec .UserSecrets .ProxyRO , utils .GenerateNaivePassword ())
321+ if err != nil {
322+ return errors .Wrap (err , "Create secret for user proxyro" )
323+ }
324+ err = utils .CreatePasswordSecret (ctx , obcluster .Namespace , obcluster .Spec .UserSecrets .Operator , utils .GenerateNaivePassword ())
325+ if err != nil {
326+ return errors .Wrap (err , "Create secret for user operator" )
327+ }
328+ return nil
298329}
299330
300331// AddFlags adds base and specific feature flags, Only support observer and zone config
@@ -319,7 +350,7 @@ func (o *CreateOptions) AddBaseFlags(cmd *cobra.Command) {
319350 baseFlags .StringVarP (& o .ClusterName , FLAG_CLUSTER_NAME , "n" , "" , "Cluster name, if not specified, use resource name in k8s instead" )
320351 baseFlags .StringVar (& o .Namespace , FLAG_NAMESPACE , DEFAULT_NAMESPACE , "The namespace of the cluster" )
321352 baseFlags .Int64Var (& o .ClusterId , FLAG_CLUSTER_ID , DEFAULT_ID , "The id of the cluster" )
322- baseFlags .StringVarP (& o .RootPassword , FLAG_ROOTPASSWD , "p" , "" , "The root password of the cluster" )
353+ baseFlags .StringVarP (& o .RootPassword , FLAG_ROOT_PASSWORD , "p" , "" , "The root password of the cluster" )
323354 baseFlags .StringVar (& o .Mode , FLAG_MODE , "" , "The mode of the cluster" )
324355}
325356
@@ -358,6 +389,6 @@ func (o *CreateOptions) AddBackupVolumeFlags(cmd *cobra.Command) {
358389// AddParameterFlags adds the parameter-related flags, e.g. __min_full_resource_pool_memory, to the command
359390func (o * CreateOptions ) AddParameterFlags (cmd * cobra.Command ) {
360391 parameterFlags := pflag .NewFlagSet (FLAGSET_PARAMETERS , pflag .ContinueOnError )
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" )
392+ parameterFlags .StringToStringVar (& o .KvParameters , FLAG_PARAMETERS , map [string ]string {FLAG_MIN_FULL_RESOURCE_POOL_MEMORY : DEFAULT_MIN_FULL_RESOURCE_POOL_MEMORY , FLAG_SYSTEM_MEMORY : DEFAULT_SYSTEM_MEMORY }, "Other parameter settings in OBCluster, e.g., __min_full_resource_pool_memory" )
362393 cmd .Flags ().AddFlagSet (parameterFlags )
363394}
0 commit comments