Skip to content

Add flag for setting log levels and log errors for debugging. #139

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

Merged
merged 3 commits into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions cli/cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/cli/internal/git"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -26,7 +26,7 @@ var prepareTransformGitLinks = &cobra.Command{
// Read the contents of the given file
content, err := ioutil.ReadFile(fileName)
if err != nil {
logrus.Fatal(err)
log.Logger.Fatal(err)
}

// Perform git url transformation via regex
Expand All @@ -44,7 +44,8 @@ var prepareTransformGitLinks = &cobra.Command{
// Overwrite the file
err = ioutil.WriteFile(fileName, []byte(processedText), 0640)
if err != nil {
logrus.Fatal("Unable to write the changes back to the file")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to write the changes back to the file")
}
}

Expand All @@ -59,7 +60,8 @@ var prepareComputeFileSha256sum = &cobra.Command{
fileName := args[0]
hash, err := utils.GetSha256Sum(fileName)
if err != nil {
logrus.Fatal("Unable to compute the hash")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to compute the hash")
} else {
fmt.Println(hash)
}
Expand Down
10 changes: 9 additions & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"os"
"strings"

"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/packager"

"github.com/spf13/cobra"
)

var zarfLogLevel = ""

var rootCmd = &cobra.Command{
Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML",
Use: "zarf COMMAND|ZARF-PACKAGE|ZARF-YAML",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
log.SetLogLevel(zarfLogLevel)
},
Short: "Small tool to bundle dependencies with K3s for airgapped deployments",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -36,4 +43,5 @@ func Execute() {

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "info", "Log level when runnning Zarf. Valid options are: debug, info, warn, error, fatal")
Copy link
Contributor Author

@YrrepNoj YrrepNoj Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets the default logging level to info (which is what logrus would use if we didn't specify anything else). The way it is currently written would have this interaction:
./zarf init --log-level error -> Runs Zarf with the log level set to error
./zarf deploy. -> Runs Zarf with the log level set to info

Which is what I would expect it to do but I could see others expecting it to stay in error mode. Does anyone think this is something we might want to do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the default log level should be the quieter one, with the ability to make it more noisy if you want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info is the default the loggrus uses so it's what we're currently using right now. I can easily make it warn though.

}
6 changes: 3 additions & 3 deletions cli/cmd/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (

"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/git"
"github.com/defenseunicorns/zarf/cli/internal/log"
craneCmd "github.com/google/go-containerregistry/cmd/crane/cmd"
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/mholt/archiver/v3"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -32,7 +32,7 @@ var archiverCompressCmd = &cobra.Command{
sourceFiles, destinationArchive := args[:len(args)-1], args[len(args)-1]
err := archiver.Archive(sourceFiles, destinationArchive)
if err != nil {
logrus.Fatal(err)
log.Logger.Fatal(err)
}
},
}
Expand All @@ -45,7 +45,7 @@ var archiverDecompressCmd = &cobra.Command{
sourceArchive, destinationPath := args[0], args[1]
err := archiver.Unarchive(sourceArchive, destinationPath)
if err != nil {
logrus.Fatal(err)
log.Logger.Fatal(err)
}
},
}
Expand Down
10 changes: 7 additions & 3 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"time"

"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/goccy/go-yaml"
"github.com/sirupsen/logrus"
)

const K3sBinary = "/usr/local/bin/k3s"
Expand Down Expand Up @@ -49,22 +49,24 @@ func GetComponents() []ZarfComponent {
}

func Load(path string) {
logContext := logrus.WithField("path", path)
logContext := log.Logger.WithField("path", path)
logContext.Info("Loading dynamic config")
file, err := ioutil.ReadFile(path)

if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to load the config file")
}

err = yaml.Unmarshal(file, &config)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to parse the config file")
}
}

func WriteConfig(path string) {
logContext := logrus.WithField("path", path)
logContext := log.Logger.WithField("path", path)
now := time.Now()
currentUser, userErr := user.Current()
hostname, hostErr := os.Hostname()
Expand All @@ -88,11 +90,13 @@ func WriteConfig(path string) {
// Save the parsed output to the config path given
content, err := yaml.Marshal(config)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to process the config data")
}

err = ioutil.WriteFile(path, content, 0400)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to write the config file")
}
}
6 changes: 5 additions & 1 deletion cli/internal/git/checkout.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package git

import (
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/sirupsen/logrus"
)

func CheckoutTag(path string, tag string) {

logContext := logrus.WithFields(logrus.Fields{
logContext := log.Logger.WithFields(logrus.Fields{
"Path": path,
"Tag": tag,
})

// Open the given repo
repo, err := git.PlainOpen(path)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Not a valid git repo or unable to open")
return
}

// Get the working tree so we can change refs
tree, err := repo.Worktree()
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to load the git repo")
}

Expand All @@ -31,6 +34,7 @@ func CheckoutTag(path string, tag string) {
Branch: plumbing.ReferenceName("refs/tags/" + tag),
})
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to checkout the given tag")
}
}
4 changes: 3 additions & 1 deletion cli/internal/git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"os"

"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
Expand All @@ -22,7 +23,7 @@ func Pull(gitUrl string, targetFolder string) {
}

func pull(gitUrl string, targetFolder string) {
logContext := logrus.WithFields(logrus.Fields{
logContext := log.Logger.WithFields(logrus.Fields{
"Remote": gitUrl,
})
logContext.Info("Processing git repo")
Expand All @@ -46,6 +47,7 @@ func pull(gitUrl string, targetFolder string) {
if err == git.ErrRepositoryAlreadyExists {
logContext.Info("Repo already cloned")
} else if err != nil {
logContext.Debug(err)
logContext.Fatal("Not a valid git repo or unable to clone")
}

Expand Down
4 changes: 2 additions & 2 deletions cli/internal/git/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package git

import (
"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/go-git/go-git/v5"
goConfig "github.com/go-git/go-git/v5/config"
"github.com/sirupsen/logrus"
)

const offlineRemoteName = "offline-downstream"
Expand All @@ -19,7 +19,7 @@ func PushAllDirectories(localPath string) {

func push(localPath string) {

logContext := logrus.WithField("repo", localPath)
logContext := log.Logger.WithField("repo", localPath)
logContext.Info("Processing git repo")

// Open the given repo
Expand Down
14 changes: 9 additions & 5 deletions cli/internal/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/defenseunicorns/zarf/cli/internal/utils"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/sirupsen/logrus"
Expand All @@ -22,7 +23,7 @@ func MutateGitUrlsInText(host string, text string) string {
extractPathRegex := regexp.MustCompilePOSIX(`https?://[^/]+/(.*\.git)`)
output := extractPathRegex.ReplaceAllStringFunc(text, func(match string) string {
if strings.Contains(match, "/zarf-git-user/") {
logrus.WithField("Match", match).Warn("This url seems to have been previously patched.")
log.Logger.WithField("Match", match).Warn("This url seems to have been previously patched.")
return match
}
return transformURL(host, match)
Expand All @@ -38,7 +39,7 @@ func transformURLtoRepoName(url string) string {
func transformURL(baseUrl string, url string) string {
replaced := transformURLtoRepoName(url)
output := baseUrl + "/zarf-git-user/" + replaced
logrus.WithFields(logrus.Fields{
log.Logger.WithFields(logrus.Fields{
"Old": url,
"New": output,
}).Info("Transformed Git URL")
Expand Down Expand Up @@ -121,7 +122,8 @@ func CredentialsGenerator() string {

credentialsFile, err := os.OpenFile(credentialsPath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
logrus.Fatal("Unable to access the git credentials file")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to access the git credentials file")
}
defer credentialsFile.Close()

Expand All @@ -137,13 +139,15 @@ func CredentialsGenerator() string {
// Write the entry to the file
_, err = credentialsFile.WriteString(credentialsText)
if err != nil {
logrus.Fatal("Unable to update the git credentials file")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to update the git credentials file")
}

// Save the change
err = credentialsFile.Sync()
if err != nil {
logrus.Fatal("Unable to update the git credentials file")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to update the git credentials file")
}

return gitSecret
Expand Down
7 changes: 5 additions & 2 deletions cli/internal/helm/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/internal/git"
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
Expand All @@ -17,7 +18,7 @@ import (
)

func DownloadChartFromGit(chart config.ZarfChart, destination string) {
logContext := logrus.WithFields(logrus.Fields{
logContext := log.Logger.WithFields(logrus.Fields{
"Chart": chart.Name,
"URL": chart.Url,
"Version": chart.Version,
Expand All @@ -40,7 +41,7 @@ func DownloadChartFromGit(chart config.ZarfChart, destination string) {
}

func DownloadPublishedChart(chart config.ZarfChart, destination string) {
logContext := logrus.WithFields(logrus.Fields{
logContext := log.Logger.WithFields(logrus.Fields{
"Chart": chart.Name,
"URL": chart.Url,
"Version": chart.Version,
Expand All @@ -66,12 +67,14 @@ func DownloadPublishedChart(chart config.ZarfChart, destination string) {
// Perform simple chart download
chartURL, err := repo.FindChartInRepoURL(chart.Url, chart.Name, chart.Version, pull.CertFile, pull.KeyFile, pull.CaFile, getter.All(pull.Settings))
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to pull the helm chart")
}

// Download the file (we don't control what name helm creates here)
saved, _, err := downloader.DownloadTo(chartURL, pull.Version, destination)
if err != nil {
logContext.Debug(err)
logContext.Fatal("Unable to download the helm chart")
}

Expand Down
11 changes: 6 additions & 5 deletions cli/internal/images/pull.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package images

import (
"github.com/defenseunicorns/zarf/cli/internal/log"
"github.com/google/go-containerregistry/pkg/crane"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/cache"
"github.com/sirupsen/logrus"
)

const cachePath = ".image-cache"

func PullAll(buildImageList []string, imageTarballPath string) {
logrus.Info("Loading images")
log.Logger.Info("Loading images")
cranePlatformOptions := crane.WithPlatform(&v1.Platform{OS: "linux", Architecture: "amd64"})
imageMap := map[string]v1.Image{}

for _, src := range buildImageList {
logContext := logrus.WithField("image", src)
logContext := log.Logger.WithField("image", src)
logContext.Info("Fetching image metadata")
img, err := crane.Pull(src, cranePlatformOptions)
if err != nil {
Expand All @@ -25,8 +25,9 @@ func PullAll(buildImageList []string, imageTarballPath string) {
imageMap[src] = img
}

logrus.Info("Creating image tarball (this will take a while)")
log.Logger.Info("Creating image tarball (this will take a while)")
if err := crane.MultiSave(imageMap, imageTarballPath); err != nil {
logrus.Fatal("Unable to save the tarball")
log.Logger.Debug(err)
log.Logger.Fatal("Unable to save the tarball")
}
}
Loading