Skip to content
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

refactor: move variables into separate package #2414

Merged
merged 30 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3cab72e
chore: refactor variables into separate package
Racer159 Apr 3, 2024
25729e9
update schema
Racer159 Apr 3, 2024
747e765
fix var config
Racer159 Apr 3, 2024
4490882
Update src/internal/packager/helm/common.go
Racer159 Apr 3, 2024
87b524d
implement slog
Racer159 Apr 3, 2024
ed4666f
remove unneeded code
Racer159 Apr 4, 2024
307c938
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 4, 2024
a67c3fc
just do the one chart yaml
Racer159 Apr 4, 2024
703f59c
Merge branch 'main' into 2253-variables-refactor
AustinAbro321 Apr 5, 2024
4d1e142
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 9, 2024
40edb01
fix merge issues and some feedback
Racer159 Apr 9, 2024
30d8b86
fix the dang test
Racer159 Apr 10, 2024
7fb339f
Update src/pkg/variables/variables.go
Racer159 Apr 10, 2024
28dac9c
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 10, 2024
9048f19
remove docs
Racer159 Apr 10, 2024
ed4f9eb
fix the site
Racer159 Apr 10, 2024
e74db47
fix more docs stuff
Racer159 Apr 10, 2024
6345a39
allow for magic unwrapping
Racer159 Apr 10, 2024
d44a211
lint fixes
Racer159 Apr 10, 2024
3b3414d
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 15, 2024
429fbfc
remove width
Racer159 Apr 15, 2024
7e0ac72
initial rewrite of variables docs
Racer159 Apr 16, 2024
65629b0
refine docs further
Racer159 Apr 16, 2024
dfa52ab
further cleanup
Racer159 Apr 16, 2024
2b1d4b4
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 16, 2024
cb4bfc3
further cleanup
Racer159 Apr 16, 2024
e7facee
update tmp to include the l
Racer159 Apr 16, 2024
7c9f9af
fix little issues
Racer159 Apr 16, 2024
4f6f054
fix nit
Racer159 Apr 22, 2024
04108bd
Merge branch 'main' into 2253-variables-refactor
Racer159 Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Zarf eliminates the [complexity of air gap software delivery](https://www.itopst

## 🛠️ Configurable Features

- Customizable [variables and package templates](https://docs.zarf.dev/ref/variables/) with defaults and user prompting
- Customizable [variables and package templates](https://docs.zarf.dev/ref/values/) with defaults and user prompting
- [Composable packages](https://docs.zarf.dev/ref/components/#component-imports) to include multiple sub-packages/components
- Component-level OS/architecture filtering

Expand All @@ -67,7 +67,7 @@ To discover more about Zarf and explore its features, please visit [docs.zarf.de
- [packages](https://docs.zarf.dev/ref/packages)
- [components](https://docs.zarf.dev/ref/components)
- [actions](https://docs.zarf.dev/ref/actions)
- [variables](https://docs.zarf.dev/ref/variables)
- [variables](https://docs.zarf.dev/ref/values)
- [SBOMs](https://docs.zarf.dev/ref/sboms)
- and more!

Expand Down
4 changes: 2 additions & 2 deletions hack/check-zarf-docs-and-schema.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env sh

if [ -z "$(git status -s docs/ zarf.schema.json)" ]; then
if [ -z "$(git status -s ./site/src/content/docs/commands/ ./zarf.schema.json)" ]; then
echo "Success!"
exit 0
else
git diff docs/ zarf.schema.json
git diff ./site/src/content/docs/commands/ ./zarf.schema.json
exit 1
fi
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
348 changes: 204 additions & 144 deletions site/src/assets/zarf-bubbles.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,035 changes: 530 additions & 505 deletions site/src/assets/zarf-underwater-behind-rock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion site/src/components/SchemaItemProperties.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
const { item, include, invert } = Astro.props;
const { item, include, invert, unwrap } = Astro.props;

const includesElement = (key: string) => {
if (!include) {
Expand All @@ -16,6 +16,21 @@ const json = await import("../assets/zarf.schema.json");
// @ts-expect-error - We don't import a TS type for the schema, but we know it's structured correctly
const itemSchema = json.definitions[item];

if (unwrap) {
unwrap.forEach((wrapped: string) => {
if (itemSchema.properties[wrapped]) {
// @ts-expect-error - We don't import a TS type for the schema, but we know it's structured correctly
const wrappedSchema = json.definitions[wrapped];

delete itemSchema.properties[wrapped]
itemSchema.required = itemSchema.required.filter((elem: string) => elem != wrapped)

itemSchema.properties = {...wrappedSchema.properties, ...itemSchema.properties}
itemSchema.required = [...wrappedSchema.required, ...itemSchema.required]
}
});
}

const properties = Object.keys(itemSchema.properties)
.filter(includesElement)
.sort()
Expand Down
2 changes: 1 addition & 1 deletion site/src/content/docs/faq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ Typically you should not deploy a Zarf package in YOLO mode if the cluster has a

A `skeleton` package is a bare-bones Zarf package definition alongside its associated local files and manifests that has been published to an OCI registry. These packages are intended for use with [component composability](/ref/components) to provide versioned imports for components that you wish to mix and match or modify with merge-overrides across multiple separate packages.

Skeleton packages have not been run through the `zarf package create` process yet, and thus do not have any remote resources included (no images, repos, or remote manifests and files) thereby retaining any [create-time package configuration templates](/ref/variables) as they were defined in the original `zarf.yaml` (i.e. untemplated).
Skeleton packages have not been run through the `zarf package create` process yet, and thus do not have any remote resources included (no images, repos, or remote manifests and files) thereby retaining any [create-time package configuration templates](/ref/values) as they were defined in the original `zarf.yaml` (i.e. untemplated).
4 changes: 2 additions & 2 deletions site/src/content/docs/ref/components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Zarf dynamically generates a Helm Chart from the named manifest entries that you

:::note

Kustomizations are handled a bit differently than normal manifests in that Zarf will automatically run `kustomize build` on them during `zarf package create`, thus rendering the Kustomization into a single manifest file. This prevents needing to grab any remote Kustomization resources during `zarf package deploy` but also means that any Zarf [`variables`](/ref/variables/) will only apply to the rendered manifest not the `kustomize build` process.
Kustomizations are handled a bit differently than normal manifests in that Zarf will automatically run `kustomize build` on them during `zarf package create`, thus rendering the Kustomization into a single manifest file. This prevents needing to grab any remote Kustomization resources during `zarf package deploy` but also means that any Zarf [`variables`](/ref/values/) will only apply to the rendered manifest not the `kustomize build` process.

:::

Expand Down Expand Up @@ -222,7 +222,7 @@ The `import` key in Zarf supports two modes to pull in a component:

:::caution

The import `path` or `url` must be statically defined at create time. You cannot use [package templates](/ref/variables/#create-time-package-configuration-templates) within them.
The import `path` or `url` must be statically defined at create time. You cannot use [package templates](/ref/create/#package-templates) within them.

:::

Expand Down
47 changes: 47 additions & 0 deletions site/src/content/docs/ref/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,50 @@ You do not need to create init configs by yourself unless you want to customize
To deploy a Zarf Package, you can use the command `zarf package deploy`. This will prompt you to select from all of the files in your current directory that match the name `zarf-package-*.tar.zst`. Alternatively, if you already know which package you want to deploy, you can simply use the command `zarf package deploy {PACKAGE_NAME}`.

During the deployment process, Zarf will leverage the infrastructure created during the 'init' process (such as the Docker registry and Git server) to push all the necessary images and repositories required for the package to operate.


### Package Templates

:::caution

`zarf package create` templates only template `###ZARF_PKG_TMPL_*###` entries the `zarf.yaml` file while `zarf package deploy` only templates other `manifests`, `charts`, `files`, and `actions`. To learn more about using deployment values see the [Deployment Values](/ref/values) page.

:::

You can also specify `zarf.yaml` package configuration templates at package create time by including `###_ZARF_PKG_TMPL_*###` as the value for any string-type data in your package definition. These values are discovered during `zarf package create` and will always be prompted for if not using `--confirm` or `--set`. An example of this is below:

```yaml
kind: ZarfPackageConfig
metadata:
name: 'pkg-variables'
description: 'Prompt for a variables during package create'

constants:
- name: PROMPT_IMAGE
value: '###ZARF_PKG_TMPL_PROMPT_ON_CREATE###'

components:
- name: zarf-prompt-image
required: true
images:
- '###ZARF_PKG_TMPL_PROMPT_ON_CREATE###'
```

:::caution

It is not recommended to use package configuration templates for any `sensitive` data as this will be baked into the package as plain text. Please use a deploy-time variable with the `sensitive` key set instead.

:::

:::note

You can only template string values in this way as non-string values will not marshal/unmarshal properly through the yaml.

:::

:::note

You cannot template the component import path using package configuration templates

:::
Racer159 marked this conversation as resolved.
Show resolved Hide resolved

151 changes: 151 additions & 0 deletions site/src/content/docs/ref/values.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
title: Deployment Values
sidebar:
order: 35
---

import Properties from '@components/SchemaItemProperties.astro';
import ExampleYAML from "@components/ExampleYAML.astro";

Deploy time values are used throughout the Zarf deployment process to template `manifests` and `files`, set Helm values, or provide environment variables for `actions`, and can be provided by the `zarf package deploy` user, the `zarf package create` user, or internally by Zarf itself.

## Using Values

### Value Templates

Values can be templated into `manifests`, `files`, and `charts` using a unique `###ZARF_<VALUE_KEY>###` syntax anywhere in the file, where `<VALUE_KEY>` is the name of the value along with any associated prefix in ALL_CAPS (see [Setting Values](#setting-values) below). This allows you to punch through other templating engines to place values precisely where you want in a given file. You can also control how this templating happens when defining a value such as with the `autoIndent` key on `constants` and `variables`. A simple value template for a [Zarf Variable](#variables-zarf_var_) named `DATABASE_USERNAME` would look like the following:

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: db-configmap
data:
username: ###ZARF_VAR_DATABASE_USERNAME###
```

### Helm Chart Mapping

[Zarf Variables](#variables-zarf_var_) can also be mapped directly to Helm values within a given `charts` definition. This is done with the `variables` key within a chart that allows you to take a Zarf Variable `name` and map it to a YAML path within that chart's Helm values. This chart's Helm value will then be set to the current value of the Zarf Variable at the time of it's installation.

```yaml
charts:
- name: wordpress
version: 16.0.4
namespace: wordpress
localPath: chart
variables:
- name: DATABASE_USERNAME
path: db.username
```

<Properties item="ZarfChartVariable" />

:::caution

Chart `variables` in Zarf are currently an `alpha` feature and may be subject to change in later versions of Zarf.

:::

### Environment Variables

Zarf `actions` can also pull values from the shell's environment when running a `cmd`. These values are available under the same `ZARF_<VALUE_KEY>` as value templates (without any `#`s) and can be used like the below:

```yaml
actions:
onDeploy:
after:
- cmd: echo ${ZARF_VAR_DATABASE_USERNAME}
```

## Setting Values

:::note

All value `name` fields must match the regex pattern `^[A-Z0-9_]+$` ([Test](https://regex101.com/r/BG5ZqW/1)).

:::

### Variables (`ZARF_VAR_`)

Variables are dynamic values that are set by the `zarf package deploy` user or internally by the outputs of `actions`. When used they are prefixed with `ZARF_VAR`, and they can be defined with the top-level `variables` key for input by the user:

```yaml
variables:
name: DATABASE_USERNAME
description: 'The username for the database'
```

<Properties item="InteractiveVariable" unwrap={["Variable"]} />

Or can be set within an `actions` `setVariables` key to take the value from the standard output of the given `cmd`:

```yaml
components:
- name: set-variable-example
actions:
onDeploy:
after:
- cmd: echo "username-value"
setVariables:
- name: DATABASE_USERNAME
```

<Properties item="Variable" />

:::note

Variables with `type: file` will be set to the filepath when used in `actions` (see [Environment Variables](#environment-variables) above) due to constraints on the size of environment variables in the shell. This also allows for additional processing of the file by its filename.

:::

:::tip


For user-specified variables, you can specify a `default` value for the variable to take in case a user does not provide one on deploy, and can specify whether to `prompt` the user for the variable when not using the `--confirm` or `--set` flags.

```yaml
variables:
name: DATABASE_USERNAME
default: 'postgres'
prompt: true
```

When not specifying `default`, `prompt`, `sensitive`, `autoIndent`, or `type` Zarf will default to `default: ""`, `prompt: false`, `sensitive: false`, `autoIndent: false`, and `type: "raw"`

:::

### Constants (`ZARF_CONST_`)

Constants are static values that are set by the `zarf package create` user and are used as a way to bake in a common value that the package creator would like to template or use within the deployment process. They are useful to centralize the setting of resources that will be baked into the package (such as image references) to have a singular place to update potentially many downstream references. They are set with a top-level `constants` key as in the below:

```yaml
constants:
name: DATABASE_USERNAME
description: 'The username for the database'
value: 'postgres'
```

<Properties item="Constant" />


### Internal Values (`ZARF_`)

In addition to user supplied Variables, and Constants, Zarf maintains a list of internal variables that components can use for more advanced functionality (such as within [`init` packages](/ref/init-package)). Below are the current values Zarf supports:

- `STORAGE_CLASS`: Storage class specified for persistent storage within the Kubernetes cluster (maps to `--storage-class` on `zarf init`)
- `REGISTRY`: Address of the registry server used during `zarf init` (maps to `--registry-url` on `zarf init`)
- `NODEPORT`: Nodeport used for a registry internal to the cluster (maps to `--nodeport` on `zarf init`)
- `REGISTRY_AUTH_PUSH`: Password for pushing images to the registry (maps to `--registry-push-password` on `zarf init`)
- `REGISTRY_AUTH_PULL`: Password for pulling images from the registry (maps to `--registry-pull-password` on `zarf init`)
- `GIT_PUSH`: Username utilized for pushing changes to the Git server (maps to `--git-push-username` on `zarf init`)
- `GIT_AUTH_PUSH`: Password required for pushing changes to the Git server (maps to `--git-push-password` on `zarf init`)
- `GIT_PULL`: Username employed for pulling changes from the Git server (maps to `--git-pull-username` on `zarf init`)
- `GIT_AUTH_PULL`: Password required for pulling changes from the Git server (maps to `--git-pull-password` on `zarf init`)
- `DATA_INJECTION_MARKER`: The marker used within a `dataInjection` target Pod `spec` that Zarf uses to track a data injection

:::note

Because files can be deployed without a Kubernetes cluster, some built-in values such as `###ZARF_REGISTRY###` may not be available if no previous component has required access to the cluster. If you need one of these built-in values, a prior component will need to have been called that requires access to the cluster, such as `images`, `repos`, `charts`, `manifests`, or `dataInjections`.

:::
Loading
Loading