Skip to content

Commit fecd8c3

Browse files
guozhi.licndoit18
authored andcommitted
git squash
1 parent 9919114 commit fecd8c3

File tree

8 files changed

+92
-7
lines changed

8 files changed

+92
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
88
### Added
9+
* `MysqlDatabase` `MysqlUser` Add delete policy
910
### Changed
1011
### Removed
1112
### Fixed

examples/example-database.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apiVersion: mysql.presslabs.org/v1alpha1
22
kind: MysqlDatabase
33
metadata:
44
name: my-database
5+
# annotations:
6+
# mysql-operator.presslabs.org/resourceDeletionPolicy: retain # When the MysqlDatabase is deleted, the MySQL DB will be preserved
57
spec:
68
database: db-name-in-mysql
79
clusterRef:

examples/example-user.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ apiVersion: mysql.presslabs.org/v1alpha1
1010
kind: MysqlUser
1111
metadata:
1212
name: my-user
13+
# annotations:
14+
# mysql-operator.presslabs.org/resourceDeletionPolicy: retain # When the MysqlUser is deleted, the MySQL user will be preserved
1315
spec:
1416
user: user-name-in-mysql
1517
clusterRef:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package v1alpha1
2+
3+
import "sigs.k8s.io/controller-runtime/pkg/client"
4+
5+
// MysqlResourceDeletionPolicy delete policy defined
6+
type MysqlResourceDeletionPolicy string
7+
8+
const (
9+
// MysqlResourceDeletionPolicyAnnotationKey delete policy key defined
10+
MysqlResourceDeletionPolicyAnnotationKey = "mysql-operator.presslabs.org/resourceDeletionPolicy"
11+
// MysqlResourceDeletionPolicyDelete delete policy delete
12+
MysqlResourceDeletionPolicyDelete = MysqlResourceDeletionPolicy("delete")
13+
// MysqlResourceDeletionPolicyRetain delete policy retain
14+
MysqlResourceDeletionPolicyRetain = MysqlResourceDeletionPolicy("retain")
15+
)
16+
17+
// DeletionPolicyRetain parse obj is need to retain
18+
func DeletionPolicyRetain(obj client.Object) bool {
19+
annotations := obj.GetAnnotations()
20+
return annotations != nil && MysqlResourceDeletionPolicy(annotations[MysqlResourceDeletionPolicyAnnotationKey]) == MysqlResourceDeletionPolicyRetain
21+
}

pkg/controller/mysqldatabase/db_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func (r *ReconcileMySQLDatabase) Reconcile(ctx context.Context, request reconcil
101101

102102
// remove finalizer
103103
utilmeta.RemoveFinalizer(&db.ObjectMeta, mysqlPreventDeletionFinalizer)
104-
105104
// update resource to remove finalizer, no status
106105
return reconcile.Result{}, r.Update(ctx, db.Unwrap())
107106
}
@@ -124,6 +123,10 @@ func (r *ReconcileMySQLDatabase) Reconcile(ctx context.Context, request reconcil
124123
}
125124

126125
func (r *ReconcileMySQLDatabase) deleteDatabase(ctx context.Context, db *mysqldatabase.Database) error {
126+
// if it's retain,do nothing.
127+
if mysqlv1alpha1.DeletionPolicyRetain(db) {
128+
return nil
129+
}
127130
log.Info("deleting MySQL database", "name", db.Name, "database", db.Spec.Database)
128131
sql, closeConn, err := r.SQLRunnerFactory(mysql.NewConfigFromClusterKey(r.Client, db.GetClusterKey()))
129132
if apierrors.IsNotFound(err) {

pkg/controller/mysqldatabase/db_controller_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ var _ = Describe("MySQL database controller", func() {
157157
Expect(c.Get(context.TODO(), dbObjKey(db), db.Unwrap())).To(Succeed())
158158
Expect(db.Finalizers).To(ContainElement(mysqlPreventDeletionFinalizer))
159159
})
160-
161160
It("should drop the database when deleted ", func() {
162161
fakeQR.AddExpectedCalls(
163162
func(query string, args ...interface{}) error {
@@ -203,6 +202,35 @@ var _ = Describe("MySQL database controller", func() {
203202
return c.Get(context.TODO(), dbObjKey(db), db.Unwrap())
204203
}).ShouldNot(Succeed())
205204
})
205+
It("should delete resource and database should retain", func() {
206+
fakeQR.AddExpectedCalls(
207+
func(query string, args ...interface{}) error {
208+
defer GinkgoRecover()
209+
210+
PanicWith("Shouldn't call this!")
211+
return nil
212+
},
213+
)
214+
215+
if db.ObjectMeta.Annotations == nil {
216+
db.ObjectMeta.Annotations = map[string]string{}
217+
}
218+
db.ObjectMeta.Annotations[mysqlv1alpha1.MysqlResourceDeletionPolicyAnnotationKey] = string(mysqlv1alpha1.MysqlResourceDeletionPolicyRetain)
219+
220+
// delete the resource
221+
Expect(c.Delete(context.TODO(), db.Unwrap())).To(Succeed())
222+
223+
// expect the reconcile event to delete db and remove finalizer
224+
Eventually(requests).Should(Receive(Equal(expectedRequest)))
225+
226+
// the database should be gone
227+
Expect(c.Get(context.TODO(), dbObjKey(db), db.Unwrap())).ShouldNot(Succeed())
228+
229+
// last event but already deleted
230+
Eventually(requests).Should(Receive(Equal(expectedRequest)))
231+
232+
delete(db.ObjectMeta.Annotations, mysqlv1alpha1.MysqlResourceDeletionPolicyAnnotationKey)
233+
})
206234
})
207235
})
208236

pkg/controller/mysqluser/mysqluser_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ func (r *ReconcileMySQLUser) Reconcile(ctx context.Context, request reconcile.Re
114114
func (r *ReconcileMySQLUser) removeUser(ctx context.Context, user *mysqluser.MySQLUser) error {
115115
// The resource has been deleted
116116
if meta.HasFinalizer(&user.ObjectMeta, userFinalizer) {
117-
// Drop the user if the finalizer is still present
118-
if err := r.dropUserFromDB(ctx, user); err != nil {
119-
return err
117+
// if it's retain,don't drop user.
118+
if !mysqlv1alpha1.DeletionPolicyRetain(user) {
119+
// Drop the user if the finalizer is still present
120+
if err := r.dropUserFromDB(ctx, user); err != nil {
121+
return err
122+
}
120123
}
121-
122124
meta.RemoveFinalizer(&user.ObjectMeta, userFinalizer)
123125

124126
// update resource so it will remove the finalizer

pkg/controller/mysqluser/mysqluser_controller_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ var _ = Describe("MySQL user controller", func() {
503503
// We need to make sure that the controller does not create infinite loops
504504
Consistently(requests).ShouldNot(Receive(Equal(expectedRequest)))
505505
})
506-
507506
It("removes the user finalizer, and the resource is deleted", func() {
508507
fakeSQL.AllowExtraCalls()
509508

@@ -520,6 +519,33 @@ var _ = Describe("MySQL user controller", func() {
520519
err := c.Get(context.TODO(), userKey, user.Unwrap())
521520
Expect(apierrors.IsNotFound(err)).To(BeTrue())
522521
})
522+
It("removes the user finalizer, and the resource is deleted,database user retain", func() {
523+
fakeSQL.AddExpectedCalls(func(query string, args ...interface{}) error {
524+
defer GinkgoRecover()
525+
526+
PanicWith("Shouldn't call this!")
527+
return nil
528+
})
529+
530+
if user.ObjectMeta.Annotations == nil {
531+
user.ObjectMeta.Annotations = map[string]string{}
532+
}
533+
user.ObjectMeta.Annotations[mysqlv1alpha1.MysqlResourceDeletionPolicyAnnotationKey] = string(mysqlv1alpha1.MysqlResourceDeletionPolicyRetain)
534+
535+
Expect(c.Delete(context.TODO(), user.Unwrap())).To(Succeed())
536+
// Wait for initial reconciliation
537+
Eventually(requests, timeout).Should(Receive(Equal(expectedRequest)))
538+
539+
// Wait for second reconciliation triggered by finalizer removal
540+
Eventually(requests, timeout).Should(Receive(Equal(expectedRequest)))
541+
542+
// We need to make sure that the controller does not create infinite loops
543+
Consistently(requests).ShouldNot(Receive(Equal(expectedRequest)))
544+
545+
err := c.Get(context.TODO(), userKey, user.Unwrap())
546+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
547+
delete(user.ObjectMeta.Annotations, mysqlv1alpha1.MysqlResourceDeletionPolicyAnnotationKey)
548+
})
523549
})
524550
Context("and the user cannot be deleted in mysql", func() {
525551
deletionResult := errors.New("couldn't delete user")

0 commit comments

Comments
 (0)