Skip to content

Commit

Permalink
Generate inventory using Nix
Browse files Browse the repository at this point in the history
This commit replaces the update_inventory.py script with a Nix module
based on Flake Parts [1] which defines all configuration options
including types and default values.

Only values that are exposed by the module can be set in the
configuration. Exposed values have been removed from the default/
main.yaml in order to have only a single source of truth. As a result,
all variables (set or not) are rendered into the inventory.

The cluster initialization is now backed by Nix Flake templates.
Template files for new clusters can be found at ./nix/templates/
cluster-repo.

The cluster repository layout is changed in such a way that the
./config directory is completely handled by the user and the ./inventory
directory is completely generated and may be ignored from the VCS. A
./state directory is added which is both input and output of the
inventory generation and which has to be added to VCS.

                   +---------+
                   | ./state |
                   +--+---^--+
                      |   |
               +------v---+---------+
+----------+   |                    |   +-------------+
| ./config +--->     Nix module     +---> ./inventory |
+----------+   |                    |   +-------------+
               +--------------------+

Resource request and limit handling has been changed such that
unflattened arrays are used instead of individual options for each
value.

[1] https://flake.parts
  • Loading branch information
Lykos153 committed Oct 28, 2024
1 parent fb78c33 commit c0be6c5
Show file tree
Hide file tree
Showing 214 changed files with 6,080 additions and 5,677 deletions.
4 changes: 2 additions & 2 deletions .envrc.lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ use_flake_if_nix() {
_poetry_common "${flake_dir}"
watch_file "${flake_dir}/nix/poetry.nix"
if [ "${MINIMAL_ACCESS_VENV:-false}" == "true" ]; then
use flake "${flake_dir}?shallow=1#minimal"
use flake "${flake_dir}#minimal"
else
use flake "${flake_dir}?shallow=1#${YAOOK_K8S_DEVSHELL:-default}"
use flake "${flake_dir}#${YAOOK_K8S_DEVSHELL:-default}"
fi
export NIX_FLAKE_ACTIVE="${NIX_FLAKE_ACTIVE}:${flake_dir}"
else
Expand Down
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ build-docs-check:
- export PATH="$VIRTUAL_ENV/bin:$PATH"
script:
- towncrier build --version x.x.x --keep
- nix build .#docsRST -o docs/user/reference/options
- sphinx-build -W docs _build/html
- mv _build/html public
artifacts:
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: trailing-whitespace
exclude: '.*(\.drawio|\.svg)$'
- id: end-of-file-fixer
exclude: '.*(\.drawio|\.svg|\.nix)$'
exclude: '.*(\.drawio|\.svg|\.nix)$' # Nix files are fixed by nix-fmt
- id: mixed-line-ending
- id: check-executables-have-shebangs
- id: check-merge-conflict
Expand All @@ -36,13 +36,13 @@ repos:
stages: [pre-commit, pre-push, manual]
- id: check-flake
name: check flake
files: "^flake.nix$"
files: .*\.nix$'
entry: ci/lint/check-flake.sh
language: script
stages: [pre-commit, pre-push, manual]
- id: nix-fmt
name: nix-fmt
files: "^flake.nix$"
files: '.*\.nix$'
entry: ci/lint/format-flake.sh
language: script
stages: [pre-commit, pre-push, manual]
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Breaking changes
rather than across separate lists for each type of value.

Furthermore you now have control over the whole name of Terraform nodes,
see :ref:`the documentation <cluster-configuration.configuring-terraform>`
see :ref:`the documentation <configuration-options.yk8s.terraform>`
for further details.

.. code:: diff
Expand Down
4 changes: 4 additions & 0 deletions actions/apply-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ actions_dir="$(dirname "$0")"

# shellcheck source=actions/lib.sh
. "$actions_dir/lib.sh"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars

check_venv
Expand Down
7 changes: 4 additions & 3 deletions actions/apply-custom.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ actions_dir="$(dirname "$0")"

# shellcheck source=actions/lib.sh
. "$actions_dir/lib.sh"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars

check_venv
Expand All @@ -14,9 +18,6 @@ require_vault_token

install_prerequisites

# Ensure that the latest config is deployed to the inventory
python3 "$actions_dir/update_inventory.py"

# Bring the wireguard interface up if configured so
"$actions_dir/wg-up.sh"

Expand Down
7 changes: 3 additions & 4 deletions actions/apply-k8s-core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ execute_playbook() {
local playbook="$1"
notef "Executing playbook $playbook\n"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars
check_venv
check_conf_sanity
require_vault_token
install_prerequisites

# Ensure that the latest config is deployed to the inventory
python3 "$actions_dir/update_inventory.py"
# Bring the wireguard interface up if configured so
"$actions_dir/wg-up.sh"

set_kubeconfig

pushd "$ansible_k8s_core_dir"
# Include k8s-core roles
ANSIBLE_ROLES_PATH="$ansible_k8s_core_dir/roles" \
Expand Down
5 changes: 3 additions & 2 deletions actions/apply-k8s-supplements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ execute_playbook() {
local playbook="$1"
notef "Executing playbook $playbook\n"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars
check_conf_sanity
check_venv
require_vault_token
install_prerequisites

# Ensure that the latest config is deployed to the inventory
python3 "$actions_dir/update_inventory.py"
# Bring the wireguard interface up if configured so
"$actions_dir/wg-up.sh"

Expand Down
7 changes: 4 additions & 3 deletions actions/apply-prepare-gw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ actions_dir="$(dirname "$0")"

# shellcheck source=actions/lib.sh
. "$actions_dir/lib.sh"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars

check_venv
Expand All @@ -14,9 +18,6 @@ require_vault_token

install_prerequisites

# Ensure that the latest config is deployed to the inventory
python3 "$actions_dir/update_inventory.py"

if [ "${tf_usage:-true}" == 'false' ]; then
errorf "It seems like you're not running on top of OpenStack,"
errorf "because terraform.enabled is false."
Expand Down
9 changes: 5 additions & 4 deletions actions/apply-terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ actions_dir="$(realpath "$(dirname "$0")")"

# shellcheck source=actions/lib.sh
. "$actions_dir/lib.sh"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars

check_venv

# Ensure that the latest config is deployed to the inventory
python3 "$actions_dir/update_inventory.py"

if [ "$("$actions_dir/helpers/semver2.sh" "$(terraform -v -json | jq -r '.terraform_version')" "$terraform_min_version")" -lt 0 ]; then
errorf 'Please upgrade Terraform to at least v'"$terraform_min_version"
exit 5
Expand Down Expand Up @@ -166,7 +167,7 @@ if [ $rc == $RC_DISRUPTION ]; then
# shellcheck disable=SC2016
errorf 'terraform would delete or recreate a resource, but not all of the following is set' >&2
errorf ' - MANAGED_K8S_DISRUPT_THE_HARBOUR=true' >&2
errorf " - ${terraform_disruption_setting}=false in ${config_file}" >&2
errorf " - terraform.prevent_disruption = false in the config" >&2
errorf 'aborting due to destructive change without approval.' >&2
exit 3
fi
Expand Down
7 changes: 6 additions & 1 deletion actions/destroy.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail
actions_dir="$(realpath "$(dirname "$0")")"

# shellcheck source=actions/lib.sh
. "$actions_dir/lib.sh"

# Ensure that the latest config is deployed to the inventory
"$actions_dir/update-inventory.sh"

load_conf_vars

check_venv
Expand Down Expand Up @@ -92,4 +97,4 @@ if [ "$(jq -r .backend.type "$terraform_state_dir/.terraform/terraform.tfstate")
fi

# Purge the remaining terraform directory. Its existence is a condition for additional disruption checks.
rm -f "$terraform_state_dir/config.tfvars.json"
rm -fr "$terraform_state_dir"
Loading

0 comments on commit c0be6c5

Please sign in to comment.