Skip to content

Commit 031c0bd

Browse files
jabbrwckyJens Hausherr
andauthored
Fix Grant revocation (#118)
* Fix Grant revocation If the grant is already gone when the reconclier tries to revoke it, it should mark the resource as successfully removed, not error. Signed-off-by: Jens Hausherr <[email protected]> * Fix linter findings Signed-off-by: Jens Hausherr <[email protected]> Signed-off-by: Jens Hausherr <[email protected]> Co-authored-by: Jens Hausherr <[email protected]>
1 parent e4ab206 commit 031c0bd

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pkg/controller/mysql/grant/reconciler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error {
329329
)
330330

331331
if err := c.db.Exec(ctx, xsql.Query{String: query}); err != nil {
332+
var myErr *mysqldriver.MySQLError
333+
if errors.As(err, &myErr) && myErr.Number == errCodeNoSuchGrant {
334+
// MySQL automatically deletes related grants if the user has been deleted
335+
return nil
336+
}
332337
return errors.Wrap(err, errRevokeGrant)
333338
}
334339
err := c.db.Exec(ctx, xsql.Query{String: "FLUSH PRIVILEGES"})

pkg/controller/mysql/grant/reconciler_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,27 @@ func TestDelete(t *testing.T) {
748748
},
749749
want: nil,
750750
},
751+
"SuccessGrantGone": {
752+
reason: "No error should be returned if the grant is already revoked",
753+
args: args{
754+
mg: &v1alpha1.Grant{
755+
Spec: v1alpha1.GrantSpec{
756+
ForProvider: v1alpha1.GrantParameters{
757+
Database: pointer.StringPtr("test-example"),
758+
User: pointer.StringPtr("test-example"),
759+
},
760+
},
761+
},
762+
},
763+
fields: fields{
764+
db: &mockDB{
765+
MockExec: func(ctx context.Context, q xsql.Query) error {
766+
return &mysql.MySQLError{Number: errCodeNoSuchGrant}
767+
},
768+
},
769+
},
770+
want: nil,
771+
},
751772
}
752773

753774
for name, tc := range cases {

0 commit comments

Comments
 (0)