diff --git a/src/cmd/dev.go b/src/cmd/dev.go index 322b82fb84..f561260f45 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -53,7 +53,7 @@ var devDeployCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -78,7 +78,7 @@ var devGenerateCmd = &cobra.Command{ pkgConfig.CreateOpts.BaseDir = "." pkgConfig.FindImagesOpts.RepoHelmChartPath = pkgConfig.GenerateOpts.GitPath - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -229,7 +229,7 @@ var devFindImagesCmd = &cobra.Command{ v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -276,7 +276,7 @@ var devLintCmd = &cobra.Command{ pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } diff --git a/src/cmd/initialize.go b/src/cmd/initialize.go index 4d1c61363b..808563b64d 100644 --- a/src/cmd/initialize.go +++ b/src/cmd/initialize.go @@ -19,6 +19,7 @@ import ( "github.com/zarf-dev/zarf/src/cmd/common" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager" "github.com/zarf-dev/zarf/src/pkg/packager/sources" @@ -63,7 +64,7 @@ var initCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } @@ -127,7 +128,7 @@ func downloadInitPackage(ctx context.Context, cacheDirectory string) (string, er // Give the user the choice to download the init-package and note that this does require an internet connection message.Question(fmt.Sprintf(lang.CmdInitPullAsk, url)) - message.Note(lang.CmdInitPullNote) + logging.FromContextOrDiscard(ctx).Info("Downloading init package will require an internet connection") // Prompt the user if --confirm not specified if !confirmDownload { diff --git a/src/cmd/internal.go b/src/cmd/internal.go index 4a393d01e6..bd94cc7d2e 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" ) @@ -67,7 +68,7 @@ var httpProxyCmd = &cobra.Command{ var genCLIDocs = &cobra.Command{ Use: "gen-cli-docs", Short: lang.CmdInternalGenerateCliDocsShort, - RunE: func(_ *cobra.Command, _ []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { // Don't include the datestamp in the output rootCmd.DisableAutoGenTag = true @@ -161,7 +162,7 @@ tableOfContents: false if err := doc.GenMarkdownTreeCustom(rootCmd, "./site/src/content/docs/commands", prependTitle, linkHandler); err != nil { return err } - message.Success(lang.CmdInternalGenerateCliDocsSuccess) + logging.FromContextOrDiscard(cmd.Context()).Info("Successfully createed the CLI documentation") return nil }, } diff --git a/src/cmd/package.go b/src/cmd/package.go index 8b1405e25c..b98331610f 100644 --- a/src/cmd/package.go +++ b/src/cmd/package.go @@ -14,6 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/cmd/common" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/sources" "github.com/zarf-dev/zarf/src/types" @@ -54,7 +55,7 @@ var packageCreateCmd = &cobra.Command{ pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -84,7 +85,7 @@ var packageDeployCmd = &cobra.Command{ pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap( v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper) - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -112,7 +113,7 @@ var packageMirrorCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -136,11 +137,11 @@ var packageInspectCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - src, err := identifyAndFallbackToClusterSource() + src, err := identifyAndFallbackToClusterSource(cmd.Context()) if err != nil { return err } - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } @@ -208,11 +209,11 @@ var packageRemoveCmd = &cobra.Command{ return err } pkgConfig.PkgOpts.PackageSource = packageSource - src, err := identifyAndFallbackToClusterSource() + src, err := identifyAndFallbackToClusterSource(cmd.Context()) if err != nil { return err } - pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src)) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig, packager.WithSource(src)) if err != nil { return err } @@ -253,7 +254,7 @@ var packagePublishCmd = &cobra.Command{ pkgConfig.PublishOpts.PackageDestination = ref.String() - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -273,7 +274,7 @@ var packagePullCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pkgConfig.PkgOpts.PackageSource = args[0] - pkgClient, err := packager.New(&pkgConfig) + pkgClient, err := packager.New(cmd.Context(), &pkgConfig) if err != nil { return err } @@ -311,10 +312,10 @@ func choosePackage(args []string) (string, error) { } // TODO: This code does not seem to do what it was intended. -func identifyAndFallbackToClusterSource() (sources.PackageSource, error) { +func identifyAndFallbackToClusterSource(ctx context.Context) (sources.PackageSource, error) { identifiedSrc := sources.Identify(pkgConfig.PkgOpts.PackageSource) if identifiedSrc == "" { - message.Debugf(lang.CmdPackageClusterSourceFallback, pkgConfig.PkgOpts.PackageSource) + logging.FromContextOrDiscard(ctx).Debug("package source does not satisfy any current sources, assuming it is a package deployed to a cluster", "source", pkgConfig.PkgOpts.PackageSource) src, err := sources.NewClusterSource(&pkgConfig.PkgOpts) if err != nil { return nil, fmt.Errorf("unable to identify source from %s: %w", pkgConfig.PkgOpts.PackageSource, err) diff --git a/src/cmd/root.go b/src/cmd/root.go index 62e0582c8e..41090c1cd4 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -7,6 +7,7 @@ package cmd import ( "context" "fmt" + "log/slog" "os" "slices" "strings" @@ -19,6 +20,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -87,6 +89,10 @@ var rootCmd = &cobra.Command{ // Execute is the entrypoint for the CLI. func Execute(ctx context.Context) { + handler := logging.NewPtermHandler() + log := slog.New(handler) + ctx = logging.NewContext(ctx, log) + cmd, err := rootCmd.ExecuteContextC(ctx) if err == nil { return diff --git a/src/cmd/tools/crane.go b/src/cmd/tools/crane.go index dd76955339..fd9d57c522 100644 --- a/src/cmd/tools/crane.go +++ b/src/cmd/tools/crane.go @@ -5,6 +5,7 @@ package tools import ( + "context" "errors" "fmt" "os" @@ -20,6 +21,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" @@ -239,13 +241,15 @@ func pruneImages(cmd *cobra.Command, _ []string) error { if tunnel != nil { message.Notef(lang.CmdToolsRegistryTunnel, registryEndpoint, zarfState.RegistryInfo.Address) defer tunnel.Close() - return tunnel.Wrap(func() error { return doPruneImagesForPackages(zarfState, zarfPackages, registryEndpoint) }) + return tunnel.Wrap(func() error { return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint) }) } - return doPruneImagesForPackages(zarfState, zarfPackages, registryEndpoint) + return doPruneImagesForPackages(ctx, zarfState, zarfPackages, registryEndpoint) } -func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.DeployedPackage, registryEndpoint string) error { +func doPruneImagesForPackages(ctx context.Context, zarfState *types.ZarfState, zarfPackages []types.DeployedPackage, registryEndpoint string) error { + log := logging.FromContextOrDiscard(ctx) + authOption := images.WithPushAuth(zarfState.RegistryInfo) spinner := message.NewProgressSpinner(lang.CmdToolsRegistryPruneLookup) @@ -320,11 +324,7 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D spinner.Success() if len(imageDigestsToPrune) > 0 { - message.Note(lang.CmdToolsRegistryPruneImageList) - - for digestRef := range imageDigestsToPrune { - message.Info(digestRef) - } + log.Info("Pruning images with digests from the registry", "digests", imageDigestsToPrune) confirm := config.CommonOptions.Confirm @@ -353,7 +353,7 @@ func doPruneImagesForPackages(zarfState *types.ZarfState, zarfPackages []types.D spinner.Success() } } else { - message.Note(lang.CmdToolsRegistryPruneNoImages) + log.Info("Tehere are no images to prune") } return nil diff --git a/src/cmd/tools/wait.go b/src/cmd/tools/wait.go index fda0344fbc..ad5184dd20 100644 --- a/src/cmd/tools/wait.go +++ b/src/cmd/tools/wait.go @@ -29,7 +29,7 @@ var waitForCmd = &cobra.Command{ Long: lang.CmdToolsWaitForLong, Example: lang.CmdToolsWaitForExample, Args: cobra.MinimumNArgs(1), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { // Parse the timeout string timeout, err := time.ParseDuration(waitTimeout) if err != nil { @@ -51,7 +51,7 @@ var waitForCmd = &cobra.Command{ } // Execute the wait command. - if err := utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil { + if err := utils.ExecuteWait(cmd.Context(), waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil { return err } return err diff --git a/src/cmd/tools/zarf.go b/src/cmd/tools/zarf.go index 5a9cd11718..4ca0517806 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" @@ -127,7 +128,7 @@ var updateCredsCmd = &cobra.Command{ confirm := config.CommonOptions.Confirm if confirm { - message.Note(lang.CmdToolsUpdateCredsConfirmProvided) + logging.FromContextOrDiscard(ctx).Info("Confirm flag specified, continuing without prompting") } else { prompt := &survey.Confirm{ Message: lang.CmdToolsUpdateCredsConfirmContinue, @@ -204,12 +205,13 @@ var clearCacheCmd = &cobra.Command{ Use: "clear-cache", Aliases: []string{"c"}, Short: lang.CmdToolsClearCacheShort, - RunE: func(_ *cobra.Command, _ []string) error { - message.Notef(lang.CmdToolsClearCacheDir, config.GetAbsCachePath()) + RunE: func(cmd *cobra.Command, _ []string) error { + log := logging.FromContextOrDiscard(cmd.Context()) + log.Info("Using cache directory", "path", config.GetAbsCachePath()) if err := os.RemoveAll(config.GetAbsCachePath()); err != nil { return fmt.Errorf("unable to clear the cache directory %s: %w", config.GetAbsCachePath(), err) } - message.Successf(lang.CmdToolsClearCacheSuccess, config.GetAbsCachePath()) + log.Info("Successfully cleared the cache", "path", config.GetAbsCachePath()) return nil }, } @@ -237,7 +239,7 @@ var generatePKICmd = &cobra.Command{ Aliases: []string{"pki"}, Short: lang.CmdToolsGenPkiShort, Args: cobra.ExactArgs(1), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { pki, err := pki.GeneratePKI(args[0], subAltNames...) if err != nil { return err @@ -251,7 +253,7 @@ var generatePKICmd = &cobra.Command{ if err := os.WriteFile("tls.key", pki.Key, helpers.ReadWriteUser); err != nil { return err } - message.Successf(lang.CmdToolsGenPkiSuccess, args[0]) + logging.FromContextOrDiscard(cmd.Context()).Info("Successfully created a chain of trust", "host", args[0]) return nil }, } @@ -260,7 +262,7 @@ var generateKeyCmd = &cobra.Command{ Use: "gen-key", Aliases: []string{"key"}, Short: lang.CmdToolsGenKeyShort, - RunE: func(_ *cobra.Command, _ []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { // Utility function to prompt the user for the password to the private key passwordFunc := func(bool) ([]byte, error) { // perform the first prompt @@ -323,7 +325,7 @@ var generateKeyCmd = &cobra.Command{ return err } - message.Successf(lang.CmdToolsGenKeySuccess, prvKeyFileName, pubKeyFileName) + logging.FromContextOrDiscard(cmd.Context()).Info("Generated key pair", "private key", prvKeyFileName, "public key", pubKeyFileName) return nil }, } diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 50ce790c44..b12579185f 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -142,7 +142,6 @@ $ zarf init --artifact-push-password={PASSWORD} --artifact-push-username={USERNA CmdInitErrValidateArtifact = "the 'artifact-push-username' and 'artifact-push-token' flags must be provided if the 'artifact-url' flag is provided" CmdInitPullAsk = "It seems the init package could not be found locally, but can be pulled from oci://%s" - CmdInitPullNote = "Note: This will require an internet connection." CmdInitPullConfirm = "Do you want to pull this init package?" CmdInitPullErrManual = "pull the init package manually and place it in the current working directory" @@ -183,8 +182,7 @@ $ zarf init --artifact-push-password={PASSWORD} --artifact-push-username={USERNA "This command starts up a http proxy that can be used by running pods to transform queries " + "that conform to Gitea / Gitlab repository and package URLs in the airgap." - CmdInternalGenerateCliDocsShort = "Creates auto-generated markdown of all the commands for the CLI" - CmdInternalGenerateCliDocsSuccess = "Successfully created the CLI documentation" + CmdInternalGenerateCliDocsShort = "Creates auto-generated markdown of all the commands for the CLI" CmdInternalConfigSchemaShort = "Generates a JSON schema for the zarf.yaml configuration" @@ -315,9 +313,8 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a ar $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a skeleton` CmdPackagePullFlagOutputDirectory = "Specify the output directory for the pulled Zarf package" - CmdPackageChoose = "Choose or type the package file" - CmdPackageClusterSourceFallback = "%q does not satisfy any current sources, assuming it is a package deployed to a cluster" - CmdPackageInvalidSource = "Unable to identify source from %q: %s" + CmdPackageChoose = "Choose or type the package file" + CmdPackageInvalidSource = "Unable to identify source from %q: %s" // zarf dev (prepare is an alias for dev) CmdDevShort = "Commands useful for developing packages" @@ -418,8 +415,6 @@ $ zarf tools registry digest reg.example.com/stefanprodan/podinfo:6.4.0 CmdToolsRegistryPruneShort = "Prunes images from the registry that are not currently being used by any Zarf packages." CmdToolsRegistryPruneFlagConfirm = "Confirm the image prune action to prevent accidental deletions" - CmdToolsRegistryPruneImageList = "The following image digests will be pruned from the registry:" - CmdToolsRegistryPruneNoImages = "There are no images to prune" CmdToolsRegistryPruneLookup = "Looking up images within package definitions" CmdToolsRegistryPruneCatalog = "Cataloging images in the registry" CmdToolsRegistryPruneCalculate = "Calculating images to prune" @@ -483,15 +478,12 @@ zarf tools yq e '.a.b = "cool"' -i file.yaml CmdToolsHelmLong = "Subset of the Helm CLI that includes the repo and dependency commands for managing helm charts destined for the air gap." CmdToolsClearCacheShort = "Clears the configured git and image cache directory" - CmdToolsClearCacheDir = "Cache directory set to: %s" - CmdToolsClearCacheSuccess = "Successfully cleared the cache from %s" CmdToolsClearCacheFlagCachePath = "Specify the location of the Zarf artifact cache (images and git repositories)" CmdToolsDownloadInitShort = "Downloads the init package for the current Zarf version into the specified directory" CmdToolsDownloadInitFlagOutputDirectory = "Specify a directory to place the init package in." CmdToolsGenPkiShort = "Generates a Certificate Authority and PKI chain of trust for the given host" - CmdToolsGenPkiSuccess = "Successfully created a chain of trust for %s" CmdToolsGenPkiFlagAltName = "Specify Subject Alternative Names for the certificate" CmdToolsGenKeyShort = "Generates a cosign public/private keypair that can be used to sign packages" @@ -500,7 +492,6 @@ zarf tools yq e '.a.b = "cool"' -i file.yaml CmdToolsGenKeyPromptExists = "File %s already exists. Overwrite? " CmdToolsGenKeyErrUnableGetPassword = "unable to get password for private key: %s" CmdToolsGenKeyErrPasswordsNotMatch = "passwords do not match" - CmdToolsGenKeySuccess = "Generated key pair and written to %s and %s" CmdToolsSbomShort = "Generates a Software Bill of Materials (SBOM) for the given package" @@ -574,7 +565,6 @@ $ zarf tools update-creds artifact --artifact-push-username={USERNAME} --artifac # NOTE: Not specifying a pull username/password will keep the previous pull username/password. ` CmdToolsUpdateCredsConfirmFlag = "Confirm updating credentials without prompting" - CmdToolsUpdateCredsConfirmProvided = "Confirm flag specified, continuing without prompting." CmdToolsUpdateCredsConfirmContinue = "Continue with these changes?" CmdToolsUpdateCredsUnableUpdateRegistry = "Unable to update Zarf Registry values: %s" CmdToolsUpdateCredsUnableUpdateAgent = "Unable to update Zarf Agent TLS secrets: %s" @@ -596,7 +586,6 @@ $ zarf tools update-creds artifact --artifact-push-username={USERNAME} --artifac // These are only seen in the Kubernetes logs. const ( AgentInfoWebhookAllowed = "Webhook [%s - %s] - Allowed: %t" - AgentInfoPort = "Server running in port: %s" AgentWarnNotOCIType = "Skipping HelmRepo mutation because the type is not OCI: %s" AgentWarnSemVerRef = "Detected a semver OCI ref (%s) - continuing but will be unable to guarantee against collisions if multiple OCI artifacts with the same name are brought in from different registries" AgentErrBadRequest = "could not read request body: %s" diff --git a/src/internal/agent/hooks/argocd-application.go b/src/internal/agent/hooks/argocd-application.go index b234f29e84..8f978218a8 100644 --- a/src/internal/agent/hooks/argocd-application.go +++ b/src/internal/agent/hooks/argocd-application.go @@ -13,7 +13,6 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" @@ -65,8 +64,6 @@ func mutateApplication(ctx context.Context, r *v1.AdmissionRequest, cluster *clu return nil, err } - message.Debugf("Using the url of (%s) to mutate the ArgoCD Application", state.GitServer.Address) - app := Application{} if err = json.Unmarshal(r.Object.Raw, &app); err != nil { return nil, fmt.Errorf(lang.ErrUnmarshal, err) @@ -125,7 +122,6 @@ func getPatchedRepoURL(repoURL string, gs types.GitServerInfo, r *v1.AdmissionRe return "", fmt.Errorf("%s: %w", AgentErrTransformGitURL, err) } patchedURL = transformedURL.String() - message.Debugf("original repoURL of (%s) got mutated to (%s)", repoURL, patchedURL) } return patchedURL, nil diff --git a/src/internal/agent/hooks/argocd-repository.go b/src/internal/agent/hooks/argocd-repository.go index 1875772d05..9a8d4167c5 100644 --- a/src/internal/agent/hooks/argocd-repository.go +++ b/src/internal/agent/hooks/argocd-repository.go @@ -14,7 +14,6 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" v1 "k8s.io/api/admission/v1" @@ -57,8 +56,6 @@ func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster return nil, err } - message.Infof("Using the url of (%s) to mutate the ArgoCD Repository Secret", state.GitServer.Address) - secret := corev1.Secret{} if err = json.Unmarshal(r.Object.Raw, &secret); err != nil { return nil, fmt.Errorf(lang.ErrUnmarshal, err) @@ -91,7 +88,6 @@ func mutateRepositorySecret(ctx context.Context, r *v1.AdmissionRequest, cluster return nil, fmt.Errorf("unable the git url: %w", err) } patchedURL = transformedURL.String() - message.Debugf("original url of (%s) got mutated to (%s)", repoCreds.URL, patchedURL) } patches := populateArgoRepositoryPatchOperations(patchedURL, state.GitServer) diff --git a/src/internal/agent/hooks/flux-gitrepo.go b/src/internal/agent/hooks/flux-gitrepo.go index 2fda2969bb..53e123e729 100644 --- a/src/internal/agent/hooks/flux-gitrepo.go +++ b/src/internal/agent/hooks/flux-gitrepo.go @@ -16,7 +16,6 @@ import ( "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/operations" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" v1 "k8s.io/api/admission/v1" ) @@ -51,8 +50,6 @@ func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster return nil, err } - message.Debugf("Using the url of (%s) to mutate the flux repository", state.GitServer.Address) - repo := flux.GitRepository{} if err = json.Unmarshal(r.Object.Raw, &repo); err != nil { return nil, fmt.Errorf(lang.ErrUnmarshal, err) @@ -78,7 +75,6 @@ func mutateGitRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster return nil, fmt.Errorf("%s: %w", AgentErrTransformGitURL, err) } patchedURL = transformedURL.String() - message.Debugf("original git URL of (%s) got mutated to (%s)", repo.Spec.URL, patchedURL) } // Patch updates of the repo spec diff --git a/src/internal/agent/hooks/flux-helmrepo.go b/src/internal/agent/hooks/flux-helmrepo.go index a2fca0b9a4..0af950a14f 100644 --- a/src/internal/agent/hooks/flux-helmrepo.go +++ b/src/internal/agent/hooks/flux-helmrepo.go @@ -65,8 +65,6 @@ func mutateHelmRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluste return nil, err } - message.Debugf("Using the url of (%s) to mutate the flux HelmRepository", registryAddress) - patchedSrc, err := transform.ImageTransformHost(registryAddress, src.Spec.URL) if err != nil { return nil, fmt.Errorf("unable to transform the HelmRepo URL: %w", err) @@ -78,8 +76,6 @@ func mutateHelmRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluste } patchedURL := helpers.OCIURLPrefix + patchedRefInfo.Name - message.Debugf("original HelmRepo URL of (%s) got mutated to (%s)", src.Spec.URL, patchedURL) - patches := populateHelmRepoPatchOperations(patchedURL, zarfState.RegistryInfo.IsInternal()) patches = append(patches, getLabelPatch(src.Labels)) diff --git a/src/internal/agent/hooks/flux-ocirepo.go b/src/internal/agent/hooks/flux-ocirepo.go index e8c3d21a0f..770596f760 100644 --- a/src/internal/agent/hooks/flux-ocirepo.go +++ b/src/internal/agent/hooks/flux-ocirepo.go @@ -44,7 +44,7 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster src.Spec.Reference = &flux.OCIRepositoryRef{} } - // If we have a semver we want to continue since we wil still have the upstream tag + // If we have a semver we want to continue since we will still have the upstream tag // but should warn that we can't guarantee there won't be collisions if src.Spec.Reference.SemVer != "" { message.Warnf(lang.AgentWarnSemVerRef, src.Spec.Reference.SemVer) @@ -69,8 +69,6 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster } // For the internal registry this will be the ip & port of the service, it may look like 10.43.36.151:5000 - message.Debugf("Using the url of (%s) to mutate the flux OCIRepository", registryAddress) - ref := src.Spec.URL if src.Spec.Reference.Digest != "" { ref = fmt.Sprintf("%s@%s", ref, src.Spec.Reference.Digest) @@ -97,8 +95,6 @@ func mutateOCIRepo(ctx context.Context, r *v1.AdmissionRequest, cluster *cluster patchedRef.Tag = patchedRefInfo.Tag } - message.Debugf("original OCIRepo URL of (%s) got mutated to (%s)", src.Spec.URL, patchedURL) - patches := populateOCIRepoPatchOperations(patchedURL, zarfState.RegistryInfo.IsInternal(), patchedRef) patches = append(patches, getLabelPatch(src.Labels)) diff --git a/src/internal/agent/http/admission/handler.go b/src/internal/agent/http/admission/handler.go index 4b4d69323b..57e11fccc6 100644 --- a/src/internal/agent/http/admission/handler.go +++ b/src/internal/agent/http/admission/handler.go @@ -117,7 +117,6 @@ func (h *Handler) Serve(hook operations.Hook) http.HandlerFunc { return } - message.Infof(lang.AgentInfoWebhookAllowed, r.URL.Path, review.Request.Operation, result.Allowed) w.WriteHeader(http.StatusOK) //nolint: errcheck // ignore w.Write(jsonResponse) diff --git a/src/internal/agent/http/proxy.go b/src/internal/agent/http/proxy.go index 760ba709ec..86ce52e897 100644 --- a/src/internal/agent/http/proxy.go +++ b/src/internal/agent/http/proxy.go @@ -5,6 +5,7 @@ package http import ( + "context" "crypto/tls" "fmt" "io" @@ -14,17 +15,19 @@ import ( "strings" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/types" ) // ProxyHandler constructs a new httputil.ReverseProxy and returns an http handler. -func ProxyHandler(cluster *cluster.Cluster) http.HandlerFunc { +func ProxyHandler(ctx context.Context, cluster *cluster.Cluster) http.HandlerFunc { + log := logging.FromContextOrDiscard(ctx) + return func(w http.ResponseWriter, r *http.Request) { state, err := cluster.LoadZarfState(r.Context()) if err != nil { - message.Debugf("%#v", err) + log.Debug("load zarf state failed", "error", err) w.WriteHeader(http.StatusInternalServerError) //nolint: errcheck // ignore w.Write([]byte("unable to load Zarf state, see the Zarf HTTP proxy logs for more details")) @@ -32,7 +35,7 @@ func ProxyHandler(cluster *cluster.Cluster) http.HandlerFunc { } err = proxyRequestTransform(r, state) if err != nil { - message.Debugf("%#v", err) + log.Debug("proxy request transporm failed", "error", err) w.WriteHeader(http.StatusInternalServerError) //nolint: errcheck // ignore w.Write([]byte("unable to transform the provided request, see the Zarf HTTP proxy logs for more details")) diff --git a/src/internal/agent/start.go b/src/internal/agent/start.go index e620c3648a..43a4a22ba5 100644 --- a/src/internal/agent/start.go +++ b/src/internal/agent/start.go @@ -14,12 +14,11 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/sync/errgroup" - "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/agent/hooks" agentHttp "github.com/zarf-dev/zarf/src/internal/agent/http" "github.com/zarf-dev/zarf/src/internal/agent/http/admission" "github.com/zarf-dev/zarf/src/pkg/cluster" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" ) // Heavily influenced by https://github.com/douglasmakey/admissioncontroller and @@ -58,7 +57,7 @@ func StartWebhook(ctx context.Context, cluster *cluster.Cluster) error { // StartHTTPProxy launches the zarf agent proxy in the cluster. func StartHTTPProxy(ctx context.Context, cluster *cluster.Cluster) error { mux := http.NewServeMux() - mux.Handle("/", agentHttp.ProxyHandler(cluster)) + mux.Handle("/", agentHttp.ProxyHandler(ctx, cluster)) return startServer(ctx, httpPort, mux) } @@ -93,7 +92,7 @@ func startServer(ctx context.Context, port string, mux *http.ServeMux) error { } return nil }) - message.Infof(lang.AgentInfoPort, httpPort) + logging.FromContextOrDiscard(ctx).Info("server running", "port", httpPort) err := g.Wait() if err != nil { return err diff --git a/src/internal/git/repository.go b/src/internal/git/repository.go index 9d2b2d4918..e32df67749 100644 --- a/src/internal/git/repository.go +++ b/src/internal/git/repository.go @@ -18,7 +18,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" ) @@ -95,7 +95,7 @@ func Clone(ctx context.Context, rootPath, address string, shallow bool) (*Reposi } repo, err := git.PlainCloneContext(ctx, r.path, false, cloneOpts) if err != nil { - message.Notef("Falling back to host 'git', failed to clone the repo %q with Zarf: %s", gitURLNoRef, err.Error()) + logging.FromContextOrDiscard(ctx).Error("Failling back to the host git, failed to clone the repo with Zarf", "error", err, "repository", gitURLNoRef) err := r.gitCloneFallback(ctx, gitURLNoRef, ref, shallow) if err != nil { return nil, err @@ -146,6 +146,8 @@ func (r *Repository) Path() string { // Push pushes the repository to the remote git server. func (r *Repository) Push(ctx context.Context, address, username, password string) error { + log := logging.FromContextOrDiscard(ctx) + repo, err := git.PlainOpen(r.path) if err != nil { return fmt.Errorf("not a valid git repo or unable to open: %w", err) @@ -194,11 +196,11 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin } err = repo.FetchContext(ctx, fetchOptions) if errors.Is(err, transport.ErrRepositoryNotFound) { - message.Debugf("Repo not yet available offline, skipping fetch...") + log.Debug("repository not yet available offling, skipping fetch") } else if errors.Is(err, git.ErrForceNeeded) { - message.Debugf("Repo fetch requires force, skipping fetch...") + log.Debug("repositoy fetch requires force, skipping fetch") } else if errors.Is(err, git.NoErrAlreadyUpToDate) { - message.Debugf("Repo already up-to-date, skipping fetch...") + log.Debug("repository already update-to date, skipping fetch") } else if err != nil { return fmt.Errorf("unable to fetch the git repo prior to push: %w", err) } @@ -216,7 +218,7 @@ func (r *Repository) Push(ctx context.Context, address, username, password strin }, }) if errors.Is(err, git.NoErrAlreadyUpToDate) { - message.Debug("Repo already up-to-date") + logging.FromContextOrDiscard(ctx).Debug("Repository already up-to-date") } else if errors.Is(err, plumbing.ErrObjectNotFound) { return fmt.Errorf("unable to push repo due to likely shallow clone: %s", err.Error()) } else if err != nil { diff --git a/src/internal/packager/helm/chart.go b/src/internal/packager/helm/chart.go index daf59902e5..0a2d334a4e 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -193,8 +193,7 @@ func (h *Helm) RemoveChart(namespace string, name string, spinner *message.Spinn // Establish a new actionConfig for the namespace. _ = h.createActionConfig(namespace, spinner) // Perform the uninstall. - response, err := h.uninstallChart(name) - message.Debug(response) + _, err := h.uninstallChart(name) return err } diff --git a/src/internal/packager/helm/post-render.go b/src/internal/packager/helm/post-render.go index c316375acb..309c281092 100644 --- a/src/internal/packager/helm/post-render.go +++ b/src/internal/packager/helm/post-render.go @@ -16,6 +16,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/types" @@ -208,6 +209,8 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error { } func (r *renderer) editHelmResources(ctx context.Context, resources []releaseutil.Manifest, finalManifestsOutput *bytes.Buffer) error { + log := logging.FromContextOrDiscard(ctx) + dc, err := dynamic.NewForConfig(r.cluster.RestConfig) if err != nil { return err @@ -232,7 +235,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti if err := runtime.DefaultUnstructuredConverter.FromUnstructured(rawData.UnstructuredContent(), namespace); err != nil { message.WarnErrf(err, "could not parse namespace %s", rawData.GetName()) } else { - message.Debugf("Matched helm namespace %s for zarf annotation", namespace.Name) + log.Debug("matched Helm namespace for Zarf annotation", "namespace", namespace.Name) namespace.Labels = cluster.AdoptZarfManagedLabels(namespace.Labels) // Add it to the stack r.namespaces[namespace.Name] = namespace @@ -252,7 +255,7 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti } if key, keyExists := labels[cluster.ZarfConnectLabelName]; keyExists { // If there is a zarf-connect label - message.Debugf("Match helm service %s for zarf connection %s", rawData.GetName(), key) + log.Debug("match Helm service for Zarf connection", "service", rawData.GetName(), "connection", key) // Add the connectString for processing later in the deployment r.connectStrings[key] = types.ConnectString{ diff --git a/src/internal/packager/helm/repo.go b/src/internal/packager/helm/repo.go index 24f3a7f4b0..13a35b3cd9 100644 --- a/src/internal/packager/helm/repo.go +++ b/src/internal/packager/helm/repo.go @@ -16,6 +16,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/git" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -39,7 +40,7 @@ func (h *Helm) PackageChart(ctx context.Context, cosignKeyPath string) error { // check if the chart is a git url with a ref (if an error is returned url will be empty) isGitURL := strings.HasSuffix(url, ".git") if err != nil { - message.Debugf("unable to parse the url, continuing with %s", h.chart.URL) + logging.FromContextOrDiscard(ctx).Debug("continuing with original url as the url could not be parsed", "url", h.chart.URL, "error", err) } if isGitURL { @@ -147,7 +148,7 @@ func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string) // Not returning the error here since the repo file is only needed if we are pulling from a repo that requires authentication if err != nil { - message.Debugf("Unable to load the repo file at %q: %s", pull.Settings.RepositoryConfig, err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to load the repository file", "path", pull.Settings.RepositoryConfig, "error", err) } var username string diff --git a/src/internal/packager/helm/zarf.go b/src/internal/packager/helm/zarf.go index 03e4db2ed0..c742d37f5e 100644 --- a/src/internal/packager/helm/zarf.go +++ b/src/internal/packager/helm/zarf.go @@ -112,7 +112,7 @@ func (h *Helm) UpdateZarfAgentValues(ctx context.Context) error { Value: agentImage.Tag, }, }) - applicationTemplates, err := template.GetZarfTemplates("zarf-agent", h.state) + applicationTemplates, err := template.GetZarfTemplates(ctx, "zarf-agent", h.state) if err != nil { return fmt.Errorf("error setting up the templates: %w", err) } diff --git a/src/internal/packager/images/pull.go b/src/internal/packager/images/pull.go index 2e206742c6..241db6486e 100644 --- a/src/internal/packager/images/pull.go +++ b/src/internal/packager/images/pull.go @@ -225,7 +225,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er doneSaving := make(chan error) updateText := fmt.Sprintf("Pulling %d images", imageCount) - go utils.RenderProgressBarForLocalDirWrite(cfg.DestinationDirectory, totalBytes.Load(), doneSaving, updateText, updateText) + go utils.RenderProgressBarForLocalDirWrite(ctx, cfg.DestinationDirectory, totalBytes.Load(), doneSaving, updateText, updateText) toPull := maps.Clone(fetched) diff --git a/src/internal/packager/images/push.go b/src/internal/packager/images/push.go index 7bac363001..b3722b7fbf 100644 --- a/src/internal/packager/images/push.go +++ b/src/internal/packager/images/push.go @@ -115,8 +115,6 @@ func Push(ctx context.Context, cfg PushConfig) error { return err } - message.Debugf("push %s -> %s)", refInfo.Reference, offlineName) - if err = pushImage(img, offlineName); err != nil { return err } diff --git a/src/internal/packager/sbom/catalog.go b/src/internal/packager/sbom/catalog.go index aadeeea257..a25f532731 100755 --- a/src/internal/packager/sbom/catalog.go +++ b/src/internal/packager/sbom/catalog.go @@ -5,6 +5,7 @@ package sbom import ( + "context" "embed" "fmt" "os" @@ -27,6 +28,7 @@ import ( v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/transform" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -48,7 +50,7 @@ var transformRegex = regexp.MustCompile(`(?m)[^a-zA-Z0-9\.\-]`) var componentPrefix = "zarf-component-" // Catalog catalogs the given components and images to create an SBOM. -func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transform.Image, paths *layout.PackagePaths) error { +func Catalog(ctx context.Context, componentSBOMs map[string]*layout.ComponentSBOM, imageList []transform.Image, paths *layout.PackagePaths) error { imageCount := len(imageList) componentCount := len(componentSBOMs) builder := Builder{ @@ -103,7 +105,7 @@ func Catalog(componentSBOMs map[string]*layout.ComponentSBOM, imageList []transf builder.spinner.Updatef("Creating component file SBOMs (%d of %d): %s", currComponent, componentCount, component) if componentSBOMs[component] == nil { - message.Debugf("Component %s has invalid SBOM, skipping", component) + logging.FromContextOrDiscard(ctx).Debug("component has invalid SBOM, skipping", "component", component) continue } diff --git a/src/internal/packager/template/template.go b/src/internal/packager/template/template.go index 872754ce4d..61b701055e 100644 --- a/src/internal/packager/template/template.go +++ b/src/internal/packager/template/template.go @@ -5,6 +5,7 @@ package template import ( + "context" "encoding/base64" "fmt" "log/slog" @@ -16,6 +17,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/interactive" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/variables" @@ -41,7 +43,7 @@ func GetZarfVariableConfig() *variables.VariableConfig { } // GetZarfTemplates returns the template keys and values to be used for templating. -func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) { +func GetZarfTemplates(ctx context.Context, componentName string, state *types.ZarfState) (templateMap map[string]*variables.TextTemplate, err error) { templateMap = make(map[string]*variables.TextTemplate) if state != nil { @@ -100,7 +102,7 @@ func GetZarfTemplates(componentName string, state *types.ZarfState) (templateMap } } - debugPrintTemplateMap(templateMap) + debugPrintTemplateMap(ctx, templateMap) return templateMap, nil } @@ -125,7 +127,7 @@ func generateHtpasswd(regInfo *types.RegistryInfo) (string, error) { return "", nil } -func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) { +func debugPrintTemplateMap(ctx context.Context, templateMap map[string]*variables.TextTemplate) { debugText := "templateMap = { " for key, template := range templateMap { @@ -137,6 +139,5 @@ func debugPrintTemplateMap(templateMap map[string]*variables.TextTemplate) { } debugText += " }" - - message.Debug(debugText) + logging.FromContextOrDiscard(ctx).Debug(debugText) } diff --git a/src/pkg/cluster/data.go b/src/pkg/cluster/data.go index 68148003a7..d2fdf4339a 100644 --- a/src/pkg/cluster/data.go +++ b/src/pkg/cluster/data.go @@ -15,16 +15,16 @@ import ( "strings" "time" + "github.com/avast/retry-go/v4" + "github.com/defenseunicorns/pkg/helpers/v2" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - "github.com/avast/retry-go/v4" - "github.com/defenseunicorns/pkg/helpers/v2" - "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/utils/exec" @@ -33,6 +33,8 @@ import ( // HandleDataInjection waits for the target pod(s) to come up and inject the data into them // todo: this currently requires kubectl but we should have enough k8s work to make this native now. func (c *Cluster) HandleDataInjection(ctx context.Context, data v1alpha1.ZarfDataInjection, componentPath *layout.ComponentPaths, dataIdx int) error { + log := logging.FromContextOrDiscard(ctx) + injectionCompletionMarker := filepath.Join(componentPath.DataInjections, config.GetDataInjectionMarker()) if err := os.WriteFile(injectionCompletionMarker, []byte("🦄"), helpers.ReadWriteUser); err != nil { return fmt.Errorf("unable to create the data injection completion marker: %w", err) @@ -60,7 +62,7 @@ func (c *Cluster) HandleDataInjection(ctx context.Context, data v1alpha1.ZarfDat return fmt.Errorf("unable to execute tar, ensure it is installed in the $PATH: %w", err) } - message.Debugf("Attempting to inject data into %s", data.Target) + log.Debug("Attempting to inject data", "target", data.Target) source := filepath.Join(componentPath.DataInjections, filepath.Base(data.Target.Path)) if helpers.InvalidPath(source) { @@ -171,6 +173,8 @@ type podFilter func(pod corev1.Pod) bool // If the timeout is reached, an empty list will be returned. // TODO: Test, refactor and/or remove. func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interface, target podLookup, include podFilter) ([]corev1.Pod, error) { + log := logging.FromContextOrDiscard(ctx) + readyPods, err := retry.DoWithData(func() ([]corev1.Pod, error) { listOpts := metav1.ListOptions{ LabelSelector: target.Selector, @@ -179,7 +183,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac if err != nil { return nil, err } - message.Debugf("Found %d pods for target %#v", len(podList.Items), target) + log.Debug("found pods for container", "count", len(podList.Items), "target", target) // Sort the pods from newest to oldest sort.Slice(podList.Items, func(i, j int) bool { return podList.Items[i].CreationTimestamp.After(podList.Items[j].CreationTimestamp.Time) @@ -187,7 +191,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac readyPods := []corev1.Pod{} for _, pod := range podList.Items { - message.Debugf("Testing pod %q", pod.Name) + log.Debug("testing pod", "pod", pod.Name) // If an include function is provided, only keep pods that return true if include != nil && !include(pod) { @@ -196,7 +200,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac // Handle container targeting if target.Container != "" { - message.Debugf("Testing pod %q for container %q", pod.Name, target.Container) + log.Debug("testing pod for container", "pod", pod.Name, "container", target.Container) // Check the status of initContainers for a running match for _, initContainer := range pod.Status.InitContainerStatuses { @@ -218,7 +222,7 @@ func waitForPodsAndContainers(ctx context.Context, clientset kubernetes.Interfac } } else { status := pod.Status.Phase - message.Debugf("Testing pod %q phase, want (%q) got (%q)", pod.Name, corev1.PodRunning, status) + log.Debug("testing pod phase", "pod", pod.Name, "wanted", corev1.PodRunning, "got", status) // Regular status checking without a container if status == corev1.PodRunning { readyPods = append(readyPods, pod) diff --git a/src/pkg/cluster/secrets.go b/src/pkg/cluster/secrets.go index aa693c6a44..b6424e36d8 100644 --- a/src/pkg/cluster/secrets.go +++ b/src/pkg/cluster/secrets.go @@ -17,6 +17,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/zarf-dev/zarf/src/config" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -198,7 +199,7 @@ func (c *Cluster) GetServiceInfoFromRegistryAddress(ctx context.Context, stateRe // If this is an internal service then we need to look it up and svc, port, err := serviceInfoFromNodePortURL(serviceList.Items, stateRegistryAddress) if err != nil { - message.Debugf("registry appears to not be a nodeport service, using original address %q", stateRegistryAddress) + logging.FromContextOrDiscard(ctx).Debug("registry appears to not be a node port service, using original address", "address", stateRegistryAddress) return stateRegistryAddress, nil } diff --git a/src/pkg/cluster/state.go b/src/pkg/cluster/state.go index 3c31ccf128..c8854904c3 100644 --- a/src/pkg/cluster/state.go +++ b/src/pkg/cluster/state.go @@ -20,6 +20,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/pki" "github.com/zarf-dev/zarf/src/types" @@ -203,7 +204,7 @@ func (c *Cluster) LoadZarfState(ctx context.Context) (state *types.ZarfState, er if err != nil { return nil, fmt.Errorf("%w: %w", stateErr, err) } - c.debugPrintZarfState(state) + c.debugPrintZarfState(ctx, state) return state, nil } @@ -228,7 +229,7 @@ func (c *Cluster) sanitizeZarfState(state *types.ZarfState) *types.ZarfState { return state } -func (c *Cluster) debugPrintZarfState(state *types.ZarfState) { +func (c *Cluster) debugPrintZarfState(ctx context.Context, state *types.ZarfState) { if state == nil { return } @@ -239,12 +240,12 @@ func (c *Cluster) debugPrintZarfState(state *types.ZarfState) { if err != nil { return } - message.Debugf("ZarfState - %s", string(b)) + logging.FromContextOrDiscard(ctx).Debug("ZarfState", "state", string(b)) } // SaveZarfState takes a given state and persists it to the Zarf/zarf-state secret. func (c *Cluster) SaveZarfState(ctx context.Context, state *types.ZarfState) error { - c.debugPrintZarfState(state) + c.debugPrintZarfState(ctx, state) data, err := json.Marshal(&state) if err != nil { diff --git a/src/pkg/cluster/tunnel.go b/src/pkg/cluster/tunnel.go index 764ae9a6eb..6df32889f5 100644 --- a/src/pkg/cluster/tunnel.go +++ b/src/pkg/cluster/tunnel.go @@ -23,7 +23,7 @@ import ( "github.com/avast/retry-go/v4" "github.com/defenseunicorns/pkg/helpers/v2" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/types" ) @@ -218,8 +218,6 @@ func (c *Cluster) checkForZarfConnectLabel(ctx context.Context, name string) (Tu // Add the url suffix too. zt.urlSuffix = svc.Annotations[ZarfConnectAnnotationURL] - - message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.RemotePort) } else { return zt, fmt.Errorf("no matching services found for %s", name) } @@ -391,6 +389,8 @@ func (tunnel *Tunnel) Close() { // establish opens a tunnel to a kubernetes resource, as specified by the provided tunnel struct. func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { + log := logging.FromContextOrDiscard(ctx) + var err error // Track this locally as we may need to retry if the tunnel fails. @@ -403,12 +403,12 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { // since there is a brief moment between `GetAvailablePort` and `forwarder.ForwardPorts` where the selected port // is available for selection again. if localPort == 0 { - message.Debugf("Requested local port is 0. Selecting an open port on host system") + log.Debug("requested local port is 0, selection an open port on host system") localPort, err = helpers.GetAvailablePort() if err != nil { return "", fmt.Errorf("unable to find an available port: %w", err) } - message.Debugf("Selected port %d", localPort) + log.Debug("selected port", "port", localPort) globalMutex.Lock() defer globalMutex.Unlock() } @@ -420,14 +420,14 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { tunnel.resourceName, tunnel.namespace, ) - message.Debugf(msg) + log.Debug(msg) // Find the pod to port forward to podName, err := tunnel.getAttachablePodForResource(ctx) if err != nil { return "", fmt.Errorf("unable to find pod attached to given resource: %w", err) } - message.Debugf("Selected pod %s to open port forward to", podName) + log.Debug("selected pod to open port forward to", "pod", podName) // Build url to the port forward endpoint. // Example: http://localhost:8080/api/v1/namespaces/helm/pods/tiller-deploy-9itlq/portforward. @@ -440,7 +440,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { SubResource("portforward"). URL() - message.Debugf("Using URL %s to create portforward", portForwardCreateURL) + log.Debug("using URL to create port forward", "url", portForwardCreateURL) // Construct the spdy client required by the client-go portforward library. transport, upgrader, err := spdy.RoundTripperFor(tunnel.restConfig) @@ -475,7 +475,7 @@ func (tunnel *Tunnel) establish(ctx context.Context) (string, error) { // Store the error channel to listen for errors tunnel.errChan = errChan - message.Debugf("Creating port forwarding tunnel at %s", url) + log.Debug("creation for forwarding tunnel", "url", url) return url, nil } } diff --git a/src/pkg/cluster/zarf.go b/src/pkg/cluster/zarf.go index 14c3e2f31a..6390205d0b 100644 --- a/src/pkg/cluster/zarf.go +++ b/src/pkg/cluster/zarf.go @@ -12,15 +12,16 @@ import ( "strings" "time" + "github.com/avast/retry-go/v4" autoscalingV2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/avast/retry-go/v4" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/gitea" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/types" ) @@ -181,7 +182,7 @@ func (c *Cluster) RecordPackageDeployment(ctx context.Context, pkg v1alpha1.Zarf var componentWebhooks map[string]map[string]types.Webhook existingPackageSecret, err := c.GetDeployedPackage(ctx, packageName) if err != nil { - message.Debugf("Unable to fetch existing secret for package '%s': %s", packageName, err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to fetch existing secret for package", "package", packageName, "error", err) } if existingPackageSecret != nil { componentWebhooks = existingPackageSecret.ComponentWebhooks diff --git a/src/pkg/layout/component.go b/src/pkg/layout/component.go index fee2d90082..39c9b8ed93 100644 --- a/src/pkg/layout/component.go +++ b/src/pkg/layout/component.go @@ -12,8 +12,8 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/mholt/archiver/v3" + "github.com/zarf-dev/zarf/src/api/v1alpha1" - "github.com/zarf-dev/zarf/src/pkg/message" ) // ComponentPaths contains paths for a component. @@ -58,7 +58,6 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) } if size > 0 { tb := fmt.Sprintf("%s.tar", base) - message.Debugf("Archiving %q", name) if err := helpers.CreateReproducibleTarballFromDir(base, name, tb); err != nil { return err } @@ -66,8 +65,6 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) c.Tarballs = make(map[string]string) } c.Tarballs[name] = tb - } else { - message.Debugf("Component %q is empty, skipping archiving", name) } delete(c.Dirs, name) @@ -126,11 +123,9 @@ func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) { // if the component is already unarchived, skip if !helpers.InvalidPath(cs.Base) { - message.Debugf("Component %q already unarchived", name) return nil } - message.Debugf("Unarchiving %q", filepath.Base(tb)) if err := archiver.Unarchive(tb, c.Base); err != nil { return err } diff --git a/src/pkg/layout/package.go b/src/pkg/layout/package.go index a38ec599a1..5437836c0d 100644 --- a/src/pkg/layout/package.go +++ b/src/pkg/layout/package.go @@ -99,7 +99,6 @@ func (pp *PackagePaths) MigrateLegacy() (err error) { legacySBOMs := filepath.Join(base, "sboms") if !helpers.InvalidPath(legacySBOMs) { pp = pp.AddSBOMs() - message.Debugf("Migrating %q to %q", legacySBOMs, pp.SBOMs.Path) if err := os.Rename(legacySBOMs, pp.SBOMs.Path); err != nil { return err } @@ -109,7 +108,6 @@ func (pp *PackagePaths) MigrateLegacy() (err error) { legacyImagesTar := filepath.Join(base, "images.tar") if !helpers.InvalidPath(legacyImagesTar) { pp = pp.AddImages() - message.Debugf("Migrating %q to %q", legacyImagesTar, pp.Images.Base) defer os.Remove(legacyImagesTar) imgTags := []string{} for _, component := range pkg.Components { @@ -309,8 +307,6 @@ func (pp *PackagePaths) SetFromPaths(paths []string) { pp.Components.Tarballs = make(map[string]string) } pp.Components.Tarballs[componentName] = filepath.Join(pp.Base, path) - default: - message.Debug("ignoring path", path) } } } diff --git a/src/pkg/lint/lint.go b/src/pkg/lint/lint.go index 3c4be87eaf..6030897a04 100644 --- a/src/pkg/lint/lint.go +++ b/src/pkg/lint/lint.go @@ -14,7 +14,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/packager/composer" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/types" @@ -43,7 +43,7 @@ func Validate(ctx context.Context, createOpts types.ZarfCreateOptions) error { findings = append(findings, schemaFindings...) if len(findings) == 0 { - message.Successf("0 findings for %q", pkg.Metadata.Name) + logging.FromContextOrDiscard(ctx).Info("No findings found", "package", pkg.Metadata.Name) return nil } PrintFindings(findings, SevWarn, createOpts.BaseDir, pkg.Metadata.Name) diff --git a/src/pkg/logging/handler.go b/src/pkg/logging/handler.go new file mode 100644 index 0000000000..3b524a6772 --- /dev/null +++ b/src/pkg/logging/handler.go @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +package logging + +import ( + "context" + "fmt" + "log/slog" + "strings" + + "github.com/zarf-dev/zarf/src/pkg/message" +) + +// PtermHandler is a slog handler that prints using Pterm. +type PtermHandler struct { + attrs []slog.Attr + group string +} + +// NewPtermHandler returns a new instance of PtermHandler. +func NewPtermHandler() *PtermHandler { + return &PtermHandler{} +} + +//nolint:revive // ignore +func (h *PtermHandler) Enabled(context.Context, slog.Level) bool { + return true +} + +//nolint:revive // ignore +func (h *PtermHandler) Handle(ctx context.Context, r slog.Record) error { + attrs := []string{} + r.Attrs(func(a slog.Attr) bool { + attrs = append(attrs, a.String()) + return true + }) + out := fmt.Sprintf("%s %s", r.Message, strings.Join(attrs, " ")) + switch r.Level { + case slog.LevelDebug: + message.Debug(out) + case slog.LevelInfo: + message.Info(out) + case slog.LevelWarn: + message.Warn(out) + case slog.LevelError: + message.Warn(out) + } + return nil +} + +//nolint:revive // ignore +func (h *PtermHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &PtermHandler{ + attrs: append(h.attrs, attrs...), + group: h.group, + } +} + +//nolint:revive // ignore +func (h *PtermHandler) WithGroup(name string) slog.Handler { + return &PtermHandler{ + attrs: h.attrs, + group: name, + } +} diff --git a/src/pkg/logging/logging.go b/src/pkg/logging/logging.go new file mode 100644 index 0000000000..b9be19b8a8 --- /dev/null +++ b/src/pkg/logging/logging.go @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +// Package logging contains logging related functionality. +package logging + +import ( + "context" + "io" + "log/slog" +) + +type contextKey struct{} + +// NewContext returns a child context containing the given logger. +func NewContext(ctx context.Context, log *slog.Logger) context.Context { + return context.WithValue(ctx, contextKey{}, log) +} + +// FromContextOrDiscard returns the logger stored in the context. +func FromContextOrDiscard(ctx context.Context) *slog.Logger { + v := ctx.Value(contextKey{}) + if v == nil { + return slog.New(slog.NewJSONHandler(io.Discard, nil)) + } + log, ok := v.(*slog.Logger) + if !ok { + // This should never happen. + panic("unexpected logger type") + } + return log +} diff --git a/src/pkg/message/message.go b/src/pkg/message/message.go index e271cbf9d4..632fded66f 100644 --- a/src/pkg/message/message.go +++ b/src/pkg/message/message.go @@ -119,12 +119,6 @@ func Debug(payload ...any) { debugPrinter(2, payload...) } -// Debugf prints a debug message with a given format. -func Debugf(format string, a ...any) { - message := fmt.Sprintf(format, a...) - debugPrinter(2, message) -} - // Warn prints a warning message. func Warn(message string) { Warnf("%s", message) @@ -162,11 +156,6 @@ func Infof(format string, a ...any) { } } -// Success prints a success message. -func Success(message string) { - Successf("%s", message) -} - // Successf prints a success message with a given format. func Successf(format string, a ...any) { message := Paragraph(format, a...) diff --git a/src/pkg/packager/actions/actions.go b/src/pkg/packager/actions/actions.go index dbef82b613..8d5451ecec 100644 --- a/src/pkg/packager/actions/actions.go +++ b/src/pkg/packager/actions/actions.go @@ -15,6 +15,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/internal/packager/template" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/utils/exec" @@ -205,7 +206,7 @@ func convertWaitToCmd(_ context.Context, wait v1alpha1.ZarfComponentActionWait, } // Perform some basic string mutations to make commands more useful. -func actionCmdMutation(_ context.Context, cmd string, shellPref v1alpha1.Shell) (string, error) { +func actionCmdMutation(ctx context.Context, cmd string, shellPref v1alpha1.Shell) (string, error) { zarfCommand, err := utils.GetFinalExecutableCommand() if err != nil { return cmd, err @@ -227,7 +228,7 @@ func actionCmdMutation(_ context.Context, cmd string, shellPref v1alpha1.Shell) get, err := helpers.MatchRegex(envVarRegex, cmd) if err == nil { newCmd := strings.ReplaceAll(cmd, get("envIndicator"), fmt.Sprintf("$Env:%s", get("varName"))) - message.Debugf("Converted command \"%s\" to \"%s\" t", cmd, newCmd) + logging.FromContextOrDiscard(ctx).Debug("converted command", "old", cmd, "new", newCmd) cmd = newCmd } } @@ -278,7 +279,7 @@ func actionGetCfg(_ context.Context, cfg v1alpha1.ZarfComponentActionDefaults, a func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cmd string, shellPref v1alpha1.Shell, spinner *message.Spinner) (string, error) { shell, shellArgs := exec.GetOSShell(shellPref) - message.Debugf("Running command in %s: %s", shell, cmd) + logging.FromContextOrDiscard(ctx).Debug("running command", "shell", shell, "command", cmd) execCfg := exec.Config{ Env: cfg.Env, @@ -293,7 +294,7 @@ func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cm out, errOut, err := exec.CmdWithContext(ctx, execCfg, shell, append(shellArgs, cmd)...) // Dump final complete output (respect mute to prevent sensitive values from hitting the logs). if !cfg.Mute { - message.Debug(cmd, out, errOut) + logging.FromContextOrDiscard(ctx).Debug(cmd, "stdout", out, "stderr", errOut) } return out, err diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index 6a3ec004e7..a04922d602 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -20,6 +20,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/template" "github.com/zarf-dev/zarf/src/pkg/cluster" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/deprecated" "github.com/zarf-dev/zarf/src/pkg/packager/sources" @@ -71,7 +72,7 @@ New creates a new package instance with the provided config. Note: This function creates a tmp directory that should be cleaned up with p.ClearTempPaths(). */ -func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { +func New(ctx context.Context, cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { if cfg == nil { return nil, fmt.Errorf("no config provided") } @@ -110,7 +111,7 @@ func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) { if err != nil { return nil, fmt.Errorf("unable to create package temp paths: %w", err) } - message.Debug("Using temporary directory:", dir) + logging.FromContextOrDiscard(ctx).Debug("Using temporary directory", "directory", dir) pkgr.layout = layout.New(dir) } diff --git a/src/pkg/packager/composer/oci.go b/src/pkg/packager/composer/oci.go index 2541e45f32..05299ca03e 100644 --- a/src/pkg/packager/composer/oci.go +++ b/src/pkg/packager/composer/oci.go @@ -17,7 +17,7 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/pkg/zoci" ocistore "oras.land/oras-go/v2/content/oci" @@ -80,7 +80,7 @@ func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error { dir = filepath.Join(cache, "dirs", id) - message.Debug("creating empty directory for remote component:", filepath.Join("", "oci", "dirs", id)) + logging.FromContextOrDiscard(ctx).Debug("Creaing empty directory for remote component", "path", filepath.Join("", "oci", "dirs", id)) } else { tb = filepath.Join(cache, "blobs", "sha256", componentDesc.Digest.Encoded()) dir = filepath.Join(cache, "dirs", componentDesc.Digest.Encoded()) @@ -97,7 +97,7 @@ func (ic *ImportChain) fetchOCISkeleton(ctx context.Context) error { } else if !exists { doneSaving := make(chan error) successText := fmt.Sprintf("Pulling %q", helpers.OCIURLPrefix+remote.Repo().Reference.String()) - go utils.RenderProgressBarForLocalDirWrite(cache, componentDesc.Size, doneSaving, "Pulling", successText) + go utils.RenderProgressBarForLocalDirWrite(ctx, cache, componentDesc.Size, doneSaving, "Pulling", successText) err = remote.CopyToTarget(ctx, []ocispec.Descriptor{componentDesc}, store, remote.GetDefaultCopyOpts()) doneSaving <- err <-doneSaving diff --git a/src/pkg/packager/create.go b/src/pkg/packager/create.go index 517726eba4..db5e76a834 100755 --- a/src/pkg/packager/create.go +++ b/src/pkg/packager/create.go @@ -12,7 +12,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/packager/creator" ) @@ -27,7 +27,7 @@ func (p *Packager) Create(ctx context.Context) error { return fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err) } - message.Note(fmt.Sprintf("Using build directory %s", p.cfg.CreateOpts.BaseDir)) + logging.FromContextOrDiscard(ctx).Info("Using build directory", "path", p.cfg.CreateOpts.BaseDir) pc := creator.NewPackageCreator(p.cfg.CreateOpts, cwd) diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index 847a22003e..d3e89c0806 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -28,6 +28,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/kustomize" "github.com/zarf-dev/zarf/src/internal/packager/sbom" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -138,7 +139,7 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths onFailure := func() { if err := actions.Run(ctx, onCreate.Defaults, onCreate.OnFailure, nil); err != nil { - message.Debugf("unable to run component failure action: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Debug("unable to run component failure action", "error", err) } } @@ -213,10 +214,10 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths // Ignore SBOM creation if the flag is set. if skipSBOMFlagUsed { - message.Debug("Skipping image SBOM processing per --skip-sbom flag") + logging.FromContextOrDiscard(ctx).Debug("Skipping image SBOm processing per --skip-sbom flag") } else { dst.AddSBOMs() - if err := sbom.Catalog(componentSBOMs, sbomImageList, dst); err != nil { + if err := sbom.Catalog(ctx, componentSBOMs, sbomImageList, dst); err != nil { return fmt.Errorf("unable to create an SBOM catalog for the package: %w", err) } } diff --git a/src/pkg/packager/deploy.go b/src/pkg/packager/deploy.go index 2c5f2e5f2d..d276f52b9d 100644 --- a/src/pkg/packager/deploy.go +++ b/src/pkg/packager/deploy.go @@ -33,6 +33,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/template" "github.com/zarf-dev/zarf/src/pkg/cluster" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -48,7 +49,7 @@ var ( func (p *Packager) resetRegistryHPA(ctx context.Context) { if p.isConnectedToCluster() && p.hpaModified { if err := p.cluster.EnableRegHPAScaleDown(ctx); err != nil { - message.Debugf("unable to reenable the registry HPA scale down: %s", err.Error()) + logging.FromContextOrDiscard(ctx).Error("Unable to reenable the registry HPA scale down", "error", err) } } } @@ -127,7 +128,7 @@ func (p *Packager) Deploy(ctx context.Context) error { } // Notify all the things about the successful deployment - message.Successf("Zarf deployment complete") + logging.FromContextOrDiscard(ctx).Info("Zarf deployment complete") err = p.printTablesForDeployment(ctx, deployedComponents) if err != nil { @@ -139,6 +140,8 @@ func (p *Packager) Deploy(ctx context.Context) error { // deployComponents loops through a list of ZarfComponents and deploys them. func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []types.DeployedComponent, err error) { + log := logging.FromContextOrDiscard(ctx) + // Process all the components we are deploying for _, component := range p.cfg.Pkg.Components { // Connect to cluster if a component requires it. @@ -170,7 +173,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t if p.isConnectedToCluster() { deployedComponent.InstalledCharts, err = p.cluster.GetInstalledChartsForComponent(ctx, p.cfg.Pkg.Metadata.Name, component) if err != nil { - message.Debugf("Unable to fetch installed Helm charts for component '%s': %s", component.Name, err.Error()) + log.Debug("unable to fetch installed Helm charts for the component", "component", component.Name, "error", err) } } @@ -180,7 +183,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t // Update the package secret to indicate that we are attempting to deploy this component if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %s: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -197,7 +200,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t onFailure := func() { if err := actions.Run(ctx, onDeploy.Defaults, onDeploy.OnFailure, p.variableConfig); err != nil { - message.Debugf("unable to run component failure action: %s", err.Error()) + log.Debug("unable to run component failure action", "error", err) } } @@ -208,7 +211,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t deployedComponents[idx].Status = types.ComponentStatusFailed if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -220,7 +223,7 @@ func (p *Packager) deployComponents(ctx context.Context) (deployedComponents []t deployedComponents[idx].Status = types.ComponentStatusSucceeded if p.isConnectedToCluster() { if _, err := p.cluster.RecordPackageDeploymentAndWait(ctx, p.cfg.Pkg, deployedComponents, p.connectStrings, packageGeneration, component, p.cfg.DeployOpts.SkipWebhooks); err != nil { - message.Debugf("Unable to record package deployment for component %q: this will affect features like `zarf package remove`: %s", component.Name, err.Error()) + log.Debug("unable to record package deployment for component", "component", component.Name, "error", err) } } @@ -254,7 +257,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z } if hasExternalRegistry && (isSeedRegistry || isInjector || isRegistry) { - message.Notef("Not deploying the component (%s) since external registry information was provided during `zarf init`", component.Name) + logging.FromContextOrDiscard(ctx).Info("Skipping deploying the component since external registyr information was provided during init", "component", component.Name) return nil, nil } @@ -288,6 +291,8 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z // Deploy a Zarf Component. func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfComponent, noImgChecksum bool, noImgPush bool) (charts []types.InstalledChart, err error) { + log := logging.FromContextOrDiscard(ctx) + // Toggles for general deploy operations componentPath := p.layout.Components.Dirs[component.Name] @@ -314,14 +319,14 @@ func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfC // Disable the registry HPA scale down if we are deploying images and it is not already disabled if hasImages && !p.hpaModified && p.state.RegistryInfo.IsInternal() { if err := p.cluster.DisableRegHPAScaleDown(ctx); err != nil { - message.Debugf("unable to disable the registry HPA scale down: %s", err.Error()) + log.Error("Unable to disable the registry HPA scale down", "error", err.Error()) } else { p.hpaModified = true } } } - err = p.populateComponentAndStateTemplates(component.Name) + err = p.populateComponentAndStateTemplates(ctx, component.Name) if err != nil { return charts, err } @@ -505,8 +510,8 @@ func (p *Packager) setupState(ctx context.Context) (err error) { return nil } -func (p *Packager) populateComponentAndStateTemplates(componentName string) error { - applicationTemplates, err := template.GetZarfTemplates(componentName, p.state) +func (p *Packager) populateComponentAndStateTemplates(ctx context.Context, componentName string) error { + applicationTemplates, err := template.GetZarfTemplates(ctx, componentName, p.state) if err != nil { return err } diff --git a/src/pkg/packager/deploy_test.go b/src/pkg/packager/deploy_test.go index a1f32aa346..40d2bb3d7d 100644 --- a/src/pkg/packager/deploy_test.go +++ b/src/pkg/packager/deploy_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/pkg/packager/sources" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" ) @@ -212,7 +213,7 @@ func TestGenerateValuesOverrides(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - p, err := New(&types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{})) + p, err := New(testutil.TestContext(t), &types.PackagerConfig{DeployOpts: tt.deployOpts}, WithSource(&sources.TarballSource{})) require.NoError(t, err) for k, v := range tt.setVariables { p.variableConfig.SetVariable(k, v, false, false, v1alpha1.RawVariableType) diff --git a/src/pkg/packager/dev.go b/src/pkg/packager/dev.go index 49232ddc1b..09cb889355 100644 --- a/src/pkg/packager/dev.go +++ b/src/pkg/packager/dev.go @@ -11,6 +11,7 @@ import ( "runtime" "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/saferwall/pe/log" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/message" @@ -95,7 +96,7 @@ func (p *Packager) DevDeploy(ctx context.Context) error { } // Notify all the things about the successful deployment - message.Successf("Zarf dev deployment complete") + log.Info("Zarf dev deployment complete") message.HorizontalRule() message.Title("Next steps:", "") diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index 37b80f0ec4..5ed14041c2 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -29,6 +29,7 @@ import ( "github.com/zarf-dev/zarf/src/internal/packager/images" "github.com/zarf-dev/zarf/src/internal/packager/kustomize" "github.com/zarf-dev/zarf/src/pkg/layout" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/creator" "github.com/zarf-dev/zarf/src/pkg/utils" @@ -53,7 +54,7 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil { return nil, fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err) } - message.Note(fmt.Sprintf("Using build directory %s", p.cfg.CreateOpts.BaseDir)) + logging.FromContextOrDiscard(ctx).Info("Using build directory", "path", p.cfg.CreateOpts.BaseDir) c := creator.NewPackageCreator(p.cfg.CreateOpts, cwd) @@ -75,12 +76,11 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) // TODO: Refactor to return output string instead of printing inside of function. func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) { + log := logging.FromContextOrDiscard(ctx) + for _, component := range p.cfg.Pkg.Components { if len(component.Repos) > 0 && p.cfg.FindImagesOpts.RepoHelmChartPath == "" { - message.Note("This Zarf package contains git repositories, " + - "if any repos contain helm charts you want to template and " + - "search for images, make sure to specify the helm chart path " + - "via the --repo-chart-path flag") + log.Info("This Zarf package contains git repositories, if any repos contain helm charts you want to template and search for images, make sure to specify the helm chart path via the --repo-chart-path flag") break } } @@ -140,7 +140,7 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) if err != nil { return nil, err } - err = p.populateComponentAndStateTemplates(component.Name) + err = p.populateComponentAndStateTemplates(ctx, component.Name) if err != nil { return nil, err } @@ -288,10 +288,10 @@ func (p *Packager) findImages(ctx context.Context) (map[string][]string, error) for _, image := range sortedExpectedImages { if descriptor, err := crane.Head(image, images.WithGlobalInsecureFlag()...); err != nil { // Test if this is a real image, if not just quiet log to debug, this is normal - message.Debugf("Suspected image does not appear to be valid: %#v", err) + log.Error("Suspected image does not appear to be valid", "error", err) } else { // Otherwise, add to the list of images - message.Debugf("Imaged digest found: %s", descriptor.Digest) + log.Debug("Image digest found", "digest", descriptor.Digest) validImages = append(validImages, image) } } @@ -395,7 +395,6 @@ func processUnstructuredImages(resource *unstructured.Unstructured, matchedImage // Capture any custom images matches := imageCheck.FindAllStringSubmatch(string(b), -1) for _, group := range matches { - message.Debugf("Found unknown match, Kind: %s, Value: %s", resource.GetKind(), group[1]) matchedImages[group[1]] = true } } @@ -403,7 +402,6 @@ func processUnstructuredImages(resource *unstructured.Unstructured, matchedImage // Capture "maybe images" too for all kinds because they might be in unexpected places.... 👀 matches := imageFuzzyCheck.FindAllStringSubmatch(string(b), -1) for _, group := range matches { - message.Debugf("Found possible fuzzy match, Kind: %s, Value: %s", resource.GetKind(), group[1]) maybeImages[group[1]] = true } diff --git a/src/pkg/packager/prepare_test.go b/src/pkg/packager/prepare_test.go index f2fb760534..d860d842ef 100644 --- a/src/pkg/packager/prepare_test.go +++ b/src/pkg/packager/prepare_test.go @@ -91,7 +91,7 @@ func TestFindImages(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - p, err := New(tt.cfg) + p, err := New(ctx, tt.cfg) require.NoError(t, err) images, err := p.FindImages(ctx) if tt.expectedErr != "" { diff --git a/src/pkg/packager/remove.go b/src/pkg/packager/remove.go index 4edcdd5b05..54fd983ae6 100644 --- a/src/pkg/packager/remove.go +++ b/src/pkg/packager/remove.go @@ -22,6 +22,7 @@ import ( "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/internal/packager/helm" "github.com/zarf-dev/zarf/src/pkg/cluster" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/actions" "github.com/zarf-dev/zarf/src/pkg/packager/filters" @@ -171,7 +172,7 @@ func (p *Packager) removeComponent(ctx context.Context, deployedPackage *types.D onRemove := c.Actions.OnRemove onFailure := func() { if err := actions.Run(ctx, onRemove.Defaults, onRemove.OnFailure, nil); err != nil { - message.Debugf("Unable to run the failure action: %s", err) + logging.FromContextOrDiscard(ctx).Error("unable to run the failure action", "error", err) } } diff --git a/src/pkg/packager/sources/split.go b/src/pkg/packager/sources/split.go index f4f97a25a6..0e505ddb15 100644 --- a/src/pkg/packager/sources/split.go +++ b/src/pkg/packager/sources/split.go @@ -17,7 +17,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/packager/filters" "github.com/zarf-dev/zarf/src/types" ) @@ -33,7 +33,7 @@ type SplitTarballSource struct { } // Collect turns a split tarball into a full tarball. -func (s *SplitTarballSource) Collect(_ context.Context, dir string) (string, error) { +func (s *SplitTarballSource) Collect(ctx context.Context, dir string) (string, error) { pattern := strings.Replace(s.PackageSource, ".part000", ".part*", 1) fileList, err := filepath.Glob(pattern) if err != nil { @@ -105,7 +105,7 @@ func (s *SplitTarballSource) Collect(_ context.Context, dir string) (string, err } // communicate to the user that the package was reassembled - message.Infof("Reassembled package to: %q", reassembled) + logging.FromContextOrDiscard(ctx).Info("reassembled package", "path", reassembled) return reassembled, nil } diff --git a/src/pkg/packager/sources/validate.go b/src/pkg/packager/sources/validate.go index 1c7914ea69..d6a2e4ba39 100644 --- a/src/pkg/packager/sources/validate.go +++ b/src/pkg/packager/sources/validate.go @@ -17,7 +17,7 @@ import ( "github.com/defenseunicorns/pkg/helpers/v2" "github.com/zarf-dev/zarf/src/config" "github.com/zarf-dev/zarf/src/pkg/layout" - "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/utils" ) @@ -36,7 +36,7 @@ func ValidatePackageSignature(ctx context.Context, paths *layout.PackagePaths, p } if publicKeyPath != "" { - message.Debugf("Using public key %q for signature validation", publicKeyPath) + logging.FromContextOrDiscard(ctx).Debug("using public key for signature validation", "path", publicKeyPath) } // Handle situations where there is no signature within the package diff --git a/src/pkg/utils/bytes.go b/src/pkg/utils/bytes.go index 7dd159b91f..2fc7930d47 100644 --- a/src/pkg/utils/bytes.go +++ b/src/pkg/utils/bytes.go @@ -7,12 +7,14 @@ package utils import ( + "context" "fmt" "math" "strconv" "time" "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" ) @@ -59,7 +61,9 @@ func ByteFormat(inputNum float64, precision int) string { // RenderProgressBarForLocalDirWrite creates a progress bar that continuously tracks the progress of writing files to a local directory and all of its subdirectories. // NOTE: This function runs infinitely until either completeChan or errChan is triggered, this function should be run in a goroutine while a different thread/process is writing to the directory. -func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, completeChan chan error, updateText string, successText string) { +func RenderProgressBarForLocalDirWrite(ctx context.Context, filepath string, expectedTotal int64, completeChan chan error, updateText string, successText string) { + log := logging.FromContextOrDiscard(ctx) + // Create a progress bar title := fmt.Sprintf("%s (%s of %s)", updateText, ByteFormat(float64(0), 2), ByteFormat(float64(expectedTotal), 2)) progressBar := message.NewProgressBar(expectedTotal, title) @@ -74,7 +78,7 @@ func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, com return } else { if err := progressBar.Close(); err != nil { - message.Debugf("unable to close progress bar: %s", err.Error()) + log.Error("Unable to close progress bar", "error", err) } completeChan <- nil return @@ -83,7 +87,7 @@ func RenderProgressBarForLocalDirWrite(filepath string, expectedTotal int64, com // Read the directory size currentBytes, dirErr := helpers.GetDirSize(filepath) if dirErr != nil { - message.Debugf("unable to get updated progress: %s", dirErr.Error()) + log.Error("Unable to get updated progress", "error", dirErr) time.Sleep(200 * time.Millisecond) continue } diff --git a/src/pkg/utils/cosign.go b/src/pkg/utils/cosign.go index 1e129ffb5c..c05d2de389 100644 --- a/src/pkg/utils/cosign.go +++ b/src/pkg/utils/cosign.go @@ -16,7 +16,9 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/pkg/errors" "github.com/zarf-dev/zarf/src/config/lang" + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" + "sigs.k8s.io/controller-runtime/pkg/log" "github.com/sigstore/cosign/v2/cmd/cosign/cli/fulcio" "github.com/sigstore/cosign/v2/cmd/cosign/cli/options" @@ -37,6 +39,8 @@ import ( // // Forked from https://github.com/sigstore/cosign/blob/v1.7.1/pkg/sget/sget.go func Sget(ctx context.Context, image, key string, out io.Writer) error { + log := logging.FromContextOrDiscard(ctx) + message.Warnf(lang.WarnSGetDeprecation) // Remove the custom protocol header from the url @@ -131,11 +135,11 @@ func Sget(ctx context.Context, image, key string, out io.Writer) error { for _, sig := range sp { if cert, err := sig.Cert(); err == nil && cert != nil { - message.Debugf("Certificate subject: %s", cert.Subject) + log.Info("Certificate subject", "subject", cert.Subject) ce := cosign.CertExtensions{Cert: cert} if issuerURL := ce.GetIssuer(); issuerURL != "" { - message.Debugf("Certificate issuer URL: %s", issuerURL) + log.Debug("Certificate issues", "url", issuerURL) } } @@ -144,7 +148,7 @@ func Sget(ctx context.Context, image, key string, out io.Writer) error { spinner.Errorf(err, "Error getting payload") return err } - message.Debug(string(p)) + log.Debug(string(p)) } // TODO(mattmoor): Depending on what this is, use the higher-level stuff. @@ -182,7 +186,7 @@ func CosignVerifyBlob(ctx context.Context, blobRef string, sigRef string, keyPat } err := cmd.Exec(ctx, blobRef) if err == nil { - message.Successf("Package signature validated!") + log.FromContext(ctx).Info("Package signature validated") } return err diff --git a/src/pkg/utils/wait.go b/src/pkg/utils/wait.go index dbcbbf0267..822adf1b92 100644 --- a/src/pkg/utils/wait.go +++ b/src/pkg/utils/wait.go @@ -5,6 +5,7 @@ package utils import ( + "context" "errors" "fmt" "net" @@ -15,9 +16,9 @@ import ( "time" "github.com/zarf-dev/zarf/src/api/v1alpha1" - "github.com/zarf-dev/zarf/src/pkg/utils/exec" - + "github.com/zarf-dev/zarf/src/pkg/logging" "github.com/zarf-dev/zarf/src/pkg/message" + "github.com/zarf-dev/zarf/src/pkg/utils/exec" ) // isJSONPathWaitType checks if the condition is a JSONPath or condition. @@ -30,11 +31,13 @@ func isJSONPathWaitType(condition string) bool { } // ExecuteWait executes the wait-for command. -func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, timeout time.Duration) error { +func ExecuteWait(ctx context.Context, waitTimeout, waitNamespace, condition, kind, identifier string, timeout time.Duration) error { + log := logging.FromContextOrDiscard(ctx) + // Handle network endpoints. switch kind { case "http", "https", "tcp": - return waitForNetworkEndpoint(kind, identifier, condition, timeout) + return waitForNetworkEndpoint(ctx, kind, identifier, condition, timeout) } // Type of wait, condition or JSONPath @@ -96,13 +99,13 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, zarfKubectlGet := fmt.Sprintf("%s tools kubectl get %s %s %s", zarfCommand, namespaceFlag, kind, identifier) stdout, stderr, err := exec.Cmd(shell, append(shellArgs, zarfKubectlGet)...) if err != nil { - message.Debug(stdout, stderr, err) + log.Debug(zarfKubectlGet, "stdout", stdout, "stderr", stderr, "error", err) continue } resourceNotFound := strings.Contains(stderr, "No resources found") && identifier == "" if resourceNotFound { - message.Debug(stdout, stderr, err) + log.Debug("could not get Kubernetes resource", "stdout", stdout, "stderr", stderr, "error", err) continue } @@ -120,7 +123,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, // If there is an error, log it and try again. if stdout, stderr, err := exec.Cmd(shell, append(shellArgs, zarfKubectlWait)...); err != nil { - message.Debug(stdout, stderr, err) + log.Debug("could not wait for Kubernetes resource", "stdout", stdout, "stderr", stderr, "error", err) continue } @@ -132,7 +135,9 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, } // waitForNetworkEndpoint waits for a network endpoint to respond. -func waitForNetworkEndpoint(resource, name, condition string, timeout time.Duration) error { +func waitForNetworkEndpoint(ctx context.Context, resource, name, condition string, timeout time.Duration) error { + log := logging.FromContextOrDiscard(ctx) + // Set the timeout for the wait-for command. expired := time.After(timeout) @@ -167,7 +172,7 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // If the status code is not in the 2xx range, try again. if err != nil || resp.StatusCode < 200 || resp.StatusCode > 299 { - message.Debug(err) + log.Debug("network request failed", "error", err) continue } @@ -187,14 +192,14 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // Try to get the URL and check the status code. resp, err := http.Get(url) if err != nil || resp.StatusCode != code { - message.Debug(err) + log.Debug("network request failed", "error", err) continue } default: // Fallback to any generic protocol using net.Dial conn, err := net.Dial(resource, name) if err != nil { - message.Debug(err) + log.Debug("network connection failed", "error", err) continue } defer conn.Close() diff --git a/src/pkg/zoci/pull.go b/src/pkg/zoci/pull.go index 9fd76e9ccc..90f3fa5ebc 100644 --- a/src/pkg/zoci/pull.go +++ b/src/pkg/zoci/pull.go @@ -56,7 +56,7 @@ func (r *Remote) PullPackage(ctx context.Context, destinationDir string, concurr successText := fmt.Sprintf("Pulling %q", helpers.OCIURLPrefix+r.Repo().Reference.String()) layerSize := oci.SumDescsSize(layersToPull) - go utils.RenderProgressBarForLocalDirWrite(destinationDir, layerSize, doneSaving, "Pulling", successText) + go utils.RenderProgressBarForLocalDirWrite(ctx, destinationDir, layerSize, doneSaving, "Pulling", successText) dst, err := file.New(destinationDir) if err != nil {