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 expose it as receivers #2419

Merged
merged 7 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 1 addition & 2 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/deprecated"
"github.com/defenseunicorns/zarf/src/pkg/utils"
Expand Down Expand Up @@ -142,7 +141,7 @@ func NewImportChain(head types.ZarfComponent, index int, originalPackageName, ar
}

// TODO: stuff like this should also happen in linting
if err := validate.ImportDefinition(&node.ZarfComponent); err != nil {
if err := node.ZarfComponent.ValidateImportDefinition(); err != nil {
return ic, err
}

Expand Down
3 changes: 1 addition & 2 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/creator"
Expand Down Expand Up @@ -41,7 +40,7 @@ func (p *Packager) Create() (err error) {
}

// Perform early package validation.
if err := validate.Run(p.cfg.Pkg); err != nil {
if err := p.cfg.Pkg.Validate(); err != nil {
return fmt.Errorf("unable to validate package: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/creator"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (p *Packager) DevDeploy() error {
return err
}

if err := validate.Run(p.cfg.Pkg); err != nil {
if err := p.cfg.Pkg.Validate(); err != nil {
return fmt.Errorf("unable to validate package: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/filters/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent,

selectState, matchedRequest := includedOrExcluded(component.Name, f.requestedComponents)

if !isRequired(component) {
if !component.IsRequired() {
if selectState == excluded {
// If the component was explicitly excluded, record the match and continue
matchedRequests[matchedRequest] = true
Expand Down Expand Up @@ -161,7 +161,7 @@ func (f *deploymentFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent,
} else {
component := groupedComponents[groupKey][0]

if isRequired(component) {
if component.IsRequired() {
selectedComponents = append(selectedComponents, component)
continue
}
Expand Down
5 changes: 2 additions & 3 deletions src/pkg/packager/filters/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ package filters
import (
"testing"

"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
)

func TestLocalOSFilter(t *testing.T) {

pkg := types.ZarfPackage{}
for _, os := range validate.SupportedOS() {
for _, os := range types.SupportedOS() {
pkg.Components = append(pkg.Components, types.ZarfComponent{
Only: types.ZarfComponentOnlyTarget{
LocalOS: os,
},
})
}

for _, os := range validate.SupportedOS() {
for _, os := range types.SupportedOS() {
filter := ByLocalOS(os)
result, err := filter.Apply(pkg)
if os == "" {
Expand Down
13 changes: 0 additions & 13 deletions src/pkg/packager/filters/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package filters
import (
"path"
"strings"

"github.com/defenseunicorns/zarf/src/types"
)

type selectState int
Expand Down Expand Up @@ -42,14 +40,3 @@ func includedOrExcluded(componentName string, requestedComponentNames []string)
// All other cases we don't know if we should include or exclude yet
return unknown, ""
}

// isRequired returns if the component is required or not.
func isRequired(c types.ZarfComponent) bool {
requiredExists := c.Required != nil
required := requiredExists && *c.Required

if requiredExists {
return required
}
return false
}
3 changes: 1 addition & 2 deletions src/pkg/packager/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
Expand Down Expand Up @@ -73,7 +72,7 @@ func (p *Packager) Generate() (err error) {
p.cfg.Pkg.Components[i].Images = images[name]
}

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

Expand Down
3 changes: 1 addition & 2 deletions src/pkg/packager/sources/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/internal/packager/validate"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
Expand All @@ -23,7 +22,7 @@ var (

// NewClusterSource creates a new cluster source.
func NewClusterSource(pkgOpts *types.ZarfPackageOptions) (PackageSource, error) {
if !validate.IsLowercaseNumberHyphenNoStartHyphen(pkgOpts.PackageSource) {
if !types.IsLowercaseNumberHyphenNoStartHyphen(pkgOpts.PackageSource) {
return nil, fmt.Errorf("invalid package name %q", pkgOpts.PackageSource)
}
cluster, err := cluster.NewClusterWithWait(cluster.DefaultTimeout)
Expand Down
35 changes: 35 additions & 0 deletions src/pkg/variables/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// Package variables contains functions for interacting with variables
package variables

import (
"fmt"
"regexp"

"github.com/defenseunicorns/zarf/src/config/lang"
)

// VariableType represents a type of a Zarf package variable
type VariableType string

Expand All @@ -14,6 +21,12 @@ const (
FileVariableType VariableType = "file"
)

var (
// IsUppercaseNumberUnderscore is a regex for uppercase, numbers and underscores.
// https://regex101.com/r/tfsEuZ/1
IsUppercaseNumberUnderscore = regexp.MustCompile(`^[A-Z0-9_]+$`).MatchString
)

// Variable represents a variable that has a value set programmatically
type Variable struct {
Name string `json:"name" jsonschema:"description=The name to be used for the variable,pattern=^[A-Z0-9_]+$"`
Expand Down Expand Up @@ -46,3 +59,25 @@ type SetVariable struct {
Variable `json:",inline"`
Value string `json:"value" jsonschema:"description=The value the variable is currently set with"`
}

// Validate runs all validation checks on a package variable.
func (v Variable) Validate() error {
if !IsUppercaseNumberUnderscore(v.Name) {
return fmt.Errorf(lang.PkgValidateMustBeUppercase, v.Name)
}
return nil
}

// Validate runs all validation checks on a package constant.
func (c Constant) Validate() error {
// ensure the constant name is only capitals and underscores
if !IsUppercaseNumberUnderscore(c.Name) {
return fmt.Errorf(lang.PkgValidateErrPkgConstantName, c.Name)
}

if !regexp.MustCompile(c.Pattern).MatchString(c.Value) {
return fmt.Errorf(lang.PkgValidateErrPkgConstantPattern, c.Name, c.Pattern)
}

return nil
}
9 changes: 9 additions & 0 deletions src/types/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ func (c ZarfComponent) RequiresCluster() bool {
return false
}

// IsRequired returns if the component is required or not.
func (c ZarfComponent) IsRequired() bool {
if c.Required != nil {
return *c.Required
}

return false
}

// ZarfComponentOnlyTarget filters a component to only show it for a given local OS and cluster.
type ZarfComponentOnlyTarget struct {
LocalOS string `json:"localOS,omitempty" jsonschema:"description=Only deploy component to specified OS,enum=linux,enum=darwin,enum=windows"`
Expand Down
Loading
Loading