mirror of
https://github.com/mariadb-operator/mariadb-operator.git
synced 2025-07-28 23:28:17 +00:00
Initial database created via Database CR
This commit is contained in:
@ -189,3 +189,11 @@ func (m *MariaDB) MariadbSysGrantKey() types.NamespacedName {
|
||||
Namespace: m.Namespace,
|
||||
}
|
||||
}
|
||||
|
||||
// MariadbDatabaseKey defines the key for the initial database
|
||||
func (m *MariaDB) MariadbDatabaseKey() types.NamespacedName {
|
||||
return types.NamespacedName{
|
||||
Name: fmt.Sprintf("%s-database", m.Name),
|
||||
Namespace: m.Namespace,
|
||||
}
|
||||
}
|
||||
|
@ -362,7 +362,7 @@ type MariaDBSpec struct {
|
||||
// +optional
|
||||
// +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:booleanSwitch", "urn:alm:descriptor:com.tectonic.ui:advanced"}
|
||||
RootEmptyPassword *bool `json:"rootEmptyPassword,omitempty" webhook:"inmutableinit"`
|
||||
// Database is the database to be created on bootstrap.
|
||||
// Database is the initial database to be created by the operator once MariaDB is ready.
|
||||
// +optional
|
||||
// +operator-sdk:csv:customresourcedefinitions:type=spec,xDescriptors={"urn:alm:descriptor:com.tectonic.ui:advanced"}
|
||||
Database *string `json:"database,omitempty" webhook:"inmutable"`
|
||||
|
@ -3829,7 +3829,8 @@ spec:
|
||||
type: string
|
||||
type: object
|
||||
database:
|
||||
description: Database is the database to be created on bootstrap.
|
||||
description: Database is the initial database to be created by the
|
||||
operator once MariaDB is ready.
|
||||
type: string
|
||||
env:
|
||||
description: Env represents the environment variables to be injected
|
||||
|
@ -726,7 +726,46 @@ func (r *MariaDBReconciler) reconcileSQL(ctx context.Context, mariadb *mariadbv1
|
||||
log.FromContext(ctx).V(1).Info("MariaDB not ready. Requeuing SQL resources")
|
||||
return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
|
||||
}
|
||||
if err := r.reconcileDatabase(ctx, mariadb); err != nil {
|
||||
return ctrl.Result{}, fmt.Errorf("error reconciling database: %v", err)
|
||||
}
|
||||
if result, err := r.reconcileUsers(ctx, mariadb); !result.IsZero() || err != nil {
|
||||
return result, err
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (r *MariaDBReconciler) reconcileDatabase(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB) error {
|
||||
if mariadb.Spec.Database == nil {
|
||||
return nil
|
||||
}
|
||||
var database mariadbv1alpha1.Database
|
||||
err := r.Get(ctx, mariadb.MariadbDatabaseKey(), &database)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return fmt.Errorf("error getting database: %v", err)
|
||||
}
|
||||
|
||||
opts := builder.DatabaseOpts{
|
||||
Name: *mariadb.Spec.Database,
|
||||
Metadata: mariadb.Spec.InheritMetadata,
|
||||
MariaDBRef: mariadbv1alpha1.MariaDBRef{
|
||||
ObjectReference: corev1.ObjectReference{
|
||||
Name: mariadb.Name,
|
||||
Namespace: mariadb.Namespace,
|
||||
},
|
||||
},
|
||||
}
|
||||
db, err := r.Builder.BuildDatabase(mariadb.MariadbDatabaseKey(), mariadb, opts)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error building database: %v", err)
|
||||
}
|
||||
return r.Create(ctx, db)
|
||||
}
|
||||
|
||||
func (r *MariaDBReconciler) reconcileUsers(ctx context.Context, mariadb *mariadbv1alpha1.MariaDB) (ctrl.Result, error) {
|
||||
userKey := mariadb.MariadbSysUserKey()
|
||||
userOpts := builder.UserOpts{
|
||||
MariaDBRef: mariadbv1alpha1.MariaDBRef{
|
||||
|
@ -406,12 +406,6 @@ func mariadbEnv(mariadb *mariadbv1alpha1.MariaDB) []corev1.EnvVar {
|
||||
}
|
||||
|
||||
if !mariadb.Replication().Enabled {
|
||||
if mariadb.Spec.Database != nil {
|
||||
env = append(env, corev1.EnvVar{
|
||||
Name: "MARIADB_DATABASE",
|
||||
Value: *mariadb.Spec.Database,
|
||||
})
|
||||
}
|
||||
if mariadb.Spec.Username != nil {
|
||||
env = append(env, corev1.EnvVar{
|
||||
Name: "MARIADB_USER",
|
||||
|
@ -78,3 +78,27 @@ func (b *Builder) BuildGrant(key types.NamespacedName, owner metav1.Object, opts
|
||||
}
|
||||
return grant, nil
|
||||
}
|
||||
|
||||
type DatabaseOpts struct {
|
||||
Name string
|
||||
Metadata *mariadbv1alpha1.Metadata
|
||||
MariaDBRef mariadbv1alpha1.MariaDBRef
|
||||
}
|
||||
|
||||
func (b *Builder) BuildDatabase(key types.NamespacedName, owner metav1.Object, opts DatabaseOpts) (*mariadbv1alpha1.Database, error) {
|
||||
objMeta :=
|
||||
metadata.NewMetadataBuilder(key).
|
||||
WithMetadata(opts.Metadata).
|
||||
Build()
|
||||
database := &mariadbv1alpha1.Database{
|
||||
ObjectMeta: objMeta,
|
||||
Spec: mariadbv1alpha1.DatabaseSpec{
|
||||
MariaDBRef: opts.MariaDBRef,
|
||||
Name: opts.Name,
|
||||
},
|
||||
}
|
||||
if err := controllerutil.SetControllerReference(owner, database, b.scheme); err != nil {
|
||||
return nil, fmt.Errorf("error setting controller reference to Database: %v", err)
|
||||
}
|
||||
return database, nil
|
||||
}
|
||||
|
@ -188,16 +188,6 @@ func (r *ReplicationConfig) reconcilePrimarySql(ctx context.Context, mariadb *ma
|
||||
}
|
||||
}
|
||||
|
||||
if mariadb.Spec.Database != nil {
|
||||
databaseOpts := sqlClient.DatabaseOpts{
|
||||
CharacterSet: "utf8",
|
||||
Collate: "utf8_general_ci",
|
||||
}
|
||||
if err := client.CreateDatabase(ctx, *mariadb.Spec.Database, databaseOpts); err != nil {
|
||||
return fmt.Errorf("error creating database: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
opts := userSqlOpts{
|
||||
username: replUser,
|
||||
host: replUserHost,
|
||||
|
Reference in New Issue
Block a user