Skip to content

Commit

Permalink
fix: refactor and fix mysql-schema sample and its instructions (#2681)
Browse files Browse the repository at this point in the history
Signed-off-by: xstefank <[email protected]>
  • Loading branch information
xstefank authored Feb 5, 2025
1 parent 03eb78d commit d6a81ab
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
45 changes: 36 additions & 9 deletions sample-operators/mysql-schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use it as is with real databases.
### Try

To try how the operator works you will need the following:
* JDK installed (minimum version 11, tested with 11 and 15)
* JDK installed (minimum version 11, tested with 11, 15, and 23)
* Maven installed (tested with 3.6.3)
* A working Kubernetes cluster (tested with v1.15.9-gke.24)
* A working Kubernetes cluster (tested with v1.15.9-gke.24 and minikube v1.35.0)
* kubectl installed (tested with v1.15.5)
* Docker installed (tested with 19.03.8)
* Container image registry
Expand Down Expand Up @@ -59,18 +59,45 @@ you want to use, you can skip this step, but you will have to configure the oper
`kubectl apply -f k8s/mysql-db.yaml`
1. Deploy the CRD:

`kubectl apply -f k8s/crd.yaml`
`kubectl apply -f target/classes/META-INF/fabric8/mysqlschemas.mysql.sample.javaoperatorsdk-v1.yml`

1. Make a copy of `k8s/operator.yaml` and replace ${DOCKER_REGISTRY} and ${OPERATOR_VERSION} to the
right values. You will want to set `OPERATOR_VERSION` to the one used for building the Docker image. `DOCKER_REGISTRY` should
be the same as you set the docker-registry property in your `pom.xml`.
1. Make a copy of `k8s/operator.yaml` and replace `spec.template.spec.containers[0].image` (`$ yq 'select(di == 1).spec.template.spec.containers[0].image' k8s/operator.yaml`) with the operator image that you pushed to your registry. This should be the same as you set the docker-registry
property in your `pom.xml`.
If you look at the environment variables you will notice this is where the access to the MySQL server is configured.
The default values assume the server is running in another Kubernetes namespace (called `mysql`), uses the `root` user
with a not very secure password. In case you want to use a different MySQL server, this is where you configure it.

1. Run `kubectl apply -f copy-of-operator.yaml` to deploy the operator. You can wait for the deployment to succeed using
this command: `kubectl rollout status deployment mysql-schema-operator -w`. `-w` will cause kubectl to continuously monitor
the deployment until you stop it.
this command: `kubectl rollout status deployment -n mysql-schema-operator mysql-schema-operator -w`. `-w` will cause kubectl to continuously monitor the deployment until you stop it.

1. Now you are ready to create some databases! To create a database schema called `mydb` just apply the `k8s/schema.yaml`
file with kubectl: `kubectl apply -f k8s/schema.yaml`. You can modify the database name in the file to create more schemas.
file with kubectl: `kubectl apply -f k8s/schema.yaml`. You can modify the database name in the file to create more schemas. To verify, that the schema is installed you need to expose your `LoadBalancer` `mysql` service and you can use the `mysql`
CLI to run `show schemas;` command. For instance, with minikube, this can be done like this:

```
$ minikube service mysql -n mysql --url
http://192.168.49.2:30317

$ mysql -h 192.168.49.2 -P 30317 --protocol=tcp -u root -ppassword
...

MariaDB [(none)]> show schemas;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.000 sec)
```
Or you can verify it directly with `kubectl` like this:
```
$ kubectl get mysqlschemas
NAME AGE
mydb 102s
```
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
apiVersion: v1
kind: Namespace
metadata:
name: mysql
labels:
name: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -23,4 +30,16 @@ spec:
value: password
ports:
- containerPort: 3306
name: mysql
name: mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
type: LoadBalancer
11 changes: 0 additions & 11 deletions sample-operators/mysql-schema/k8s/mysql-service.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion sample-operators/mysql-schema/k8s/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
serviceAccountName: mysql-schema-operator # specify the ServiceAccount under which's RBAC persmissions the operator will be executed under
containers:
- name: operator
image: mysql-schema-operator
image: mysql-schema-operator # TODO Change this to point to your pushed mysql-schema-operator image
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class MySQLSchemaOperatorE2E {
infrastructure.add(
new NamespaceBuilder().withNewMetadata().withName(MY_SQL_NS).endMetadata().build());
try {
infrastructure.addAll(client.load(new FileInputStream("k8s/mysql-deployment.yaml")).items());
infrastructure.addAll(client.load(new FileInputStream("k8s/mysql-service.yaml")).items());
infrastructure.addAll(client.load(new FileInputStream("k8s/mysql-db.yaml")).items());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit d6a81ab

Please sign in to comment.