Optimizing Custom GraphOS Router Builds

Enhance performance, minimize build time, and address system requirements


While running the GraphOS Router in production has great performance, this comes at the cost of increased build time and higher system requirements when compiling from source.

Building the router including custom plugins from source is a memory-expensive process, and as such it's not suited for all build environments.

Some cloud build pipelines such as Google Cloud Build, will require changes to the default build configuration to increase the memory and CPU capacity of the build worker pool.

The router will also download a large collection of dependencies during the build process each build by default. We can use Kaniko to cache intermediate build layers.

In this example for Google Cloud Build, we have replaced the standard docker gcr.io/cloud-builders/docker container image, with gcr.io/kaniko-project/executor . This is all that's needed to enable caching of the intermediate build steps. Each step will be pushed to the destination container repo as soon as each layer is built, so even failed builds will contribute to increased build speeds as successful layers are cached.

We also changed the default options.machineType value to E2_HIGHCPU_8, this is because the default worker pool doesn't have the CPU or memory resources necessary to compile the router.

YAML
1#cloudbuild.yaml
2
3steps:
4  - id: Build Router
5    name: 'gcr.io/kaniko-project/executor:latest'
6    args:
7      - '--cache=true'
8      - '--dockerfile=$_DOCKERFILE'
9      - '--context=dir://$_IMAGE_NAME'
10      - >-
11        --destination=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_IMAGE_NAME:$COMMIT_SHA
12  - id: Deploy Router
13    name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
14    entrypoint: gcloud
15    args:
16      - run
17      - services
18      - update
19      - $_SERVICE_NAME
20      - '--platform=managed'
21      - '--image=$_GCR_HOSTNAME/$PROJECT_ID/$REPO_NAME/$_IMAGE_NAME:$COMMIT_SHA'
22      - '--region=$_DEPLOY_REGION'
23      - '--quiet'
24options:
25  machineType: E2_HIGHCPU_8
26substitutions:
27  _DOCKERFILE: custom-router/Dockerfile
28  _IMAGE_NAME: custom-router
29  _SERVICE_NAME: apollo-router
30  _DEPLOY_REGION: northamerica-northeast1
31  _GCR_HOSTNAME: northamerica-northeast1-docker.pkg.dev
32  _PLATFORM: managed