This blog post will guide you through the process of setting up a development environment for Bucketeer using a dev container. A dev container provides a consistent and isolated environment, ensuring that all developers have the necessary tools and dependencies to build and run the project.
Why Use a Dev Container?
Dev containers offer several advantages, including:
- Consistency: Everyone on the team works in the same environment, reducing "it works on my machine" issues.
- Isolation: Project dependencies are isolated from your local system, avoiding conflicts.
- Ease of Setup: The environment can be quickly set up, saving time and effort.
Setting up the Dev Container
There are two main ways to set up the development environment using a dev container:
- Using GitHub Codespaces: This is the easiest method, where the dev container runs directly in the cloud.
- Building Locally with VS Code: You can also build the dev container locally using the VS Code Dev Containers extension.
Option 1: Using GitHub Codespaces
This is the quickest way to start developing.
- Navigate to the Bucketeer project on GitHub.
- Click the Code button and select Codespaces.
- Click Create codespace on main.
- Wait for the dev container to be ready.
Option 2: Building Locally with VS Code
This option requires that you have VS Code and the Dev Containers extension installed.
- Clone the Bucketeer repository to your local machine.
- Open the project folder in VS Code.
- VS Code should prompt you to open the project in a dev container or you can use the command palette and select "Reopen in Container".
- The dev container is defined by the devcontainer.json file located in the .devcontainer directory.
- The file specifies the base image, customizations such as VS Code settings and extensions, host requirements such as CPUs, memory, and storage, and commands to run after the container is created.
Setting up Minikube and Services
Once your dev container is up and running, you need to set up Minikube and the services that Bucketeer depends on.
- Use the command
make start-minikube
to set up Minikube and the required services. This may take some time because it set up MySQL, Redis, Google Pub/Sub Emulator, and Google Big Query Emulator. - Important: Always use
make start-minikube
to start the Minikube cluster, notminikube start
directly. - This command adds two hosts to
/etc/hosts
which point to the minikube IP address for API Gateway and Web Gateway services. It also creates tables for MySQL and Google Big Query Emulator.
Deploying Bucketeer
To deploy Bucketeer, use the following command:
make deploy-bucketeer
This command will deploy all the Bucketeer services at once. You can deploy single services by using helm install commands. Now you can see all pods are ready to go!
kubectl get pod
NAME READY STATUS RESTARTS AGE
api-765bc5b867-58hfg 2/2 Running 0 2m14s
batch-server-7b48d58d57-vpzgp 3/3 Running 0 2m15s
batch-server-api-key-cacher-28908288-kllwh 0/1 Completed 0 33s
batch-server-auto-ops-rules-cacher-28908288-289r8 0/1 Completed 0 24s
batch-server-experiment-cacher-28908288-z2qm9 0/1 Completed 0 26s
batch-server-experiment-calculator-28908288-dzghs 0/1 Completed 0 35s
batch-server-experiment-status-updater-28908288-9lgvd 0/1 Completed 0 31s
batch-server-feature-flag-cacher-28908288-kcdcl 0/1 Completed 0 22s
batch-server-ops-datetime-watcher-28908288-rt2jf 0/1 Completed 0 42s
batch-server-ops-event-count-watcher-28908288-vmrpl 0/1 Completed 0 29s
batch-server-ops-progressive-rollout-watcher-28908288-zczfr 0/1 Completed 0 39s
batch-server-segment-user-cacher-28908288-7nrk5 0/1 Completed 0 37s
bucketeer-migrate-ctpl6 0/1 Completed 0 2m22s
localenv-bq-76d9485d5b-s8kkb 1/1 Running 0 10m
localenv-mysql-0 1/1 Running 0 10m
localenv-pubsub-5f4589c4b9-sc8vt 1/1 Running 0 10m
localenv-redis-master-0 1/1 Running 0 10m
localenv-vault-0 1/1 Running 0 10m
localenv-vault-agent-injector-7dd8f7998-rdntt 1/1 Running 0 10m
subscriber-5cc47d5559-8b848 2/2 Running 0 2m15s
web-6c9d9cc5ff-ltzr9 2/2 Running 0 2m15s
Running E2E Tests
Before running end-to-end tests, you must create API keys for the Server and Client SDKs, which only need to be created once.
- To create API Keys, use the following commands, updating names as needed:
WEB_GATEWAY_URL=web-gateway.bucketeer.io \
WEB_GATEWAY_CERT_PATH=/workspaces/bucketeer/tools/dev/cert/tls.crt \
SERVICE_TOKEN_PATH=/workspaces/bucketeer/tools/dev/cert/service-token \
API_KEY_NAME="e2e-test-$(date +%s)-client" \
API_KEY_PATH=/workspaces/bucketeer/tools/dev/cert/api_key_client \
API_KEY_ROLE=SDK_CLIENT \
ENVIRONMENT_NAMESPACE=e2e \
make create-api-key
WEB_GATEWAY_URL=web-gateway.bucketeer.io \
WEB_GATEWAY_CERT_PATH=/workspaces/bucketeer/tools/dev/cert/tls.crt \
SERVICE_TOKEN_PATH=/workspaces/bucketeer/tools/dev/cert/service-token \
API_KEY_NAME="e2e-test-$(date +%s)-server" \
API_KEY_PATH=/workspaces/bucketeer/tools/dev/cert/api_key_server \
API_KEY_ROLE=SDK_SERVER \
ENVIRONMENT_NAMESPACE=e2e \
make create-api-key
- To run the E2E tests, use the following command:
WEB_GATEWAY_URL=web-gateway.bucketeer.io \
GATEWAY_URL=api-gateway.bucketeer.io \
WEB_GATEWAY_CERT_PATH=/workspaces/bucketeer/tools/dev/cert/tls.crt \
GATEWAY_CERT_PATH=/workspaces/bucketeer/tools/dev/cert/tls.crt \
SERVICE_TOKEN_PATH=/workspaces/bucketeer/tools/dev/cert/service-token \
API_KEY_PATH=/workspaces/bucketeer/tools/dev/cert/api_key_client \
API_KEY_SERVER_PATH=/workspaces/bucketeer/tools/dev/cert/api_key_server \
ENVIRONMENT_NAMESPACE=e2e \
make e2e
Database Schema Migrations
Bucketeer uses the Atlas tool for database schema migrations. The migrations run automatically when you install the Bucketeer Helm application, and if any migration fails, the application will not install.
- To create migration files, you must port-forward the localenv-mysql service with
kubectl port-forward svc/localenv-mysql 3306:3306
. - Then, use the command
make create-migration NAME=<MIGRATION_FILE_NAME> HOST=localhost USER=bucketeer PASS=bucketeer PORT=3306 DB=bucketeer
. - Migration file names should have specific prefixes such as:
- create: For new tables (e.g.,
create_users_table
). - update: For altering existing tables (e.g.,
update_users_table
). - drop: For dropping tables (e.g.,
drop_users_table
).
- create: For new tables (e.g.,
- If no structure changes were executed, you can create a migration file manually.
Customize your own dev container
You can customize your dev container. For instance;
- Run SSH agent in your host machine to authenticate commands in dev container.
- Download your dotfiles when the dev container starts to run.
- Sync VSCode settings in Codespace.
Please check the following links to do those things.
- https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials
- https://code.visualstudio.com/docs/devcontainers/containers#_personalizing-with-dotfile-repositories
- https://docs.github.com/ja/codespaces/setting-your-user-preferences/personalizing-github-codespaces-for-your-account
Conclusion
Developing Bucketeer inside a dev container provides a streamlined and consistent development experience. By following these steps, you can quickly set up your environment, build and deploy Bucketeer, and run tests effectively. This approach not only ensures a smooth development process but also helps in maintaining a reliable and efficient workflow. Let's develop Bucketeer together!