-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom Zarf variables in k8s resources (#474)
Co-authored-by: Jon Perry <[email protected]>
- Loading branch information
1 parent
e90fa27
commit 2c08a35
Showing
11 changed files
with
125 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,19 @@ | ||
# Component Variables | ||
|
||
This example demonstrates how to define variables in your component that will be templated across the manifests and charts that your component uses. | ||
|
||
With the templating feature, you can define values in the zarf.yaml file without having to define them in every manifest and chart. | ||
This becomes really useful when you are working with an upstream chart that is often changing, or a lot of the charts you use have slightly different conventions for their values. Now you can standardize all that from your zarf.yaml file. | ||
|
||
| ||
|
||
## How to Use Component Variables | ||
The 'placeholder' text in the yaml should have you desired key name in all caps with `###ZARF_` prepended and `###` appened at the end. | ||
|
||
For example, if I wanted to create a template for a database username I would do something like `###ZARF_DATABASE_USERNAME###` in the yaml. | ||
|
||
In the zarf.yaml you would add the key-value pair in the variables section. For that same example as above, I would have (note that we don't include the `###ZARF_` and `###` and the beginning and end): | ||
```yaml | ||
variables: | ||
database_username: "iamdata" | ||
``` |
This file contains 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,12 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: simple-configmap | ||
namespace: zarf | ||
data: | ||
templateme.properties: | | ||
horse=neigh | ||
dog=###ZARF_DOG### | ||
cat=###ZARF_CAT### | ||
cow=moo | ||
zebra=###ZARF_ZEBRA### |
This file contains 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,16 @@ | ||
kind: ZarfPackageConfig | ||
metadata: | ||
name: component-variables | ||
description: "Test component to demonstrate script timeout feature" | ||
|
||
components: | ||
# Demonstrates injecting custom variables into a K8s resource, e.g. ###ZARF_DOG### | ||
- name: variable-example | ||
required: true | ||
variables: | ||
dog: "woof" | ||
cat: "meow" | ||
manifests: | ||
- name: variable-example-configmap | ||
files: | ||
- simple-configmap.yaml |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,31 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestE2eComponentVariables(t *testing.T) { | ||
defer e2e.cleanupAfterTest(t) | ||
|
||
path := fmt.Sprintf("../../../build/zarf-package-component-variables-%s.tar.zst", e2e.arch) | ||
|
||
//run `zarf init` | ||
output, err := e2e.execZarfCommand("init", "--confirm") | ||
require.NoError(t, err, output) | ||
|
||
// Deploy the simple configmap | ||
output, err = e2e.execZarfCommand("package", "deploy", path, "--confirm") | ||
require.NoError(t, err, output) | ||
|
||
// Verify the configmap was properly templated | ||
kubectlOut, _ := exec.Command("kubectl", "-n", "zarf", "get", "configmap", "simple-configmap", "-o", "jsonpath='{.data.templateme\\.properties}' ").Output() | ||
assert.Contains(t, string(kubectlOut), "dog=woof") | ||
assert.Contains(t, string(kubectlOut), "cat=meow") | ||
// zebra should remain unset as it is not a component variable | ||
assert.Contains(t, string(kubectlOut), "zebra=###ZARF_ZEBRA###") | ||
} |
This file contains 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
This file contains 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