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 validate to lint #2839

Merged
merged 46 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
2d57404
switching json description to go comments
AustinAbro321 Jul 29, 2024
e466577
switching json description to go comments
AustinAbro321 Jul 29, 2024
a54b4d6
switching json description to go comments
AustinAbro321 Jul 29, 2024
d746067
switching json description to go comments
AustinAbro321 Jul 29, 2024
7e16860
make gen types work again
AustinAbro321 Jul 29, 2024
8dfe1a5
wording
AustinAbro321 Jul 29, 2024
be388f8
add json tags back
AustinAbro321 Jul 29, 2024
59bc21f
add back json tags
AustinAbro321 Jul 29, 2024
cde411a
inline
AustinAbro321 Jul 29, 2024
8f2d3ec
Merge branch 'main' into utilize-invopop-features
AustinAbro321 Jul 30, 2024
fd680fc
back to omitempty
AustinAbro321 Jul 30, 2024
cbc25f2
add back test
AustinAbro321 Jul 30, 2024
da9ea48
removing json options
AustinAbro321 Jul 30, 2024
e8a11ac
add back json data
AustinAbro321 Jul 30, 2024
de0bd7c
bb required
AustinAbro321 Jul 30, 2024
96d703d
de-dup
AustinAbro321 Jul 30, 2024
380d2e7
add period to json descriptions
AustinAbro321 Jul 30, 2024
5e91b84
use more invopop features
AustinAbro321 Jul 30, 2024
4623784
merge
AustinAbro321 Jul 31, 2024
457a23e
move v1alpha1 to it's own package
AustinAbro321 Jul 31, 2024
4094292
Merge remote-tracking branch 'origin/use-more-invopop-features' into …
AustinAbro321 Jul 31, 2024
8efabbb
Merge branch 'main' into add-api-version
AustinAbro321 Jul 31, 2024
cd3f497
adding go mod to api package
AustinAbro321 Jul 31, 2024
d16a7d7
move bigbang over
AustinAbro321 Jul 31, 2024
2716d41
avoid introducing dependencies
AustinAbro321 Jul 31, 2024
648bbf0
header title
AustinAbro321 Jul 31, 2024
df9c182
package
AustinAbro321 Jul 31, 2024
d1a934d
Merge branch 'main' into add-api-version
AustinAbro321 Jul 31, 2024
2bddac1
update go mod
AustinAbro321 Aug 1, 2024
c955d6f
move validate to lint
AustinAbro321 Aug 1, 2024
9b4fbe9
moving validate to lint and separating out packages
AustinAbro321 Aug 5, 2024
35a1dfc
merge
AustinAbro321 Aug 7, 2024
3a9f928
merge
AustinAbro321 Aug 7, 2024
b1ce576
move validate to lint
AustinAbro321 Aug 7, 2024
7d017ab
merge
AustinAbro321 Aug 8, 2024
1d307e2
move lint back
AustinAbro321 Aug 8, 2024
16dead0
call validate component
AustinAbro321 Aug 8, 2024
9d62adf
unexport validate functions
AustinAbro321 Aug 8, 2024
9fface6
merge
AustinAbro321 Aug 12, 2024
9c4d24b
re-delete file
AustinAbro321 Aug 12, 2024
759414e
static check
AustinAbro321 Aug 12, 2024
a881426
remove unnecessary lint checks
AustinAbro321 Aug 12, 2024
265df7c
move validate compose code to compose
AustinAbro321 Aug 12, 2024
e831b01
move compose check to compose
AustinAbro321 Aug 12, 2024
e704281
Merge branch 'main' into move-validate-to-lint
AustinAbro321 Aug 12, 2024
89fe7cd
Merge branch 'main' into move-validate-to-lint
schristoff Aug 12, 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 main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/zarf-dev/zarf/src/cmd"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/rules"
)

//go:embed cosign.pub
Expand Down Expand Up @@ -41,6 +41,6 @@ func main() {
}()

config.CosignPublicKey = cosignPublicKey
lint.ZarfSchema = zarfSchema
rules.ZarfSchema = zarfSchema
cmd.Execute(ctx)
}
8 changes: 8 additions & 0 deletions src/api/v1alpha1/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"github.com/zarf-dev/zarf/src/pkg/variables"
)

// Zarf looks for these strings in zarf.yaml to make dynamic changes
const (
ZarfPackageTemplatePrefix = "###ZARF_PKG_TMPL_"
ZarfPackageVariablePrefix = "###ZARF_PKG_VAR_"
ZarfPackageArch = "###ZARF_PKG_ARCH###"
ZarfComponentName = "###ZARF_COMPONENT_NAME###"
)

// ZarfPackageKind is an enum of the different kinds of Zarf packages.
type ZarfPackageKind string

Expand Down
27 changes: 14 additions & 13 deletions src/pkg/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager/composer"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
)

// Validate lints the given Zarf package
func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error {
var findings []PackageFinding
var findings []rules.PackageFinding
if err := os.Chdir(createOpts.BaseDir); err != nil {
return fmt.Errorf("unable to access directory %q: %w", createOpts.BaseDir, err)
}
Expand All @@ -36,7 +37,7 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error {
return err
}
findings = append(findings, compFindings...)
schemaFindings, err := ValidatePackageSchema()
schemaFindings, err := rules.ValidatePackageSchema()
if err != nil {
return err
}
Expand All @@ -46,15 +47,15 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error {
message.Successf("0 findings for %q", pkg.Metadata.Name)
return nil
}
PrintFindings(findings, SevWarn, createOpts.BaseDir, pkg.Metadata.Name)
if HasSevOrHigher(findings, SevErr) {
rules.PrintFindings(findings, rules.SevWarn, createOpts.BaseDir, pkg.Metadata.Name)
if rules.HasSevOrHigher(findings, rules.SevErr) {
return errors.New("errors during lint")
}
return nil
}

func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts types.ZarfCreateOptions) ([]PackageFinding, error) {
var findings []PackageFinding
func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts types.ZarfCreateOptions) ([]rules.PackageFinding, error) {
var findings []rules.PackageFinding

for i, component := range pkg.Components {
arch := config.GetArch(pkg.Metadata.Architecture)
Expand All @@ -75,7 +76,7 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty
if err != nil {
return nil, err
}
compFindings = append(compFindings, CheckComponentValues(component, node.Index())...)
compFindings = append(compFindings, rules.CheckComponentValues(component, node.Index())...)
for i := range compFindings {
compFindings[i].PackagePathOverride = node.ImportLocation()
compFindings[i].PackageNameOverride = node.OriginalPackageName()
Expand All @@ -87,8 +88,8 @@ func lintComponents(ctx context.Context, pkg v1alpha1.ZarfPackage, createOpts ty
return findings, nil
}

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

setVarsAndWarn := func(templatePrefix string, deprecated bool) error {
Expand All @@ -100,19 +101,19 @@ func fillComponentTemplate(c *v1alpha1.ZarfComponent, createOpts types.ZarfCreat
var unSetTemplates bool
for key := range yamlTemplates {
if deprecated {
findings = append(findings, PackageFinding{
findings = append(findings, rules.PackageFinding{
Description: fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key),
Severity: SevWarn,
Severity: rules.SevWarn,
})
}
if _, present := createOpts.SetVariables[key]; !present {
unSetTemplates = true
}
}
if unSetTemplates {
findings = append(findings, PackageFinding{
findings = append(findings, rules.PackageFinding{
Description: lang.UnsetVarLintWarning,
Severity: SevWarn,
Severity: rules.SevWarn,
})
}
for key, value := range createOpts.SetVariables {
Expand Down
7 changes: 4 additions & 3 deletions src/pkg/lint/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/types"
)

Expand Down Expand Up @@ -50,13 +51,13 @@ func TestFillComponentTemplate(t *testing.T) {

findings, err := fillComponentTemplate(&component, createOpts)
require.NoError(t, err)
expectedFindings := []PackageFinding{
expectedFindings := []rules.PackageFinding{
{
Severity: SevWarn,
Severity: rules.SevWarn,
Description: "There are templates that are not set and won't be evaluated during lint",
},
{
Severity: SevWarn,
Severity: rules.SevWarn,
Description: fmt.Sprintf(lang.PkgValidateTemplateDeprecation, "KEY2", "KEY2", "KEY2"),
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/zarf-dev/zarf/src/extensions/bigbang"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/packager/deprecated"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/pkg/variables"
"github.com/zarf-dev/zarf/src/pkg/zoci"
Expand Down Expand Up @@ -141,8 +142,7 @@ func NewImportChain(ctx context.Context, head v1alpha1.ZarfComponent, index int,
return ic, nil
}

// TODO: stuff like this should also happen in linting
if err := node.ZarfComponent.Validate(); err != nil {
if err := rules.ValidateComponent(node.ZarfComponent); err != nil {
return ic, err
}

Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/types"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func TestLoadPackageDefinition(t *testing.T) {
}
b, err := os.ReadFile("../../../../zarf.schema.json")
require.NoError(t, err)
lint.ZarfSchema = &mockSchemaLoader{b: b}
rules.ZarfSchema = &mockSchemaLoader{b: b}

for _, tt := range tests {
tt := tt
Expand Down
10 changes: 5 additions & 5 deletions src/pkg/packager/creator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import (

"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/packager/deprecated"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/types"
)

// Validate errors if a package violates the schema or any runtime validations
// This must be run while in the parent directory of the zarf.yaml being validated
func Validate(pkg v1alpha1.ZarfPackage, baseDir string) error {
if err := pkg.Validate(); err != nil {
if err := rules.ValidatePackage(pkg); err != nil {
return fmt.Errorf("package validation failed: %w", err)
}

findings, err := lint.ValidatePackageSchema()
findings, err := rules.ValidatePackageSchema()
if err != nil {
return fmt.Errorf("unable to check schema: %w", err)
}

if lint.HasSevOrHigher(findings, lint.SevErr) {
lint.PrintFindings(findings, lint.SevErr, baseDir, pkg.Metadata.Name)
if rules.HasSevOrHigher(findings, rules.SevErr) {
rules.PrintFindings(findings, rules.SevErr, baseDir, pkg.Metadata.Name)
return fmt.Errorf("found errors in schema")
}

Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/filters/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (

"github.com/stretchr/testify/require"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/pkg/rules"
)

func TestLocalOSFilter(t *testing.T) {
pkg := v1alpha1.ZarfPackage{}
for _, os := range v1alpha1.SupportedOS() {
for _, os := range rules.SupportedOS() {
pkg.Components = append(pkg.Components, v1alpha1.ZarfComponent{
Only: v1alpha1.ZarfComponentOnlyTarget{
LocalOS: os,
},
})
}

for _, os := range v1alpha1.SupportedOS() {
for _, os := range rules.SupportedOS() {
filter := ByLocalOS(os)
result, err := filter.Apply(pkg)
if os == "" {
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/packager/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/rules"
)

// Generate generates a Zarf package definition.
Expand Down Expand Up @@ -73,7 +74,7 @@ func (p *Packager) Generate(ctx context.Context) (err error) {
p.cfg.Pkg.Components[i].Images = images[name]
}

if err := p.cfg.Pkg.Validate(); err != nil {
if err := rules.ValidatePackage(p.cfg.Pkg); err != nil {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion src/pkg/packager/sources/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/layout"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/rules"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
)
Expand All @@ -24,7 +25,7 @@ var (

// NewClusterSource creates a new cluster source.
func NewClusterSource(pkgOpts *types.ZarfPackageOptions) (PackageSource, error) {
if !v1alpha1.IsLowercaseNumberHyphenNoStartHyphen(pkgOpts.PackageSource) {
if !rules.IsLowercaseNumberHyphenNoStartHyphen(pkgOpts.PackageSource) {
return nil, fmt.Errorf("invalid package name %q", pkgOpts.PackageSource)
}

Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/findings.go → src/pkg/rules/findings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/rules.go → src/pkg/rules/rules.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/rules_test.go → src/pkg/rules/rules_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"errors"
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/schema.go → src/pkg/rules/schema.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/lint/schema_test.go → src/pkg/rules/schema_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package lint contains functions for verifying zarf yaml files are valid
package lint
// Package rules checks Zarf packages and reports any findings or errors
package rules

import (
"fmt"
Expand Down
Loading
Loading