Development guide#
This page provides procedures and guidelines for developing and contributing to Gafaelfawr.
Scope of contributions#
Gafaelfawr is an open source package, meaning that you can contribute to Gafaelfawr itself, or fork Gafaelfawr for your own purposes.
Since Gafaelfawr is intended for internal use by Rubin Observatory, community contributions can only be accepted if they align with Rubin Observatory’s aims. For that reason, it’s a good idea to propose changes with a new GitHub issue before investing time in making a pull request.
Gafaelfawr is developed by the LSST SQuaRE team.
Setting up a local development environment#
To develop Gafaelfawr, create a virtual environment with your method of choice (like virtualenvwrapper) and then clone or fork, and install:
git clone https://github.com/lsst-sqre/gafaelfawr.git
cd gafaelfawr
make init
This init step does three things:
Installs Gafaelfawr in an editable mode with its “dev” extra that includes test and documentation dependencies.
Installs pre-commit, tox, and tox-docker.
Installs the pre-commit hooks.
On macOS hosts, you may also need to run the following in the terminal window where you run make init and where you intend to run tox commands:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
Otherwise, OpenSSL isn’t on the default linker path and some Python extensions may not build.
Pre-commit hooks#
The pre-commit hooks, which are automatically installed by running the make init command on set up, ensure that files are valid and properly formatted. Some pre-commit hooks automatically reformat code:
isortAutomatically sorts imports in Python modules.
blackAutomatically formats Python code.
blacken-docsAutomatically formats Python code in reStructuredText documentation and docstrings.
When these hooks fail, your Git commit will be aborted. To proceed, stage the new modifications and proceed with your Git commit.
Building the UI#
Before running tests or starting a local development server, you must build the UI.
The Gafaelfawr UI is written in JavaScript and contained in the ui subdirectory.
To build it, run (from the top level):
make ui
You will need to have Node.js and npm installed.
The easiest way to do this is generally to use nvm.
Gafaelfawr provides an .nvmrc file that sets the version of Node.js to what is currently used to build the UI in GitHub Actions for the official Docker image.
Running tests#
To test all components of Gafaelfawr other than the Kubernetes operator (see below), run tox, which tests the library the same way that the CI workflow does:
tox
This uses tox-docker to start PostgreSQL and Redis Docker containers for the tess to use, so Docker must be installed and the user running tox must have permission to create Docker containers.
To run the Selenium tests, you will need to have chromedriver installed.
On Debian and Ubuntu systems, you can install this with apt install chromium-driver.
To run the tests with coverage analysis and generate a report, run:
tox -e py-coverage,coverage-report
To see a listing of test environments, run:
tox -av
To run a specific test or list of tests, you can add test file names (and any other pytest options) after -- when executing the py or py-full tox environment.
For example:
tox -e py -- tests/handlers/api_tokens_test.py
Testing the Kubernetes operator#
To test the Kubernetes operator, you must have a Kubernetes cluster available that is not already running Gafaelfawr. This is only tested with Minikube, which is the approach used by CI.
Warning
The default Kubernetes credentials in your local Kubernetes configuration will be used to run the tests, whatever cluster that points to.
In theory, you can use a regular Kubernetes cluster and only test namespaces starting with test- will be affected.
In practice, this is not tested, and it is possible the tests will damage or destroy other applications or data running on the same Kubernetes cluster.
If you want to run these tests manually rather than via CI, using Minikube for tests and carefully verifying that the default Kubernetes credentials are for the Minikube environment is strongly encouraged.
To set up Minikube:
Install Minikube for your platform.
Start a cluster using the Docker driver with the minimum recommended resources:
minikube start --driver=docker --cpus=4 --memory=8g --disk-size=100g --kubernetes-version=1.21.5
The
--kubernetes-versionoption can be used to specify the Kubernetes version to use.Enable the NGINX Ingress Controller using the Minikube ingress addon:
minikube addons enable ingress
To run all of the tests including Kubernetes tests, first check that your default Kubernetes environment is the one in which you want to run tests:
kubectl config current-context
Then, run:
tox -e py-full
Add the coverage-report environment to also get a test coverage report.
Starting a development server#
There are two methods to run Gafaelfawr interactively on your local machine for development and testing the UI: outside Docker or inside Docker. In both cases, you will need Docker to be installed on your local machine.
For either approach, you will first need to create a GitHub OAuth app for Gafaelfawr to use. On GitHub, go to your personal settings page, select developer settings, and then select OAuth Apps. Create a new OAuth App with the following settings:
Homepage:
http://localhost:8080/Authorization callback URL:
http://localhost:8080/login
The rest can be set to whatever you want.
Replace <github-client-id> in examples/docker/gafaelfawr.yaml and examples/gafaelfawr-dev.yaml with the resulting client ID.
Put the resulting secret in examples/secrets/github-client-secret.
Now, use one of the two methods below for running Gafaelfawr.
Outside Docker#
Run:
tox -e run
This will use docker-compose to start Redis and PostgreSQL servers, and then will start Gafaelfawr in the foreground outside of Docker.
You can now go to http://localhost:8080/auth/tokens and will be redirected to GitHub for authentication.
To stop the running server, use Ctrl-C. You will then need to run:
docker-compose down
to stop the Redis and PostgreSQL containers.
The advantage of this method is that the running code and UI will be taken from your current working directory, so you can update it on the fly and immediately see the effects.
Inside Docker#
Build a Docker image and start the development instance of Gafaelfawr with:
docker-compose -f examples/docker/docker-compose.yaml --project-directory . build
docker-compose -f examples/docker/docker-compose.yaml --project-directory . up
You can then go to http://localhost:8080/auth/tokens and will be redirected to GitHub for authentication.
To stop the running server, use Ctrl -C. To fully clean up the services, then run:
docker-compose -f examples/docker/docker-compose.yaml --project-directory . down
This way of running Gafaelfawr doesn’t require you to have its dependencies installed locally and more closely simulates a production deployment. However, you will need to stop Gafaelfawr, rebuild the Docker container, and then start it again after each change to see your changes reflected.
Building documentation#
Documentation is built with Sphinx:
tox -e docs
The build documentation is located in the docs/_build/html directory.
Updating the change log#
Each pull request should update the change log (CHANGELOG.md).
Add a description of new features and fixes as list items under a section at the top of the change log, using unreleased for the date portion.
The version number for that heading should be chosen or updated based on the semver rules.
## X.Y.Z (unreleased)
### Subheading (see below)
- Description of the feature or fix.
All changelog entries should be divided into sections (each starting with ###) chosen from the following:
Backward-incompatible changes (which should also come with a bump in the major version except in unusual cases)
New features
Bug fixes
Other changes (which are mostly new features that are not significant enough to call attention to, such as logging formatting changes or updates to the documentation)
If the exact version and release date is known (because a release is being prepared), the section header is formatted as:
## X.Y.Z (YYYY-MM-DD)
Style guide#
Code#
Documentation#
Follow the LSST DM User Documentation Style Guide, which is primarily based on the Google Developer Style Guide.
Document the Python API with numpydoc-formatted docstrings. See the LSST DM Docstring Style Guide.
Follow the LSST DM ReStructuredTextStyle Guide. In particular, ensure that prose is written one-sentence-per-line for better Git diffs.