Skip to content

Commit

Permalink
Fix Grant revocation (#118)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
jabbrwcky and Jens Hausherr authored Nov 21, 2022
1 parent e4ab206 commit 031c0bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/controller/mysql/grant/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error {
)

if err := c.db.Exec(ctx, xsql.Query{String: query}); err != nil {
var myErr *mysqldriver.MySQLError
if errors.As(err, &myErr) && myErr.Number == errCodeNoSuchGrant {
// MySQL automatically deletes related grants if the user has been deleted
return nil
}
return errors.Wrap(err, errRevokeGrant)
}
err := c.db.Exec(ctx, xsql.Query{String: "FLUSH PRIVILEGES"})
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/mysql/grant/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,27 @@ func TestDelete(t *testing.T) {
},
want: nil,
},
"SuccessGrantGone": {
reason: "No error should be returned if the grant is already revoked",
args: args{
mg: &v1alpha1.Grant{
Spec: v1alpha1.GrantSpec{
ForProvider: v1alpha1.GrantParameters{
Database: pointer.StringPtr("test-example"),
User: pointer.StringPtr("test-example"),
},
},
},
},
fields: fields{
db: &mockDB{
MockExec: func(ctx context.Context, q xsql.Query) error {
return &mysql.MySQLError{Number: errCodeNoSuchGrant}
},
},
},
want: nil,
},
}

for name, tc := range cases {
Expand Down

0 comments on commit 031c0bd

Please sign in to comment.