Skip to content

Commit

Permalink
unify init/deploy & add basic chart validation before create
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy committed Feb 8, 2022
1 parent d15a3bd commit 658237f
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 36 deletions.
18 changes: 0 additions & 18 deletions cli/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"os"

"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/message"
"github.com/defenseunicorns/zarf/cli/internal/packager"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
)

Expand All @@ -21,22 +19,6 @@ var initCmd = &cobra.Command{
zarfLogo := getLogo()
_, _ = fmt.Fprintln(os.Stderr, zarfLogo)

if !config.DeployOptions.Confirm {
var confirm bool

message.Question(`
You are about to initialize a new Zarf deployment on this machine which will make
changes to your filesystem. You should not run zarf init more than once without first
running zarf destroy.`)

prompt := &survey.Confirm{Message: "Do you want to continue?"}
_ = survey.AskOne(prompt, &confirm)
if !confirm {
// Gracefully exit because they didn't want to play after all :-/
os.Exit(0)
}
}

// Continue running package deploy for all components like any other package
config.DeployOptions.PackagePath = config.PackageInitName

Expand Down
15 changes: 9 additions & 6 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const (
PackageInitName = "zarf-init.tar.zst"
PackagePrefix = "zarf-package-"

ZarfGitPushUser = "zarf-git-user"
ZarfRegistryPushUser = "zarf-push"
ZarfRegistryPullUser = "zarf-pull"
ZarfSeedPort = "45000"
ZarfRegistry = IPV4Localhost + ":45001"
ZarfLocalSeedRegistry = IPV4Localhost + ":" + ZarfSeedPort
// ZarfMaxChartNameLength limits helm chart name size to account for K8s/helm limits and zarf prefix
ZarfMaxChartNameLength = 40
ZarfGitPushUser = "zarf-git-user"
ZarfRegistryPushUser = "zarf-push"
ZarfRegistryPullUser = "zarf-pull"
ZarfSeedPort = "45000"
ZarfRegistry = IPV4Localhost + ":45001"
ZarfLocalSeedRegistry = IPV4Localhost + ":" + ZarfSeedPort

ZarfSeedTypeCLIInject = "cli-inject"
ZarfSeedTypeRuntimeRegistry = "runtime-registry"
Expand Down Expand Up @@ -56,6 +58,7 @@ func IsZarfInitConfig() bool {

func SetAcrch(arch string) {
message.Debugf("config.SetArch(%s)", arch)
config.Build.Arch = arch
ActiveCranePlatform = crane.WithPlatform(&v1.Platform{OS: "linux", Architecture: arch})
}

Expand Down
5 changes: 3 additions & 2 deletions cli/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type ZarfMetadata struct {
Name string `yaml:"name,omitempty"`
Description string `yaml:"description,omitempty"`
Version string `yaml:"version,omitempty"`
Url string `yaml:"url:omitempty"`
Image string `yaml:"image:omitempty"`
Url string `yaml:"url,omitempty"`
Image string `yaml:"image,omitempty"`
Uncompressed bool `yaml:"uncompressed,omitempty"`
}

Expand All @@ -75,6 +75,7 @@ type ZarfData struct {
type ZarfBuildData struct {
Terminal string `yaml:"terminal"`
User string `yaml:"user"`
Arch string `yaml:"arch"`
Timestamp string `yaml:"timestamp"`
Version string `yaml:"string"`
}
Expand Down
1 change: 1 addition & 0 deletions cli/internal/helm/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package helm
4 changes: 4 additions & 0 deletions cli/internal/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package packager

import (
"fmt"
"github.com/defenseunicorns/zarf/cli/internal/packager/validate"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -35,6 +36,9 @@ func Create() {
message.Fatalf(err, "Unable to write the %s file", configFile)
}

// Perform early package validation
validate.Run()

if !confirmAction(configFile, "Create") {
os.Exit(0)
}
Expand Down
20 changes: 10 additions & 10 deletions cli/internal/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ func Deploy() {
if config.IsZarfInitConfig() {
// If init config, make sure things are ready
utils.RunPreflightChecks()
} else {
// Otherwise, skip duplicate user approval
configPath := tempPath.base + "/zarf.yaml"
confirm := confirmAction(configPath, "Deploy")

// Don't continue unless the user says so
if !confirm {
cleanup(tempPath)
os.Exit(0)
}
}

// Confirm the overall package deployment
configPath := tempPath.base + "/zarf.yaml"
confirm := confirmAction(configPath, "Deploy")

// Don't continue unless the user says so
if !confirm {
cleanup(tempPath)
os.Exit(0)
}

// Verify the components requested all exist
Expand Down
82 changes: 82 additions & 0 deletions cli/internal/packager/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package validate

import (
"fmt"
"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/message"
)

// Run performs config validations and runs message.Fatal() on errors
func Run() {
components := config.GetComponents()

for _, component := range components {
for _, chart := range component.Charts {
if err := validateChart(chart); err != nil {
message.Fatalf(err, "Invalid chart definition in the %s component: %s", component.Name, err)
}
}
for _, manifest := range component.Manifests {
if err := validateManifest(manifest); err != nil {
message.Fatalf(err, "Invalid manifest definition in the %s component: %s", component.Name, err)
}
}
}

}

func validateChart(chart config.ZarfChart) error {
intro := fmt.Sprintf("chart %s", chart.Name)

// Don't allow empty names
if chart.Name == "" {
return fmt.Errorf("%s must include a name", intro)
}

// Helm max release name
if len(chart.Name) > config.ZarfMaxChartNameLength {
return fmt.Errorf("%s exceed the maximum length of %d characters",
intro,
config.ZarfMaxChartNameLength)
}

// Must have a namespace
if chart.Namespace == "" {
return fmt.Errorf("%s must include a namespace", intro)
}

// Must have a url
if chart.Url == "" {
return fmt.Errorf("%s must include a url", intro)
}

// Must have a version
if chart.Version == "" {
return fmt.Errorf("%s must include a chart version", intro)
}

return nil
}

func validateManifest(manifest config.ZarfManifest) error {
intro := fmt.Sprintf("chart %s", manifest.Name)

// Don't allow empty names
if manifest.Name == "" {
return fmt.Errorf("%s must include a name", intro)
}

// Helm max release name
if len(manifest.Name) > config.ZarfMaxChartNameLength {
return fmt.Errorf("%s exceed the maximum length of %d characters",
intro,
config.ZarfMaxChartNameLength)
}

// Require files in manifest
if len(manifest.Files) < 1 {
return fmt.Errorf("%s must have at least 1 file", intro)
}

return nil
}

0 comments on commit 658237f

Please sign in to comment.