diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5ec097a..3aea78d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: - id: end-of-file-fixer - id: check-yaml exclude: - ^(charts/speech-collect/templates|\.drone\.yml)$ + ^(charts/speech-collect/templates|\.drone\.yml) - id: check-added-large-files - id: debug-statements - repo: https://github.com/shellcheck-py/shellcheck-py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..21d4220 --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +PYTHON ?= python3.11 +KIND_KUBECONFIG ?= $(HOME)/.kube/config-kind.yaml + +# Checks system dependencies needed to run the local dev environment +check-system-deps: + @echo "Checking system dependencies..." + @command -v $(PYTHON) > /dev/null || (echo "Python 3.11 is not installed."; exit 1) + @command -v kubectl > /dev/null || (echo "kubectl is not installed."; exit 1) + @command -v docker > /dev/null || (echo "Docker is not installed."; exit 1) + @command -v kind > /dev/null || (echo "Kind is not installed."; exit 1) + @command -v ctlptl version > /dev/null || (echo "ctlptl is not installed."; exit 1) + @command -v tilt > /dev/null || (echo "Tilt is not installed."; exit 1) + @command -v helm > /dev/null || (echo "Helm is not installed."; exit 1) + @echo "All required dependencies are installed." + +start-local: + export KUBECONFIG=$(KIND_KUBECONFIG) ;\ + ctlptl apply -f kind.yaml ;\ + tilt up --context kind-kind-local + +stop-local: + export KUBECONFIG=$(KIND_KUBECONFIG) ;\ + tilt down --context kind-kind-local diff --git a/README.md b/README.md index 51c444d..a81db3d 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,30 @@ Run `pip-sync requirements.txt dev_requirements.txt`. Run the tests by invoking `py.test` in the project root. Make sure you run any pending migrations beforehand. + + +## Local Development Environment + +This project makes use of [kind](https://kind.sigs.k8s.io/) and [Tilt](https://tilt.dev/) as a local development environment. To ensure you have the system dependencies installed run: + +```shell +check-system-deps +``` + +Missing dependencies can be installed from their official sources (alternatively you can use [homebrew](https://brew.sh) for most): +- [kubctl](https://kubernetes.io/docs/tasks/tools/#kubectl) | `brew install kubectl` +- [helm docs](https://helm.sh/docs/intro/install/) | `brew install helm` +- Docker running as non-root + - [Docker Install](https://docs.docker.com/engine/install/ubuntu/) + - [Docker Post Install Steps](https://docs.docker.com/engine/install/linux-postinstall/) +- [kind installation](https://kind.sigs.k8s.io) | `brew install kind` +- [ctlptl](https://github.com/tilt-dev/ctlptl) | `brew install tilt-dev/tap/ctlptl` +- [python3.11 deadsnakes](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa) | `brew install python3.11` +- [tilt bash download ](https://docs.tilt.dev/) | `brew install tilt` + + +To start the local development environment and navigate to the : + +```shell +make start-local +``` diff --git a/Tiltfile b/Tiltfile new file mode 100644 index 0000000..ee9e09a --- /dev/null +++ b/Tiltfile @@ -0,0 +1,89 @@ +print(""" +----------------------------------------------------------------- +✨ Speech Collect Tiltfile! ✨ +----------------------------------------------------------------- +""".strip()) + +# User must manually trigger update for dirty(changed) resources in the UI. +trigger_mode(TRIGGER_MODE_MANUAL) + +# ------------- Variables ----------- +kube_namespace = 'speech-collect-local' + +db_name = "tilt-local-db" +db_user = "postgres" +db_pass = "passwd" +db_service = "pg-sql-service" +db_tag = "15" +secret_name = 'speech-collect-secret' +db_secret_key = 'PGSQL_PASSWORD' + +# ------------- Docker Image Build ----------- + +docker_build( + 'registry.runcible.io/speech-collect', + './', + build_args={"REPO_NAME": "registry.runcible.io/speech-collect", "COMMIT_SHA": "local"} +) + +# ------------- Prepare Namespace ----------- + +# load executes another Tiltfile, and imports the named variables into current scope +load('ext://namespace', 'namespace_create', 'namespace_inject') +namespace_create(kube_namespace) +k8s_resource(objects=[kube_namespace], new_name=kube_namespace+"-ns", labels='speech-collect') + +load('ext://helm_remote', 'helm_remote') +load('ext://cert_manager', 'deploy_cert_manager') +deploy_cert_manager() + + +# ------------- Secrets ----------- +print('Creating secrets') +load('ext://secret', 'secret_from_dict') +k8s_yaml(secret_from_dict(secret_name, + inputs={db_secret_key : db_pass}, + namespace=kube_namespace, +)) +# +k8s_resource(objects=[secret_name], new_name=secret_name, labels="speech-collect") + + +# ------------- Postgres ----------- + +print('Installing Postgres') +helm_remote( + 'postgresql', + namespace=kube_namespace, + repo_url='https://charts.bitnami.com/bitnami', + set=[ + "global.postgresql.auth.postgresPassword=", + "global.postgresql.auth.database=" + db_name, + "global.postgresql.auth.username=" + db_user, + "global.postgresql.auth.password=" + db_pass, + "image.tag=" + db_tag, + "fullnameOverride=" + db_service, + "nameOverride=" + db_service, + ] +) +# Modifies the resource (our postgres in this case) by portforwarding to 5432 and applying a label +k8s_resource(workload=db_service, port_forwards=["5432"], labels="local-external-platform") +# Renames the secret created by the above helm chart to pg-secret in the Tilt UI +k8s_resource(objects=[db_service+":secret"], new_name="pg-secret", labels="local-external-platform") + +# HERE IS YOUR LOCAL SERVICE +load('ext://helm_remote', 'helm_remote') + +# ------------ Local Chart ------------ +k8s_yaml( + helm( + './charts/speech-collect', + namespace=kube_namespace, + set=[ + ] + ) +) + +k8s_resource('speech-collect-api', resource_deps=[], labels="speech-collect") +k8s_resource(objects=['speech-collect-sa:serviceaccount'], new_name="speech-collect-sa", labels='speech-collect') +k8s_resource(objects=['speech-collect-configmap:configmap'], new_name="speech-collect-configmap", labels='speech-collect') diff --git a/charts/speech-collect/templates/configmap.yaml b/charts/speech-collect/templates/configmap.yaml index 3f7875b..9282d07 100644 --- a/charts/speech-collect/templates/configmap.yaml +++ b/charts/speech-collect/templates/configmap.yaml @@ -1,7 +1,7 @@ apiVersion: v1 kind: ConfigMap metadata: - name: {{ $.Release.Name }}-{{ $.Chart.Name }}-configmap + name: {{ .Chart.Name }}-configmap data: EXAMPLE_VALUE: "static" {{- range $key, $val := .Values.configEnvVars }} diff --git a/kind.yaml b/kind.yaml new file mode 100644 index 0000000..3e02a02 --- /dev/null +++ b/kind.yaml @@ -0,0 +1,10 @@ +apiVersion: ctlptl.dev/v1alpha1 +kind: Cluster +product: kind +registry: ctlptl-registry +kindV1Alpha4Cluster: + name: kind-local + nodes: + - role: control-plane + - role: worker + - role: worker