|
1 | 1 | # DbUser |
| 2 | + |
| 3 | +**DbUsers** can be used to manage users on a database server. |
| 4 | + |
| 5 | +When you create a `Database`, db-operator will create a main user, that full access to the created database. You should use this user for you workloads. |
| 6 | +If you happen to need more users on your database, you can use `DbUser` Custom Resource |
| 7 | + |
| 8 | +## How to create DbUsers |
| 9 | + |
| 10 | +Let's have a look at the manifest: |
| 11 | +``` |
| 12 | +apiVersion: kinda.rocks/v1beta1 |
| 13 | +kind: DbUser |
| 14 | +metadata: |
| 15 | + name: mysql-readwrite |
| 16 | + namespace: default |
| 17 | +spec: |
| 18 | + secretName: mysql-readwrite-secret |
| 19 | + accessType: readWrite |
| 20 | + databaseRef: mysql-db |
| 21 | +``` |
| 22 | + |
| 23 | +### Secret Name |
| 24 | + |
| 25 | +The `.secretName` we have already seen in the **Databases**, and here it's used almost in the same way. The only difference is that dbuser controller will reuse the **ConfigMap** that was created by the database controller, and will only create a **Secret**. |
| 26 | + |
| 27 | + |
| 28 | +### Access Types |
| 29 | + |
| 30 | +`DbUser` supports two access types: |
| 31 | + |
| 32 | +- readWrite (SELECT, INSERT, UPDATE, DELETE) |
| 33 | +- readOnly (SELECT) |
| 34 | + |
| 35 | +### Database Reference |
| 36 | + |
| 37 | +**DbUsers** are always attached to **Databases**. Once you have a **Database** that is ready, you can create a **DbUser** to grant additional access to it. Another important thing to consider, is that **DbUser** is a namespace resource, and it must live in the same namespace as a **Database** |
| 38 | + |
| 39 | +### Credentials Templates |
| 40 | + |
| 41 | +You can also use [templates](templates.md) to generate connection strings for users, but since it's reusing the database **ConfigMaps**, you are only allowed to write templated values to **Secrets**, so the `.secret` in the template should always be `true` |
| 42 | + |
| 43 | +### Extra Labels and Annotations |
| 44 | + |
| 45 | +With `credentials.metadata.extraLabels` and `credentials.metadata.extraAnnotations` you can add custom metadata to the **Secret** that stores the generated user credentials. These values are merged with the metadata created by the operator. Keys defined in `extraLabels` or `extraAnnotations` overwrite existing values with the same key. |
| 46 | + |
| 47 | +Example: |
| 48 | + |
| 49 | +```yaml |
| 50 | +spec: |
| 51 | + credentials: |
| 52 | + metadata: |
| 53 | + extraLabels: |
| 54 | + environment: development |
| 55 | + extraAnnotations: |
| 56 | + reflector.v1.k8s.emberstack.com/reflection-allowed: "true" |
| 57 | + reflector.v1.k8s.emberstack.com/reflection-auto-enabled: "true" |
| 58 | + reflector.v1.k8s.emberstack.com/reflection-auto-namespaces: target-namespace |
| 59 | +``` |
| 60 | +
|
| 61 | +This metadata can be used by external controllers that watch annotations or require specific labels to enable Secret synchronization or reflection across namespaces. |
| 62 | +
|
| 63 | +## Experimental features |
| 64 | +
|
| 65 | +Experimental features are added via annotations, the following features are available for `DbUsers` |
| 66 | + |
| 67 | +### Grant To Admin on Delete |
| 68 | + |
| 69 | +On instances where the admin is not a super user, it might not be able to drop owned by user, so we need to grant the user to the admin. |
| 70 | + |
| 71 | +But it's not possible on the AWS instances with the `rds_iam` roles, because then admins are not able to log in with a password anymore and the db-operator will not be able to do anything on databases. |
| 72 | + |
| 73 | +That's where this workaround might be used. It will only grant the role to the admin when a user is being removed. |
| 74 | +```yaml |
| 75 | +kinda.rocks/grant-to-admin-on-delete: "true" |
| 76 | +``` |
| 77 | + |
| 78 | +### Allow Existing User |
| 79 | + |
| 80 | +By default the operator is trying to make sure, that the user that it's trying to maintain is also created by it. Because otherwise it would be possible to make operator believe that a **Database** user that is created by the database controller should become a **readOnly** one. |
| 81 | + |
| 82 | +So if a user already exists on a server, when a **CR** is created, operator will not try to manage it. |
| 83 | + |
| 84 | +If you need the operator to be able to managed existing users, you need to set the following annotation: |
| 85 | + |
| 86 | +```yaml |
| 87 | +kinda.rocks/allow-existing-user: "true" |
| 88 | +``` |
0 commit comments