Skip to content

Commit 09b6694

Browse files
committed
add MariaDBAccount finalizer to Secret (and remove on delete)
in mariadbdatabase_funcs, the EnsureMariaDBAccount function called by external controllers adds a finalizer for that calling controller to the Secret referenced by the MariaDBAccount. This seems a little off, since the Secret is most immediately needed by the MariaDBAccount CR itself, and the controller refers to that MariaDBAccount CR also. It seems more appropriate that MariaDBAccount itself should maintain its own finalizer on that Secret, so this logic is added there. The change here causes the API function EnsureMariaDBAccount to add a finalizer to the secret that is local to the mariadbaccount, rather than the helper passed for the calling controller. Existing "remove finalizer" calls which look for the calling controller's finalizer tag in the secret are maintained however to assist with backwards compatibility. This comes up now because we are seeking to add a new class of system-level MariaDBAccount that is used only by the Galera controller itself, but also that these accounts (really all accounts, but mainly the system ones) will support in-place password changes by updating the name of the Secret to be used, implying the old one is no longer needed once the change takes place; it therefore is most appropriate that MariaDBAccount maintain its own finalizers on these secrets.
1 parent cbeb723 commit 09b6694

20 files changed

+540
-90
lines changed

api/bases/mariadb.openstack.org_mariadbaccounts.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ spec:
4848
spec:
4949
description: MariaDBAccountSpec defines the desired state of MariaDBAccount
5050
properties:
51+
accountType:
52+
default: User
53+
enum:
54+
- User
55+
- System
56+
type: string
5157
requireTLS:
5258
default: false
5359
description: Account must use TLS to connect to the database

api/v1beta1/mariadbaccount_types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ type MariaDBAccountSpec struct {
4848
// Account must use TLS to connect to the database
4949
// +kubebuilder:default=false
5050
RequireTLS bool `json:"requireTLS"`
51+
52+
// +kubebuilder:validation:Enum=User;System
53+
// +kubebuilder:default=User
54+
AccountType AccountType `json:"accountType,omitempty"`
5155
}
5256

57+
type AccountType string
58+
59+
const (
60+
User AccountType = "User"
61+
System AccountType = "System"
62+
)
63+
5364
// MariaDBAccountStatus defines the observed state of MariaDBAccount
5465
type MariaDBAccountStatus struct {
5566
// Deployment Conditions
@@ -85,3 +96,11 @@ type MariaDBAccountList struct {
8596
func init() {
8697
SchemeBuilder.Register(&MariaDBAccount{}, &MariaDBAccountList{})
8798
}
99+
100+
func (mariadbAccount MariaDBAccount) IsSystemAccount() bool {
101+
return mariadbAccount.Spec.AccountType == System
102+
}
103+
104+
func (mariadbAccount MariaDBAccount) IsUserAccount() bool {
105+
return mariadbAccount.Spec.AccountType == "" || mariadbAccount.Spec.AccountType == User
106+
}

api/v1beta1/mariadbdatabase_funcs.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ func (d *Database) DeleteFinalizer(
449449
h *helper.Helper,
450450
) error {
451451

452+
// LEGACY: remove finalizer from the secret in terms of the caller.
453+
// we now don't add the caller's finalizer to the secret, we only add
454+
// the mariadbaccount finalizer.
452455
if d.secretObj != nil && controllerutil.RemoveFinalizer(d.secretObj, h.GetFinalizer()) {
453456
err := h.GetClient().Update(ctx, d.secretObj)
454457
if err != nil && !k8s_errors.IsNotFound(err) {
@@ -548,6 +551,9 @@ func DeleteDatabaseAndAccountFinalizers(
548551
return err
549552
}
550553

554+
// LEGACY: remove finalizer from the secret in terms of the caller.
555+
// we now don't add the caller's finalizer to the secret, we only add
556+
// the mariadbaccount finalizer.
551557
if err == nil && controllerutil.RemoveFinalizer(dbSecret, h.GetFinalizer()) {
552558
err := h.GetClient().Update(ctx, dbSecret)
553559
if err != nil && !k8s_errors.IsNotFound(err) {
@@ -624,6 +630,9 @@ func DeleteUnusedMariaDBAccountFinalizers(
624630
return err
625631
}
626632

633+
// LEGACY: remove finalizer from the secret in terms of the caller.
634+
// we now don't add the caller's finalizer to the secret, we only add
635+
// the mariadbaccount finalizer.
627636
if dbSecret != nil && controllerutil.RemoveFinalizer(dbSecret, h.GetFinalizer()) {
628637
err := h.GetClient().Update(ctx, dbSecret)
629638
if err != nil && !k8s_errors.IsNotFound(err) {
@@ -708,7 +717,6 @@ func createOrPatchAccountAndSecret(
708717
// GetDatabaseByNameAndAccount to locate the Database which is how
709718
// they remove finalizers. this will return not found if secret
710719
// is not present, so finalizer will keep it around
711-
controllerutil.AddFinalizer(accountSecret, h.GetFinalizer())
712720

713721
return nil
714722
})

config/crd/bases/mariadb.openstack.org_mariadbaccounts.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ spec:
4848
spec:
4949
description: MariaDBAccountSpec defines the desired state of MariaDBAccount
5050
properties:
51+
accountType:
52+
default: User
53+
enum:
54+
- User
55+
- System
56+
type: string
5157
requireTLS:
5258
default: false
5359
description: Account must use TLS to connect to the database

controllers/galera_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,9 +1058,8 @@ func (r *GaleraReconciler) SetupWithManager(mgr ctrl.Manager) error {
10581058
Complete(r)
10591059
}
10601060

1061-
// GetDatabaseObject - returns either a Galera or MariaDB object (and an associated client.Object interface).
1061+
// GetDatabaseObject - returns a Galera object.
10621062
// used by both MariaDBDatabaseReconciler and MariaDBAccountReconciler
1063-
// this will later return only Galera objects, so as a lookup it's part of the galera controller
10641063
func GetDatabaseObject(ctx context.Context, clientObj client.Client, name string, namespace string) (*mariadbv1.Galera, error) {
10651064
dbGalera := &mariadbv1.Galera{
10661065
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)