Skip to content

Commit

Permalink
1888 - Template Component Fields into Components on Zarf.yaml (#1923)
Browse files Browse the repository at this point in the history
## Description

This PR allows for templating component name during package create and
give the ability to template with more fields in the component struct.

While the PR is roughly similar to
[FindYamlTemplates](https://github.com/defenseunicorns/zarf/blob/02150cfda1a32d5c6ebb4c446d731dc217e3961e/src/pkg/utils/yaml.go#L164)
there is extra complexity in components that required a new function
[FindComponentTemplatesAndReload](https://github.com/defenseunicorns/zarf/blob/9b3e396abc39a395ceb7999565bfb09088cd39c5/src/pkg/utils/yaml.go#L166).
Each component may have a different name that the string map would
overwrite. Due to this nature, the straightforward path was a new
function to mutate in place, FindComponentTemplatesAndReload. It
operates on the ZarfPackage object passed in by interface by iterating
over the Components and mutating by updating the templated files. This
opens up the possibility to template more fields in components rather
than just [component
names](https://github.com/defenseunicorns/zarf/blob/9b3e396abc39a395ceb7999565bfb09088cd39c5/src/pkg/utils/yaml.go#L186).

Currently there is a place-holder example of
[Component-Name-Template](https://github.com/defenseunicorns/zarf/blob/1888/examples/component-name-template/zarf.yaml)
which will need to be changed before removing draft status from PR.

This PR is in a holding pattern while waiting from feedback from
@Racer159


## Related Issue

Fixes 
<!-- or -->
Relates to #1888

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow)
followed

---------

Signed-off-by: Case Wylie <cmwylie19@defenseunicorns.com>
Co-authored-by: razzle <harry@razzle.cloud>
  • Loading branch information
cmwylie19 and Noxsios authored Aug 2, 2023
1 parent b6611b3 commit aa5f541
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/pkg/packager/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
Expand All @@ -31,7 +30,7 @@ func (p *Packager) fillActiveTemplate() error {

for key := range yamlTemplates {
if deprecated {
message.Warnf(lang.PkgValidateTemplateDeprecation, key, key, key)
p.warnings = append(p.warnings, fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key))
}

_, present := setFromCLIConfig[key]
Expand All @@ -57,6 +56,12 @@ func (p *Packager) fillActiveTemplate() error {
return nil
}

// update the component templates on the package
err := p.findComponentTemplatesAndReload(&p.cfg.Pkg)
if err != nil {
return err
}

if err := promptAndSetTemplate("###ZARF_PKG_TMPL_", false); err != nil {
return err
}
Expand Down Expand Up @@ -146,3 +151,19 @@ func (p *Packager) injectImportedConstant(importedConstant types.ZarfPackageCons
p.cfg.Pkg.Constants = append(p.cfg.Pkg.Constants, importedConstant)
}
}

// findComponentTemplatesAndReload appends ###ZARF_COMPONENT_NAME### for each component, assigns value, and reloads
func (p *Packager) findComponentTemplatesAndReload(config any) error {

// iterate through components to and find all ###ZARF_COMPONENT_NAME, assign to component Name and value
for i, component := range config.(*types.ZarfPackage).Components {
mappings := map[string]string{}
mappings["###ZARF_COMPONENT_NAME###"] = component.Name
err := utils.ReloadYamlTemplate(&config.(*types.ZarfPackage).Components[i], mappings)
if err != nil {
return err
}
}

return nil
}
27 changes: 27 additions & 0 deletions src/test/e2e/51_oci_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,33 @@ func (suite *SkeletonSuite) Test_1_Compose() {
suite.NoError(err)
}

func (suite *SkeletonSuite) Test_2_Component_Templates() {
suite.T().Log("E2E: Component Templates")
e2e.SetupWithCluster(suite.T())
importEverythingPath := fmt.Sprintf("build/zarf-package-import-everything-%s-0.0.1.tar.zst", e2e.Arch)

_, stdErr, err := e2e.Zarf("package", "inspect", importEverythingPath)
suite.NoError(err)

targets := []string{
"import-component-local == import-component-local",
"import-component-local-relative == import-component-local-relative",
"import-component-wordpress == import-component-wordpress",
"import-component-oci == import-component-oci",
"file-imports == file-imports",
"import-helm-local == import-helm-local",
"import-helm-local-relative == import-helm-local-relative",
"import-helm-oci == import-helm-oci",
"import-repos == import-repos",
"import-images == import-images",
}

for _, target := range targets {
suite.Contains(stdErr, target)
}

}

func (suite *SkeletonSuite) Test_3_FilePaths() {
suite.T().Log("E2E: Skeleton Package File Paths")

Expand Down
10 changes: 10 additions & 0 deletions src/test/packages/51-import-everything/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ metadata:

components:
- name: import-component-local
description: "import-component-local == ###ZARF_COMPONENT_NAME###"
required: false
import:
path: foo
name: baz

- name: import-component-local-relative
description: "import-component-local-relative == ###ZARF_COMPONENT_NAME###"
required: false
import:
path: ../../../../examples/dos-games
Expand All @@ -22,19 +24,22 @@ components:
- files

- name: import-component-wordpress
description: "import-component-wordpress == ###ZARF_COMPONENT_NAME###"
required: false
import:
path: ../../../../examples/wordpress
name: wordpress

- name: import-component-oci
description: "import-component-oci == ###ZARF_COMPONENT_NAME###"
required: false
import:
# name is optional, if not provided the name of the component will be used to import
name: demo-helm-local-chart
url: oci://localhost:555/helm-charts:0.0.1-skeleton

- name: file-imports
description: "file-imports == ###ZARF_COMPONENT_NAME###"
required: false
cosignKeyPath: ../../../../cosign.pub
files:
Expand Down Expand Up @@ -64,6 +69,7 @@ components:
- cmd: test ! -f files/zarf-readme.md

- name: import-helm-local
description: "import-helm-local == ###ZARF_COMPONENT_NAME###"
required: false
charts:
- name: podinfo
Expand All @@ -83,6 +89,7 @@ components:
condition: available

- name: import-helm-local-relative
description: "import-helm-local-relative == ###ZARF_COMPONENT_NAME###"
required: false
charts:
- name: podinfo
Expand All @@ -102,6 +109,7 @@ components:
condition: available

- name: import-helm-oci
description: "import-helm-oci == ###ZARF_COMPONENT_NAME###"
required: false
charts:
- name: oci-demo
Expand All @@ -128,6 +136,7 @@ components:
condition: ready

- name: import-repos
description: "import-repos == ###ZARF_COMPONENT_NAME###"
required: false
repos:
# Import a full repo via HTTPS
Expand All @@ -136,6 +145,7 @@ components:
- file:///tmp/nocode

- name: import-images
description: "import-images == ###ZARF_COMPONENT_NAME###"
required: false
images:
- ghcr.io/stefanprodan/podinfo:6.4.0

0 comments on commit aa5f541

Please sign in to comment.