-
Notifications
You must be signed in to change notification settings - Fork 157
docs: provide instructions on how to pre-load tf providers in a custom runn… #1490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thejosephstevens
wants to merge
1
commit into
flux-iac:main
Choose a base branch
from
ascend-io:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
docs/use-tf-controller/build-custom-runner-image-with-preloaded-providers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,154 @@ | ||||||
# How to pre-load Providers in your custom tf-runner image | ||||||
|
||||||
To build a custom runner image, follow the instructions in the [Build and Use a Custom Runner Image](build-and-use-a-custom-runner-image.md) guide. In this guide, we will customize the Dockerfile and the tofu-controller helm values. | ||||||
|
||||||
This guide is useful if you are trying to avoid networking costs associated with downloading providers on each run. Without caching, this can add up to significant costs in scenarios where you're downloading providers through a NAT gateway. | ||||||
|
||||||
## Prerequisites | ||||||
|
||||||
You need Docker and Git to build the image. | ||||||
|
||||||
## Set up your build directory | ||||||
|
||||||
1. Create a `Dockerfile` that extends the base image and that adds Terraform, plus any additional required tooling. For example: | ||||||
|
||||||
```Dockerfile | ||||||
# Pass in the baseimage and tag from the tf-runner image you want to extend | ||||||
ARG BASE_IMAGE | ||||||
ARG BASE_TAG | ||||||
|
||||||
FROM $BASE_IMAGE:$BASE_TAG | ||||||
|
||||||
# Set up HOME var so that the terraformrc can be found by tf cli | ||||||
ENV HOME=/home/runner | ||||||
|
||||||
# Add the terraformrc file to the image | ||||||
ADD .build-terraformrc $HOME/.terraformrc | ||||||
|
||||||
# Add the tf_providers.tf file to the image | ||||||
ADD tf_providers.tf /tmp/cache-init/tf_providers.tf | ||||||
|
||||||
# Switch to root to have permissions for operations | ||||||
USER root | ||||||
|
||||||
# Create the plugins directory and run terraform init to download the providers | ||||||
RUN mkdir -p /usr/local/share/terraform/plugins && \ | ||||||
cd /tmp/cache-init && \ | ||||||
terraform init -backend=false && \ | ||||||
cd - && \ | ||||||
rm -rf /tmp/cache-init && \ | ||||||
# Ensure proper permissions for runtime access to registry mirror | ||||||
# Note: do not use `/home/runner` or `/tmp`, as they get overwritten at runtime | ||||||
chown -R 65532:65532 /usr/local/share/terraform | ||||||
|
||||||
# Switch back to the non-root user after operations | ||||||
USER 65532:65532 | ||||||
``` | ||||||
|
||||||
2. Add `.build-terraformrc` and `tf_providers.tf` to the directory. | ||||||
|
||||||
``` | ||||||
# .build-terraformrc | ||||||
# Create a local cache dir | ||||||
plugin_cache_dir = "/usr/local/share/terraform/plugins" | ||||||
# Disable reaching out to Hashicorp for upgrade and security checks | ||||||
disable_checkpoint = true | ||||||
``` | ||||||
|
||||||
``` | ||||||
# tf_providers.tf | ||||||
terraform { | ||||||
required_providers { | ||||||
aws = { | ||||||
source = "hashicorp/aws" | ||||||
version = "5.47.0" | ||||||
} | ||||||
azuread = { | ||||||
source = "hashicorp/azuread" | ||||||
version = "2.48.0" | ||||||
} | ||||||
azurerm = { | ||||||
source = "hashicorp/azurerm" | ||||||
version = "3.101.0" | ||||||
} | ||||||
google = { | ||||||
source = "hashicorp/google" | ||||||
version = "5.26.0" | ||||||
} | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
3. Build the image from the directory containing the `Dockerfile` you created above: | ||||||
|
||||||
```bash | ||||||
export BASE_IMAGE=ghcr.io/flux-iac/tf-runner | ||||||
export BASE_TAG=v0.16.0-rc.4 | ||||||
docker build \ | ||||||
--build-arg BASE_IMAGE=${BASE_IMAGE} \ | ||||||
--build-arg BASE_TAG=${BASE_TAG} \ | ||||||
--tag my-custom-runner:${BASE_TAG} . | ||||||
docker tag my-custom-runner:${BASE_TAG} $REMOTE_REPO:${BASE_TAG} | ||||||
docker push $REMOTE_REPO:${BASE_TAG} | ||||||
``` | ||||||
|
||||||
Replace the relevant values above with the corresponding values in your organisation/implementation. | ||||||
|
||||||
4. Update the `values.runner.image` values in the TF-Controller Helm chart values to point to the new image: | ||||||
|
||||||
```yaml | ||||||
values: | ||||||
runner: | ||||||
image: | ||||||
repository: ghcr.io/my-org/custom-runnner | ||||||
tag: v0.16.0-rc.3 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
``` | ||||||
|
||||||
5. Add secret to tofu-controller namespace with the runtime tf cli rc | ||||||
|
||||||
```yaml | ||||||
apiVersion: v1 | ||||||
kind: Secret | ||||||
metadata: | ||||||
name: tf-cli-config # This name is specific, it gets referenced by the Terraform resource | ||||||
namespace: flux-system | ||||||
data: | ||||||
# This file name has to end with `.tfrc` to be picked up correctly | ||||||
### CONTENTS ### | ||||||
# # Disable reaching out to external registry | ||||||
# disable_checkpoint = true | ||||||
# # Force TF to use the local registry mirror for all providers | ||||||
# provider_installation { | ||||||
# filesystem_mirror { | ||||||
# path = "/usr/local/share/terraform/plugins" | ||||||
# include = ["registry.terraform.io/*/*"] | ||||||
# } | ||||||
# } | ||||||
### END CONTENTS ### | ||||||
# Base64 encoded contents of .runtime-terraformrc | ||||||
cli-config.tfrc: IyBEaXNhYmxlIHJlYWNoaW5nIG91dCB0byBIYXNoaWNvcnAgZm9yIHVwZ3JhZGUgYW5kIHNlY3VyaXR5IGNoZWNrcwpkaXNhYmxlX2NoZWNrcG9pbnQgPSB0cnVlCnByb3ZpZGVyX2luc3RhbGxhdGlvbiB7CiAgZmlsZXN5c3RlbV9taXJyb3IgewogICAgcGF0aCAgICA9ICIvdXNyL2xvY2FsL3NoYXJlL3RlcnJhZm9ybS9wbHVnaW5zIgogICAgaW5jbHVkZSA9IFsicmVnaXN0cnkudGVycmFmb3JtLmlvLyovKiJdCiAgfQp9Cg== | ||||||
``` | ||||||
|
||||||
7. Update all Terraform resources to use the cliConfigSecret | ||||||
|
||||||
```yaml | ||||||
apiVersion: infra.contrib.fluxcd.io/v1alpha2 | ||||||
kind: Terraform | ||||||
metadata: | ||||||
name: example-tf | ||||||
namespace: flux-system | ||||||
spec: | ||||||
cliConfigSecret: | ||||||
name: tf-cli-config | ||||||
namespace: flux-system | ||||||
``` | ||||||
|
||||||
8. Commit and push the changes to Git. Confirm that the HelmRelease has been updated: | ||||||
|
||||||
```bash | ||||||
kubectl get deployments.apps -n flux-system tf-controller -o jsonpath='{.spec.template.spec.containers[*]}' | jq '.env[] | select(.name == "RUNNER_POD_IMAGE")' | ||||||
{ | ||||||
"name": "RUNNER_POD_IMAGE", | ||||||
"value": "ghcr.io/my-org/custom-runner:v0.16.0-rc3" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.