Skip to content

Commit

Permalink
chore: begin adding logger to zarf dev (#3163)
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp authored Oct 31, 2024
1 parent 6b6869f commit 89a7fa0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/pkg/lint"
"github.com/zarf-dev/zarf/src/pkg/logger"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/packager"
"github.com/zarf-dev/zarf/src/pkg/transform"
Expand Down Expand Up @@ -113,13 +114,17 @@ var devTransformGitLinksCmd = &cobra.Command{
return fmt.Errorf("unable to read the file %s: %w", fileName, err)
}

pkgConfig.InitOpts.GitServer.Address = host
gitServer := pkgConfig.InitOpts.GitServer
gitServer.Address = host

// Perform git url transformation via regex
text := string(content)
processedText := transform.MutateGitURLsInText(message.Warnf, pkgConfig.InitOpts.GitServer.Address, text, pkgConfig.InitOpts.GitServer.PushUsername)

// TODO(mkcp): Currently uses message for its log fn. Migrate to ctx and slog
processedText := transform.MutateGitURLsInText(message.Warnf, gitServer.Address, text, gitServer.PushUsername)

// Print the differences
// TODO(mkcp): Uses pterm to print text diffs. Decouple from pterm after we release logger.
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(text, processedText, true)
diffs = dmp.DiffCleanupSemantic(diffs)
Expand Down Expand Up @@ -160,6 +165,7 @@ var devSha256SumCmd = &cobra.Command{

if helpers.IsURL(fileName) {
message.Warn(lang.CmdDevSha256sumRemoteWarning)
logger.From(cmd.Context()).Warn("this is a remote source. If a published checksum is available you should use that rather than calculating it directly from the remote link")

fileBase, err := helpers.ExtractBasePathFromURL(fileName)
if err != nil {
Expand Down Expand Up @@ -251,8 +257,14 @@ var devFindImagesCmd = &cobra.Command{
defer pkgClient.ClearTempPaths()

_, err = pkgClient.FindImages(cmd.Context())

var lintErr *lint.LintError
if errors.As(err, &lintErr) {
// HACK(mkcp): Re-initializing PTerm with a stderr writer isn't great, but it lets us render these lint
// tables below for backwards compatibility
if logger.Enabled(cmd.Context()) {
message.InitializePTerm(logger.DestinationDefault)
}
common.PrintFindings(lintErr)
}
if err != nil {
Expand Down Expand Up @@ -299,6 +311,11 @@ var devLintCmd = &cobra.Command{
err := lint.Validate(cmd.Context(), pkgConfig.CreateOpts.BaseDir, pkgConfig.CreateOpts.Flavor, pkgConfig.CreateOpts.SetVariables)
var lintErr *lint.LintError
if errors.As(err, &lintErr) {
// HACK(mkcp): Re-initializing PTerm with a stderr writer isn't great, but it lets us render these lint
// tables below for backwards compatibility
if logger.Enabled(cmd.Context()) {
message.InitializePTerm(logger.DestinationDefault)
}
common.PrintFindings(lintErr)
// Do not return an error if the findings are all warnings.
if lintErr.OnlyWarnings() {
Expand Down
25 changes: 23 additions & 2 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"context"
"errors"
"fmt"
"github.com/zarf-dev/zarf/src/pkg/logger"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -40,6 +42,7 @@ var imageFuzzyCheck = regexp.MustCompile(`(?mi)["|=]([a-z0-9\-.\/:]+:[\w.\-]*[a-

// FindImages iterates over a Zarf.yaml and attempts to parse any images.
func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) {
l := logger.From(ctx)
cwd, err := os.Getwd()
if err != nil {
return nil, err
Expand All @@ -48,12 +51,14 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error)
// Return to the original working directory
if err := os.Chdir(cwd); err != nil {
message.Warnf("Unable to return to the original working directory: %s", err.Error())
l.Warn("unable to return to the original working directory", "error", err)
}
}()
if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil {
return nil, fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err)
}
message.Note(fmt.Sprintf("Using build directory %s", p.cfg.CreateOpts.BaseDir))
l.Info("using build directory", "path", p.cfg.CreateOpts.BaseDir)

c := creator.NewPackageCreator(p.cfg.CreateOpts, cwd)

Expand All @@ -67,6 +72,7 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error)
}
for _, warning := range warnings {
message.Warn(warning)
l.Warn(warning)
}
p.cfg.Pkg = pkg

Expand All @@ -75,12 +81,15 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error)

// TODO: Refactor to return output string instead of printing inside of function.
func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) {
l := logger.From(ctx)
for _, component := range p.cfg.Pkg.Components {
if len(component.Repos) > 0 && p.cfg.FindImagesOpts.RepoHelmChartPath == "" {
message.Note("This Zarf package contains git repositories, " +
msg := "This Zarf package contains git repositories, " +
"if any repos contain helm charts you want to template and " +
"search for images, make sure to specify the helm chart path " +
"via the --repo-chart-path flag")
"via the --repo-chart-path flag"
message.Note(msg)
l.Info(msg)
break
}
}
Expand Down Expand Up @@ -261,6 +270,8 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
}
}

imgCompStart := time.Now()
l.Info("looking for images in component", "name", component.Name, "resourcesCount", len(resources))
spinner := message.NewProgressSpinner("Looking for images in component %q across %d resources", component.Name, len(resources))
defer spinner.Stop()

Expand Down Expand Up @@ -289,9 +300,11 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
if descriptor, err := crane.Head(image, images.WithGlobalInsecureFlag()...); err != nil {
// Test if this is a real image, if not just quiet log to debug, this is normal
message.Debugf("Suspected image does not appear to be valid: %#v", err)
l.Debug("suspected image does not appear to be valid", "error", err)
} else {
// Otherwise, add to the list of images
message.Debugf("Imaged digest found: %s", descriptor.Digest)
l.Debug("imaged digest found", "digest", descriptor.Digest)
validImages = append(validImages, image)
}
}
Expand All @@ -306,16 +319,23 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
}

spinner.Success()
l.Debug("done looking for images in component",
"name", component.Name,
"resourcesCount", len(resources),
"duration", time.Since(imgCompStart))

if !p.cfg.FindImagesOpts.SkipCosign {
// Handle cosign artifact lookups
if len(imagesMap[component.Name]) > 0 {
var cosignArtifactList []string
imgStart := time.Now()
spinner := message.NewProgressSpinner("Looking up cosign artifacts for discovered images (0/%d)", len(imagesMap[component.Name]))
defer spinner.Stop()
l.Info("looking up cosign artifacts for discovered images", "count", len(imagesMap[component.Name]))

for idx, image := range imagesMap[component.Name] {
spinner.Updatef("Looking up cosign artifacts for discovered images (%d/%d)", idx+1, len(imagesMap[component.Name]))
l.Debug("looking up cosign artifacts for image", "name", imagesMap[component.Name])
cosignArtifacts, err := utils.GetCosignArtifacts(image)
if err != nil {
return nil, fmt.Errorf("could not lookup the cosing artifacts for image %s: %w", image, err)
Expand All @@ -324,6 +344,7 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error)
}

spinner.Success()
l.Debug("done looking up cosign artifacts for discovered images", "count", len(imagesMap[component.Name]), "duration", time.Since(imgStart))

if len(cosignArtifactList) > 0 {
imagesMap[component.Name] = append(imagesMap[component.Name], cosignArtifactList...)
Expand Down
1 change: 1 addition & 0 deletions src/pkg/transform/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
package transform

// Log is a function that logs a message.
// TODO(mkcp): Remove Log and port over to logger once we remove message.
type Log func(string, ...any)

0 comments on commit 89a7fa0

Please sign in to comment.