Skip to content

Commit

Permalink
WIP #33: Adding basic fields for character set and collation
Browse files Browse the repository at this point in the history
  • Loading branch information
cuppett committed Jul 3, 2023
1 parent 3433373 commit 0456484
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
26 changes: 26 additions & 0 deletions api/v1alpha1/adminconnection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ type AdminConnectionStatus struct {
// Indicates current database is set and ready
// +kubebuilder:validation:Optional
ControlDatabase string `json:"controlDatabase,omitEmpty"`
// The default character set to be used for new databases where character set is not specified
// +kubebuilder:validation:Optional
// +nullable
CharacterSet string `json:"characterSet,omitEmpty"`
// The default collation to be used for new databases where collation is not specified
// +kubebuilder:validation:Optional
// +nullable
Collation string `json:"collation,omitEmpty"`
// The list of character sets and collations available in the server
// +kubebuilder:validation:Optional
// +nullable
AvailableCharsets []Charset `json:"availableCharsets,omitEmpty"`
}

type Charset struct {
// The name of the character set
Name string `json:"name,omitEmpty"`
// The list of collations available for the character set
Collations []Collation `json:"collations,omitEmpty"`
}

type Collation struct {
// The name of the collation
Name string `json:"name,omitEmpty"`
// Whether it is the default collation for the character set
Default bool `json:"default,omitEmpty"`
}

// +kubebuilder:object:root=true
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/adminconnection_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ var _ = Describe("AdminConnection_Types", func() {

JustAfterEach(func() {
if createUser {
dropQuery := "DROP USER IF EXISTS `" + Escape(user.Spec.Username) + "`"
dropQuery := "DROP USER IF EXISTS '" + Escape(user.Spec.Username) + "'"
tx := gormDB.Exec(dropQuery)
Expect(tx.Error).To(BeNil())
}
Expand Down
42 changes: 42 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions config/crd/bases/mysql.apps.cuppett.dev_adminconnections.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,47 @@ spec:
status:
description: AdminConnectionStatus defines the observed state of AdminConnection
properties:
availableCharsets:
description: The list of character sets and collations available in
the server
items:
properties:
collations:
description: The list of collations available for the character
set
items:
properties:
default:
description: Whether it is the default collation for the
character set
type: boolean
name:
description: The name of the collation
type: string
required:
- default
- name
type: object
type: array
name:
description: The name of the character set
type: string
required:
- collations
- name
type: object
nullable: true
type: array
characterSet:
description: The default character set to be used for new databases
where character set is not specified
nullable: true
type: string
collation:
description: The default collation to be used for new databases where
collation is not specified
nullable: true
type: string
controlDatabase:
description: Indicates current database is set and ready
type: string
Expand Down
32 changes: 31 additions & 1 deletion controllers/adminconnection_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package controllers

import (
"context"
"fmt"
"github.com/cuppett/mysql-dba-operator/orm"
"github.com/go-logr/logr"
"gorm.io/gorm"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -73,17 +75,45 @@ func (r *AdminConnectionReconciler) Reconcile(ctx context.Context, req ctrl.Requ
defer r.Status().Update(ctx, instance)

// Establish the database connection
_, err = instance.GetDatabaseConnection(ctx, r.Client, r.Connections)
db, err := instance.GetDatabaseConnection(ctx, r.Client, r.Connections)
if err != nil {
instance.Status.Message = "Failed to connect or ping database"
return ctrl.Result{}, err
}

instance.Status.CharacterSet, err = r.getVariable("character_set_server", db)
if err != nil {
instance.Status.Message = "Failed to retrieve default server character set"
return ctrl.Result{}, err
}

instance.Status.Collation, err = r.getVariable("collation_server", db)
if err != nil {
instance.Status.Message = "Failed to retrieve default server collation"
return ctrl.Result{}, err
}

instance.Status.Message = "Successfully pinged database"
instance.Status.ControlDatabase = orm.DatabaseName
return ctrl.Result{}, nil
}

func (r *AdminConnectionReconciler) getVariable(name string, db *gorm.DB) (string, error) {

var results []map[string]interface{}
query := "SHOW VARIABLES LIKE '" + mysqlv1alpha1.Escape(name) + "'"
tx := db.Raw(query).Scan(&results)
if tx.Error != nil {
return "", tx.Error
}

if len(results) != 1 {
return "", fmt.Errorf("expected 1 row, got %v", len(results))
}

return results[0]["Value"].(string), nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *AdminConnectionReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Expand Down

0 comments on commit 0456484

Please sign in to comment.