Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff <[email protected]>
  • Loading branch information
schristoff committed Oct 4, 2024
1 parent 3f2ed2c commit d912dc4
Show file tree
Hide file tree
Showing 37 changed files with 44 additions and 584 deletions.
1 change: 0 additions & 1 deletion src/cmd/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func SetupCLI(logLevel string, skipLogFile, noColor bool) error {
// Disable progress bars for CI envs
if os.Getenv("CI") == "true" {
message.Debug("CI environment detected, disabling progress bars")
message.NoProgress = true
}

if !skipLogFile {
Expand Down
15 changes: 2 additions & 13 deletions src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ var connectCmd = &cobra.Command{
target = args[0]
}

spinner := message.NewProgressSpinner(lang.CmdConnectPreparingTunnel, target)
defer spinner.Stop()

c, err := cluster.NewCluster()
if err != nil {
return err
Expand Down Expand Up @@ -63,21 +60,13 @@ var connectCmd = &cobra.Command{

// Dump the tunnel URL to the console for other tools to use.
fmt.Print(tunnel.FullURL())

if cliOnly {
spinner.Updatef(lang.CmdConnectEstablishedCLI, tunnel.FullURL())
} else {
spinner.Updatef(lang.CmdConnectEstablishedWeb, tunnel.FullURL())

if err := exec.LaunchURL(tunnel.FullURL()); err != nil {
return err
}
if err := exec.LaunchURL(tunnel.FullURL()); err != nil {
return err
}

// Wait for the interrupt signal or an error.
select {
case <-ctx.Done():
spinner.Successf(lang.CmdConnectTunnelClosed, tunnel.FullURL())
return nil
case err = <-tunnel.ErrChan():
return fmt.Errorf("lost connection to the service: %w", err)
Expand Down
1 change: 0 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&LogLevelCLI, "log-level", "l", v.GetString(common.VLogLevel), lang.RootCmdFlagLogLevel)
rootCmd.PersistentFlags().StringVarP(&config.CLIArch, "architecture", "a", v.GetString(common.VArchitecture), lang.RootCmdFlagArch)
rootCmd.PersistentFlags().BoolVar(&SkipLogFile, "no-log-file", v.GetBool(common.VNoLogFile), lang.RootCmdFlagSkipLogFile)
rootCmd.PersistentFlags().BoolVar(&message.NoProgress, "no-progress", v.GetBool(common.VNoProgress), lang.RootCmdFlagNoProgress)
rootCmd.PersistentFlags().BoolVar(&NoColor, "no-color", v.GetBool(common.VNoColor), lang.RootCmdFlagNoColor)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.CachePath, "zarf-cache", v.GetString(common.VZarfCache), lang.RootCmdFlagCachePath)
rootCmd.PersistentFlags().StringVar(&config.CommonOptions.TempDirectory, "tmpdir", v.GetString(common.VTmpDir), lang.RootCmdFlagTempDir)
Expand Down
17 changes: 0 additions & 17 deletions src/cmd/tools/crane.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ func pruneImages(cmd *cobra.Command, _ []string) error {

func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.DeployedPackage, registryEndpoint string) error {
authOption := images.WithPushAuth(zarfState.RegistryInfo)

spinner := message.NewProgressSpinner(lang.CmdToolsRegistryPruneLookup)
defer spinner.Stop()

// Determine which image digests are currently used by Zarf packages
pkgImages := map[string]bool{}
for _, pkg := range zarfPackages {
Expand All @@ -277,9 +273,6 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
}
}
}

spinner.Updatef(lang.CmdToolsRegistryPruneCatalog)

// Find which images and tags are in the registry currently
imageCatalog, err := crane.Catalog(registryEndpoint, authOption)
if err != nil {
Expand All @@ -302,8 +295,6 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
}
}

spinner.Updatef(lang.CmdToolsRegistryPruneCalculate)

// Figure out which images are in the registry but not needed by packages
imageDigestsToPrune := map[string]bool{}
for digestRef, digest := range referenceToDigest {
Expand All @@ -316,9 +307,6 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
imageDigestsToPrune[digestRef] = true
}
}

spinner.Success()

if len(imageDigestsToPrune) > 0 {
message.Note(lang.CmdToolsRegistryPruneImageList)

Expand All @@ -339,18 +327,13 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D
}
}
if confirm {
spinner := message.NewProgressSpinner(lang.CmdToolsRegistryPruneDelete)
defer spinner.Stop()

// Delete the digest references that are to be pruned
for digestRef := range imageDigestsToPrune {
err = crane.Delete(digestRef, authOption)
if err != nil {
return err
}
}

spinner.Success()
}
} else {
message.Note(lang.CmdToolsRegistryPruneNoImages)
Expand Down
2 changes: 0 additions & 2 deletions src/cmd/tools/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/spf13/cobra"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/utils"

// Import to initialize client auth plugins.
Expand Down Expand Up @@ -62,5 +61,4 @@ func init() {
toolsCmd.AddCommand(waitForCmd)
waitForCmd.Flags().StringVar(&waitTimeout, "timeout", "5m", lang.CmdToolsWaitForFlagTimeout)
waitForCmd.Flags().StringVarP(&waitNamespace, "namespace", "n", "", lang.CmdToolsWaitForFlagNamespace)
waitForCmd.Flags().BoolVar(&message.NoProgress, "no-progress", false, lang.RootCmdFlagNoProgress)
}
40 changes: 5 additions & 35 deletions src/internal/packager/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,
if fromMessage == "" {
fromMessage = "Zarf-generated helm chart"
}
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from %s",
h.chart.Name,
h.chart.Version,
fromMessage)
defer spinner.Stop()

// If no release name is specified, use the chart name.
if h.chart.ReleaseName == "" {
h.chart.ReleaseName = h.chart.Name
}

// Setup K8s connection.
err := h.createActionConfig(h.chart.Namespace, spinner)
err := h.createActionConfig(h.chart.Namespace)
if err != nil {
return nil, "", fmt.Errorf("unable to initialize the K8s client: %w", err)
}
Expand All @@ -63,31 +58,19 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,
var err error

releases, histErr := histClient.Run(h.chart.ReleaseName)

spinner.Updatef("Checking for existing helm deployment")

if errors.Is(histErr, driver.ErrReleaseNotFound) {
// No prior release, try to install it.
spinner.Updatef("Attempting chart installation")

_, err = h.installChart(ctx, postRender)
} else if histErr == nil && len(releases) > 0 {
// Otherwise, there is a prior release so upgrade it.
spinner.Updatef("Attempting chart upgrade")

lastRelease := releases[len(releases)-1]

_, err = h.upgradeChart(ctx, lastRelease, postRender)
} else {
// 😭 things aren't working
return fmt.Errorf("unable to verify the chart installation status: %w", histErr)
}

if err != nil {
return err
}

spinner.Success()
return nil
}, retry.Context(ctx), retry.Attempts(uint(h.retries)), retry.Delay(500*time.Millisecond))
if err != nil {
Expand All @@ -110,7 +93,6 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,
}

// Attempt to rollback on a failed upgrade.
spinner.Updatef("Performing chart rollback")
err = h.rollbackChart(h.chart.ReleaseName, previouslyDeployedVersion)
if err != nil {
return nil, "", fmt.Errorf("%w: unable to rollback: %w", installErr, err)
Expand All @@ -124,10 +106,7 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings,

// TemplateChart generates a helm template from a given chart.
func (h *Helm) TemplateChart(ctx context.Context) (manifest string, chartValues chartutil.Values, err error) {
spinner := message.NewProgressSpinner("Templating helm chart %s", h.chart.Name)
defer spinner.Stop()

err = h.createActionConfig(h.chart.Namespace, spinner)
err = h.createActionConfig(h.chart.Namespace)

// Setup K8s connection.
if err != nil {
Expand Down Expand Up @@ -182,16 +161,13 @@ func (h *Helm) TemplateChart(ctx context.Context) (manifest string, chartValues
for _, hook := range templatedChart.Hooks {
manifest += fmt.Sprintf("\n---\n%s", hook.Manifest)
}

spinner.Success()

return manifest, chartValues, nil
}

// RemoveChart removes a chart from the cluster.
func (h *Helm) RemoveChart(namespace string, name string, spinner *message.Spinner) error {
func (h *Helm) RemoveChart(namespace string, name string) error {
// Establish a new actionConfig for the namespace.
_ = h.createActionConfig(namespace, spinner)
_ = h.createActionConfig(namespace)
// Perform the uninstall.
response, err := h.uninstallChart(name)
message.Debug(response)
Expand All @@ -201,10 +177,7 @@ func (h *Helm) RemoveChart(namespace string, name string, spinner *message.Spinn
// UpdateReleaseValues updates values for a given chart release
// (note: this only works on single-deep charts, charts with dependencies (like loki-stack) will not work)
func (h *Helm) UpdateReleaseValues(ctx context.Context, updatedValues map[string]interface{}) error {
spinner := message.NewProgressSpinner("Updating values for helm release %s", h.chart.ReleaseName)
defer spinner.Stop()

err := h.createActionConfig(h.chart.Namespace, spinner)
err := h.createActionConfig(h.chart.Namespace)
if err != nil {
return fmt.Errorf("unable to initialize the K8s client: %w", err)
}
Expand Down Expand Up @@ -245,9 +218,6 @@ func (h *Helm) UpdateReleaseValues(ctx context.Context, updatedValues map[string
if err != nil {
return err
}

spinner.Success()

return nil
}

Expand Down
8 changes: 0 additions & 8 deletions src/internal/packager/helm/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/pkg/cluster"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/variables"
"github.com/zarf-dev/zarf/src/types"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -80,9 +79,6 @@ func NewClusterOnly(cfg *types.PackagerConfig, variableConfig *variables.Variabl

// NewFromZarfManifest generates a helm chart and config from a given Zarf manifest.
func NewFromZarfManifest(manifest v1alpha1.ZarfManifest, manifestPath, packageName, componentName string, mods ...Modifier) (h *Helm, err error) {
spinner := message.NewProgressSpinner("Starting helm chart generation %s", manifest.Name)
defer spinner.Stop()

// Generate a new chart.
tmpChart := new(chart.Chart)
tmpChart.Metadata = new(chart.Metadata)
Expand All @@ -100,7 +96,6 @@ func NewFromZarfManifest(manifest v1alpha1.ZarfManifest, manifestPath, packageNa

// Add the manifest files so helm does its thing.
for _, file := range manifest.Files {
spinner.Updatef("Processing %s", file)
manifest := path.Join(manifestPath, file)
data, err := os.ReadFile(manifest)
if err != nil {
Expand Down Expand Up @@ -131,9 +126,6 @@ func NewFromZarfManifest(manifest v1alpha1.ZarfManifest, manifestPath, packageNa
for _, mod := range mods {
mod(h)
}

spinner.Success()

return h, nil
}

Expand Down
18 changes: 6 additions & 12 deletions src/internal/packager/helm/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package helm

import (
"fmt"
"regexp"

"github.com/zarf-dev/zarf/src/pkg/cluster"
Expand All @@ -14,17 +15,13 @@ import (

// Destroy removes ZarfInitPackage charts from the cluster and optionally all Zarf-installed charts.
func Destroy(purgeAllZarfInstallations bool) {
spinner := message.NewProgressSpinner("Removing Zarf-installed charts")
defer spinner.Stop()

h := Helm{}

// Initially load the actionConfig without a namespace
err := h.createActionConfig("", spinner)
err := h.createActionConfig("")
if err != nil {
// Don't fatal since this is a removal action
spinner.Errorf(err, "Unable to initialize the K8s client")
return
message.Warn(fmt.Sprintf("could not load the action config: %w", err))
}

// Match a name that begins with "zarf-"
Expand All @@ -41,7 +38,7 @@ func Destroy(purgeAllZarfInstallations bool) {
releases, err := list.Run()
if err != nil {
// Don't fatal since this is a removal action
spinner.Errorf(err, "Unable to get the list of installed charts")
message.Warn(fmt.Sprint("unable to get the list of installed charts: %w", err))
}

// Iterate over all releases
Expand All @@ -52,13 +49,10 @@ func Destroy(purgeAllZarfInstallations bool) {
}
// Filter on zarf releases
if zarfPrefix.MatchString(release.Name) {
spinner.Updatef("Uninstalling helm chart %s/%s", release.Namespace, release.Name)
if err = h.RemoveChart(release.Namespace, release.Name, spinner); err != nil {
if err = h.RemoveChart(release.Namespace, release.Name); err != nil {
// Don't fatal since this is a removal action
spinner.Errorf(err, "Unable to uninstall the chart")
message.Warn(fmt.Sprintf("unable to uninstall the chart: %w", err))
}
}
}

spinner.Success()
}
15 changes: 0 additions & 15 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ func (h *Helm) PackageChart(ctx context.Context, cosignKeyPath string) error {

// PackageChartFromLocalFiles creates a chart archive from a path to a chart on the host os.
func (h *Helm) PackageChartFromLocalFiles(ctx context.Context, cosignKeyPath string) error {
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from %s", h.chart.Name, h.chart.Version, h.chart.LocalPath)
defer spinner.Stop()

// Load and validate the chart
cl, _, err := h.loadAndValidateChart(h.chart.LocalPath)
if err != nil {
Expand Down Expand Up @@ -105,17 +102,11 @@ func (h *Helm) PackageChartFromLocalFiles(ctx context.Context, cosignKeyPath str
if err != nil {
return err
}

spinner.Success()

return nil
}

// PackageChartFromGit is a special implementation of chart archiving that supports the https://p1.dso.mil/#/products/big-bang/ model.
func (h *Helm) PackageChartFromGit(ctx context.Context, cosignKeyPath string) error {
spinner := message.NewProgressSpinner("Processing helm chart %s", h.chart.Name)
defer spinner.Stop()

// Retrieve the repo containing the chart
gitPath, err := DownloadChartFromGitToTemp(ctx, h.chart.URL)
if err != nil {
Expand All @@ -130,9 +121,6 @@ func (h *Helm) PackageChartFromGit(ctx context.Context, cosignKeyPath string) er

// DownloadPublishedChart loads a specific chart version from a remote repo.
func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string) error {
spinner := message.NewProgressSpinner("Processing helm chart %s:%s from repo %s", h.chart.Name, h.chart.Version, h.chart.URL)
defer spinner.Stop()

// Set up the helm pull config
pull := action.NewPull()
pull.Settings = cli.New()
Expand Down Expand Up @@ -186,7 +174,6 @@ func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string)

// Set up the chart chartDownloader
chartDownloader := downloader.ChartDownloader{
Out: spinner,
RegistryClient: regClient,
// TODO: Further research this with regular/OCI charts
Verify: downloader.VerifyNever,
Expand Down Expand Up @@ -221,8 +208,6 @@ func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string)
return err
}

spinner.Success()

return nil
}

Expand Down
Loading

0 comments on commit d912dc4

Please sign in to comment.