Skip to content

mariadb in productcatalogservice #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,7 @@ minikube start --cpus=4 --memory 4096 --disk-size 32g

4. Run `kubectl get pods` to verify the Pods are ready and running.

5. Access the web frontend through your browser

- **Minikube** requires you to run a command to access the frontend service:
```shell
minikube service frontend-external
```

- **Docker For Desktop** should automatically provide the frontend at
http://localhost:80

5. Access the web frontend via port forwarding.

### Cleanup

Expand Down
7 changes: 7 additions & 0 deletions helm-manifests/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -e

pushd .
cd "$(dirname "$0")"
helm install mariadb bitnami/mariadb --version=11.5.0 -f ./values.yaml
popd
35 changes: 35 additions & 0 deletions helm-manifests/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
initdbScripts:
backup.sql: |
CREATE TABLE `products` (
`id` VARCHAR(1024),
`name` VARCHAR(1024),
`description` VARCHAR(1024),
`picture` VARCHAR(1024),
`price_usd` JSON,
`categories` JSON
);

INSERT INTO `products` VALUES
('16BEE20109','Honeybee Plush','Adorable plush toy that wants to be snuggled like any other fuzzy.','/static/img/products/bee-plush.jpg','{ "currency_code": "USD", "units": 30, "nanos": 780000000 }','["toy"]'),
('999SLO4D20','20 sided die','The ultimate observability quest tool.','/static/img/products/d20.jpg','{ "currency_code": "USD", "units": 15, "nanos": 200000000 }','["game"]'),
('OLJCESPC7Z','Vintage Typewriter','This typewriter looks good in your living room.','/static/img/products/typewriter.jpg','{ "currency_code": "USD", "units": 67, "nanos": 990000000 }','["vintage"]'),
('66VCHSJNUP','Vintage Camera Lens','You won''t have a camera to use it and it probably doesn''t work anyway.','/static/img/products/camera-lens.jpg','{ "currency_code": "USD", "units": 12, "nanos": 490000000 }','["photography","vintage"]'),
('1YMWWN1N4O','Home Barista Kit','Always wanted to brew coffee with Chemex and Aeropress at home?','/static/img/products/barista-kit.jpg','{ "currency_code": "USD", "units": 124, "nanos": 0 }','["cookware"]'),
('DG9ZAG9RCG','Toshok has a Branch','An original ''Toshok has a Branch for that'' t-shirt.','/static/img/products/toshok-branch.jpg','{ "currency_code": "USD", "units": 34, "nanos": 980000000 }','["game"]'),
('L9ECAV7KIM','Terrarium','This terrarium will looks great in your white painted living room.','/static/img/products/terrarium.jpg','{ "currency_code": "USD", "units": 36, "nanos": 450000000 }','["gardening"]'),
('2ZYFJ3GM2N','Film Camera','This camera looks like it''s a film camera, but it''s actually digital.','/static/img/products/film-camera.jpg','{ "currency_code": "USD", "units": 2245, "nanos": 0 }','["photography","vintage"]'),
('0PUK6V6EV0','Vintage Record Player','It still works.','/static/img/products/record-player.jpg','{ "currency_code": "USD", "units": 65, "nanos": 500000000 }','["music","vintage"]'),
('LS4PSXUNUM','Metal Camping Mug','You probably don''t go camping that often but this is better than plastic cups.','/static/img/products/camp-mug.jpg','{ "currency_code": "USD", "units": 24, "nanos": 330000000 }','["cookware"]'),
('9SIQT8TOJO','City Bike','This single gear bike probably cannot climb the hills of San Francisco.','/static/img/products/city-bike.jpg','{ "currency_code": "USD", "units": 789, "nanos": 500000000 }','["cycling"]'),
('6E92ZMYYFZ','Air Plant','Have you ever wondered whether air plants need water? Buy one and figure out.','/static/img/products/air-plant.jpg','{ "currency_code": "USD", "units": 12, "nanos": 300000000 }','["gardening"]');

primary:
persistence:
enabled: false

serviceAccount:
create: false

auth:
database: productcatalogservice
rootPassword: "root"
2 changes: 1 addition & 1 deletion kubernetes-manifests/adservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/adservice:latest
image: adservice:latest
ports:
- containerPort: 9555
env:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/cartservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/cartservice:latest
image: cartservice:latest
ports:
- containerPort: 7070
env:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/checkoutservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
serviceAccountName: default
containers:
- name: server
image: signadot/checkoutservice:latest
image: checkoutservice:latest
ports:
- containerPort: 5050
# readinessProbe:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/currencyservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/currencyservice:latest
image: currencyservice:latest
ports:
- name: grpc
containerPort: 7000
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/emailservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/emailservice:latest
image: emailservice:latest
ports:
- containerPort: 8080
env:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
serviceAccountName: default
containers:
- name: server
image: signadot/frontend:latest
image: frontend:latest
ports:
- containerPort: 8080
# readinessProbe:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/loadgenerator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
restartPolicy: Always
containers:
- name: main
image: signadot/loadgenerator:latest
image: loadgenerator:latest
env:
- name: FRONTEND_ADDR
value: http://frontend
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/paymentservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/paymentservice:latest
image: paymentservice:latest
ports:
- containerPort: 50051
env:
Expand Down
6 changes: 5 additions & 1 deletion kubernetes-manifests/productcatalogservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/productcatalogservice:latest
image: productcatalogservice:latest
ports:
- containerPort: 3550
env:
- name: DB_HOST
value: "mariadb.shop.svc"
- name: DB_PORT
value: "3306"
- name: PORT
value: "3550"
- name: OTEL_EXPORTER_OTLP_ENDPOINT
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/recommendationservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: signadot/recommendationservice:latest
image: recommendationservice:latest
ports:
- containerPort: 8080
# readinessProbe:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-manifests/shippingservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
serviceAccountName: default
containers:
- name: server
image: signadot/shippingservice:latest
image: shippingservice:latest
ports:
- containerPort: 50051
env:
Expand Down
66 changes: 66 additions & 0 deletions plugins/productcatalog-mariadb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# ProductCatalog MariaDB Plugin

This is a resource plugin that provisions a temporary mariadb server for use within a sandbox associated
with the Product Catalog service.

## Installing the Plugin

Before installing the plugin, create the required service account and RBAC permissions:

```sh
kubectl -n signadot create -f ./k8s/mariadb-init.yaml
```

Using the `signadot` CLI, register the plugin in Signadot Control Plane:

```sh
signadot resourceplugin apply -f ./plugin.yaml
```

## Using the Plugin

When creating a Signadot Sandbox, you can request a temporary MariaDB instance
with a specified database name from this plugin by specifying the plugin name
`mariadb` and passing the following parameters.

Parameter | Description | Example
--------- | ----------- | -------
`dbname` | The name of the empty database to create | `testdb`

After the resource is provisioned, the following output keys will be available
for use by forked workloads in the sandbox:

Output Key | Description | Example
---------- | ----------- | -------
`provision.host` | The hostname of the database | `testdb-k5ncuujcjllj2.my-namespace.svc`
`provision.port` | The port of the database | `3306`
`provision.root-password` | The password for mariadb root access | `xxj87hd`

[`example-sandbox.yaml`](./example-sandbox.yaml) is an example of a sandbox that uses this plugin.
To run it, you will need to install the [`example-baseline`](./../example-baseline/) application
in your cluster, and use `signadot` CLI to create the sandbox (replacing `<cluster-name>` with your
cluster name, and `<example-baseline-namespace>` with the namespace where `example-baseline` was deployed):

```sh
signadot sandbox apply -f ./example-sandbox.yaml --set cluster=<cluster-name> --set namespace=<example-baseline-namespace>
```

Now, in the [Signadot Dashboard](https://app.signadot.com/sandboxes), you can follow the status of your sandbox,
and once ready, you will be able to access the preview endpoint, where you will see the added env vars:
`DB_HOST`, `DB_PORT` and `DB_ROOT_PASSWORD`.


## Removing the Plugin

Make sure all sandboxes that used the chart are deleted, so that the plugin gets
a chance to deprovision anything that was provisioned, and then use `signadot` CLI to uninstall the plugin:

```sh
signadot resourceplugin delete -f ./plugin.yaml
```

Finally delete the service account and RBAC permissions:

```sh
kubectl -n signadot delete -f ./k8s/mariadb-init.yaml
```
35 changes: 35 additions & 0 deletions plugins/productcatalog-mariadb/k8s/mariadb-init.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: signadot
name: sd-productcatalog-mariadb
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: signadot
name: sd-productcatalog-mariadb
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["configmaps", "secrets", "services"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: ["apps"]
resources: ["statefulsets"]
verbs: ["get", "list", "watch", "create", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: signadot
name: sd-productcatalog-mariadb
subjects:
- kind: ServiceAccount
namespace: signadot
name: sd-productcatalog-mariadb
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: sd-productcatalog-mariadb
34 changes: 34 additions & 0 deletions plugins/productcatalog-mariadb/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: productcatalog-mariadb
spec:
description: |
Provision a MariaDB instance for productcatalog
Sandbox should provide input 'values' for the values.yaml.

runner:
image: dtzar/helm-kubectl
namespace: signadot
podTemplateOverlay: |
spec:
serviceAccountName: sd-productcatalog-mariadb

create:
- name: provision
inputs:
- name: values
valueFromSandbox: true
as:
env: VALUES

outputs:
- name: host
valueFromPath: /tmp/host
- name: port
valueFromPath: /tmp/port
- name: root-password
valueFromPath: /tmp/root-password

script: "@{embed: ./plugin/provision.sh}"

delete:
- name: deprovision
script: "@{embed: ./plugin/deprovision.sh}"
12 changes: 12 additions & 0 deletions plugins/productcatalog-mariadb/plugin/deprovision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# exit when any command fails
set -e

echo "Sandbox id: ${SIGNADOT_SANDBOX_ID}"
echo "Resource name: ${SIGNADOT_RESOURCE_NAME}"

# Undeploy the temporary DB for this Sandbox.
export NAMESPACE=signadot
RELEASE_NAME="signadot-${SIGNADOT_RESOURCE_NAME,,}-${SIGNADOT_SANDBOX_ID}"
echo "Deleting Helm release: ${RELEASE_NAME}"
helm -n ${NAMESPACE} uninstall "${RELEASE_NAME}" --wait --timeout 5m0s
35 changes: 35 additions & 0 deletions plugins/productcatalog-mariadb/plugin/provision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# exit when any command fails
set -e

echo "Provisioning a MariaDB server"
echo "Sandbox id: ${SIGNADOT_SANDBOX_ID}"
echo "Resource name: ${SIGNADOT_RESOURCE_NAME}"
echo "values: ${VALUES}"

# Install bitnami helm chart repo
helm repo add bitnami https://charts.bitnami.com/bitnami

# values.yaml
echo "${VALUES}" > ./values.json
yq -P '.' ./values.json --output-format=yaml > ./values.yaml
cat ./values.yaml

# Deploy a temporary DB for this Sandbox
export NAMESPACE=signadot
RELEASE_NAME="signadot-${SIGNADOT_RESOURCE_NAME,,}-${SIGNADOT_SANDBOX_ID}"
echo "Installing Helm release: ${RELEASE_NAME}"
helm -n ${NAMESPACE} install "${RELEASE_NAME}" bitnami/mariadb \
--version 11.5.0 --wait --timeout 5m0s \
--set fullnameOverride="${RELEASE_NAME}-mariadb" \
-f ./values.yaml

# Get the generated password, based on instructions from the Helm chart.
MYSQL_ROOT_PASSWORD=$(kubectl -n ${NAMESPACE} get secret "${RELEASE_NAME}-mariadb" -o jsonpath="{.data.mariadb-root-password}" | base64 -d)

# Populate the outputs
DBHOST="${RELEASE_NAME}-mariadb.${NAMESPACE}.svc"

echo -n "${DBHOST}" > /tmp/host
echo -n 3306 > /tmp/port
echo -n "${MYSQL_ROOT_PASSWORD}" > /tmp/root-password
Loading