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

feat: import all vars exported from package #428

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ packages:
package: output-var
```

Variables that you want to make available to other package are in the `export` block of the Zarf package to export a variable from. To have another package ingest an exported variable, use the `imports` key to name both the `variable` and `package` that the variable is exported from.
Variables that you want to make available to other package are in the `export` block of the Zarf package to export a variable from. To have another package ingest an exported variable, use the `imports` key to name both the `variable` and `package` that the variable is exported from. You can also import all of the variables for a package by omitting the variable name and just providing the package name.

In the example above, the `OUTPUT` variable is created as part of a Zarf Action in the [output-var](src/test/packages/zarf/no-cluster/output-var) package, and the [receive-var](src/test/packages/zarf/no-cluster/receive-var) package expects a variable called `OUTPUT`.

Expand Down
18 changes: 15 additions & 3 deletions src/pkg/bundle/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,22 @@ func validateBundleVars(packages []types.Package) error {
// ensure imports have a matching export
if pkg.Imports != nil {
for _, v := range pkg.Imports {
if _, ok := exports[v.Name]; ok && v.Package == exports[v.Name] {
continue
// check import names if they are set, otherwise importing all the variables for the given package
decleaver marked this conversation as resolved.
Show resolved Hide resolved
if v.Name != "" {
if _, ok := exports[v.Name]; ok && v.Package == exports[v.Name] {
continue
}
return fmt.Errorf("import var %s does not have a matching export", v.Name)
// Check that the import package is valid
} else {
for _, pkgName := range exports {
if pkgName == v.Package {
break
} else {
return fmt.Errorf("import package %s does not match any exporting package", v.Package)
}
}
}
return fmt.Errorf("import var %s does not have a matching export", v.Name)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ func (b *Bundle) loadVariables(pkg types.Package, bundleExportedVars map[string]
// Set variables in order or precendence (least specific to most specific)
// imported vars
for _, imp := range pkg.Imports {
if imp.Name == "" {
for key, value := range bundleExportedVars[imp.Package] {
pkgVars[strings.ToUpper(key)] = value
}
}
pkgVars[strings.ToUpper(imp.Name)] = bundleExportedVars[imp.Package][imp.Name]
}
// shared vars
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
kind: UDSBundle
metadata:
name: import-all-bad-name
description: show how import all works
version: 0.0.1

packages:
- name: output-var
repository: localhost:888/output-var
ref: 0.0.1
exports:
- name: OUTPUT
- name: PRECEDENCE

- name: receive-var
repository: localhost:888/receive-var
ref: 0.0.1
imports:
- package: output-varz
19 changes: 19 additions & 0 deletions src/test/bundles/02-simple-vars/import-all/uds-bundle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
kind: UDSBundle
metadata:
name: import-all
description: show how import all works
decleaver marked this conversation as resolved.
Show resolved Hide resolved
version: 0.0.1

packages:
- name: output-var
repository: localhost:888/output-var
ref: 0.0.1
exports:
- name: OUTPUT
- name: PRECEDENCE

- name: receive-var
repository: localhost:888/receive-var
ref: 0.0.1
imports:
- package: output-var
6 changes: 6 additions & 0 deletions src/test/e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func createLocal(t *testing.T, bundlePath string, arch string) {
require.NoError(t, err)
}

func createLocalError(t *testing.T, bundlePath string, arch string) (stderr string) {
cmd := strings.Split(fmt.Sprintf("create %s --insecure --confirm -a %s", bundlePath, arch), " ")
_, stderr, _ = e2e.UDS(cmd...)
return stderr
}

func createRemoteInsecure(t *testing.T, bundlePath, registry, arch string) {
cmd := strings.Split(fmt.Sprintf("create %s -o %s --confirm --insecure -a %s", bundlePath, registry, arch), " ")
_, _, err := e2e.UDS(cmd...)
Expand Down
18 changes: 18 additions & 0 deletions src/test/e2e/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,25 @@ func TestBundleVariables(t *testing.T) {
os.Setenv("UDS_CONFIG", filepath.Join("src/test/bundles/02-simple-vars", "uds-config.yaml"))

_, stderr := deploy(t, bundleTarballPath)
bunleVariablesTestChecks(t, stderr, bundleTarballPath)
remove(t, bundleTarballPath)

// Run same test checks but with package that is importing all of the exported variables by just providing the packageName in the import
bundleDir = "src/test/bundles/02-simple-vars/import-all"
bundleTarballPath = filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-import-all-%s-0.0.1.tar.zst", e2e.Arch))
createLocal(t, bundleDir, e2e.Arch)
createRemoteInsecure(t, bundleDir, "localhost:888", e2e.Arch)
decleaver marked this conversation as resolved.
Show resolved Hide resolved
_, stderr = deploy(t, bundleTarballPath)
bunleVariablesTestChecks(t, stderr, bundleTarballPath)

// Test with bad package name in import
bundleDir = "src/test/bundles/02-simple-vars/import-all-bad-name"
// bundleTarballPath = filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-import-all-bad-name-%s-0.0.1.tar.zst", e2e.Arch))
stderr = createLocalError(t, bundleDir, e2e.Arch)
require.Contains(t, stderr, "does not match any exporting package")
}

func bunleVariablesTestChecks(t *testing.T, stderr string, bundleTarballPath string) {
require.NotContains(t, stderr, "CLIVersion is set to 'unset' which can cause issues with package creation and deployment")
require.Contains(t, stderr, "This fun-fact was imported: Unicorns are the national animal of Scotland")
require.Contains(t, stderr, "This fun-fact demonstrates precedence: The Red Dragon is the national symbol of Wales")
Expand Down
2 changes: 1 addition & 1 deletion src/types/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type BundleChartVariable struct {

// BundleVariableImport represents variables in the bundle
type BundleVariableImport struct {
Name string `json:"name" jsonschema:"name=Name of the variable"`
Name string `json:"name,omitempty" jsonschema:"name=Name of the variable"`
Package string `json:"package" jsonschema:"name=Name of the Zarf package to get the variable from"`
Description string `json:"description,omitempty" jsonschema:"name=Description of the variable"`
}
Expand Down
1 change: 0 additions & 1 deletion uds.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
},
"BundleVariableImport": {
"required": [
"name",
"package"
],
"properties": {
Expand Down
Loading