Skip to content

Commit

Permalink
Creates dev microk8s environment using skaffold (#4)
Browse files Browse the repository at this point in the history
* Creates dev microk8s environment using skaffold

* Add app label to service too

* Update readme to use kubectl instead of microk8s.kubectl

* Small improvements
  • Loading branch information
omar-selo authored Apr 14, 2023
1 parent 022ad1f commit 2c429af
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 7 deletions.
95 changes: 95 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Git
.git
.gitignore
.gitattributes


# CI
.codeclimate.yml
.travis.yml
.taskcluster.yml

# Docker
docker-compose.yml
Dockerfile
.docker
.dockerignore

# Byte-compiled / optimized / DLL files
**/__pycache__/
**/*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Virtual environment
.env
.venv/
venv/

# PyCharm
.idea

# Python mode for VIM
.ropeproject
**/.ropeproject

# Vim swap files
**/*.swp

# VS Code
.vscode/

# No need for markdowns like README in container
*.md

# Emacs
flycheck*
17 changes: 17 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.10

EXPOSE 30000

WORKDIR /home/app

COPY poetry.lock .

COPY pyproject.toml .

RUN pip install poetry && \
poetry config virtualenvs.create false && \
poetry install --no-interaction --no-ansi --without dev

COPY . .

CMD [ "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "30000", "--reload" ]
24 changes: 20 additions & 4 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

## Development Setup

This project uses [Poetry](https://python-poetry.org/) for dependency management.
This project supports [microk8s](https://microk8s.io/) development environment with the help of [skaffold](https://skaffold.dev/). It also uses [Poetry](https://python-poetry.org/) for dependency management.

1. [Install](https://python-poetry.org/docs/#installation) the latest version of Poetry
2. Install dependencies with `poetry install` (No need to create a virtual environment, Poetry does it)
3. Run the server with `uvicorn src.main:app --reload`
### 1. Install required tools

- Install [microk8s](https://microk8s.io/docs/getting-started) and setup permissions
- Install [Skaffold](https://skaffold.dev/docs/install/#standalone-binary)
- Install [Poetry](https://python-poetry.org/docs/#installation)

### 2. Setup Skaffold and microk8s

- Skaffold requires a k8s registry to push and pull images from so just run `$ microk8s enable registry`
- Skaffold uses kubectl so create an alias for `microk8s.kubectl` using `snap alias microk8s.kubectl kubectl`
- In order for Skaffold to connect to microk8s it needs it's configuration, so run `$ microk8s config > ~/.kube/config`. Note that if you get an error connecting to the cluster, it could be that the cluster's IP has changed for some reason, so you have to run this command again

### 3. Install python dependencies for linting and completions

While technically not required to run the code, it helps to have dependencies installed on host system to get code completions and linting. To do that just run `$ poetry install`. Note that poetry will create a virtual environment for you, but if you want poetry to create that virtual environment inside this project's directory just run `$ poetry config virtualenvs.in-project true` before installing dependencies.

### 4. Start the development environment

Assuming that your microk8s cluster is running, you can start the development environment by simply running `$ skaffold dev`. This command will build the docker images and push them to your microk8s registry, then apply your k8s manifest to start the cluster and pull those images. Additionally, skaffold will watch for file changes and either sync them directly inside the running containers or rebuild and redeploy k8s cluster for you automatically.

## Dependency Management

Expand Down
37 changes: 37 additions & 0 deletions backend/k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
labels:
app: api
spec:
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: localhost:32000/api
ports:
- containerPort: 30000

---

# Expose port on localhost
apiVersion: v1
kind: Service
metadata:
name: api-service
labels:
app: api
spec:
type: NodePort
selector:
app: api
ports:
- port: 30000
nodePort: 30000
4 changes: 1 addition & 3 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
name = "test-observer"
version = "0.1.0"
description = "Observe the status and state of certification tests for various artefacts"
authors = ["Omar Selo <omar.selo@canonical.com>"]
readme = "README.md"
packages = [{include = "src"}]
authors = []

[tool.poetry.dependencies]
python = "^3.10"
Expand Down
26 changes: 26 additions & 0 deletions backend/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: skaffold/v4beta4
kind: Config
metadata:
name: api
build:
artifacts:
- image: localhost:32000/api
docker:
dockerfile: Dockerfile
# Sync these files to avoid rebuilding images
sync:
manual:
- dest: "src/"
src: "src/**"
strip: "src/"
- dest: "tests/"
src: "tests/**"
strip: "tests/"
# Use microk8s registry
insecureRegistries:
- localhost:32000
manifests:
rawYaml:
- k8s.yaml
deploy:
kubeContext: microk8s

0 comments on commit 2c429af

Please sign in to comment.