Skip to content

Latest commit

 

History

History
301 lines (258 loc) · 15.8 KB

development.md

File metadata and controls

301 lines (258 loc) · 15.8 KB

Development

TESK API is a Spring Boot 4.1 application (Spring Framework 7, Spring Security 7, Jackson 3) on Java 21, built with Maven 3.9 (through the wrapper ./mvnw, which downloads the version pinned in .mvn/wrapper/maven-wrapper.properties). The server stub is generated from the TES specification with the OpenAPI Generator Maven plugin (see Generating a new API stub), springdoc-openapi serves the API description and the Swagger UI, and the Kubernetes Java client talks to the Kubernetes API.

The JSON of the API and of the tasks stored in Kubernetes is written and read with Jackson 3 with the defaults of Jackson 2 (spring.jackson.use-jackson2-defaults=true in application.properties), so that it stays as it was with Spring Boot 2.7. The model classes keep the annotations of Jackson 2 (com.fasterxml.jackson.annotation), which Jackson 3 reads.

The Kubernetes client needs newer versions of some libraries than Spring Boot manages (SnakeYAML, Gson, Commons Lang and Commons Codec): pom.xml sets them to those of the client's parent POM (io.kubernetes:client-java-parent), so update them together with the client. The models of the client refuse JSON with fields they don't know; KubernetesJson installs a Gson that ignores them (see Deployment), so test fixtures of Kubernetes objects may leave out fields that Kubernetes requires. The executors' jobs in the taskmaster's input are written without the empty lists and maps of the client's models (GSON_WITHOUT_EMPTY_COLLECTIONS).

The wrapper is the only-script type of the Apache Maven Wrapper, so there is no binary maven-wrapper.jar in the repository. It needs curl or wget and unzip (without unzip it would fetch the .tar.gz, which does not match the checksum), and it checks the downloaded Maven distribution against distributionSha256Sum in .mvn/wrapper/maven-wrapper.properties; a mismatch stops the build.

Dependabot updates the Maven version and the checksum of the wrapper together (in the maven group). To bump them by hand, change both, or every build fails:

  1. Download apache-maven-<version>-bin.zip from Maven Central and check it against the .sha512 file next to it.
  2. Compute its SHA-256 (sha256sum).
  3. Put the new version in distributionUrl and the sum in distributionSha256Sum. If you regenerate the scripts with ./mvnw wrapper:wrapper -Dtype=only-script -Dmaven=<version>, check that distributionSha256Sum is still there and has the new sum.

Building and testing

To build TESK API and run the unit tests (JUnit, Surefire, in src/test/java) and the integration tests (Failsafe, in src/integration-test/java; the Kubernetes API and the OpenID Connect provider are mocked with WireMock, so no cluster is needed):

./mvnw clean verify

The tests are written for JUnit Jupiter (JUnit 5 and later; no JUnit 4 or vintage engine): @ExtendWith and @RegisterExtension instead of runners and rules, e.g. WireMockExtension for WireMock servers (built with failOnUnmatchedRequests(true), so that a request that no stub matches fails the test) and Spring Boot's OutputCaptureExtension for logs. Spring Boot's test annotations (@SpringBootTest, @WebMvcTest, …) include the Spring extension. Settings that the code reads from environment variables are read through Spring's Environment (e.g. those of the security context, CONFIGMAP and the like), so that tests set them with a MockEnvironment or test properties rather than by changing the process's environment. Surefire and Failsafe load Mockito as a Java agent at startup (-javaagent in their argLine): Java 21 warns about agents that are loaded later, as Mockito would load itself, and later Java versions refuse them.

Test coverage

The unit and integration tests together must cover all lines and branches: ./mvnw clean verify fails otherwise (check of jacoco-maven-plugin in pom.xml, on the merged data of both in target/jacoco-merged.exec), and so does Codecov (codecov.yml), to which CI sends the merged report. Not measured are only:

  • the files that openapi-generator generates from the TES specification (listed in src/main/java/.openapi-generator/FILES, and excluded from formatting and lint as well): data classes, and the API stubs, whose default methods the controllers override. Keep the list in pom.xml the same as that file;
  • TeskApplication, whose main only starts Spring Boot, as the tests do.

JaCoCo writes the reports of the unit tests to target/site/jacoco/, those of the integration tests to target/site/jacoco-it/, and that of both together, which the check uses and CI sends to Codecov, to target/site/jacoco-merged/ (index.html for a browser, jacoco.xml for tools). To see what the tests miss, open target/site/jacoco-merged/index.html after ./mvnw clean verify.

Cover new code with tests that check its behaviour, rather than excluding it; code that no test can reach is usually dead and can go.

Formatting and linting

The Java code is formatted with google-java-format in AOSP style (4-space indentation, lines of at most 100 characters), through the Spotless Maven plugin, and linted with Checkstyle (rules in checkstyle.xml: likely bugs and problematic constructs, e.g. missing braces or equals without hashCode; the layout is left to the formatter; wildcard imports are allowed). The generated files (listed in src/main/java/.openapi-generator/FILES) are excluded from both. To format the code, and to check it as the CI job Lint does (./mvnw verify checks neither):

./mvnw spotless:apply
./mvnw spotless:check checkstyle:check

Like building and testing, these two commands need Java 21.

The commits that only reformatted the code are listed in .git-blame-ignore-revs. GitHub skips them in its blame view; for git blame, run once:

git config blame.ignoreRevsFile .git-blame-ignore-revs

Building the image and the docs

To build the image (the Dockerfile builds the application from source, so nothing needs to be built first):

docker build --tag tesk-api .

To build these docs, with Python 3:

python -m venv .venv
.venv/bin/pip install --requirement docs/requirements.txt
.venv/bin/mkdocs serve

Continuous integration

The CI workflow (.github/workflows/ci.yml) runs on pull requests and pushes to dev: it checks the pull request title, lints the workflows, checks the formatting and lint of the Java code, builds and tests TESK API (with coverage to Codecov), builds the docs strictly, starts the image in both profiles, and scans the image for known vulnerabilities. For now, the scan only reports its findings (to code scanning) and doesn't fail, until the dependencies are updated (#60); then it fails on critical and high vulnerabilities that have a fix, except those accepted in .trivyignore.yaml, each with a reason and an expiry date. The release workflow's scan fails on them already. On pushes to dev, it publishes the scanned and signed image as <org>/tesk-api:dev (and sha-<commit>), where <org> is the variable DOCKERHUB_ORG (by default elixircloud); this needs the secrets DOCKERHUB_LOGIN and DOCKERHUB_TOKEN in the environment dev-images, which only allows branch dev. Forks without DOCKERHUB_ORG run the checks only. The job CI passed sums up all the others and is the only required check. The scan workflow (.github/workflows/scan.yml) scans the image of the latest release weekly.

Versioning and releases

Pull requests go to dev (squash merges; titles in the Conventional Commits format, at most 50 characters). For a release, main is moved forward to a commit of dev. Pushing a signed, annotated version tag runs the release workflow (.github/workflows/release.yml), which publishes the image to Docker Hub as <org>/tesk-api and creates the GitHub release:

Tag Image tags Example
vX.Y.Z X.Y.Z, and latest if it is the highest release on main v1.2.0 publishes 1.2.0
vX.Y.ZrcN (also aN, bN) X.Y.ZrcN only v1.2.0rc1 publishes 1.2.0rc1

The commit must be on main, CI must have passed for its push to dev, and the versions must match the tag, so set them in a pull request first:

  • pom.xml: X.Y.Z, also for the pre-releases of X.Y.Z (e.g., 1.2.0 for v1.2.0rc1);
  • CITATION.cff: the tag without v (e.g., 1.2.0rc1 for v1.2.0rc1), with date-released;
  • docs/releases/<version>.md, with the version as in CITATION.cff (e.g., docs/releases/1.2.0rc1.md): the curated release notes, in the organization's format: an intro paragraph saying what the release brings, with a link to the upgrade notes if operators or clients must act, then the sections ## Breaking changes, ## Features, ## Security, ## Fixes, ## Documentation and ## Maintenance (in this order, empty ones left out), holding bullets only, each ending with its issue and pull request numbers, e.g., (#12, #34). Links must be absolute, as the notes are published on GitHub. The release workflow adds GitHub's generated notes; the shared action publish-release describes the format and its checks.

latest goes by tags, so if a release fails, follow it with a higher version. Only admins can create release tags, and nobody can move or delete them (tag rulesets). Pushing and publishing the image each wait for an approval in the release environment, which only allows version tags. The image is scanned before it is tagged; images are signed with Sigstore and carry provenance attestations.

Once the image is published, the job Publish release creates the GitHub release of the tag (no manual step): the curated notes of the tagged commit, then GitHub's generated notes under ## Commits (without their ## What's Changed header, with "New Contributors"), and GitHub's **Full Changelog** line last. Versions below 1.0.0 and a, b and rc versions are pre-releases. The job needs no approval and no secrets, only contents: write. Prepare release runs the same checks first (the notes, the tag and its commit, published releases of the tag), so that a release with missing or malformed notes fails before anything is published. It can't see draft releases, so delete any draft of the tag before you push the tag. If only Publish release fails, re-run the failed jobs (not all jobs: once the GitHub release exists, Prepare release fails); a re-run passes without changes if the earlier attempt created the release after all. Publish release runs one release at a time, so that latest is set in order; if several tags are released at once, GitHub can cancel a waiting run of the job: re-run it the same way. Releases are never edited afterwards: fix mistakes in the notes of the next release.

After publishing the image (without waiting for the GitHub release), the workflow registers it, with the release tag, at Pubgrade (POST /repositories/{id}/builds, through Pubgrade's action actions/notify), which notifies the deployments that subscribed to upgrades, also for pre-releases, if they subscribed to those. The action masks the token and retries a request only if it never reached Pubgrade, so that one run registers the build at most once; re-running the job registers it again. This needs Pubgrade's publish mode (#33) and these settings:

Setting Kind Where Meaning
PUBGRADE_URL Variable Repository Base URL of the Pubgrade API, with https://. Switches the notification on.
PUBGRADE_REPOSITORY_ID Variable Repository, or the release environment The identifier from registering the repository in Pubgrade
PUBGRADE_TOKEN Secret The release environment The access token from registering the repository in Pubgrade

Without PUBGRADE_URL, the job Notify Pubgrade is skipped, so releases don't wait for its approval. Set PUBGRADE_URL for the repository, not in the release environment: a job's condition is evaluated before the job enters its environment, so a variable there doesn't switch the job on (and would override the repository's URL within the job). An organization variable would switch it on for every repository it is shared with, also those without a Pubgrade token. With PUBGRADE_URL, the job waits for an approval in the release environment, like pushing and publishing, and fails if one of the other settings is missing or invalid: the identifier and the token may only hold letters, digits, ., ~ and - (as Pubgrade issues them). If only this job fails or is rejected, the image is published all the same: fix the settings and re-run the job, or leave it; don't release a new version for it. Unless it failed on an invalid setting or with "Could not reach Pubgrade (curl exit status 6)" or "(… 7)" (no connection), Pubgrade may have registered the build all the same: check the repository's builds (GET /repositories/{id}/builds) before re-running it, as re-running notifies the subscribed deployments again. Settings of the former pubgrade environment are no longer read: move them as above.

Generating a new API stub

The TES specification (v1.0) is linked as the submodule tes. After updating it to a new version of the specification, run:

./mvnw clean generate-sources -P generate-swagger

This generates new versions of the model (uk.ac.ebi.tsc.tesk.tes.model) and the API stub (uk.ac.ebi.tsc.tesk.tes.api). If the list of generated files (src/main/java/.openapi-generator/FILES) changes, change the exclusions of Spotless, Checkstyle (checkstyle.xml) and JaCoCo in pom.xml alike (see Test coverage). The model classes carry Bean Validation annotations beyond those derived from the specification (e.g. @Digits and @DecimalMin on the resources of TesResources); restore them by hand in the generated code, and reconcile any other manual changes, e.g. the internal field userIdFromLegacyLabel of TesTaskLog (not serialized), which the access check of a task needs, and the check isUrlOrContentGiven of TesInput (a generator that adds @ScriptAssert(lang = "javascript") instead brings back a check that fails on Java 15 and later, which have no JavaScript engine). Until the experimental private image credential workaround remains supported, also restore the docker_registry, docker_login and docker_pass fields, validation, write-only handling, and experimental API descriptions in TesExecutor; the API and converter tests check this extension.

The generator (OpenAPI Generator 7, with useSpringBoot3 and useJakartaEe) writes Jakarta and OpenAPI 3 (io.swagger.v3) annotations. The committed stubs were generated with version 6.6.0 and moved to these annotations by hand with the move to Spring Boot 4, so the next generation also changes their layout (e.g. constructors with the required fields; no TesServiceInfoAllOf and TesServiceTypeAllOf). Keep the annotations that make the API description match the API: requiredMode = NOT_REQUIRED on type of TesInput and TesOutput (which may be left out), accessMode = WRITE_ONLY on docker_login and docker_pass, and the schema name tesResources of TesResources.