|
Some checks failed
CI / build (push) Failing after 12s
GitOrigin-RevId: 6370f6ea785709295b6abcf9c60717cacf3ac432 |
||
|---|---|---|
| .. | ||
| environments | ||
| templates | ||
| base.libsonnet | ||
| BUILD.bazel | ||
| images.libsonnet | ||
| k.libsonnet | ||
| README.md | ||
k8s/configs
WARNING: Provisional Readme
This directory contains kubernetes configurations, which are defined in jsonnet and which can also depend upon loaded helm charts.
Manifests are built using custom Tanka-like build rules, as in this example:
load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_to_json")
load("//tools:tanka.bzl", "tanka_environment")
# Generate a json manifest containing all of the manifests
jsonnet_to_json(
name = "main",
src = "main.jsonnet",
outs = ["main.json"],
data = [
# Depend on a helm chart (transitively, in this case)
"@helm_coderv2_coder//:chart",
],
visibility = ["//visibility:public"],
deps = [
"//k8s/configs/templates",
],
)
# Defines three targets
# - example.show: Prints the list of output entities
# - example.diff: Diffs the list of entities against the live entities
# - example.apply: Applies the changes to Kubernetes.
tanka_environment(
name = "example",
main = ":main",
spec = "spec.json",
)
Secret Management with SOPS
We use SOPS to manage secrets in this repository. Encrypted files are checked into version control, and Bazel handles decryption during the build process, keeping secrets in memory or temporary build artifacts (which are not committed).
Prerequisites
- SOPS: The
sopsbinary is automatically managed by Bazel (fetched viaMODULE.bazel), so you don't strictly need it installed on your system to build, but you do need it to edit or create secrets.- Install:
brew install sops(macOS) or download from GitHub Releases.
- Install:
- Encryption Key: You must have a configured Age key or PGP key that matches the
.sops.yamlconfiguration (if one exists at the repo root) or pass the keys explicitly via command line.
Workflow
-
Create/Edit Encrypted File: Create a file (e.g.,
secrets.sops.yamlorsecrets.sops.json) and encrypt it.# Example: Encrypting a new file sops --encrypt --age <your-age-public-key> secrets.json > secrets.sops.json # Example: Editing an existing encrypted file SOPS_AGE_KEY_FILE="./key.txt" sops secrets.sops.json -
Define Bazel Target: In the
BUILD.bazelfile of your environment (e.g.,k8s/configs/environments/media/BUILD.bazel), use thesops_decryptrule to decrypt the file at build time.load("//tools:sops.bzl", "sops_decrypt") load("@rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library") # 1. Decrypt the secrets file sops_decrypt( name = "secrets", src = "secrets.sops.yaml", # The encrypted source file out = "secrets.json", # The decrypted output filename ) # 2. Wrap it in a jsonnet_library so it can be imported jsonnet_library( name = "secrets_lib", srcs = [":secrets"], ) -
Use in Jsonnet: Update your
jsonnet_to_jsontarget to depend on the library, and import the secrets in your Jsonnet code.BUILD.bazel:
jsonnet_to_json( name = "main", src = "main.jsonnet", deps = [ ":secrets_lib", # ... other deps ... ], # ... )main.jsonnet:
local secrets = import "k8s/.../secrets.json"; { secrets: { examplePostgres: postgres.Secret(postgres.SecretParams{ name: "example-postgres", namespace: "example", password: secrets.example_psql_db_pwd, }), } }
Safety
- Do not commit decrypted files. The
sops_decryptrule places files in thebazel-outdirectory, which is ignored by git. - Ensure your
.gitignoreincludes*.jsonor specific secret patterns if you are working with them locally outside of Bazel.