Skip to content

Commit

Permalink
move validate interface out
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinAbro321 committed Jul 10, 2024
1 parent 45f9e36 commit d11dfa9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/pkg/packager/creator/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
// Creator is an interface for creating Zarf packages.
type Creator interface {
LoadPackageDefinition(ctx context.Context, src *layout.PackagePaths) (pkg types.ZarfPackage, warnings []string, err error)
Validate(ctx context.Context, pkg types.ZarfPackage) error
Assemble(ctx context.Context, dst *layout.PackagePaths, components []types.ZarfComponent, arch string) error
Output(ctx context.Context, dst *layout.PackagePaths, pkg *types.ZarfPackage) error
}
9 changes: 3 additions & 6 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/kustomize"
"github.com/defenseunicorns/zarf/src/internal/packager/sbom"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/lint"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/actions"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
Expand Down Expand Up @@ -118,18 +119,14 @@ func (pc *PackageCreator) LoadPackageDefinition(ctx context.Context, src *layout
}
}

if err := pc.Validate(ctx, pkg); err != nil {
if findings, err := Validate(pkg); err != nil {
lint.PrintFindings(findings, lint.SevErr, pc.createOpts.BaseDir, pkg.Metadata.Name)
return types.ZarfPackage{}, nil, err
}

return pkg, warnings, nil
}

// Validate ensures that the package is valid
func (pc *PackageCreator) Validate(_ context.Context, pkg types.ZarfPackage) error {
return validate(pc.createOpts, pkg)
}

// Assemble assembles all of the package assets into Zarf's tmp directory layout.
func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths, components []types.ZarfComponent, arch string) error {
var imageList []transform.Image
Expand Down
9 changes: 3 additions & 6 deletions src/pkg/packager/creator/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/helm"
"github.com/defenseunicorns/zarf/src/internal/packager/kustomize"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/lint"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/zoci"
Expand Down Expand Up @@ -70,18 +71,14 @@ func (sc *SkeletonCreator) LoadPackageDefinition(ctx context.Context, src *layou
message.Warn(warning)
}

if err := sc.Validate(ctx, pkg); err != nil {
if findings, err := Validate(pkg); err != nil {
lint.PrintFindings(findings, lint.SevErr, sc.createOpts.BaseDir, pkg.Metadata.Name)
return types.ZarfPackage{}, nil, err
}

return pkg, warnings, nil
}

// Validate ensures that the package is valid
func (sc *SkeletonCreator) Validate(_ context.Context, pkg types.ZarfPackage) error {
return validate(sc.createOpts, pkg)
}

// Assemble updates all components of the loaded Zarf package with necessary modifications for package assembly.
//
// It processes each component to ensure correct structure and resource locations.
Expand Down
13 changes: 7 additions & 6 deletions src/pkg/packager/creator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ import (
"github.com/defenseunicorns/zarf/src/types"
)

func validate(createOpts types.ZarfCreateOptions, pkg types.ZarfPackage) error {
// Validate errors if a package violates the schema or any runtime validations
// This must be run from the directory of the Zarf.yaml validations are occurring on
func Validate(pkg types.ZarfPackage) ([]lint.PackageFinding, error) {
if err := pkg.Validate(); err != nil {
return fmt.Errorf("package validation failed: %w", err)
return nil, fmt.Errorf("package validation failed: %w", err)
}

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

lint.PrintFindings(findings, lint.SevErr, createOpts.BaseDir, pkg.Metadata.Name)
if lint.HasSeverity(findings, lint.SevErr) {
return fmt.Errorf("found errors in package")
return findings, fmt.Errorf("found errors in package")
}

return nil
return nil, nil
}

// recordPackageMetadata records various package metadata during package create.
Expand Down
4 changes: 3 additions & 1 deletion src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/lint"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/creator"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
Expand Down Expand Up @@ -53,7 +54,8 @@ func (p *Packager) DevDeploy(ctx context.Context) error {
return err
}

if err := pc.Validate(ctx, p.cfg.Pkg); err != nil {
if findings, err := creator.Validate(p.cfg.Pkg); err != nil {
lint.PrintFindings(findings, lint.SevErr, p.cfg.CreateOpts.BaseDir, p.cfg.Pkg.Metadata.Name)
return fmt.Errorf("package validation failed: %w", err)
}

Expand Down

0 comments on commit d11dfa9

Please sign in to comment.