Skip to content

Commit

Permalink
use fill object template
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Aug 20, 2024
1 parent 2ef66a5 commit 0a00d6d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 47 deletions.
13 changes: 0 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,3 @@ repos:
types: [go]
language: golang
pass_filenames: false
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.14.0
hooks:
- id: check-jsonschema
name: "Validate Zarf Configs Against Schema"
files: "zarf.yaml"
types: [yaml]
args: ["--schemafile", "zarf.schema.json"]
exclude: |
(?x)^(
src/test/packages/12-lint/.*|
src/pkg/lint/testdata/package-with-templates/zarf.yaml
)$
12 changes: 6 additions & 6 deletions src/pkg/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty
node := chain.Head()
for node != nil {
component := node.ZarfComponent
compFindings, err := fillComponentTemplate(&component, createOpts)
compFindings, err := fillObjTemplate(&component, createOpts.SetVariables)
if err != nil {
return nil, err
}
Expand All @@ -87,12 +87,12 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty
return findings, nil
}

func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreateOptions) ([]PackageFinding, error) {
func fillObjTemplate(rawObj any, setVariables map[string]string) ([]PackageFinding, error) {
var findings []PackageFinding
templateMap := map[string]string{}

setVarsAndWarn := func(templatePrefix string, deprecated bool) error {
yamlTemplates, err := utils.FindYamlTemplates(c, templatePrefix, "###")
yamlTemplates, err := utils.FindYamlTemplates(rawObj, templatePrefix, "###")
if err != nil {
return err
}
Expand All @@ -105,7 +105,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat
Severity: SevWarn,
})
}
if _, present := createOpts.SetVariables[key]; !present {
if _, present := setVariables[key]; !present {
unSetTemplates = true
}
}
Expand All @@ -115,7 +115,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat
Severity: SevWarn,
})
}
for key, value := range createOpts.SetVariables {
for key, value := range setVariables {
templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value
}
return nil
Expand All @@ -130,7 +130,7 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat
return nil, err
}

if err := utils.ReloadYamlTemplate(c, templateMap); err != nil {
if err := utils.ReloadYamlTemplate(rawObj, templateMap); err != nil {
return nil, err
}
return findings, nil
Expand Down
12 changes: 5 additions & 7 deletions src/pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ func TestLintComponents(t *testing.T) {
require.Error(t, err)
})
}
func TestFillComponentTemplate(t *testing.T) {
createOpts := types.ZarfCreateOptions{
SetVariables: map[string]string{
"KEY1": "value1",
"KEY2": "value2",
},
func TestFillObjTemplate(t *testing.T) {
SetVariables := map[string]string{
"KEY1": "value1",
"KEY2": "value2",
}

component := v1alpha1.ZarfComponent{
Expand All @@ -48,7 +46,7 @@ func TestFillComponentTemplate(t *testing.T) {
},
}

findings, err := fillComponentTemplate(&component, createOpts)
findings, err := fillObjTemplate(&component, SetVariables)
require.NoError(t, err)
expectedFindings := []PackageFinding{
{
Expand Down
20 changes: 1 addition & 19 deletions src/pkg/lint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"regexp"

"github.com/xeipuuv/gojsonschema"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/utils"
)
Expand All @@ -30,31 +29,14 @@ func ValidatePackageSchema(setVariables map[string]string) ([]PackageFinding, er
return nil, err
}

untypedZarfPackage, err = templateRawYaml(untypedZarfPackage, setVariables)
_, err = fillObjTemplate(&untypedZarfPackage, setVariables)
if err != nil {
return nil, err
}

return getSchemaFindings(jsonSchema, untypedZarfPackage)
}

func templateRawYaml(untypedPackage any, values map[string]string) (any, error) {
templateMap := map[string]string{}

for key, value := range values {
templateMap[fmt.Sprintf("%s%s###", v1alpha1.ZarfPackageVariablePrefix, key)] = value
}

for key, value := range values {
templateMap[fmt.Sprintf("%s%s###", v1alpha1.ZarfPackageTemplatePrefix, key)] = value
}

if err := utils.ReloadYamlTemplate(&untypedPackage, templateMap); err != nil {
return nil, err
}
return untypedPackage, nil
}

func makeFieldPathYqCompat(field string) string {
if field == "(root)" {
return field
Expand Down
20 changes: 20 additions & 0 deletions src/pkg/lint/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package lint
import (
"fmt"
"os"
"path/filepath"
"testing"

goyaml "github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/test/testutil"
)

func TestZarfSchema(t *testing.T) {
Expand Down Expand Up @@ -190,6 +192,24 @@ components:
})
}

func TestValidatePackageSchema(t *testing.T) {
ZarfSchema = testutil.LoadSchema(t, "../../../zarf.schema.json")
setVariables := map[string]string{
"PACKAGE_NAME": "test-package",
"MY_COMP_NAME": "test-comp",
}
cwd, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(filepath.Join("testdata", "package-with-templates"))
require.NoError(t, err)
defer func() {
require.NoError(t, os.Chdir(cwd))
}()
findings, err := ValidatePackageSchema(setVariables)
require.Empty(t, findings)
require.NoError(t, err)
}

func TestYqCompat(t *testing.T) {
t.Parallel()
t.Run("Wrap standalone numbers in bracket", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/testdata/package-with-templates/zarf.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kind: ZarfPackageConfig
metadata:
name: "###ZARF_PKG_TMPL_PACKAGE_NAME###"
name: "###ZARF_PKG_VAR_PACKAGE_NAME###"
components:
- name: ok
- name: "###ZARF_PKG_TMPL_MY_COMP_NAME###"

0 comments on commit 0a00d6d

Please sign in to comment.