diff --git a/src/cmd/destroy.go b/src/cmd/destroy.go index 1b71740aa3..f9fc6bc546 100644 --- a/src/cmd/destroy.go +++ b/src/cmd/destroy.go @@ -11,15 +11,17 @@ 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/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils/exec" - - "github.com/spf13/cobra" ) var confirmDestroy bool @@ -63,7 +65,7 @@ var destroyCmd = &cobra.Command{ // Run the matched script err := exec.CmdWithPrint(script) if errors.Is(err, os.ErrPermission) { - message.Warnf(lang.CmdDestroyErrScriptPermissionDenied, script) + logging.FromContextOrDiscard(cmd.Context()).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/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 494bb0f0f7..f50df85bcc 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 7c6ba91e88..bd35562772 100644 --- a/src/cmd/tools/zarf.go +++ b/src/cmd/tools/zarf.go @@ -25,6 +25,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" @@ -41,8 +42,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"}) }, } @@ -102,6 +103,7 @@ var updateCredsCmd = &cobra.Command{ } ctx := cmd.Context() + log := logging.FromContextOrDiscard(ctx) timeoutCtx, cancel := context.WithTimeout(ctx, cluster.DefaultTimeout) defer cancel() @@ -173,7 +175,7 @@ var updateCredsCmd = &cobra.Command{ }) if err != nil { // Warn if we couldn't actually update the git server (it might not be installed and we should try to continue) - message.Warnf(lang.CmdToolsUpdateCredsUnableCreateToken, err.Error()) + log.Warn("Unable to create the new Gitea artifact token", "error", err) } } @@ -190,7 +192,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() { @@ -221,14 +223,14 @@ var updateCredsCmd = &cobra.Command{ }) if err != nil { // Warn if we couldn't actually update the git server (it might not be installed and we should try to continue) - message.Warnf(lang.CmdToolsUpdateCredsUnableUpdateGit, err.Error()) + log.Warn("Unable to update Zarf Git Server values", "error", err) } } if slices.Contains(args, message.AgentKey) { 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 7b84e5a033..c7424f7b36 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -53,9 +53,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" + @@ -100,8 +97,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 " + @@ -266,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." @@ -331,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" + @@ -428,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" @@ -656,8 +648,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..85fc6f9248 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,7 @@ 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 { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r.Method != http.MethodPost { @@ -70,7 +72,7 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { Kind: "AdmissionReview", } if err != nil { - message.Warnf("%s: %s", lang.AgentErrBindHandler, err.Error()) + logging.FromContextOrDiscard(ctx).Warn("Unable to bind the webhook handler", "error", err) admissionResponse := corev1.AdmissionReview{ TypeMeta: admissionMeta, Response: &corev1.AdmissionResponse{ 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 6c473828b4..878829ced9 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -26,6 +26,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "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) @@ -286,9 +287,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) } @@ -364,7 +365,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 { @@ -410,7 +411,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..36f9ee1ea1 100644 --- a/src/internal/packager/helm/post-render.go +++ b/src/internal/packager/helm/post-render.go @@ -142,7 +142,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{}) diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 84b310b286..4abcddd837 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" @@ -42,6 +43,8 @@ import ( // 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 @@ -110,7 +113,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) @@ -127,9 +130,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. @@ -147,7 +148,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er } if refInfo.Digest != "" && desc != nil && types.MediaType(desc.MediaType).IsIndex() { - message.Warn("Zarf does not currently support direct consumption of OCI image indexes or Docker manifest lists") + log.Warn("Zarf does not currently support direct consumption of OCI image indexes or Docker manifest lists") var idx v1.IndexManifest if err := json.Unmarshal(desc.Manifest, &idx); err != nil { @@ -161,7 +162,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er for _, desc := range idx.Manifests { lines = append(lines, fmt.Sprintf("\n(%s) %s@%s", desc.Platform, name, desc.Digest)) } - message.Warn(strings.Join(lines, "\n")) + log.Warn(strings.Join(lines, "\n")) return fmt.Errorf("%s resolved to an index, please select a specific platform to use", refInfo.Reference) } diff --git a/src/pkg/cluster/state.go b/src/pkg/cluster/state.go index 5f95442cea..086028d8de 100644 --- a/src/pkg/cluster/state.go +++ b/src/pkg/cluster/state.go @@ -36,6 +36,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() @@ -142,7 +144,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO return err } if kerrors.IsNotFound(err) { - logging.FromContextOrDiscard(ctx).Debug("Service account not found, retrying", "namespace", ns, "name", name) + log.Debug("Service account not found, retrying", "namespace", ns, "name", name) timer.Reset(1 * time.Second) continue } @@ -168,16 +170,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/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 218830b75f..997cec7c9e 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); err != nil { diff --git a/src/pkg/packager/deploy.go b/src/pkg/packager/deploy.go index b0efa1cf06..49b4fcfb36 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 1c1208be14..543adc3a84 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 cc3393dbb5..9ec7d8ff6b 100644 --- a/src/pkg/packager/generate.go +++ b/src/pkg/packager/generate.go @@ -16,18 +16,21 @@ 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" ) // 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 @@ -65,7 +68,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 4ecaae369f..a4d7a17594 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -41,6 +41,8 @@ type imageMap map[string]bool // 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 { @@ -69,7 +71,7 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) p.cfg.Pkg = pkg for _, warning := range warnings { - message.Warn(warning) + log.Warn(warning) } return p.findImages(ctx) @@ -132,7 +134,7 @@ func (p *Packager) findImages(ctx context.Context) (imgMap map[string][]string, for _, repo := range component.Repos { matches := strings.Split(repo, "@") if len(matches) < 2 { - message.Warnf("Cannot convert git repo %s to helm chart without a version tag", repo) + log.Warn("Cannot convert Git repository to Helm chart without version tag", "repository", repo) continue } @@ -350,7 +352,7 @@ func (p *Packager) findImages(ctx context.Context) (imgMap map[string][]string, if whyImage != "" { if len(whyResources) == 0 { - message.Warnf("image %q not found in any charts or manifests", whyImage) + log.Warn("image not found in any charts or manifests", "image", whyImage) } return nil, nil } 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 043c7ed12b..eaab80a8df 100644 --- a/src/pkg/utils/cosign.go +++ b/src/pkg/utils/cosign.go @@ -17,7 +17,6 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/pkg/errors" "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" @@ -42,7 +41,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.") // If this is a DefenseUnicorns package, use an internal sget public key if strings.HasPrefix(image, fmt.Sprintf("%s://defenseunicorns", helpers.SGETURLScheme)) {