From ea28ea2f6039b7bc2b921d51317da7644add1d40 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Mon, 29 Jul 2024 10:56:34 +0200 Subject: [PATCH 1/2] refactor: replace message.Debug with context logger Signed-off-by: Philip Laine --- src/cmd/dev.go | 8 +-- src/cmd/initialize.go | 2 +- src/cmd/package.go | 23 +++---- src/cmd/root.go | 6 ++ src/cmd/tools/wait.go | 4 +- src/config/lang/english.go | 5 +- .../agent/hooks/argocd-application.go | 4 -- src/internal/agent/hooks/argocd-repository.go | 1 - src/internal/agent/hooks/flux-gitrepo.go | 4 -- src/internal/agent/hooks/flux-helmrepo.go | 4 -- src/internal/agent/hooks/flux-ocirepo.go | 6 +- src/internal/agent/http/proxy.go | 11 ++-- src/internal/agent/start.go | 2 +- src/internal/git/repository.go | 11 ++-- src/internal/packager/helm/chart.go | 3 +- src/internal/packager/helm/post-render.go | 7 +- src/internal/packager/helm/repo.go | 5 +- src/internal/packager/helm/zarf.go | 2 +- src/internal/packager/images/pull.go | 2 +- src/internal/packager/images/push.go | 2 - src/internal/packager/sbom/catalog.go | 6 +- src/internal/packager/template/template.go | 11 ++-- src/pkg/cluster/data.go | 20 +++--- src/pkg/cluster/secrets.go | 3 +- src/pkg/cluster/state.go | 9 +-- src/pkg/cluster/tunnel.go | 18 ++--- src/pkg/cluster/zarf.go | 5 +- src/pkg/layout/component.go | 7 +- src/pkg/layout/package.go | 4 -- src/pkg/logging/handler.go | 66 +++++++++++++++++++ src/pkg/logging/logging.go | 32 +++++++++ src/pkg/message/message.go | 6 -- src/pkg/packager/actions/actions.go | 9 +-- src/pkg/packager/common.go | 5 +- src/pkg/packager/composer/oci.go | 6 +- src/pkg/packager/creator/normal.go | 7 +- src/pkg/packager/deploy.go | 25 ++++--- src/pkg/packager/deploy_test.go | 3 +- src/pkg/packager/prepare.go | 11 ++-- src/pkg/packager/prepare_test.go | 2 +- src/pkg/packager/remove.go | 3 +- src/pkg/packager/sources/validate.go | 4 +- src/pkg/utils/bytes.go | 10 ++- src/pkg/utils/cosign.go | 9 ++- src/pkg/utils/wait.go | 27 ++++---- src/pkg/zoci/pull.go | 2 +- 46 files changed, 267 insertions(+), 155 deletions(-) create mode 100644 src/pkg/logging/handler.go create mode 100644 src/pkg/logging/logging.go diff --git a/src/cmd/dev.go b/src/cmd/dev.go index 322b82fb84..f561260f45 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -53,7 +53,7 @@ var devDeployCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -78,7 +78,7 @@ var devGenerateCmd = &cobra.Command{ pkgConfig.CreateOpts.BaseDir = "." pkgConfig.FindImagesOpts.RepoHelmChartPath = pkgConfig.GenerateOpts.GitPath - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -229,7 +229,7 @@ var devFindImagesCmd = &cobra.Command{ v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -276,7 +276,7 @@ var devLintCmd = &cobra.Command{ pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 4d1c61363b..17e98b4bf5 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -63,7 +63,7 @@ var initCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } diff --git a/src/cmd/package.go b/src/cmd/package.go index 8b1405e25c..b98331610f 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -14,6 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/cmd/common" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/sources" "github.com/zarf-dev/zarf/src/types" @@ -54,7 +55,7 @@ var packageCreateCmd = &cobra.Command{ pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -84,7 +85,7 @@ var packageDeployCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -112,7 +113,7 @@ var packageMirrorCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -136,11 +137,11 @@ var packageInspectCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - src, err := identifyAndFallbackToClusterSource() + src, err := identifyAndFallbackToClusterSource(cmd.Context()) if err != nil { return err } - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } @@ -208,11 +209,11 @@ var packageRemoveCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - src, err := identifyAndFallbackToClusterSource() + src, err := identifyAndFallbackToClusterSource(cmd.Context()) if err != nil { return err } - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } @@ -253,7 +254,7 @@ var packagePublishCmd = &cobra.Command{ pkgConfig.PublishOpts.PackageDestination = ref.String() - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -273,7 +274,7 @@ var packagePullCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pkgConfig.PkgOpts.PackageSource = args[0] - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -311,10 +312,10 @@ func choosePackage(args []string) (string, error) { } // TODO: This code does not seem to do what it was intended. -func identifyAndFallbackToClusterSource() (sources.PackageSource, error) { +func identifyAndFallbackToClusterSource(ctx context.Context) (sources.PackageSource, error) { identifiedSrc := sources.Identify(pkgConfig.PkgOpts.PackageSource) if identifiedSrc == "" { - message.Debugf(lang.CmdPackageClusterSourceFallback, pkgConfig.PkgOpts.PackageSource) + logging.FromContextOrDiscard(ctx).Debug("package source does not satisfy any current sources, assuming it is a package deployed to a cluster", "source", pkgConfig.PkgOpts.PackageSource) src, err := sources.NewClusterSource(&pkgConfig.PkgOpts) if err != nil { return nil, fmt.Errorf("unable to identify source from %s: %w", pkgConfig.PkgOpts.PackageSource, err) diff --git a/src/cmd/root.go b/src/cmd/root.go index 62e0582c8e..41090c1cd4 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -7,6 +7,7 @@ package cmd import ( "context" "fmt" + "log/slog" "os" "slices" "strings" @@ -19,6 +20,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -87,6 +89,10 @@ var rootCmd = &cobra.Command{ // Execute is the entrypoint for the CLI. func Execute(ctx context.Context) { + handler := logging.NewPtermHandler() + log := slog.New(handler) + ctx = logging.NewContext(ctx, log) + cmd, err := rootCmd.ExecuteContextC(ctx) if err == nil { return diff --git a/src/cmd/tools/wait.go b/src/cmd/tools/wait.go index fda0344fbc..ad5184dd20 100644 --- a/src/cmd/tools/wait.go +++ b/src/cmd/tools/wait.go @@ -29,7 +29,7 @@ var waitForCmd = &cobra.Command{ Long: lang.CmdToolsWaitForLong, Example: lang.CmdToolsWaitForExample, Args: cobra.MinimumNArgs(1), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { // Parse the timeout string timeout, err := time.ParseDuration(waitTimeout) if err != nil { @@ -51,7 +51,7 @@ var waitForCmd = &cobra.Command{ } // Execute the wait command. - if err := utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil { + if err := utils.ExecuteWait(cmd.Context(), waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil { return err } return err diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 50ce790c44..25cde72908 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -315,9 +315,8 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a ar $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a skeleton` CmdPackagePullFlagOutputDirectory = "Specify the output directory for the pulled Zarf package" - CmdPackageChoose = "Choose or type the package file" - CmdPackageClusterSourceFallback = "%q does not satisfy any current sources, assuming it is a package deployed to a cluster" - CmdPackageInvalidSource = "Unable to identify source from %q: %s" + CmdPackageChoose = "Choose or type the package file" + CmdPackageInvalidSource = "Unable to identify source from %q: %s" // zarf dev (prepare is an alias for dev) CmdDevShort = "Commands useful for developing packages" diff --git a/src/internal/agent/hooks/argocd-application.go b/src/internal/agent/hooks/argocd-application.go index b234f29e84..8f978218a8 100644 --- a/src/internal/agent/hooks/argocd-application.go +++ b/src/internal/agent/hooks/argocd-application.go @@ -13,7 +13,6 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" @@ -65,8 +64,6 @@ func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *clu return nil, err } - message.Debugf("Using the url of (%s) to mutate the ArgoCD Application", state.GitServer.Address) - app := Application{} if err = json.Unmarshal(r.Object.Raw, &app); err != nil { return nil, fmt.Errorf(lang.ErrUnmarshal, err) @@ -125,7 +122,6 @@ func getPatchedRepoURL(repoURL string, gs types.GitServerInfo, r *v1.AdmissionRe return "", fmt.Errorf("%s: %w", AgentErrTransformGitURL, err) } patchedURL = transformedURL.String() - message.Debugf("original repoURL of (%s) got mutated to (%s)", repoURL, patchedURL) } return patchedURL, nil diff --git a/src/internal/agent/hooks/argocd-repository.go b/src/internal/agent/hooks/argocd-repository.go index 1875772d05..d3dc97c21b 100644 --- a/src/internal/agent/hooks/argocd-repository.go +++ b/src/internal/agent/hooks/argocd-repository.go @@ -91,7 +91,6 @@ func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster return nil, fmt.Errorf("unable the git url: %w", err) } patchedURL = transformedURL.String() - message.Debugf("original url of (%s) got mutated to (%s)", repoCreds.URL, patchedURL) } patches := populateArgoRepositoryPatchOperations(patchedURL, state.GitServer) diff --git a/src/internal/agent/hooks/flux-gitrepo.go b/src/internal/agent/hooks/flux-gitrepo.go index 2fda2969bb..53e123e729 100644 --- a/src/internal/agent/hooks/flux-gitrepo.go +++ b/src/internal/agent/hooks/flux-gitrepo.go @@ -16,7 +16,6 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" v1 "k8s.io/api/admission/v1" ) @@ -51,8 +50,6 @@ func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster return nil, err } - message.Debugf("Using the url of (%s) to mutate the flux repository", state.GitServer.Address) - repo := flux.GitRepository{} if err = json.Unmarshal(r.Object.Raw, &repo); err != nil { return nil, fmt.Errorf(lang.ErrUnmarshal, err) @@ -78,7 +75,6 @@ func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster return nil, fmt.Errorf("%s: %w", AgentErrTransformGitURL, err) } patchedURL = transformedURL.String() - message.Debugf("original git URL of (%s) got mutated to (%s)", repo.Spec.URL, patchedURL) } // Patch updates of the repo spec diff --git a/src/internal/agent/hooks/flux-helmrepo.go b/src/internal/agent/hooks/flux-helmrepo.go index a2fca0b9a4..0af950a14f 100644 --- a/src/internal/agent/hooks/flux-helmrepo.go +++ b/src/internal/agent/hooks/flux-helmrepo.go @@ -65,8 +65,6 @@ func mutateHelmRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluste return nil, err } - message.Debugf("Using the url of (%s) to mutate the flux HelmRepository", registryAddress) - patchedSrc, err := transform.ImageTransformHost(registryAddress, src.Spec.URL) if err != nil { return nil, fmt.Errorf("unable to transform the HelmRepo URL: %w", err) @@ -78,8 +76,6 @@ func mutateHelmRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluste } patchedURL := helpers.OCIURLPrefix + patchedRefInfo.Name - message.Debugf("original HelmRepo URL of (%s) got mutated to (%s)", src.Spec.URL, patchedURL) - patches := populateHelmRepoPatchOperations(patchedURL, zarfState.RegistryInfo.IsInternal()) patches = append(patches, getLabelPatch(src.Labels)) diff --git a/src/internal/agent/hooks/flux-ocirepo.go b/src/internal/agent/hooks/flux-ocirepo.go index e8c3d21a0f..770596f760 100644 --- a/src/internal/agent/hooks/flux-ocirepo.go +++ b/src/internal/agent/hooks/flux-ocirepo.go @@ -44,7 +44,7 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster src.Spec.Reference = &flux.OCIRepositoryRef{} } - // If we have a semver we want to continue since we wil still have the upstream tag + // If we have a semver we want to continue since we will still have the upstream tag // but should warn that we can't guarantee there won't be collisions if src.Spec.Reference.SemVer != "" { message.Warnf(lang.AgentWarnSemVerRef, src.Spec.Reference.SemVer) @@ -69,8 +69,6 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster } // For the internal registry this will be the ip & port of the service, it may look like 10.43.36.151:5000 - message.Debugf("Using the url of (%s) to mutate the flux OCIRepository", registryAddress) - ref := src.Spec.URL if src.Spec.Reference.Digest != "" { ref = fmt.Sprintf("%s@%s", ref, src.Spec.Reference.Digest) @@ -97,8 +95,6 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster patchedRef.Tag = patchedRefInfo.Tag } - message.Debugf("original OCIRepo URL of (%s) got mutated to (%s)", src.Spec.URL, patchedURL) - patches := populateOCIRepoPatchOperations(patchedURL, zarfState.RegistryInfo.IsInternal(), patchedRef) patches = append(patches, getLabelPatch(src.Labels)) diff --git a/src/internal/agent/http/proxy.go b/src/internal/agent/http/proxy.go index 760ba709ec..86ce52e897 100644 --- a/src/internal/agent/http/proxy.go +++ b/src/internal/agent/http/proxy.go @@ -5,6 +5,7 @@ package http import ( + "context" "crypto/tls" "fmt" "io" @@ -14,17 +15,19 @@ import ( "strings" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" ) // ProxyHandler constructs a new httputil.ReverseProxy and returns an http handler. -func ProxyHandler(cluster *cluster.Cluster) http.HandlerFunc { +func ProxyHandler(ctx context.Context, cluster *cluster.Cluster) http.HandlerFunc { + log := logging.FromContextOrDiscard(ctx) + return func(w http.ResponseWriter, r *http.Request) { state, err := cluster.LoadZarfState(r.Context()) if err != nil { - message.Debugf("%#v", err) + log.Debug("load zarf state failed", "error", err) w.WriteHeader(http.StatusInternalServerError) //nolint: errcheck // ignore w.Write([]byte("unable to load Zarf state, see the Zarf HTTP proxy logs for more details")) @@ -32,7 +35,7 @@ func ProxyHandler(cluster *cluster.Cluster) http.HandlerFunc { } err = proxyRequestTransform(r, state) if err != nil { - message.Debugf("%#v", err) + log.Debug("proxy request transporm failed", "error", err) w.WriteHeader(http.StatusInternalServerError) //nolint: errcheck // ignore w.Write([]byte("unable to transform the provided request, see the Zarf HTTP proxy logs for more details")) diff --git a/src/internal/agent/start.go b/src/internal/agent/start.go index e620c3648a..40594d0bf3 100644 --- a/src/internal/agent/start.go +++ b/src/internal/agent/start.go @@ -58,7 +58,7 @@ func StartWebhook(ctx context.Context, cluster *cluster.Cluster) error { // StartHTTPProxy launches the zarf agent proxy in the cluster. func StartHTTPProxy(ctx context.Context, cluster *cluster.Cluster) error { mux := http.NewServeMux() - mux.Handle("/", agentHttp.ProxyHandler(cluster)) + mux.Handle("/", agentHttp.ProxyHandler(ctx, cluster)) return startServer(ctx, httpPort, mux) } diff --git a/src/internal/git/repository.go b/src/internal/git/repository.go index 9d2b2d4918..d694f307a9 100644 --- a/src/internal/git/repository.go +++ b/src/internal/git/repository.go @@ -18,6 +18,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -146,6 +147,8 @@ func (r *Repository) Path() string { // Push pushes the repository to the remote git server. func (r *Repository) Push(ctx context.Context, address, username, password string) error { + log := logging.FromContextOrDiscard(ctx) + repo, err := git.PlainOpen(r.path) if err != nil { return fmt.Errorf("not a valid git repo or unable to open: %w", err) @@ -194,11 +197,11 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin } err = repo.FetchContext(ctx, fetchOptions) if errors.Is(err, transport.ErrRepositoryNotFound) { - message.Debugf("Repo not yet available offline, skipping fetch...") + log.Debug("repository not yet available offling, skipping fetch") } else if errors.Is(err, git.ErrForceNeeded) { - message.Debugf("Repo fetch requires force, skipping fetch...") + log.Debug("repositoy fetch requires force, skipping fetch") } else if errors.Is(err, git.NoErrAlreadyUpToDate) { - message.Debugf("Repo already up-to-date, skipping fetch...") + log.Debug("repository already update-to date, skipping fetch") } else if err != nil { return fmt.Errorf("unable to fetch the git repo prior to push: %w", err) } @@ -216,7 +219,7 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin }, }) if errors.Is(err, git.NoErrAlreadyUpToDate) { - message.Debug("Repo already up-to-date") + logging.FromContextOrDiscard(ctx).Debug("Repository already up-to-date") } else if errors.Is(err, plumbing.ErrObjectNotFound) { return fmt.Errorf("unable to push repo due to likely shallow clone: %s", err.Error()) } else if err != nil { diff --git a/src/internal/packager/helm/chart.go b/src/internal/packager/helm/chart.go index daf59902e5..0a2d334a4e 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -193,8 +193,7 @@ func (h *Helm) RemoveChart(namespace string, name string, spinner *message.Spinn // Establish a new actionConfig for the namespace. _ = h.createActionConfig(namespace, spinner) // Perform the uninstall. - response, err := h.uninstallChart(name) - message.Debug(response) + _, err := h.uninstallChart(name) return err } diff --git a/src/internal/packager/helm/post-render.go b/src/internal/packager/helm/post-render.go index c316375acb..309c281092 100644 --- a/src/internal/packager/helm/post-render.go +++ b/src/internal/packager/helm/post-render.go @@ -16,6 +16,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/types" @@ -208,6 +209,8 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { } func (r *renderer) editHelmResources(ctx context.Context, resources []releaseutil.Manifest, finalManifestsOutput *bytes.Buffer) error { + log := logging.FromContextOrDiscard(ctx) + dc, err := dynamic.NewForConfig(r.cluster.RestConfig) if err != nil { return err @@ -232,7 +235,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti if err := runtime.DefaultUnstructuredConverter.FromUnstructured(rawData.UnstructuredContent(), namespace); err != nil { message.WarnErrf(err, "could not parse namespace %s", rawData.GetName()) } else { - message.Debugf("Matched helm namespace %s for zarf annotation", namespace.Name) + log.Debug("matched Helm namespace for Zarf annotation", "namespace", namespace.Name) namespace.Labels = cluster.AdoptZarfManagedLabels(namespace.Labels) // Add it to the stack r.namespaces[namespace.Name] = namespace @@ -252,7 +255,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti } if key, keyExists := labels[cluster.ZarfConnectLabelName]; keyExists { // If there is a zarf-connect label - message.Debugf("Match helm service %s for zarf connection %s", rawData.GetName(), key) + log.Debug("match Helm service for Zarf connection", "service", rawData.GetName(), "connection", key) // Add the connectString for processing later in the deployment r.connectStrings[key] = types.ConnectString{ diff --git a/src/internal/packager/helm/repo.go b/src/internal/packager/helm/repo.go index 24f3a7f4b0..13a35b3cd9 100644 --- a/src/internal/packager/helm/repo.go +++ b/src/internal/packager/helm/repo.go @@ -16,6 +16,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/git" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -39,7 +40,7 @@ func (h *Helm) PackageChart(ctx context.Context, cosignKeyPath string) error { // check if the chart is a git url with a ref (if an error is returned url will be empty) isGitURL := strings.HasSuffix(url, ".git") if err != nil { - message.Debugf("unable to parse the url, continuing with %s", h.chart.URL) + logging.FromContextOrDiscard(ctx).Debug("continuing with original url as the url could not be parsed", "url", h.chart.URL, "error", err) } if isGitURL { @@ -147,7 +148,7 @@ func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string) // Not returning the error here since the repo file is only needed if we are pulling from a repo that requires authentication if err != nil { - message.Debugf("Unable to load the repo file at %q: %s", pull.Settings.RepositoryConfig, err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to load the repository file", "path", pull.Settings.RepositoryConfig, "error", err) } var username string diff --git a/src/internal/packager/helm/zarf.go b/src/internal/packager/helm/zarf.go index 03e4db2ed0..c742d37f5e 100644 --- a/src/internal/packager/helm/zarf.go +++ b/src/internal/packager/helm/zarf.go @@ -112,7 +112,7 @@ func (h *Helm) UpdateZarfAgentValues(ctx context.Context) error { Value: agentImage.Tag, }, }) - applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state) + applicationTemplates, err := template.GetZarfTemplates(ctx, "zarf-agent", h.state) if err != nil { return fmt.Errorf("error setting up the templates: %w", err) } diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 2e206742c6..241db6486e 100644 --- a/src/internal/packager/images/pull.go +++ b/src/internal/packager/images/pull.go @@ -225,7 +225,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er doneSaving := make(chan error) updateText := fmt.Sprintf("Pulling %d images", imageCount) - go utils.RenderProgressBarForLocalDirWrite(cfg.DestinationDirectory, totalBytes.Load(), doneSaving, updateText, updateText) + go utils.RenderProgressBarForLocalDirWrite(ctx, cfg.DestinationDirectory, totalBytes.Load(), doneSaving, updateText, updateText) toPull := maps.Clone(fetched) diff --git a/src/internal/packager/images/push.go b/src/internal/packager/images/push.go index 7bac363001..b3722b7fbf 100644 --- a/src/internal/packager/images/push.go +++ b/src/internal/packager/images/push.go @@ -115,8 +115,6 @@ func Push(ctx context.Context, cfg PushConfig) error { return err } - message.Debugf("push %s -> %s)", refInfo.Reference, offlineName) - if err = pushImage(img, offlineName); err != nil { return err } diff --git a/src/internal/packager/sbom/catalog.go b/src/internal/packager/sbom/catalog.go index aadeeea257..a25f532731 100755 --- a/src/internal/packager/sbom/catalog.go +++ b/src/internal/packager/sbom/catalog.go @@ -5,6 +5,7 @@ package sbom import ( + "context" "embed" "fmt" "os" @@ -27,6 +28,7 @@ import ( v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -48,7 +50,7 @@ var transformRegex = regexp.MustCompile(`(?m)[^a-zA-Z0-9\.\-]`) var componentPrefix = "zarf-component-" // Catalog catalogs the given components and images to create an SBOM. -func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transform.Image, paths *layout.PackagePaths) error { +func Catalog(ctx context.Context, componentSBOMs map[string]*layout.ComponentSBOM, imageList []transform.Image, paths *layout.PackagePaths) error { imageCount := len(imageList) componentCount := len(componentSBOMs) builder := Builder{ @@ -103,7 +105,7 @@ func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transf builder.spinner.Updatef("Creating component file SBOMs (%d of %d): %s", currComponent, componentCount, component) if componentSBOMs[component] == nil { - message.Debugf("Component %s has invalid SBOM, skipping", component) + logging.FromContextOrDiscard(ctx).Debug("component has invalid SBOM, skipping", "component", component) continue } diff --git a/src/internal/packager/template/template.go b/src/internal/packager/template/template.go index 872754ce4d..61b701055e 100644 --- a/src/internal/packager/template/template.go +++ b/src/internal/packager/template/template.go @@ -5,6 +5,7 @@ package template import ( + "context" "encoding/base64" "fmt" "log/slog" @@ -16,6 +17,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/interactive" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/variables" @@ -41,7 +43,7 @@ func GetZarfVariableConfig() *variables.VariableConfig { } // GetZarfTemplates returns the template keys and values to be used for templating. -func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) { +func GetZarfTemplates(ctx context.Context, componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) { templateMap = make(map[string]*variables.TextTemplate) if state != nil { @@ -100,7 +102,7 @@ func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap } } - debugPrintTemplateMap(templateMap) + debugPrintTemplateMap(ctx, templateMap) return templateMap, nil } @@ -125,7 +127,7 @@ func generateHtpasswd(regInfo *types.RegistryInfo) (string, error) { return "", nil } -func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) { +func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variables.TextTemplate) { debugText := "templateMap = { " for key, template := range templateMap { @@ -137,6 +139,5 @@ func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) { } debugText += " }" - - message.Debug(debugText) + logging.FromContextOrDiscard(ctx).Debug(debugText) } diff --git a/src/pkg/cluster/data.go b/src/pkg/cluster/data.go index 68148003a7..d2fdf4339a 100644 --- a/src/pkg/cluster/data.go +++ b/src/pkg/cluster/data.go @@ -15,16 +15,16 @@ import ( "strings" "time" + "github.com/avast/retry-go/v4" + "github.com/defenseunicorns/pkg/helpers/v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - "github.com/avast/retry-go/v4" - "github.com/defenseunicorns/pkg/helpers/v2" - "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/utils/exec" @@ -33,6 +33,8 @@ import ( // HandleDataInjection waits for the target pod(s) to come up and inject the data into them // todo: this currently requires kubectl but we should have enough k8s work to make this native now. func (c *Cluster) HandleDataInjection(ctx context.Context, data v1alpha1.ZarfDataInjection, componentPath *layout.ComponentPaths, dataIdx int) error { + log := logging.FromContextOrDiscard(ctx) + injectionCompletionMarker := filepath.Join(componentPath.DataInjections, config.GetDataInjectionMarker()) if err := os.WriteFile(injectionCompletionMarker, []byte("🦄"), helpers.ReadWriteUser); err != nil { return fmt.Errorf("unable to create the data injection completion marker: %w", err) @@ -60,7 +62,7 @@ func (c *Cluster) HandleDataInjection(ctx context.Context, data v1alpha1.ZarfDat return fmt.Errorf("unable to execute tar, ensure it is installed in the $PATH: %w", err) } - message.Debugf("Attempting to inject data into %s", data.Target) + log.Debug("Attempting to inject data", "target", data.Target) source := filepath.Join(componentPath.DataInjections, filepath.Base(data.Target.Path)) if helpers.InvalidPath(source) { @@ -171,6 +173,8 @@ type podFilter func(pod corev1.Pod) bool // If the timeout is reached, an empty list will be returned. // TODO: Test, refactor and/or remove. func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interface, target podLookup, include podFilter) ([]corev1.Pod, error) { + log := logging.FromContextOrDiscard(ctx) + readyPods, err := retry.DoWithData(func() ([]corev1.Pod, error) { listOpts := metav1.ListOptions{ LabelSelector: target.Selector, @@ -179,7 +183,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac if err != nil { return nil, err } - message.Debugf("Found %d pods for target %#v", len(podList.Items), target) + log.Debug("found pods for container", "count", len(podList.Items), "target", target) // Sort the pods from newest to oldest sort.Slice(podList.Items, func(i, j int) bool { return podList.Items[i].CreationTimestamp.After(podList.Items[j].CreationTimestamp.Time) @@ -187,7 +191,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac readyPods := []corev1.Pod{} for _, pod := range podList.Items { - message.Debugf("Testing pod %q", pod.Name) + log.Debug("testing pod", "pod", pod.Name) // If an include function is provided, only keep pods that return true if include != nil && !include(pod) { @@ -196,7 +200,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac // Handle container targeting if target.Container != "" { - message.Debugf("Testing pod %q for container %q", pod.Name, target.Container) + log.Debug("testing pod for container", "pod", pod.Name, "container", target.Container) // Check the status of initContainers for a running match for _, initContainer := range pod.Status.InitContainerStatuses { @@ -218,7 +222,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac } } else { status := pod.Status.Phase - message.Debugf("Testing pod %q phase, want (%q) got (%q)", pod.Name, corev1.PodRunning, status) + log.Debug("testing pod phase", "pod", pod.Name, "wanted", corev1.PodRunning, "got", status) // Regular status checking without a container if status == corev1.PodRunning { readyPods = append(readyPods, pod) diff --git a/src/pkg/cluster/secrets.go b/src/pkg/cluster/secrets.go index aa693c6a44..b6424e36d8 100644 --- a/src/pkg/cluster/secrets.go +++ b/src/pkg/cluster/secrets.go @@ -17,6 +17,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/zarf-dev/zarf/src/config" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -198,7 +199,7 @@ func (c *Cluster) GetServiceInfoFromRegistryAddress(ctx context.Context, stateRe // If this is an internal service then we need to look it up and svc, port, err := serviceInfoFromNodePortURL(serviceList.Items, stateRegistryAddress) if err != nil { - message.Debugf("registry appears to not be a nodeport service, using original address %q", stateRegistryAddress) + logging.FromContextOrDiscard(ctx).Debug("registry appears to not be a node port service, using original address", "address", stateRegistryAddress) return stateRegistryAddress, nil } diff --git a/src/pkg/cluster/state.go b/src/pkg/cluster/state.go index 3c31ccf128..c8854904c3 100644 --- a/src/pkg/cluster/state.go +++ b/src/pkg/cluster/state.go @@ -20,6 +20,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/pki" "github.com/zarf-dev/zarf/src/types" @@ -203,7 +204,7 @@ func (c *Cluster) LoadZarfState(ctx context.Context) (state *types.ZarfState, er if err != nil { return nil, fmt.Errorf("%w: %w", stateErr, err) } - c.debugPrintZarfState(state) + c.debugPrintZarfState(ctx, state) return state, nil } @@ -228,7 +229,7 @@ func (c *Cluster) sanitizeZarfState(state *types.ZarfState) *types.ZarfState { return state } -func (c *Cluster) debugPrintZarfState(state *types.ZarfState) { +func (c *Cluster) debugPrintZarfState(ctx context.Context, state *types.ZarfState) { if state == nil { return } @@ -239,12 +240,12 @@ func (c *Cluster) debugPrintZarfState(state *types.ZarfState) { if err != nil { return } - message.Debugf("ZarfState - %s", string(b)) + logging.FromContextOrDiscard(ctx).Debug("ZarfState", "state", string(b)) } // SaveZarfState takes a given state and persists it to the Zarf/zarf-state secret. func (c *Cluster) SaveZarfState(ctx context.Context, state *types.ZarfState) error { - c.debugPrintZarfState(state) + c.debugPrintZarfState(ctx, state) data, err := json.Marshal(&state) if err != nil { diff --git a/src/pkg/cluster/tunnel.go b/src/pkg/cluster/tunnel.go index 764ae9a6eb..6df32889f5 100644 --- a/src/pkg/cluster/tunnel.go +++ b/src/pkg/cluster/tunnel.go @@ -23,7 +23,7 @@ import ( "github.com/avast/retry-go/v4" "github.com/defenseunicorns/pkg/helpers/v2" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/types" ) @@ -218,8 +218,6 @@ func (c *Cluster) checkForZarfConnectLabel(ctx context.Context, name string) (Tu // Add the url suffix too. zt.urlSuffix = svc.Annotations[ZarfConnectAnnotationURL] - - message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.RemotePort) } else { return zt, fmt.Errorf("no matching services found for %s", name) } @@ -391,6 +389,8 @@ func (tunnel *Tunnel) Close() { // establish opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct. func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { + log := logging.FromContextOrDiscard(ctx) + var err error // Track this locally as we may need to retry if the tunnel fails. @@ -403,12 +403,12 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { // since there is a brief moment between `GetAvailablePort` and `forwarder.ForwardPorts` where the selected port // is available for selection again. if localPort == 0 { - message.Debugf("Requested local port is 0. Selecting an open port on host system") + log.Debug("requested local port is 0, selection an open port on host system") localPort, err = helpers.GetAvailablePort() if err != nil { return "", fmt.Errorf("unable to find an available port: %w", err) } - message.Debugf("Selected port %d", localPort) + log.Debug("selected port", "port", localPort) globalMutex.Lock() defer globalMutex.Unlock() } @@ -420,14 +420,14 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { tunnel.resourceName, tunnel.namespace, ) - message.Debugf(msg) + log.Debug(msg) // Find the pod to port forward to podName, err := tunnel.getAttachablePodForResource(ctx) if err != nil { return "", fmt.Errorf("unable to find pod attached to given resource: %w", err) } - message.Debugf("Selected pod %s to open port forward to", podName) + log.Debug("selected pod to open port forward to", "pod", podName) // Build url to the port forward endpoint. // Example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward. @@ -440,7 +440,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { SubResource("portforward"). URL() - message.Debugf("Using URL %s to create portforward", portForwardCreateURL) + log.Debug("using URL to create port forward", "url", portForwardCreateURL) // Construct the spdy client required by the client-go portforward library. transport, upgrader, err := spdy.RoundTripperFor(tunnel.restConfig) @@ -475,7 +475,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { // Store the error channel to listen for errors tunnel.errChan = errChan - message.Debugf("Creating port forwarding tunnel at %s", url) + log.Debug("creation for forwarding tunnel", "url", url) return url, nil } } diff --git a/src/pkg/cluster/zarf.go b/src/pkg/cluster/zarf.go index 14c3e2f31a..6390205d0b 100644 --- a/src/pkg/cluster/zarf.go +++ b/src/pkg/cluster/zarf.go @@ -12,15 +12,16 @@ import ( "strings" "time" + "github.com/avast/retry-go/v4" autoscalingV2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/avast/retry-go/v4" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/gitea" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -181,7 +182,7 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.Zarf var componentWebhooks map[string]map[string]types.Webhook existingPackageSecret, err := c.GetDeployedPackage(ctx, packageName) if err != nil { - message.Debugf("Unable to fetch existing secret for package '%s': %s", packageName, err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to fetch existing secret for package", "package", packageName, "error", err) } if existingPackageSecret != nil { componentWebhooks = existingPackageSecret.ComponentWebhooks diff --git a/src/pkg/layout/component.go b/src/pkg/layout/component.go index fee2d90082..39c9b8ed93 100644 --- a/src/pkg/layout/component.go +++ b/src/pkg/layout/component.go @@ -12,8 +12,8 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/mholt/archiver/v3" + "github.com/zarf-dev/zarf/src/api/v1alpha1" - "github.com/zarf-dev/zarf/src/pkg/message" ) // ComponentPaths contains paths for a component. @@ -58,7 +58,6 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) } if size > 0 { tb := fmt.Sprintf("%s.tar", base) - message.Debugf("Archiving %q", name) if err := helpers.CreateReproducibleTarballFromDir(base, name, tb); err != nil { return err } @@ -66,8 +65,6 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) c.Tarballs = make(map[string]string) } c.Tarballs[name] = tb - } else { - message.Debugf("Component %q is empty, skipping archiving", name) } delete(c.Dirs, name) @@ -126,11 +123,9 @@ func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) { // if the component is already unarchived, skip if !helpers.InvalidPath(cs.Base) { - message.Debugf("Component %q already unarchived", name) return nil } - message.Debugf("Unarchiving %q", filepath.Base(tb)) if err := archiver.Unarchive(tb, c.Base); err != nil { return err } diff --git a/src/pkg/layout/package.go b/src/pkg/layout/package.go index a38ec599a1..5437836c0d 100644 --- a/src/pkg/layout/package.go +++ b/src/pkg/layout/package.go @@ -99,7 +99,6 @@ func (pp *PackagePaths) MigrateLegacy() (err error) { legacySBOMs := filepath.Join(base, "sboms") if !helpers.InvalidPath(legacySBOMs) { pp = pp.AddSBOMs() - message.Debugf("Migrating %q to %q", legacySBOMs, pp.SBOMs.Path) if err := os.Rename(legacySBOMs, pp.SBOMs.Path); err != nil { return err } @@ -109,7 +108,6 @@ func (pp *PackagePaths) MigrateLegacy() (err error) { legacyImagesTar := filepath.Join(base, "images.tar") if !helpers.InvalidPath(legacyImagesTar) { pp = pp.AddImages() - message.Debugf("Migrating %q to %q", legacyImagesTar, pp.Images.Base) defer os.Remove(legacyImagesTar) imgTags := []string{} for _, component := range pkg.Components { @@ -309,8 +307,6 @@ func (pp *PackagePaths) SetFromPaths(paths []string) { pp.Components.Tarballs = make(map[string]string) } pp.Components.Tarballs[componentName] = filepath.Join(pp.Base, path) - default: - message.Debug("ignoring path", path) } } } diff --git a/src/pkg/logging/handler.go b/src/pkg/logging/handler.go new file mode 100644 index 0000000000..3b524a6772 --- /dev/null +++ b/src/pkg/logging/handler.go @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +package logging + +import ( + "context" + "fmt" + "log/slog" + "strings" + + "github.com/zarf-dev/zarf/src/pkg/message" +) + +// PtermHandler is a slog handler that prints using Pterm. +type PtermHandler struct { + attrs []slog.Attr + group string +} + +// NewPtermHandler returns a new instance of PtermHandler. +func NewPtermHandler() *PtermHandler { + return &PtermHandler{} +} + +//nolint:revive // ignore +func (h *PtermHandler) Enabled(context.Context, slog.Level) bool { + return true +} + +//nolint:revive // ignore +func (h *PtermHandler) Handle(ctx context.Context, r slog.Record) error { + attrs := []string{} + r.Attrs(func(a slog.Attr) bool { + attrs = append(attrs, a.String()) + return true + }) + out := fmt.Sprintf("%s %s", r.Message, strings.Join(attrs, " ")) + switch r.Level { + case slog.LevelDebug: + message.Debug(out) + case slog.LevelInfo: + message.Info(out) + case slog.LevelWarn: + message.Warn(out) + case slog.LevelError: + message.Warn(out) + } + return nil +} + +//nolint:revive // ignore +func (h *PtermHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &PtermHandler{ + attrs: append(h.attrs, attrs...), + group: h.group, + } +} + +//nolint:revive // ignore +func (h *PtermHandler) WithGroup(name string) slog.Handler { + return &PtermHandler{ + attrs: h.attrs, + group: name, + } +} diff --git a/src/pkg/logging/logging.go b/src/pkg/logging/logging.go new file mode 100644 index 0000000000..b9be19b8a8 --- /dev/null +++ b/src/pkg/logging/logging.go @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package logging contains logging related functionality. +package logging + +import ( + "context" + "io" + "log/slog" +) + +type contextKey struct{} + +// NewContext returns a child context containing the given logger. +func NewContext(ctx context.Context, log *slog.Logger) context.Context { + return context.WithValue(ctx, contextKey{}, log) +} + +// FromContextOrDiscard returns the logger stored in the context. +func FromContextOrDiscard(ctx context.Context) *slog.Logger { + v := ctx.Value(contextKey{}) + if v == nil { + return slog.New(slog.NewJSONHandler(io.Discard, nil)) + } + log, ok := v.(*slog.Logger) + if !ok { + // This should never happen. + panic("unexpected logger type") + } + return log +} diff --git a/src/pkg/message/message.go b/src/pkg/message/message.go index e271cbf9d4..9d2c7de6bc 100644 --- a/src/pkg/message/message.go +++ b/src/pkg/message/message.go @@ -119,12 +119,6 @@ func Debug(payload ...any) { debugPrinter(2, payload...) } -// Debugf prints a debug message with a given format. -func Debugf(format string, a ...any) { - message := fmt.Sprintf(format, a...) - debugPrinter(2, message) -} - // Warn prints a warning message. func Warn(message string) { Warnf("%s", message) diff --git a/src/pkg/packager/actions/actions.go b/src/pkg/packager/actions/actions.go index dbef82b613..8d5451ecec 100644 --- a/src/pkg/packager/actions/actions.go +++ b/src/pkg/packager/actions/actions.go @@ -15,6 +15,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/internal/packager/template" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/utils/exec" @@ -205,7 +206,7 @@ func convertWaitToCmd(_ context.Context, wait v1alpha1.ZarfComponentActionWait, } // Perform some basic string mutations to make commands more useful. -func actionCmdMutation(_ context.Context, cmd string, shellPref v1alpha1.Shell) (string, error) { +func actionCmdMutation(ctx context.Context, cmd string, shellPref v1alpha1.Shell) (string, error) { zarfCommand, err := utils.GetFinalExecutableCommand() if err != nil { return cmd, err @@ -227,7 +228,7 @@ func actionCmdMutation(_ context.Context, cmd string, shellPref v1alpha1.Shell) get, err := helpers.MatchRegex(envVarRegex, cmd) if err == nil { newCmd := strings.ReplaceAll(cmd, get("envIndicator"), fmt.Sprintf("$Env:%s", get("varName"))) - message.Debugf("Converted command \"%s\" to \"%s\" t", cmd, newCmd) + logging.FromContextOrDiscard(ctx).Debug("converted command", "old", cmd, "new", newCmd) cmd = newCmd } } @@ -278,7 +279,7 @@ func actionGetCfg(_ context.Context, cfg v1alpha1.ZarfComponentActionDefaults, a func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cmd string, shellPref v1alpha1.Shell, spinner *message.Spinner) (string, error) { shell, shellArgs := exec.GetOSShell(shellPref) - message.Debugf("Running command in %s: %s", shell, cmd) + logging.FromContextOrDiscard(ctx).Debug("running command", "shell", shell, "command", cmd) execCfg := exec.Config{ Env: cfg.Env, @@ -293,7 +294,7 @@ func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cm out, errOut, err := exec.CmdWithContext(ctx, execCfg, shell, append(shellArgs, cmd)...) // Dump final complete output (respect mute to prevent sensitive values from hitting the logs). if !cfg.Mute { - message.Debug(cmd, out, errOut) + logging.FromContextOrDiscard(ctx).Debug(cmd, "stdout", out, "stderr", errOut) } return out, err diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index 6a3ec004e7..a04922d602 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -20,6 +20,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/template" "github.com/zarf-dev/zarf/src/pkg/cluster" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/deprecated" "github.com/zarf-dev/zarf/src/pkg/packager/sources" @@ -71,7 +72,7 @@ New creates a new package instance with the provided config. Note: This function creates a tmp directory that should be cleaned up with p.ClearTempPaths(). */ -func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { +func New(ctx context.Context, cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { if cfg == nil { return nil, fmt.Errorf("no config provided") } @@ -110,7 +111,7 @@ func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { if err != nil { return nil, fmt.Errorf("unable to create package temp paths: %w", err) } - message.Debug("Using temporary directory:", dir) + logging.FromContextOrDiscard(ctx).Debug("Using temporary directory", "directory", dir) pkgr.layout = layout.New(dir) } diff --git a/src/pkg/packager/composer/oci.go b/src/pkg/packager/composer/oci.go index 2541e45f32..05299ca03e 100644 --- a/src/pkg/packager/composer/oci.go +++ b/src/pkg/packager/composer/oci.go @@ -17,7 +17,7 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/zoci" ocistore "oras.land/oras-go/v2/content/oci" @@ -80,7 +80,7 @@ func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error { dir = filepath.Join(cache, "dirs", id) - message.Debug("creating empty directory for remote component:", filepath.Join("", "oci", "dirs", id)) + logging.FromContextOrDiscard(ctx).Debug("Creaing empty directory for remote component", "path", filepath.Join("", "oci", "dirs", id)) } else { tb = filepath.Join(cache, "blobs", "sha256", componentDesc.Digest.Encoded()) dir = filepath.Join(cache, "dirs", componentDesc.Digest.Encoded()) @@ -97,7 +97,7 @@ func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error { } else if !exists { doneSaving := make(chan error) successText := fmt.Sprintf("Pulling %q", helpers.OCIURLPrefix+remote.Repo().Reference.String()) - go utils.RenderProgressBarForLocalDirWrite(cache, componentDesc.Size, doneSaving, "Pulling", successText) + go utils.RenderProgressBarForLocalDirWrite(ctx, cache, componentDesc.Size, doneSaving, "Pulling", successText) err = remote.CopyToTarget(ctx, []ocispec.Descriptor{componentDesc}, store, remote.GetDefaultCopyOpts()) doneSaving <- err <-doneSaving diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index 847a22003e..d3e89c0806 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -28,6 +28,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/kustomize" "github.com/zarf-dev/zarf/src/internal/packager/sbom" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -138,7 +139,7 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths onFailure := func() { if err := actions.Run(ctx, onCreate.Defaults, onCreate.OnFailure, nil); err != nil { - message.Debugf("unable to run component failure action: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to run component failure action", "error", err) } } @@ -213,10 +214,10 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths // Ignore SBOM creation if the flag is set. if skipSBOMFlagUsed { - message.Debug("Skipping image SBOM processing per --skip-sbom flag") + logging.FromContextOrDiscard(ctx).Debug("Skipping image SBOm processing per --skip-sbom flag") } else { dst.AddSBOMs() - if err := sbom.Catalog(componentSBOMs, sbomImageList, dst); err != nil { + if err := sbom.Catalog(ctx, componentSBOMs, sbomImageList, dst); err != nil { return fmt.Errorf("unable to create an SBOM catalog for the package: %w", err) } } diff --git a/src/pkg/packager/deploy.go b/src/pkg/packager/deploy.go index 2c5f2e5f2d..aa067fbce7 100644 --- a/src/pkg/packager/deploy.go +++ b/src/pkg/packager/deploy.go @@ -33,6 +33,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/template" "github.com/zarf-dev/zarf/src/pkg/cluster" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -48,7 +49,7 @@ var ( func (p *Packager) resetRegistryHPA(ctx context.Context) { if p.isConnectedToCluster() && p.hpaModified { if err := p.cluster.EnableRegHPAScaleDown(ctx); err != nil { - message.Debugf("unable to reenable the registry HPA scale down: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Error("Unable to reenable the registry HPA scale down", "error", err) } } } @@ -139,6 +140,8 @@ func (p *Packager) Deploy(ctx context.Context) error { // deployComponents loops through a list of ZarfComponents and deploys them. func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []types.DeployedComponent, err error) { + log := logging.FromContextOrDiscard(ctx) + // Process all the components we are deploying for _, component := range p.cfg.Pkg.Components { // Connect to cluster if a component requires it. @@ -170,7 +173,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t if p.isConnectedToCluster() { deployedComponent.InstalledCharts, err = p.cluster.GetInstalledChartsForComponent(ctx, p.cfg.Pkg.Metadata.Name, component) if err != nil { - message.Debugf("Unable to fetch installed Helm charts for component '%s': %s", component.Name, err.Error()) + log.Debug("unable to fetch installed Helm charts for the component", "component", component.Name, "error", err) } } @@ -180,7 +183,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t // Update the package secret to indicate that we are attempting to deploy this component if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %s: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -197,7 +200,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t onFailure := func() { if err := actions.Run(ctx, onDeploy.Defaults, onDeploy.OnFailure, p.variableConfig); err != nil { - message.Debugf("unable to run component failure action: %s", err.Error()) + log.Debug("unable to run component failure action", "error", err) } } @@ -208,7 +211,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t deployedComponents[idx].Status = types.ComponentStatusFailed if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -220,7 +223,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t deployedComponents[idx].Status = types.ComponentStatusSucceeded if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -288,6 +291,8 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z // Deploy a Zarf Component. func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfComponent, noImgChecksum bool, noImgPush bool) (charts []types.InstalledChart, err error) { + log := logging.FromContextOrDiscard(ctx) + // Toggles for general deploy operations componentPath := p.layout.Components.Dirs[component.Name] @@ -314,14 +319,14 @@ func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfC // Disable the registry HPA scale down if we are deploying images and it is not already disabled if hasImages && !p.hpaModified && p.state.RegistryInfo.IsInternal() { if err := p.cluster.DisableRegHPAScaleDown(ctx); err != nil { - message.Debugf("unable to disable the registry HPA scale down: %s", err.Error()) + log.Error("Unable to disable the registry HPA scale down", "error", err.Error()) } else { p.hpaModified = true } } } - err = p.populateComponentAndStateTemplates(component.Name) + err = p.populateComponentAndStateTemplates(ctx, component.Name) if err != nil { return charts, err } @@ -505,8 +510,8 @@ func (p *Packager) setupState(ctx context.Context) (err error) { return nil } -func (p *Packager) populateComponentAndStateTemplates(componentName string) error { - applicationTemplates, err := template.GetZarfTemplates(componentName, p.state) +func (p *Packager) populateComponentAndStateTemplates(ctx context.Context, componentName string) error { + applicationTemplates, err := template.GetZarfTemplates(ctx, componentName, p.state) if err != nil { return err } diff --git a/src/pkg/packager/deploy_test.go b/src/pkg/packager/deploy_test.go index a1f32aa346..40d2bb3d7d 100644 --- a/src/pkg/packager/deploy_test.go +++ b/src/pkg/packager/deploy_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/pkg/packager/sources" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" ) @@ -212,7 +213,7 @@ func TestGenerateValuesOverrides(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - p, err := New(&types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{})) + p, err := New(testutil.TestContext(t), &types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{})) require.NoError(t, err) for k, v := range tt.setVariables { p.variableConfig.SetVariable(k, v, false, false, v1alpha1.RawVariableType) diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index 37b80f0ec4..0581c022b8 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -29,6 +29,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/images" "github.com/zarf-dev/zarf/src/internal/packager/kustomize" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/creator" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -75,6 +76,8 @@ 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) { + log := logging.FromContextOrDiscard(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, " + @@ -140,7 +143,7 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) if err != nil { return nil, err } - err = p.populateComponentAndStateTemplates(component.Name) + err = p.populateComponentAndStateTemplates(ctx, component.Name) if err != nil { return nil, err } @@ -288,10 +291,10 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) for _, image := range sortedExpectedImages { 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) + log.Error("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) + log.Debug("Image digest found", "digest", descriptor.Digest) validImages = append(validImages, image) } } @@ -395,7 +398,6 @@ func processUnstructuredImages(resource *unstructured.Unstructured, matchedImage // Capture any custom images matches := imageCheck.FindAllStringSubmatch(string(b), -1) for _, group := range matches { - message.Debugf("Found unknown match, Kind: %s, Value: %s", resource.GetKind(), group[1]) matchedImages[group[1]] = true } } @@ -403,7 +405,6 @@ func processUnstructuredImages(resource *unstructured.Unstructured, matchedImage // Capture "maybe images" too for all kinds because they might be in unexpected places.... 👀 matches := imageFuzzyCheck.FindAllStringSubmatch(string(b), -1) for _, group := range matches { - message.Debugf("Found possible fuzzy match, Kind: %s, Value: %s", resource.GetKind(), group[1]) maybeImages[group[1]] = true } diff --git a/src/pkg/packager/prepare_test.go b/src/pkg/packager/prepare_test.go index f2fb760534..d860d842ef 100644 --- a/src/pkg/packager/prepare_test.go +++ b/src/pkg/packager/prepare_test.go @@ -91,7 +91,7 @@ func TestFindImages(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p, err := New(tt.cfg) + p, err := New(ctx, tt.cfg) require.NoError(t, err) images, err := p.FindImages(ctx) if tt.expectedErr != "" { diff --git a/src/pkg/packager/remove.go b/src/pkg/packager/remove.go index 4edcdd5b05..54fd983ae6 100644 --- a/src/pkg/packager/remove.go +++ b/src/pkg/packager/remove.go @@ -22,6 +22,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/packager/helm" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -171,7 +172,7 @@ func (p *Packager) removeComponent(ctx context.Context, deployedPackage *types.D onRemove := c.Actions.OnRemove onFailure := func() { if err := actions.Run(ctx, onRemove.Defaults, onRemove.OnFailure, nil); err != nil { - message.Debugf("Unable to run the failure action: %s", err) + logging.FromContextOrDiscard(ctx).Error("unable to run the failure action", "error", err) } } diff --git a/src/pkg/packager/sources/validate.go b/src/pkg/packager/sources/validate.go index 1c7914ea69..d6a2e4ba39 100644 --- a/src/pkg/packager/sources/validate.go +++ b/src/pkg/packager/sources/validate.go @@ -17,7 +17,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/utils" ) @@ -36,7 +36,7 @@ func ValidatePackageSignature(ctx context.Context, paths *layout.PackagePaths, p } if publicKeyPath != "" { - message.Debugf("Using public key %q for signature validation", publicKeyPath) + logging.FromContextOrDiscard(ctx).Debug("using public key for signature validation", "path", publicKeyPath) } // Handle situations where there is no signature within the package diff --git a/src/pkg/utils/bytes.go b/src/pkg/utils/bytes.go index 7dd159b91f..2fc7930d47 100644 --- a/src/pkg/utils/bytes.go +++ b/src/pkg/utils/bytes.go @@ -7,12 +7,14 @@ package utils import ( + "context" "fmt" "math" "strconv" "time" "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" ) @@ -59,7 +61,9 @@ func ByteFormat(inputNum float64, precision int) string { // RenderProgressBarForLocalDirWrite creates a progress bar that continuously tracks the progress of writing files to a local directory and all of its subdirectories. // NOTE: This function runs infinitely until either completeChan or errChan is triggered, this function should be run in a goroutine while a different thread/process is writing to the directory. -func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, completeChan chan error, updateText string, successText string) { +func RenderProgressBarForLocalDirWrite(ctx context.Context, filepath string, expectedTotal int64, completeChan chan error, updateText string, successText string) { + log := logging.FromContextOrDiscard(ctx) + // Create a progress bar title := fmt.Sprintf("%s (%s of %s)", updateText, ByteFormat(float64(0), 2), ByteFormat(float64(expectedTotal), 2)) progressBar := message.NewProgressBar(expectedTotal, title) @@ -74,7 +78,7 @@ func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, com return } else { if err := progressBar.Close(); err != nil { - message.Debugf("unable to close progress bar: %s", err.Error()) + log.Error("Unable to close progress bar", "error", err) } completeChan <- nil return @@ -83,7 +87,7 @@ func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, com // Read the directory size currentBytes, dirErr := helpers.GetDirSize(filepath) if dirErr != nil { - message.Debugf("unable to get updated progress: %s", dirErr.Error()) + log.Error("Unable to get updated progress", "error", dirErr) time.Sleep(200 * time.Millisecond) continue } diff --git a/src/pkg/utils/cosign.go b/src/pkg/utils/cosign.go index 1e129ffb5c..7b2800b6cf 100644 --- a/src/pkg/utils/cosign.go +++ b/src/pkg/utils/cosign.go @@ -16,6 +16,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/pkg/errors" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio" @@ -37,6 +38,8 @@ import ( // // Forked from https://github.com/sigstore/cosign/blob/v1.7.1/pkg/sget/sget.go func Sget(ctx context.Context, image, key string, out io.Writer) error { + log := logging.FromContextOrDiscard(ctx) + message.Warnf(lang.WarnSGetDeprecation) // Remove the custom protocol header from the url @@ -131,11 +134,11 @@ func Sget(ctx context.Context, image, key string, out io.Writer) error { for _, sig := range sp { if cert, err := sig.Cert(); err == nil && cert != nil { - message.Debugf("Certificate subject: %s", cert.Subject) + log.Info("Certificate subject", "subject", cert.Subject) ce := cosign.CertExtensions{Cert: cert} if issuerURL := ce.GetIssuer(); issuerURL != "" { - message.Debugf("Certificate issuer URL: %s", issuerURL) + log.Debug("Certificate issues", "url", issuerURL) } } @@ -144,7 +147,7 @@ func Sget(ctx context.Context, image, key string, out io.Writer) error { spinner.Errorf(err, "Error getting payload") return err } - message.Debug(string(p)) + log.Debug(string(p)) } // TODO(mattmoor): Depending on what this is, use the higher-level stuff. diff --git a/src/pkg/utils/wait.go b/src/pkg/utils/wait.go index dbcbbf0267..822adf1b92 100644 --- a/src/pkg/utils/wait.go +++ b/src/pkg/utils/wait.go @@ -5,6 +5,7 @@ package utils import ( + "context" "errors" "fmt" "net" @@ -15,9 +16,9 @@ import ( "time" "github.com/zarf-dev/zarf/src/api/v1alpha1" - "github.com/zarf-dev/zarf/src/pkg/utils/exec" - + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/utils/exec" ) // isJSONPathWaitType checks if the condition is a JSONPath or condition. @@ -30,11 +31,13 @@ func isJSONPathWaitType(condition string) bool { } // ExecuteWait executes the wait-for command. -func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, timeout time.Duration) error { +func ExecuteWait(ctx context.Context, waitTimeout, waitNamespace, condition, kind, identifier string, timeout time.Duration) error { + log := logging.FromContextOrDiscard(ctx) + // Handle network endpoints. switch kind { case "http", "https", "tcp": - return waitForNetworkEndpoint(kind, identifier, condition, timeout) + return waitForNetworkEndpoint(ctx, kind, identifier, condition, timeout) } // Type of wait, condition or JSONPath @@ -96,13 +99,13 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, zarfKubectlGet := fmt.Sprintf("%s tools kubectl get %s %s %s", zarfCommand, namespaceFlag, kind, identifier) stdout, stderr, err := exec.Cmd(shell, append(shellArgs, zarfKubectlGet)...) if err != nil { - message.Debug(stdout, stderr, err) + log.Debug(zarfKubectlGet, "stdout", stdout, "stderr", stderr, "error", err) continue } resourceNotFound := strings.Contains(stderr, "No resources found") && identifier == "" if resourceNotFound { - message.Debug(stdout, stderr, err) + log.Debug("could not get Kubernetes resource", "stdout", stdout, "stderr", stderr, "error", err) continue } @@ -120,7 +123,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, // If there is an error, log it and try again. if stdout, stderr, err := exec.Cmd(shell, append(shellArgs, zarfKubectlWait)...); err != nil { - message.Debug(stdout, stderr, err) + log.Debug("could not wait for Kubernetes resource", "stdout", stdout, "stderr", stderr, "error", err) continue } @@ -132,7 +135,9 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, } // waitForNetworkEndpoint waits for a network endpoint to respond. -func waitForNetworkEndpoint(resource, name, condition string, timeout time.Duration) error { +func waitForNetworkEndpoint(ctx context.Context, resource, name, condition string, timeout time.Duration) error { + log := logging.FromContextOrDiscard(ctx) + // Set the timeout for the wait-for command. expired := time.After(timeout) @@ -167,7 +172,7 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // If the status code is not in the 2xx range, try again. if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 { - message.Debug(err) + log.Debug("network request failed", "error", err) continue } @@ -187,14 +192,14 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // Try to get the URL and check the status code. resp, err := http.Get(url) if err != nil || resp.StatusCode != code { - message.Debug(err) + log.Debug("network request failed", "error", err) continue } default: // Fallback to any generic protocol using net.Dial conn, err := net.Dial(resource, name) if err != nil { - message.Debug(err) + log.Debug("network connection failed", "error", err) continue } defer conn.Close() diff --git a/src/pkg/zoci/pull.go b/src/pkg/zoci/pull.go index 9fd76e9ccc..90f3fa5ebc 100644 --- a/src/pkg/zoci/pull.go +++ b/src/pkg/zoci/pull.go @@ -56,7 +56,7 @@ func (r *Remote) PullPackage(ctx context.Context, destinationDir string, concurr successText := fmt.Sprintf("Pulling %q", helpers.OCIURLPrefix+r.Repo().Reference.String()) layerSize := oci.SumDescsSize(layersToPull) - go utils.RenderProgressBarForLocalDirWrite(destinationDir, layerSize, doneSaving, "Pulling", successText) + go utils.RenderProgressBarForLocalDirWrite(ctx, destinationDir, layerSize, doneSaving, "Pulling", successText) dst, err := file.New(destinationDir) if err != nil { From 6fcbe7d60189a0a852d735f51716d0f9e3924386 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Wed, 7 Aug 2024 11:40:15 +0200 Subject: [PATCH 2/2] refactor: replace message.Warn with context logger Signed-off-by: Philip Laine --- src/cmd/destroy.go | 13 ++++++----- src/cmd/dev.go | 8 +++---- src/cmd/internal.go | 3 ++- src/cmd/package.go | 2 +- src/cmd/root.go | 5 +++-- src/cmd/tools/crane.go | 3 ++- src/cmd/tools/zarf.go | 10 +++++---- src/config/lang/english.go | 22 ++++--------------- .../agent/hooks/argocd-application_test.go | 7 +++--- .../agent/hooks/argocd-repository_test.go | 7 +++--- src/internal/agent/hooks/flux-gitrepo_test.go | 7 +++--- src/internal/agent/hooks/flux-helmrepo.go | 7 +++--- .../agent/hooks/flux-helmrepo_test.go | 7 +++--- src/internal/agent/hooks/flux-ocirepo.go | 7 +++--- src/internal/agent/hooks/flux-ocirepo_test.go | 6 ++--- src/internal/agent/hooks/pods_test.go | 6 ++--- src/internal/agent/http/admission/handler.go | 14 +++++++----- src/internal/agent/start.go | 12 +++++----- src/internal/packager/helm/chart.go | 11 +++++----- src/internal/packager/helm/post-render.go | 11 +++++----- src/internal/packager/helm/repo.go | 14 +----------- src/internal/packager/images/pull.go | 15 +++++++------ src/pkg/cluster/data.go | 3 +-- src/pkg/cluster/state.go | 11 +++++----- src/pkg/message/message.go | 6 ----- src/pkg/packager/common.go | 8 ++++--- src/pkg/packager/create.go | 2 +- src/pkg/packager/creator/skeleton.go | 3 ++- src/pkg/packager/deploy.go | 6 ++--- src/pkg/packager/dev.go | 3 ++- src/pkg/packager/generate.go | 7 ++++-- src/pkg/packager/interactive.go | 10 ++++++--- src/pkg/packager/mirror.go | 2 +- src/pkg/packager/prepare.go | 6 +++-- src/pkg/packager/remove.go | 14 ++++++------ src/pkg/packager/sources/oci.go | 3 ++- src/pkg/packager/sources/tarball.go | 3 ++- src/pkg/transform/git.go | 6 +++-- src/pkg/transform/git_test.go | 6 +++-- src/pkg/transform/types.go | 7 ------ src/pkg/utils/cosign.go | 8 +++---- 41 files changed, 155 insertions(+), 156 deletions(-) delete mode 100644 src/pkg/transform/types.go diff --git a/src/cmd/destroy.go b/src/cmd/destroy.go index 1b71740aa3..72c0d21f8f 100644 --- a/src/cmd/destroy.go +++ b/src/cmd/destroy.go @@ -11,15 +11,16 @@ import ( "os" "regexp" + "github.com/spf13/cobra" + "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/packager/helm" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/utils/exec" - - "github.com/spf13/cobra" ) var confirmDestroy bool @@ -32,6 +33,8 @@ var destroyCmd = &cobra.Command{ Long: lang.CmdDestroyLong, RunE: func(cmd *cobra.Command, _ []string) error { ctx := cmd.Context() + log := logging.FromContextOrDiscard(ctx) + timeoutCtx, cancel := context.WithTimeout(cmd.Context(), cluster.DefaultTimeout) defer cancel() c, err := cluster.NewClusterWithWait(timeoutCtx) @@ -44,7 +47,7 @@ var destroyCmd = &cobra.Command{ // the scripts to remove k3s, we will still try to remove a locally installed k3s cluster state, err := c.LoadZarfState(ctx) if err != nil { - message.WarnErr(err, err.Error()) + log.Warn("could not load Zarf state", "error", err) } // If Zarf deployed the cluster, burn it all down @@ -63,7 +66,7 @@ var destroyCmd = &cobra.Command{ // Run the matched script err := exec.CmdWithPrint(script) if errors.Is(err, os.ErrPermission) { - message.Warnf(lang.CmdDestroyErrScriptPermissionDenied, script) + log.Warn("Received 'permission denied' when trying to execute the script (%s). Please double-check you have the correct kube-context.", "script", script) // Don't remove scripts we can't execute so the user can try to manually run continue diff --git a/src/cmd/dev.go b/src/cmd/dev.go index f561260f45..9557a56084 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -23,7 +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/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/packager" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -97,7 +97,7 @@ var devTransformGitLinksCmd = &cobra.Command{ Aliases: []string{"p"}, Short: lang.CmdDevPatchGitShort, Args: cobra.ExactArgs(2), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { host, fileName := args[0], args[1] // Read the contents of the given file @@ -110,7 +110,7 @@ var devTransformGitLinksCmd = &cobra.Command{ // Perform git url transformation via regex text := string(content) - processedText := transform.MutateGitURLsInText(message.Warnf, pkgConfig.InitOpts.GitServer.Address, text, pkgConfig.InitOpts.GitServer.PushUsername) + processedText := transform.MutateGitURLsInText(cmd.Context(), pkgConfig.InitOpts.GitServer.Address, text, pkgConfig.InitOpts.GitServer.PushUsername) // Print the differences dmp := diffmatchpatch.New() @@ -153,7 +153,7 @@ var devSha256SumCmd = &cobra.Command{ var err error if helpers.IsURL(fileName) { - message.Warn(lang.CmdDevSha256sumRemoteWarning) + logging.FromContextOrDiscard(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 { diff --git a/src/cmd/internal.go b/src/cmd/internal.go index 4a393d01e6..4ecb35b08f 100644 --- a/src/cmd/internal.go +++ b/src/cmd/internal.go @@ -24,6 +24,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/agent" "github.com/zarf-dev/zarf/src/internal/gitea" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -336,7 +337,7 @@ var updateGiteaPVC = &cobra.Command{ // There is a possibility that the pvc does not yet exist and Gitea helm chart should create it helmShouldCreate, err := c.UpdateGiteaPVC(ctx, pvcName, rollback) if err != nil { - message.WarnErr(err, lang.CmdInternalUpdateGiteaPVCErr) + logging.FromContextOrDiscard(ctx).Warn("Unable to update the existing Gitea persistent volume claim.", "error", err) } fmt.Print(helmShouldCreate) return nil diff --git a/src/cmd/package.go b/src/cmd/package.go index b98331610f..2048156d1c 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -47,7 +47,7 @@ var packageCreateCmd = &cobra.Command{ var isCleanPathRegex = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~\\:]+$`) if !isCleanPathRegex.MatchString(config.CommonOptions.CachePath) { - message.Warnf(lang.CmdPackageCreateCleanPathErr, config.ZarfDefaultCachePath) + logging.FromContextOrDiscard(cmd.Context()).Warn("Invalid characters in the Zarf cache path, defaulting to path", "default", config.ZarfDefaultCachePath) config.CommonOptions.CachePath = config.ZarfDefaultCachePath } diff --git a/src/cmd/root.go b/src/cmd/root.go index 41090c1cd4..48d2005de1 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -76,12 +76,13 @@ var rootCmd = &cobra.Command{ _, _ = fmt.Fprintln(os.Stderr, zarfLogo) cmd.Help() + log := logging.FromContextOrDiscard(cmd.Context()) if len(args) > 0 { if strings.Contains(args[0], config.ZarfPackagePrefix) || strings.Contains(args[0], "zarf-init") { - message.Warnf(lang.RootCmdDeprecatedDeploy, args[0]) + log.Warn("Deprecated: Please use \"zarf package deploy\" to deploy this package. This warning will be removed in Zarf v1.0.0.") } if args[0] == layout.ZarfYAML { - message.Warn(lang.RootCmdDeprecatedCreate) + log.Warn("Deprecated: Please use \"zarf package create\" to create this package. This warning will be removed in Zarf v1.0.0.") } } }, diff --git a/src/cmd/tools/crane.go b/src/cmd/tools/crane.go index dd76955339..d4d008435c 100644 --- a/src/cmd/tools/crane.go +++ b/src/cmd/tools/crane.go @@ -20,6 +20,7 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/packager/images" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" @@ -176,7 +177,7 @@ func zarfCraneInternalWrapper(commandToWrap func(*[]crane.Option) *cobra.Command zarfState, err := c.LoadZarfState(ctx) if err != nil { - message.Warnf("could not get Zarf state from Kubernetes cluster, continuing without state information %s", err.Error()) + logging.FromContextOrDiscard(cmd.Context()).Warn("could not get Zarf state from Kubernetes cluster, continuing without state information", "error", err) return originalListFn(cmd, args) } diff --git a/src/cmd/tools/zarf.go b/src/cmd/tools/zarf.go index 5a9cd11718..4367c3beab 100644 --- a/src/cmd/tools/zarf.go +++ b/src/cmd/tools/zarf.go @@ -24,6 +24,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/helm" "github.com/zarf-dev/zarf/src/internal/packager/template" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/sources" "github.com/zarf-dev/zarf/src/pkg/pki" @@ -40,8 +41,8 @@ var deprecatedGetGitCredsCmd = &cobra.Command{ Hidden: true, Short: lang.CmdToolsGetGitPasswdShort, Long: lang.CmdToolsGetGitPasswdLong, - Run: func(_ *cobra.Command, _ []string) { - message.Warn(lang.CmdToolsGetGitPasswdDeprecation) + Run: func(cmd *cobra.Command, _ []string) { + logging.FromContextOrDiscard(cmd.Context()).Warn("Deprecated: This command has been replaced by 'zarf tools get-creds git' and will be removed in Zarf v1.0.0.") getCredsCmd.Run(getCredsCmd, []string{"git"}) }, } @@ -101,6 +102,7 @@ var updateCredsCmd = &cobra.Command{ } ctx := cmd.Context() + log := logging.FromContextOrDiscard(ctx) timeoutCtx, cancel := context.WithTimeout(ctx, cluster.DefaultTimeout) defer cancel() @@ -179,7 +181,7 @@ var updateCredsCmd = &cobra.Command{ err = h.UpdateZarfRegistryValues(ctx) if err != nil { // Warn if we couldn't actually update the registry (it might not be installed and we should try to continue) - message.Warnf(lang.CmdToolsUpdateCredsUnableUpdateRegistry, err.Error()) + log.Warn("Unable to update Zarf Registry values", "error", err) } } if slices.Contains(args, message.GitKey) && newState.GitServer.IsInternal() && internalGitServerExists { @@ -192,7 +194,7 @@ var updateCredsCmd = &cobra.Command{ err = h.UpdateZarfAgentValues(ctx) if err != nil { // Warn if we couldn't actually update the agent (it might not be installed and we should try to continue) - message.Warnf(lang.CmdToolsUpdateCredsUnableUpdateAgent, err.Error()) + log.Warn("Unable to update Zarf Agent TLS secrets", "error", err) } } } diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 25cde72908..1883161916 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -54,9 +54,6 @@ const ( RootCmdFlagTempDir = "Specify the temporary directory to use for intermediate files" RootCmdFlagInsecure = "Allow access to insecure registries and disable other recommended security enforcements such as package checksum and signature validation. This flag should only be used if you have a specific reason and accept the reduced security posture." - RootCmdDeprecatedDeploy = "Deprecated: Please use \"zarf package deploy %s\" to deploy this package. This warning will be removed in Zarf v1.0.0." - RootCmdDeprecatedCreate = "Deprecated: Please use \"zarf package create\" to create this package. This warning will be removed in Zarf v1.0.0." - // zarf connect CmdConnectShort = "Accesses services or pods deployed in the cluster" CmdConnectLong = "Uses a k8s port-forward to connect to resources within the cluster referenced by your kube-context.\n" + @@ -101,8 +98,6 @@ const ( CmdDestroyFlagConfirm = "REQUIRED. Confirm the destroy action to prevent accidental deletions" CmdDestroyFlagRemoveComponents = "Also remove any installed components outside the zarf namespace" - CmdDestroyErrScriptPermissionDenied = "Received 'permission denied' when trying to execute the script (%s). Please double-check you have the correct kube-context." - // zarf init CmdInitShort = "Prepares a k8s cluster for the deployment of Zarf packages" CmdInitLong = "Injects an OCI registry as well as an optional git server " + @@ -202,7 +197,6 @@ $ zarf init --artifact-push-password={PASSWORD} --artifact-push-username={USERNA CmdInternalUpdateGiteaPVCShort = "Updates an existing Gitea persistent volume claim" CmdInternalUpdateGiteaPVCLong = "Updates an existing Gitea persistent volume claim by assessing if claim is a custom user provided claim or default." + "This is called internally by the supported Gitea package component." - CmdInternalUpdateGiteaPVCErr = "Unable to update the existing Gitea persistent volume claim." CmdInternalFlagUpdateGiteaPVCRollback = "Roll back previous Gitea persistent volume claim updates." CmdInternalIsValidHostnameShort = "Checks if the current machine's hostname is RFC1123 compliant" @@ -267,7 +261,6 @@ $ zarf package mirror-resources \ CmdPackageCreateFlagDifferential = "[beta] Build a package that only contains the differential changes from local resources and differing remote resources from the specified previously built package" CmdPackageCreateFlagRegistryOverride = "Specify a map of domains to override on package create when pulling images (e.g. --registry-override docker.io=dockerio-reg.enterprise.intranet)" CmdPackageCreateFlagFlavor = "The flavor of components to include in the resulting package (i.e. have a matching or empty \"only.flavor\" key)" - CmdPackageCreateCleanPathErr = "Invalid characters in Zarf cache path, defaulting to %s" CmdPackageDeployFlagConfirm = "Confirms package deployment without prompting. ONLY use with packages you trust. Skips prompts to review SBOM, configure variables, select optional components and review potential breaking changes." CmdPackageDeployFlagAdoptExistingResources = "Adopts any pre-existing K8s resources into the Helm charts managed by Zarf. ONLY use when you have existing deployments you want Zarf to takeover." @@ -332,8 +325,7 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a sk "This should only be used for manifests that are not mutated by the Zarf Agent Mutating Webhook." CmdDevPatchGitOverwritePrompt = "Overwrite the file %s with these changes?" - CmdDevSha256sumShort = "Generates a SHA256SUM for the given file" - CmdDevSha256sumRemoteWarning = "This is a remote source. If a published checksum is available you should use that rather than calculating it directly from the remote link." + CmdDevSha256sumShort = "Generates a SHA256SUM for the given file" CmdDevFindImagesShort = "Evaluates components in a Zarf file to identify images specified in their helm charts and manifests" CmdDevFindImagesLong = "Evaluates components in a Zarf file to identify images specified in their helm charts and manifests.\n\n" + @@ -429,10 +421,9 @@ $ zarf tools registry digest reg.example.com/stefanprodan/podinfo:6.4.0 CmdToolsRegistryFlagNonDist = "Allow pushing non-distributable (foreign) layers" CmdToolsRegistryFlagPlatform = "Specifies the platform in the form os/arch[/variant][:osversion] (e.g. linux/amd64)." - CmdToolsGetGitPasswdShort = "[Deprecated] Returns the push user's password for the Git server" - CmdToolsGetGitPasswdLong = "[Deprecated] Reads the password for a user with push access to the configured Git server in Zarf State. Note that this command has been replaced by 'zarf tools get-creds git' and will be removed in Zarf v1.0.0." - CmdToolsGetGitPasswdDeprecation = "Deprecated: This command has been replaced by 'zarf tools get-creds git' and will be removed in Zarf v1.0.0." - CmdToolsYqExample = ` + CmdToolsGetGitPasswdShort = "[Deprecated] Returns the push user's password for the Git server" + CmdToolsGetGitPasswdLong = "[Deprecated] Reads the password for a user with push access to the configured Git server in Zarf State. Note that this command has been replaced by 'zarf tools get-creds git' and will be removed in Zarf v1.0.0." + CmdToolsYqExample = ` # yq defaults to 'eval' command if no command is specified. See "zarf tools yq eval --help" for more examples. # read the "stuff" node from "myfile.yml" @@ -623,8 +614,3 @@ var ( ErrUnableToCheckArch = errors.New("unable to get the configured cluster's architecture") ErrUnableToGetPackages = errors.New("unable to load the Zarf Package data from the cluster") ) - -// Collection of reusable warn messages. -var ( - WarnSGetDeprecation = "Using sget to download resources is being deprecated and will removed in the v1.0.0 release of Zarf. Please publish the packages as OCI artifacts instead." -) diff --git a/src/internal/agent/hooks/argocd-application_test.go b/src/internal/agent/hooks/argocd-application_test.go index 31ec452959..22c78dd5b0 100644 --- a/src/internal/agent/hooks/argocd-application_test.go +++ b/src/internal/agent/hooks/argocd-application_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" "encoding/json" "net/http" "testing" @@ -12,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" "k8s.io/apimachinery/pkg/runtime" @@ -32,13 +32,14 @@ func createArgoAppAdmissionRequest(t *testing.T, op v1.Operation, argoApp *Appli func TestArgoAppWebhook(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := testutil.TestContext(t) + state := &types.ZarfState{GitServer: types.GitServerInfo{ Address: "https://git-server.com", PushUsername: "a-push-user", }} c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewApplicationMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewApplicationMutationHook(ctx, c)) tests := []admissionTest{ { diff --git a/src/internal/agent/hooks/argocd-repository_test.go b/src/internal/agent/hooks/argocd-repository_test.go index fdc99fe1c2..ac6aa0841b 100644 --- a/src/internal/agent/hooks/argocd-repository_test.go +++ b/src/internal/agent/hooks/argocd-repository_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" b64 "encoding/base64" "encoding/json" "net/http" @@ -13,6 +12,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" corev1 "k8s.io/api/core/v1" @@ -35,7 +35,8 @@ func createArgoRepoAdmissionRequest(t *testing.T, op v1.Operation, argoRepo *cor func TestArgoRepoWebhook(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := testutil.TestContext(t) + state := &types.ZarfState{GitServer: types.GitServerInfo{ Address: "https://git-server.com", PushUsername: "a-push-user", @@ -43,7 +44,7 @@ func TestArgoRepoWebhook(t *testing.T) { PullUsername: "a-pull-user", }} c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewRepositorySecretMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewRepositorySecretMutationHook(ctx, c)) tests := []admissionTest{ { diff --git a/src/internal/agent/hooks/flux-gitrepo_test.go b/src/internal/agent/hooks/flux-gitrepo_test.go index dc9c17a093..896dd29c72 100644 --- a/src/internal/agent/hooks/flux-gitrepo_test.go +++ b/src/internal/agent/hooks/flux-gitrepo_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" "encoding/json" "net/http" "testing" @@ -15,6 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -36,13 +36,14 @@ func createFluxGitRepoAdmissionRequest(t *testing.T, op v1.Operation, fluxGitRep func TestFluxMutationWebhook(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := testutil.TestContext(t) + state := &types.ZarfState{GitServer: types.GitServerInfo{ Address: "https://git-server.com", PushUsername: "a-push-user", }} c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewGitRepositoryMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewGitRepositoryMutationHook(ctx, c)) tests := []admissionTest{ { diff --git a/src/internal/agent/hooks/flux-helmrepo.go b/src/internal/agent/hooks/flux-helmrepo.go index 0af950a14f..b070c01028 100644 --- a/src/internal/agent/hooks/flux-helmrepo.go +++ b/src/internal/agent/hooks/flux-helmrepo.go @@ -13,13 +13,14 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/fluxcd/pkg/apis/meta" flux "github.com/fluxcd/source-controller/api/v1" + v1 "k8s.io/api/admission/v1" + "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/transform" - v1 "k8s.io/api/admission/v1" ) // NewHelmRepositoryMutationHook creates a new instance of the helm repo mutation hook. @@ -43,7 +44,7 @@ func mutateHelmRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluste // If we see a type of helm repo other than OCI we should flag a warning and return if strings.ToLower(src.Spec.Type) != "oci" { - message.Warnf(lang.AgentWarnNotOCIType, src.Spec.Type) + logging.FromContextOrDiscard(ctx).Warn("Skipping HelmRepo mutation because the type is not OCI", "type", src.Spec.Type) return &operations.Result{Allowed: true}, nil } diff --git a/src/internal/agent/hooks/flux-helmrepo_test.go b/src/internal/agent/hooks/flux-helmrepo_test.go index d0e48a0074..2202f923d1 100644 --- a/src/internal/agent/hooks/flux-helmrepo_test.go +++ b/src/internal/agent/hooks/flux-helmrepo_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" "encoding/json" "net/http" "testing" @@ -15,6 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" corev1 "k8s.io/api/core/v1" @@ -37,7 +37,8 @@ func createFluxHelmRepoAdmissionRequest(t *testing.T, op v1.Operation, fluxHelmR func TestFluxHelmMutationWebhook(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := testutil.TestContext(t) + state := &types.ZarfState{RegistryInfo: types.RegistryInfo{Address: "127.0.0.1:31999"}} tests := []admissionTest{ @@ -167,7 +168,7 @@ func TestFluxHelmMutationWebhook(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewHelmRepositoryMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewHelmRepositoryMutationHook(ctx, c)) if tt.svc != nil { _, err := c.Clientset.CoreV1().Services("zarf").Create(ctx, tt.svc, metav1.CreateOptions{}) require.NoError(t, err) diff --git a/src/internal/agent/hooks/flux-ocirepo.go b/src/internal/agent/hooks/flux-ocirepo.go index 770596f760..f9b172ecfd 100644 --- a/src/internal/agent/hooks/flux-ocirepo.go +++ b/src/internal/agent/hooks/flux-ocirepo.go @@ -12,13 +12,14 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/fluxcd/pkg/apis/meta" flux "github.com/fluxcd/source-controller/api/v1beta2" + v1 "k8s.io/api/admission/v1" + "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/transform" - v1 "k8s.io/api/admission/v1" ) // NewOCIRepositoryMutationHook creates a new instance of the oci repo mutation hook. @@ -47,7 +48,7 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster // If we have a semver we want to continue since we will still have the upstream tag // but should warn that we can't guarantee there won't be collisions if src.Spec.Reference.SemVer != "" { - message.Warnf(lang.AgentWarnSemVerRef, src.Spec.Reference.SemVer) + logging.FromContextOrDiscard(ctx).Warn("Detected a smever OCI ref, continuing but will be unable to guarantee against collisions if multiple OCI artifacts with the same name are broudt in from different registries", "semver", src.Spec.Reference.SemVer) } if src.Labels != nil && src.Labels["zarf-agent"] == "patched" { diff --git a/src/internal/agent/hooks/flux-ocirepo_test.go b/src/internal/agent/hooks/flux-ocirepo_test.go index 5cab4d2530..1ec3b0ed4d 100644 --- a/src/internal/agent/hooks/flux-ocirepo_test.go +++ b/src/internal/agent/hooks/flux-ocirepo_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" "encoding/json" "net/http" "testing" @@ -15,6 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" corev1 "k8s.io/api/core/v1" @@ -189,14 +189,14 @@ func TestFluxOCIMutationWebhook(t *testing.T) { }, } - ctx := context.Background() + ctx := testutil.TestContext(t) state := &types.ZarfState{RegistryInfo: types.RegistryInfo{Address: "127.0.0.1:31999"}} for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewOCIRepositoryMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewOCIRepositoryMutationHook(ctx, c)) if tt.svc != nil { _, err := c.Clientset.CoreV1().Services("zarf").Create(ctx, tt.svc, metav1.CreateOptions{}) require.NoError(t, err) diff --git a/src/internal/agent/hooks/pods_test.go b/src/internal/agent/hooks/pods_test.go index 60bd09d0f5..a668e782dd 100644 --- a/src/internal/agent/hooks/pods_test.go +++ b/src/internal/agent/hooks/pods_test.go @@ -4,7 +4,6 @@ package hooks import ( - "context" "encoding/json" "net/http" "testing" @@ -13,6 +12,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" corev1 "k8s.io/api/core/v1" @@ -35,11 +35,11 @@ func createPodAdmissionRequest(t *testing.T, op v1.Operation, pod *corev1.Pod) * func TestPodMutationWebhook(t *testing.T) { t.Parallel() - ctx := context.Background() + ctx := testutil.TestContext(t) state := &types.ZarfState{RegistryInfo: types.RegistryInfo{Address: "127.0.0.1:31999"}} c := createTestClientWithZarfState(ctx, t, state) - handler := admission.NewHandler().Serve(NewPodMutationHook(ctx, c)) + handler := admission.NewHandler().Serve(ctx, NewPodMutationHook(ctx, c)) tests := []admissionTest{ { diff --git a/src/internal/agent/http/admission/handler.go b/src/internal/agent/http/admission/handler.go index 4b4d69323b..e435cb84bd 100644 --- a/src/internal/agent/http/admission/handler.go +++ b/src/internal/agent/http/admission/handler.go @@ -7,6 +7,7 @@ package admission import ( + "context" "encoding/json" "fmt" "io" @@ -14,6 +15,7 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" corev1 "k8s.io/api/admission/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -34,7 +36,9 @@ func NewHandler() *Handler { } // Serve returns an http.HandlerFunc for an admission webhook. -func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { +func (h *Handler) Serve(ctx context.Context, hook operations.Hook) http.HandlerFunc { + log := logging.FromContextOrDiscard(ctx) + return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r.Method != http.MethodPost { @@ -70,7 +74,7 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { Kind: "AdmissionReview", } if err != nil { - message.Warnf("%s: %s", lang.AgentErrBindHandler, err.Error()) + log.Warn("Unable to bind the webhook handler", "error", err) admissionResponse := corev1.AdmissionReview{ TypeMeta: admissionMeta, Response: &corev1.AdmissionResponse{ @@ -79,7 +83,7 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { } jsonResponse, err := json.Marshal(admissionResponse) if err != nil { - message.WarnErr(err, lang.AgentErrMarshalResponse) + log.Warn(lang.AgentErrMarshalResponse, "error", err) http.Error(w, lang.AgentErrMarshalResponse, http.StatusInternalServerError) return } @@ -103,7 +107,7 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { jsonPatchType := corev1.PatchTypeJSONPatch patchBytes, err := json.Marshal(result.PatchOps) if err != nil { - message.WarnErr(err, lang.AgentErrMarshallJSONPatch) + log.Warn(lang.AgentErrMarshallJSONPatch, "error", err) http.Error(w, lang.AgentErrMarshallJSONPatch, http.StatusInternalServerError) } admissionResponse.Response.Patch = patchBytes @@ -112,7 +116,7 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { jsonResponse, err := json.Marshal(admissionResponse) if err != nil { - message.WarnErr(err, lang.AgentErrMarshalResponse) + log.Warn(lang.AgentErrMarshalResponse, "error", err) http.Error(w, lang.AgentErrMarshalResponse, http.StatusInternalServerError) return } diff --git a/src/internal/agent/start.go b/src/internal/agent/start.go index 40594d0bf3..df301a520e 100644 --- a/src/internal/agent/start.go +++ b/src/internal/agent/start.go @@ -45,12 +45,12 @@ func StartWebhook(ctx context.Context, cluster *cluster.Cluster) error { // Routers mux := http.NewServeMux() - mux.Handle("/mutate/pod", admissionHandler.Serve(podsMutation)) - mux.Handle("/mutate/flux-gitrepository", admissionHandler.Serve(fluxGitRepositoryMutation)) - mux.Handle("/mutate/flux-helmrepository", admissionHandler.Serve(fluxHelmRepositoryMutation)) - mux.Handle("/mutate/flux-ocirepository", admissionHandler.Serve(fluxOCIRepositoryMutation)) - mux.Handle("/mutate/argocd-application", admissionHandler.Serve(argocdApplicationMutation)) - mux.Handle("/mutate/argocd-repository", admissionHandler.Serve(argocdRepositoryMutation)) + mux.Handle("/mutate/pod", admissionHandler.Serve(ctx, podsMutation)) + mux.Handle("/mutate/flux-gitrepository", admissionHandler.Serve(ctx, fluxGitRepositoryMutation)) + mux.Handle("/mutate/flux-helmrepository", admissionHandler.Serve(ctx, fluxHelmRepositoryMutation)) + mux.Handle("/mutate/flux-ocirepository", admissionHandler.Serve(ctx, fluxOCIRepositoryMutation)) + mux.Handle("/mutate/argocd-application", admissionHandler.Serve(ctx, argocdApplicationMutation)) + mux.Handle("/mutate/argocd-repository", admissionHandler.Serve(ctx, argocdRepositoryMutation)) return startServer(ctx, httpPort, mux) } diff --git a/src/internal/packager/helm/chart.go b/src/internal/packager/helm/chart.go index 0a2d334a4e..720583b8d0 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -25,6 +25,7 @@ import ( "sigs.k8s.io/yaml" "github.com/zarf-dev/zarf/src/config" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -77,7 +78,7 @@ func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings, lastRelease := releases[len(releases)-1] - _, err = h.upgradeChart(lastRelease, postRender) + _, err = h.upgradeChart(ctx, lastRelease, postRender) } else { // 😭 things aren't working return fmt.Errorf("unable to verify the chart installation status: %w", histErr) @@ -284,9 +285,9 @@ func (h *Helm) installChart(postRender *renderer) (*release.Release, error) { return client.Run(loadedChart, chartValues) } -func (h *Helm) upgradeChart(lastRelease *release.Release, postRender *renderer) (*release.Release, error) { +func (h *Helm) upgradeChart(ctx context.Context, lastRelease *release.Release, postRender *renderer) (*release.Release, error) { // Migrate any deprecated APIs (if applicable) - err := h.migrateDeprecatedAPIs(lastRelease) + err := h.migrateDeprecatedAPIs(ctx, lastRelease) if err != nil { return nil, fmt.Errorf("unable to check for API deprecations: %w", err) } @@ -362,7 +363,7 @@ func (h *Helm) loadChartData() (*chart.Chart, chartutil.Values, error) { return loadedChart, chartValues, nil } -func (h *Helm) migrateDeprecatedAPIs(latestRelease *release.Release) error { +func (h *Helm) migrateDeprecatedAPIs(ctx context.Context, latestRelease *release.Release) error { // Get the Kubernetes version from the current cluster kubeVersion, err := h.cluster.Clientset.Discovery().ServerVersion() if err != nil { @@ -408,7 +409,7 @@ func (h *Helm) migrateDeprecatedAPIs(latestRelease *release.Release) error { // If the release was modified in the above loop, save it back to the cluster if modified { - message.Warnf("Zarf detected deprecated APIs for the '%s' helm release. Attempting automatic upgrade.", latestRelease.Name) + logging.FromContextOrDiscard(ctx).Warn("Zarf detected deprecated APIs for the Helm release. Attempting automatic upgrade.", "release", latestRelease.Name) // Update current release version to be superseded (same as the helm mapkubeapis plugin) latestRelease.Info.Status = release.StatusSuperseded diff --git a/src/internal/packager/helm/post-render.go b/src/internal/packager/helm/post-render.go index 309c281092..a8f96ccb14 100644 --- a/src/internal/packager/helm/post-render.go +++ b/src/internal/packager/helm/post-render.go @@ -17,7 +17,6 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/cluster" "github.com/zarf-dev/zarf/src/pkg/logging" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/types" "helm.sh/helm/v3/pkg/releaseutil" @@ -118,6 +117,8 @@ func (r *renderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) { } func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { + log := logging.FromContextOrDiscard(ctx) + c := r.cluster namespaceList, err := r.cluster.Clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) if err != nil { @@ -142,7 +143,7 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { // Refuse to adopt namespace if it is one of four initial Kubernetes namespaces. // https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#initial-namespaces if slices.Contains([]string{"default", "kube-node-lease", "kube-public", "kube-system"}, name) { - message.Warnf("Refusing to adopt the initial namespace: %s", name) + logging.FromContextOrDiscard(ctx).Warn("Refusing to adopt the initial namespace", "namespace", name) } else { // This is an existing namespace to adopt _, err := c.Clientset.CoreV1().Namespaces().Update(ctx, namespace, metav1.UpdateOptions{}) @@ -181,7 +182,7 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { return nil }() if err != nil { - message.WarnErrf(err, "Problem creating registry secret for the %s namespace", name) + log.Warn("problem creating registry secret for namespace", "namespace", name, "error", err) } // Create or update the zarf git server secret @@ -201,7 +202,7 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { return nil }() if err != nil { - message.WarnErrf(err, "Problem creating git server secret for the %s namespace", name) + log.Warn("problem creating git server secret for the namespace", "namespace", name, "error", err) } } } @@ -233,7 +234,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti namespace := &corev1.Namespace{} // parse the namespace resource so it can be applied out-of-band by zarf instead of helm to avoid helm ns shenanigans if err := runtime.DefaultUnstructuredConverter.FromUnstructured(rawData.UnstructuredContent(), namespace); err != nil { - message.WarnErrf(err, "could not parse namespace %s", rawData.GetName()) + log.Warn("could not parse namespace", "namespace", rawData.GetName(), "error", err) } else { log.Debug("matched Helm namespace for Zarf annotation", "namespace", namespace.Name) namespace.Labels = cluster.AdoptZarfManagedLabels(namespace.Labels) diff --git a/src/internal/packager/helm/repo.go b/src/internal/packager/helm/repo.go index 13a35b3cd9..37f2a9bc6d 100644 --- a/src/internal/packager/helm/repo.go +++ b/src/internal/packager/helm/repo.go @@ -6,7 +6,6 @@ package helm import ( "context" - "errors" "fmt" "os" "path/filepath" @@ -307,19 +306,8 @@ func (h *Helm) buildChartDependencies() error { // Build the deps from the helm chart err = man.Build() - var notFoundErr *downloader.ErrRepoNotFound - if errors.As(err, ¬FoundErr) { - // If we encounter a repo not found error point the user to `zarf tools helm repo add` - message.Warnf("%s. Please add the missing repo(s) via the following:", notFoundErr.Error()) - for _, repository := range notFoundErr.Repos { - message.ZarfCommand(fmt.Sprintf("tools helm repo add %s", repository)) - } - return err - } if err != nil { - message.ZarfCommand("tools helm dependency build --verify") - message.Warnf("Unable to perform a rebuild of Helm dependencies: %s", err.Error()) - return err + return fmt.Errorf("Unable to perform rebuild of Helm dependencies: %w", err) } return nil } diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 241db6486e..15982fca5f 100644 --- a/src/internal/packager/images/pull.go +++ b/src/internal/packager/images/pull.go @@ -34,6 +34,7 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -62,6 +63,8 @@ func checkForIndex(refInfo transform.Image, desc *remote.Descriptor) error { // Pull pulls all of the images from the given config. func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, error) { + log := logging.FromContextOrDiscard(ctx) + var longer string imageCount := len(cfg.ImageList) // Give some additional user feedback on larger image sets @@ -130,7 +133,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er return fmt.Errorf("rate limited by registry: %w", err) } - message.Warnf("Falling back to local 'docker', failed to find the manifest on a remote: %s", err.Error()) + log.Warn("Falling back to local 'docker', failed to find the manifest on a remote", "error", err.Error()) // Attempt to connect to the local docker daemon. cli, err := client.NewClientWithOpts(client.FromEnv) @@ -147,9 +150,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er // Warn the user if the image is large. if rawImg.Size > 750*1000*1000 { - message.Warnf("%s is %s and may take a very long time to load via docker. "+ - "See https://docs.zarf.dev/faq for suggestions on how to improve large local image loading operations.", - ref, utils.ByteFormat(float64(rawImg.Size), 2)) + log.Warn("Image is large and may yake a long time to load via docker. See https://docs.zarf.dev/faq for suggestions on how to improve large local image loading operations.", "image", ref, "size", utils.ByteFormat(float64(rawImg.Size), 2)) } // Use unbuffered opener to avoid OOM Kill issues https://github.com/zarf-dev/zarf/issues/1214. @@ -237,7 +238,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er return err }, retry.Context(ctx), retry.Attempts(2)) if err != nil { - message.Warnf("Failed to save images in parallel, falling back to sequential save: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Warn("failed to save images in parallel, falling back to sequential save", "error", err) err = retry.Do(func() error { saved, err := SaveSequential(ctx, cranePath, toPull) for k := range saved { @@ -329,7 +330,7 @@ func SaveSequential(ctx context.Context, cl clayout.Path, m map[transform.Image] } if err := cl.AppendImage(img, clayout.WithAnnotations(annotations)); err != nil { if err := CleanupInProgressLayers(ctx, img); err != nil { - message.WarnErr(err, "failed to clean up in-progress layers, please run `zarf tools clear-cache`") + logging.FromContextOrDiscard(ctx).Warn("failed to clean up in-progress layers, please run `zarf tools clear-cache`", "error", err) } return saved, err } @@ -361,7 +362,7 @@ func SaveConcurrent(ctx context.Context, cl clayout.Path, m map[transform.Image] if err := cl.WriteImage(img); err != nil { if err := CleanupInProgressLayers(ectx, img); err != nil { - message.WarnErr(err, "failed to clean up in-progress layers, please run `zarf tools clear-cache`") + logging.FromContextOrDiscard(ctx).Warn("failed to clean up in-progress layers, please run `zarf tools clear-cache`", "error", err) } return err } diff --git a/src/pkg/cluster/data.go b/src/pkg/cluster/data.go index d2fdf4339a..834adf716e 100644 --- a/src/pkg/cluster/data.go +++ b/src/pkg/cluster/data.go @@ -25,7 +25,6 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/logging" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/utils/exec" ) @@ -92,7 +91,7 @@ func (c *Cluster) HandleDataInjection(ctx context.Context, data v1alpha1.ZarfDat zarfCommand, err := utils.GetFinalExecutableCommand() kubectlBinPath := "kubectl" if err != nil { - message.Warnf("Unable to get the zarf executable path, falling back to host kubectl: %s", err) + log.Warn("unable to get the zarf executable path falling back to host kubectl", "error", err) } else { kubectlBinPath = fmt.Sprintf("%s tools kubectl", zarfCommand) } diff --git a/src/pkg/cluster/state.go b/src/pkg/cluster/state.go index c8854904c3..04d7491c92 100644 --- a/src/pkg/cluster/state.go +++ b/src/pkg/cluster/state.go @@ -37,6 +37,8 @@ const ( // InitZarfState initializes the Zarf state with the given temporary directory and init configs. func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitOptions) error { + log := logging.FromContextOrDiscard(ctx) + spinner := message.NewProgressSpinner("Gathering cluster state information") defer spinner.Stop() @@ -155,16 +157,13 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO state.ArtifactServer = initOptions.ArtifactServer } else { if helpers.IsNotZeroAndNotEqual(initOptions.GitServer, state.GitServer) { - message.Warn("Detected a change in Git Server init options on a re-init. Ignoring... To update run:") - message.ZarfCommand("tools update-creds git") + log.Warn("Detected a change in Git Server init options on a re-init. Ignoring... To update run: zarf tools update-creds git") } if helpers.IsNotZeroAndNotEqual(initOptions.RegistryInfo, state.RegistryInfo) { - message.Warn("Detected a change in Image Registry init options on a re-init. Ignoring... To update run:") - message.ZarfCommand("tools update-creds registry") + log.Warn("Detected a change in Image Registry init options on a re-init. Ignoring... To update run: zarf tools update-creds registry") } if helpers.IsNotZeroAndNotEqual(initOptions.ArtifactServer, state.ArtifactServer) { - message.Warn("Detected a change in Artifact Server init options on a re-init. Ignoring... To update run:") - message.ZarfCommand("tools update-creds artifact") + log.Warn("Detected a change in Artifact Server init options on a re-init. Ignoring... To update run: zarf tools update-creds artifact") } } diff --git a/src/pkg/message/message.go b/src/pkg/message/message.go index 9d2c7de6bc..2b17834ed8 100644 --- a/src/pkg/message/message.go +++ b/src/pkg/message/message.go @@ -131,12 +131,6 @@ func Warnf(format string, a ...any) { pterm.Warning.Println(message) } -// WarnErr prints an error message as a warning. -func WarnErr(err any, message string) { - debugPrinter(2, err) - Warnf(message) -} - // WarnErrf prints an error message as a warning with a given format. func WarnErrf(err any, format string, a ...any) { debugPrinter(2, err) diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index a04922d602..9c97ec4497 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -73,6 +73,8 @@ New creates a new package instance with the provided config. Note: This function creates a tmp directory that should be cleaned up with p.ClearTempPaths(). */ func New(ctx context.Context, cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { + log := logging.FromContextOrDiscard(ctx) + if cfg == nil { return nil, fmt.Errorf("no config provided") } @@ -89,7 +91,7 @@ func New(ctx context.Context, cfg *types.PackagerConfig, mods ...Modifier) (*Pac if config.CommonOptions.TempDirectory != "" { // If the cache directory is within the temp directory, warn the user if strings.HasPrefix(config.CommonOptions.CachePath, config.CommonOptions.TempDirectory) { - message.Warnf("The cache directory (%q) is within the temp directory (%q) and will be removed when the temp directory is cleaned up", config.CommonOptions.CachePath, config.CommonOptions.TempDirectory) + log.Warn("The cache directory is within the temp directory and will be removed when the temp directory is cleaned up", "cache", config.CommonOptions.CachePath, "temp", config.CommonOptions.TempDirectory) } } @@ -111,7 +113,7 @@ func New(ctx context.Context, cfg *types.PackagerConfig, mods ...Modifier) (*Pac if err != nil { return nil, fmt.Errorf("unable to create package temp paths: %w", err) } - logging.FromContextOrDiscard(ctx).Debug("Using temporary directory", "directory", dir) + log.Debug("Using temporary directory", "directory", dir) pkgr.layout = layout.New(dir) } @@ -158,7 +160,7 @@ func (p *Packager) attemptClusterChecks(ctx context.Context) (err error) { // Check the clusters architecture matches the package spec if err := p.validatePackageArchitecture(ctx); err != nil { if errors.Is(err, lang.ErrUnableToCheckArch) { - message.Warnf("Unable to validate package architecture: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Warn("Unable to validate package architecture", "error", err) } else { return err } diff --git a/src/pkg/packager/create.go b/src/pkg/packager/create.go index 517726eba4..5c258cf939 100755 --- a/src/pkg/packager/create.go +++ b/src/pkg/packager/create.go @@ -41,7 +41,7 @@ func (p *Packager) Create(ctx context.Context) error { } p.cfg.Pkg = pkg - if !p.confirmAction(config.ZarfCreateStage, warnings, nil) { + if !p.confirmAction(ctx, config.ZarfCreateStage, warnings, nil) { return fmt.Errorf("package creation canceled") } diff --git a/src/pkg/packager/creator/skeleton.go b/src/pkg/packager/creator/skeleton.go index 648fb562b3..cd82321fdc 100644 --- a/src/pkg/packager/creator/skeleton.go +++ b/src/pkg/packager/creator/skeleton.go @@ -21,6 +21,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/helm" "github.com/zarf-dev/zarf/src/internal/packager/kustomize" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/zoci" @@ -68,7 +69,7 @@ func (sc *SkeletonCreator) LoadPackageDefinition(ctx context.Context, src *layou } for _, warning := range warnings { - message.Warn(warning) + logging.FromContextOrDiscard(ctx).Warn(warning) } if err := Validate(pkg, sc.createOpts.BaseDir, sc.createOpts.SetVariables); err != nil { diff --git a/src/pkg/packager/deploy.go b/src/pkg/packager/deploy.go index aa067fbce7..d00befd34b 100644 --- a/src/pkg/packager/deploy.go +++ b/src/pkg/packager/deploy.go @@ -97,7 +97,7 @@ func (p *Packager) Deploy(ctx context.Context) error { warnings = append(warnings, sbomWarnings...) // Confirm the overall package deployment - if !p.confirmAction(config.ZarfDeployStage, warnings, sbomViewFiles) { + if !p.confirmAction(ctx, config.ZarfDeployStage, warnings, sbomViewFiles) { return fmt.Errorf("deployment cancelled") } @@ -124,7 +124,7 @@ func (p *Packager) Deploy(ctx context.Context) error { return err } if len(deployedComponents) == 0 { - message.Warn("No components were selected for deployment. Inspect the package to view the available components and select components interactively or by name with \"--components\"") + logging.FromContextOrDiscard(ctx).Warn("No components were selected for deployment. Inspect the package to view the available components and select components interactively or by name with \"--components\"") } // Notify all the things about the successful deployment @@ -499,7 +499,7 @@ func (p *Packager) setupState(ctx context.Context) (err error) { } if p.cfg.Pkg.Metadata.YOLO && state.Distro != "YOLO" { - message.Warn("This package is in YOLO mode, but the cluster was already initialized with 'zarf init'. " + + logging.FromContextOrDiscard(ctx).Warn("This package is in YOLO mode, but the cluster was already initialized with 'zarf init'. " + "This may cause issues if the package does not exclude any charts or manifests from the Zarf Agent using " + "the pod or namespace label `zarf.dev/agent: ignore'.") } diff --git a/src/pkg/packager/dev.go b/src/pkg/packager/dev.go index 49232ddc1b..cdf5689f1a 100644 --- a/src/pkg/packager/dev.go +++ b/src/pkg/packager/dev.go @@ -13,6 +13,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/creator" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -91,7 +92,7 @@ func (p *Packager) DevDeploy(ctx context.Context) error { return err } if len(deployedComponents) == 0 { - message.Warn("No components were selected for deployment. Inspect the package to view the available components and select components interactively or by name with \"--components\"") + logging.FromContextOrDiscard(ctx).Warn("No components were selected for deployment. Inspect the package to view the available components and select components interactively or by name with \"--components\"") } // Notify all the things about the successful deployment diff --git a/src/pkg/packager/generate.go b/src/pkg/packager/generate.go index 068808b171..1400bbbb94 100644 --- a/src/pkg/packager/generate.go +++ b/src/pkg/packager/generate.go @@ -17,18 +17,21 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/lint" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" ) // Generate generates a Zarf package definition. func (p *Packager) Generate(ctx context.Context) (err error) { + log := logging.FromContextOrDiscard(ctx) + generatedZarfYAMLPath := filepath.Join(p.cfg.GenerateOpts.Output, layout.ZarfYAML) spinner := message.NewProgressSpinner("Generating package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath) if !helpers.InvalidPath(generatedZarfYAMLPath) { prefixed := filepath.Join(p.cfg.GenerateOpts.Output, fmt.Sprintf("%s-%s", p.cfg.GenerateOpts.Name, layout.ZarfYAML)) - message.Warnf("%s already exists, writing to %s", generatedZarfYAMLPath, prefixed) + log.Warn("path already exist, writing to alternative path", "original", generatedZarfYAMLPath, "alternative", prefixed) generatedZarfYAMLPath = prefixed @@ -66,7 +69,7 @@ func (p *Packager) Generate(ctx context.Context) (err error) { images, err := p.findImages(ctx) if err != nil { // purposefully not returning error here, as we can still generate the package without images - message.Warnf("Unable to find images: %s", err.Error()) + log.Warn("Unable to find images", "error", err) } for i := range p.cfg.Pkg.Components { diff --git a/src/pkg/packager/interactive.go b/src/pkg/packager/interactive.go index 3a09595eab..2e1d32496e 100644 --- a/src/pkg/packager/interactive.go +++ b/src/pkg/packager/interactive.go @@ -5,6 +5,7 @@ package packager import ( + "context" "fmt" "os" "path/filepath" @@ -14,11 +15,14 @@ import ( "github.com/pterm/pterm" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" ) -func (p *Packager) confirmAction(stage string, warnings []string, sbomViewFiles []string) (confirm bool) { +func (p *Packager) confirmAction(ctx context.Context, stage string, warnings []string, sbomViewFiles []string) (confirm bool) { + log := logging.FromContextOrDiscard(ctx) + pterm.Println() message.HeaderInfof("📦 PACKAGE DEFINITION") utils.ColorPrintYAML(p.cfg.Pkg, p.getPackageYAMLHints(stage), true) @@ -48,7 +52,7 @@ func (p *Packager) confirmAction(stage string, warnings []string, sbomViewFiles pterm.Println(viewNow) pterm.Println(viewLater) } else { - message.Warn("This package does NOT contain an SBOM. If you require an SBOM, please contact the creator of this package to request a version that includes an SBOM.") + log.Warn("This package does NOT contain an SBOM. If you require an SBOM, please contact the creator of this package to request a version that includes an SBOM.") } } } @@ -57,7 +61,7 @@ func (p *Packager) confirmAction(stage string, warnings []string, sbomViewFiles message.HorizontalRule() message.Title("Package Warnings", "the following warnings were flagged while reading the package") for _, warning := range warnings { - message.Warn(warning) + log.Warn(warning) } } diff --git a/src/pkg/packager/mirror.go b/src/pkg/packager/mirror.go index 9e61604144..f715421e6a 100644 --- a/src/pkg/packager/mirror.go +++ b/src/pkg/packager/mirror.go @@ -37,7 +37,7 @@ func (p *Packager) Mirror(ctx context.Context) error { warnings = append(warnings, sbomWarnings...) // Confirm the overall package mirror - if !p.confirmAction(config.ZarfMirrorStage, warnings, sbomViewFiles) { + if !p.confirmAction(ctx, config.ZarfMirrorStage, warnings, sbomViewFiles) { return fmt.Errorf("mirror cancelled") } diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index 0581c022b8..3016cfcf5a 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -41,6 +41,8 @@ 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) { + log := logging.FromContextOrDiscard(ctx) + cwd, err := os.Getwd() if err != nil { return nil, err @@ -48,7 +50,7 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) defer func() { // 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()) + log.Warn("Unable to return the original working directory", "error", err) } }() if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil { @@ -67,7 +69,7 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) return nil, err } for _, warning := range warnings { - message.Warn(warning) + log.Warn(warning) } p.cfg.Pkg = pkg diff --git a/src/pkg/packager/remove.go b/src/pkg/packager/remove.go index 54fd983ae6..7fed15ec54 100644 --- a/src/pkg/packager/remove.go +++ b/src/pkg/packager/remove.go @@ -156,13 +156,15 @@ func (p *Packager) updatePackageSecret(ctx context.Context, deployedPackage type }() // We warn and ignore errors because we may have removed the cluster that this package was inside of if err != nil { - message.Warnf("Unable to update the '%s' package secret: '%s' (this may be normal if the cluster was removed)", secretName, err.Error()) + logging.FromContextOrDiscard(ctx).Warn("Unable to update the package secret, this may be normal if the cluster was removed", "secret", secretName, "error", err) } } return nil } func (p *Packager) removeComponent(ctx context.Context, deployedPackage *types.DeployedPackage, deployedComponent types.DeployedComponent, spinner *message.Spinner) (*types.DeployedPackage, error) { + log := logging.FromContextOrDiscard(ctx) + components := deployedPackage.Data.Components c := helpers.Find(components, func(t v1alpha1.ZarfComponent) bool { @@ -188,11 +190,9 @@ func (p *Packager) removeComponent(ctx context.Context, deployedPackage *types.D if err := helmCfg.RemoveChart(chart.Namespace, chart.ChartName, spinner); err != nil { if !errors.Is(err, driver.ErrReleaseNotFound) { onFailure() - return deployedPackage, fmt.Errorf("unable to uninstall the helm chart %s in the namespace %s: %w", - chart.ChartName, chart.Namespace, err) + return deployedPackage, fmt.Errorf("unable to uninstall the helm chart %s in the namespace %s: %w", chart.ChartName, chart.Namespace, err) } - message.Warnf("Helm release for helm chart '%s' in the namespace '%s' was not found. Was it already removed?", - chart.ChartName, chart.Namespace) + log.Warn("Helm release for Helm chart was not found, has it already been removed?", "chart", chart.ChartName, "namespace", chart.Namespace) } // Remove the uninstalled chart from the list of installed charts @@ -231,11 +231,11 @@ func (p *Packager) removeComponent(ctx context.Context, deployedPackage *types.D // We warn and ignore errors because we may have removed the cluster that this package was inside of if err != nil { - message.Warnf("Unable to delete the '%s' package secret: '%s' (this may be normal if the cluster was removed)", secretName, err.Error()) + log.Warn("Unable to get the package secret, this may be normal if the cluster was removed", "secret", secretName, "error", err) } else { err = p.cluster.Clientset.CoreV1().Secrets(packageSecret.Namespace).Delete(ctx, packageSecret.Name, metav1.DeleteOptions{}) if err != nil { - message.Warnf("Unable to delete the '%s' package secret: '%s' (this may be normal if the cluster was removed)", secretName, err.Error()) + log.Warn("Unable to delete the package secret, this may be normal if the cluster was removed", "secret", secretName, "error", err) } } } else { diff --git a/src/pkg/packager/sources/oci.go b/src/pkg/packager/sources/oci.go index 8bf6d6d1a6..ebf891a899 100644 --- a/src/pkg/packager/sources/oci.go +++ b/src/pkg/packager/sources/oci.go @@ -16,6 +16,7 @@ import ( "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/filters" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -143,7 +144,7 @@ func (s *OCISource) LoadPackageMetadata(ctx context.Context, dst *layout.Package if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil { if errors.Is(err, ErrPkgSigButNoKey) && skipValidation { - message.Warn("The package was signed but no public key was provided, skipping signature validation") + logging.FromContextOrDiscard(ctx).Warn("The package was signed but no public key was provided, skipping signature validation") } else { return pkg, nil, err } diff --git a/src/pkg/packager/sources/tarball.go b/src/pkg/packager/sources/tarball.go index db1b2ed01a..d51768fdb7 100644 --- a/src/pkg/packager/sources/tarball.go +++ b/src/pkg/packager/sources/tarball.go @@ -17,6 +17,7 @@ import ( "github.com/mholt/archiver/v3" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/filters" "github.com/zarf-dev/zarf/src/pkg/zoci" @@ -187,7 +188,7 @@ func (s *TarballSource) LoadPackageMetadata(ctx context.Context, dst *layout.Pac if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil { if errors.Is(err, ErrPkgSigButNoKey) && skipValidation { - message.Warn("The package was signed but no public key was provided, skipping signature validation") + logging.FromContextOrDiscard(ctx).Warn("The package was signed but no public key was provided, skipping signature validation") } else { return pkg, nil, err } diff --git a/src/pkg/transform/git.go b/src/pkg/transform/git.go index d215df9481..899262c4c2 100644 --- a/src/pkg/transform/git.go +++ b/src/pkg/transform/git.go @@ -5,23 +5,25 @@ package transform import ( + "context" "fmt" "net/url" "regexp" "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/zarf-dev/zarf/src/pkg/logging" ) // For further explanation: https://regex101.com/r/YxpfhC/5 var gitURLRegex = regexp.MustCompile(`^(?P[a-z]+:\/\/)(?P.+?)\/(?P[\w\-\.]+?)?(?P\.git)?(\/)?(?P@(?P\+)?(?P[\/\+\w\-\.]+))?(?P\/(?Pinfo\/.*|git-upload-pack|git-receive-pack))?$`) // MutateGitURLsInText changes the gitURL hostname to use the repository Zarf is configured to use. -func MutateGitURLsInText(logger Log, targetBaseURL string, text string, pushUser string) string { +func MutateGitURLsInText(ctx context.Context, targetBaseURL string, text string, pushUser string) string { extractPathRegex := regexp.MustCompile(`[a-z]+:\/\/[^\/]+\/(.*\.git)`) output := extractPathRegex.ReplaceAllStringFunc(text, func(match string) string { output, err := GitURL(targetBaseURL, match, pushUser) if err != nil { - logger("Unable to transform the git url, using the original url we have: %s", match) + logging.FromContextOrDiscard(ctx).Warn("Unable to transform the git url, using the original", "url", match) return match } return output.String() diff --git a/src/pkg/transform/git_test.go b/src/pkg/transform/git_test.go index 19c5778966..ba241f6429 100644 --- a/src/pkg/transform/git_test.go +++ b/src/pkg/transform/git_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/zarf-dev/zarf/src/test/testutil" ) var gitURLs = []string{ @@ -44,7 +45,8 @@ var badGitURLs = []string{ } func TestMutateGitURLsInText(t *testing.T) { - dummyLogger := func(_ string, _ ...any) {} + ctx := testutil.TestContext(t) + originalText := ` # Here we handle invalid URLs (see below comment) # We transform https://*/*.git URLs @@ -69,7 +71,7 @@ func TestMutateGitURLsInText(t *testing.T) { https://www.defenseunicorns.com/ ` - resultingText := MutateGitURLsInText(dummyLogger, "https://gitlab.com", originalText, "repo-owner") + resultingText := MutateGitURLsInText(ctx, "https://gitlab.com", originalText, "repo-owner") require.Equal(t, expectedText, resultingText) } diff --git a/src/pkg/transform/types.go b/src/pkg/transform/types.go deleted file mode 100644 index dbf4922a0f..0000000000 --- a/src/pkg/transform/types.go +++ /dev/null @@ -1,7 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// SPDX-FileCopyrightText: 2021-Present The Zarf Authors - -package transform - -// Log is a function that logs a message. -type Log func(string, ...any) diff --git a/src/pkg/utils/cosign.go b/src/pkg/utils/cosign.go index 7b2800b6cf..5cbc9d2c58 100644 --- a/src/pkg/utils/cosign.go +++ b/src/pkg/utils/cosign.go @@ -15,9 +15,6 @@ import ( "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/pkg/errors" - "github.com/zarf-dev/zarf/src/config/lang" - "github.com/zarf-dev/zarf/src/pkg/logging" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio" "github.com/sigstore/cosign/v2/cmd/cosign/cli/options" @@ -32,6 +29,9 @@ import ( _ "github.com/sigstore/sigstore/pkg/signature/kms/azure" _ "github.com/sigstore/sigstore/pkg/signature/kms/gcp" _ "github.com/sigstore/sigstore/pkg/signature/kms/hashivault" + + "github.com/zarf-dev/zarf/src/pkg/logging" + "github.com/zarf-dev/zarf/src/pkg/message" ) // Sget performs a cosign signature verification on a given image using the specified public key. @@ -40,7 +40,7 @@ import ( func Sget(ctx context.Context, image, key string, out io.Writer) error { log := logging.FromContextOrDiscard(ctx) - message.Warnf(lang.WarnSGetDeprecation) + logging.FromContextOrDiscard(ctx).Warn("Using sget to download resources is being deprecated and will removed in the v1.0.0 release of Zarf. Please publish the packages as OCI artifacts instead.") // Remove the custom protocol header from the url image = strings.TrimPrefix(image, helpers.SGETURLPrefix)