From 6fcbe7d60189a0a852d735f51716d0f9e3924386 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Wed, 7 Aug 2024 11:40:15 +0200 Subject: [PATCH] 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)