From 0d04a9f69ac2c9f8374f36f8afffacd69ba58321 Mon Sep 17 00:00:00 2001 From: schristoff <28318173+schristoff@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:31:35 -0400 Subject: [PATCH] chore: move context.TODO to context.Background() (#2742) Signed-off-by: schristoff-du <167717759+schristoff-du@users.noreply.github.com> Co-authored-by: schristoff-du <167717759+schristoff-du@users.noreply.github.com> --- src/cmd/dev.go | 4 ++-- src/extensions/bigbang/bigbang.go | 11 ++++++----- src/internal/packager/git/clone.go | 10 +++++----- src/internal/packager/git/pull.go | 9 +++++---- src/internal/packager/helm/repo.go | 13 +++++++------ src/pkg/packager/creator/normal.go | 14 +++++++------- src/pkg/packager/generate.go | 5 +++-- src/pkg/packager/prepare.go | 6 +++--- 8 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/cmd/dev.go b/src/cmd/dev.go index c31e0d5fe8..b10f2dde51 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -71,7 +71,7 @@ var devGenerateCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Short: lang.CmdDevGenerateShort, Example: lang.CmdDevGenerateExample, - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { pkgConfig.GenerateOpts.Name = args[0] pkgConfig.CreateOpts.BaseDir = "." @@ -83,7 +83,7 @@ var devGenerateCmd = &cobra.Command{ } defer pkgClient.ClearTempPaths() - err = pkgClient.Generate() + err = pkgClient.Generate(cmd.Context()) if err != nil { return err } diff --git a/src/extensions/bigbang/bigbang.go b/src/extensions/bigbang/bigbang.go index b97c8f6e25..e312f382f9 100644 --- a/src/extensions/bigbang/bigbang.go +++ b/src/extensions/bigbang/bigbang.go @@ -5,6 +5,7 @@ package bigbang import ( + "context" "fmt" "os" "path" @@ -42,7 +43,7 @@ var tenMins = metav1.Duration{ // Run mutates a component that should deploy Big Bang to a set of manifests // that contain the flux deployment of Big Bang -func Run(YOLO bool, tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (types.ZarfComponent, error) { +func Run(ctx context.Context, YOLO bool, tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (types.ZarfComponent, error) { cfg := c.Extensions.BigBang manifests := []types.ZarfManifest{} @@ -99,7 +100,7 @@ func Run(YOLO bool, tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (typ ) // Download the chart from Git and save it to a temporary directory. - err = helmCfg.PackageChartFromGit(c.DeprecatedCosignKeyPath) + err = helmCfg.PackageChartFromGit(ctx, c.DeprecatedCosignKeyPath) if err != nil { return c, fmt.Errorf("unable to download Big Bang Chart: %w", err) } @@ -220,7 +221,7 @@ func Run(YOLO bool, tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (typ gitRepo := gitRepos[hr.NamespacedSource] values := hrValues[namespacedName] - images, err := findImagesforBBChartRepo(gitRepo, values) + images, err := findImagesforBBChartRepo(ctx, gitRepo, values) if err != nil { return c, fmt.Errorf("unable to find images for chart repo: %w", err) } @@ -523,7 +524,7 @@ func addBigBangManifests(YOLO bool, manifestDir string, cfg *extensions.BigBang) } // findImagesforBBChartRepo finds and returns the images for the Big Bang chart repo -func findImagesforBBChartRepo(repo string, values chartutil.Values) (images []string, err error) { +func findImagesforBBChartRepo(ctx context.Context, repo string, values chartutil.Values) (images []string, err error) { matches := strings.Split(repo, "@") if len(matches) < 2 { return images, fmt.Errorf("cannot convert git repo %s to helm chart without a version tag", repo) @@ -532,7 +533,7 @@ func findImagesforBBChartRepo(repo string, values chartutil.Values) (images []st spinner := message.NewProgressSpinner("Discovering images in %s", repo) defer spinner.Stop() - gitPath, err := helm.DownloadChartFromGitToTemp(repo, spinner) + gitPath, err := helm.DownloadChartFromGitToTemp(ctx, repo, spinner) if err != nil { return images, err } diff --git a/src/internal/packager/git/clone.go b/src/internal/packager/git/clone.go index 6e70077511..bd38cbcdd4 100644 --- a/src/internal/packager/git/clone.go +++ b/src/internal/packager/git/clone.go @@ -18,7 +18,7 @@ import ( ) // clone performs a `git clone` of a given repo. -func (g *Git) clone(gitURL string, ref plumbing.ReferenceName, shallow bool) error { +func (g *Git) clone(ctx context.Context, gitURL string, ref plumbing.ReferenceName, shallow bool) error { cloneOptions := &git.CloneOptions{ URL: gitURL, Progress: g.Spinner, @@ -47,7 +47,7 @@ func (g *Git) clone(gitURL string, ref plumbing.ReferenceName, shallow bool) err repo, err := git.PlainClone(g.GitPath, false, cloneOptions) if err != nil { message.Notef("Falling back to host 'git', failed to clone the repo %q with Zarf: %s", gitURL, err.Error()) - return g.gitCloneFallback(gitURL, ref, shallow) + return g.gitCloneFallback(ctx, gitURL, ref, shallow) } // If we're cloning the whole repo, we need to also fetch the other branches besides the default. @@ -72,7 +72,7 @@ func (g *Git) clone(gitURL string, ref plumbing.ReferenceName, shallow bool) err } // gitCloneFallback is a fallback if go-git fails to clone a repo. -func (g *Git) gitCloneFallback(gitURL string, ref plumbing.ReferenceName, shallow bool) error { +func (g *Git) gitCloneFallback(ctx context.Context, gitURL string, ref plumbing.ReferenceName, shallow bool) error { // If we can't clone with go-git, fallback to the host clone // Only support "all tags" due to the azure clone url format including a username cloneArgs := []string{"clone", "--origin", onlineRemoteName, gitURL, g.GitPath} @@ -96,7 +96,7 @@ func (g *Git) gitCloneFallback(gitURL string, ref plumbing.ReferenceName, shallo message.Command("git %s", strings.Join(cloneArgs, " ")) - _, _, err := exec.CmdWithContext(context.TODO(), cloneExecConfig, "git", cloneArgs...) + _, _, err := exec.CmdWithContext(ctx, cloneExecConfig, "git", cloneArgs...) if err != nil { return err } @@ -113,7 +113,7 @@ func (g *Git) gitCloneFallback(gitURL string, ref plumbing.ReferenceName, shallo message.Command("git %s", strings.Join(fetchArgs, " ")) - _, _, err := exec.CmdWithContext(context.TODO(), fetchExecConfig, "git", fetchArgs...) + _, _, err := exec.CmdWithContext(ctx, fetchExecConfig, "git", fetchArgs...) if err != nil { return err } diff --git a/src/internal/packager/git/pull.go b/src/internal/packager/git/pull.go index bd2472c08c..5d9e6f1006 100644 --- a/src/internal/packager/git/pull.go +++ b/src/internal/packager/git/pull.go @@ -5,6 +5,7 @@ package git import ( + "context" "fmt" "path" "strings" @@ -16,7 +17,7 @@ import ( ) // DownloadRepoToTemp clones or updates a repo into a temp folder to perform ephemeral actions (i.e. process chart repos). -func (g *Git) DownloadRepoToTemp(gitURL string) error { +func (g *Git) DownloadRepoToTemp(ctx context.Context, gitURL string) error { g.Spinner.Updatef("g.DownloadRepoToTemp(%s)", gitURL) path, err := utils.MakeTempDir(config.CommonOptions.TempDirectory) @@ -26,7 +27,7 @@ func (g *Git) DownloadRepoToTemp(gitURL string) error { // If downloading to temp, set this as a shallow clone to only pull the exact // gitURL w/ ref that was specified since we will throw away git history anyway - if err = g.Pull(gitURL, path, true); err != nil { + if err = g.Pull(ctx, gitURL, path, true); err != nil { return fmt.Errorf("unable to pull the git repo at %s: %w", gitURL, err) } @@ -34,7 +35,7 @@ func (g *Git) DownloadRepoToTemp(gitURL string) error { } // Pull clones or updates a git repository into the target folder. -func (g *Git) Pull(gitURL, targetFolder string, shallow bool) error { +func (g *Git) Pull(ctx context.Context, gitURL, targetFolder string, shallow bool) error { g.Spinner.Updatef("Processing git repo %s", gitURL) // Split the remote url and the zarf reference @@ -59,7 +60,7 @@ func (g *Git) Pull(gitURL, targetFolder string, shallow bool) error { g.GitPath = path.Join(targetFolder, repoFolder) // Clone the git repository. - err = g.clone(gitURLNoRef, ref, shallow) + err = g.clone(ctx, gitURLNoRef, ref, shallow) if err != nil { return fmt.Errorf("not a valid git repo or unable to clone (%s): %w", gitURL, err) } diff --git a/src/internal/packager/helm/repo.go b/src/internal/packager/helm/repo.go index c3ea9c646f..148a4176a4 100644 --- a/src/internal/packager/helm/repo.go +++ b/src/internal/packager/helm/repo.go @@ -5,6 +5,7 @@ package helm import ( + "context" "fmt" "os" "path/filepath" @@ -32,7 +33,7 @@ import ( ) // PackageChart creates a chart archive from a path to a chart on the host os and builds chart dependencies -func (h *Helm) PackageChart(cosignKeyPath string) error { +func (h *Helm) PackageChart(ctx context.Context, cosignKeyPath string) error { if len(h.chart.URL) > 0 { url, refPlain, err := transform.GitURLSplitRef(h.chart.URL) // check if the chart is a git url with a ref (if an error is returned url will be empty) @@ -47,7 +48,7 @@ func (h *Helm) PackageChart(cosignKeyPath string) error { h.chart.URL = fmt.Sprintf("%s@%s", h.chart.URL, h.chart.Version) } - err = h.PackageChartFromGit(cosignKeyPath) + err = h.PackageChartFromGit(ctx, cosignKeyPath) if err != nil { return fmt.Errorf("unable to pull the chart %q from git: %w", h.chart.Name, err) } @@ -113,12 +114,12 @@ func (h *Helm) PackageChartFromLocalFiles(cosignKeyPath string) error { } // PackageChartFromGit is a special implementation of chart archiving that supports the https://p1.dso.mil/#/products/big-bang/ model. -func (h *Helm) PackageChartFromGit(cosignKeyPath string) error { +func (h *Helm) PackageChartFromGit(ctx context.Context, cosignKeyPath string) error { spinner := message.NewProgressSpinner("Processing helm chart %s", h.chart.Name) defer spinner.Stop() // Retrieve the repo containing the chart - gitPath, err := DownloadChartFromGitToTemp(h.chart.URL, spinner) + gitPath, err := DownloadChartFromGitToTemp(ctx, h.chart.URL, spinner) if err != nil { return err } @@ -232,12 +233,12 @@ func (h *Helm) DownloadPublishedChart(cosignKeyPath string) error { } // DownloadChartFromGitToTemp downloads a chart from git into a temp directory -func DownloadChartFromGitToTemp(url string, spinner *message.Spinner) (string, error) { +func DownloadChartFromGitToTemp(ctx context.Context, url string, spinner *message.Spinner) (string, error) { // Create the Git configuration and download the repo gitCfg := git.NewWithSpinner(types.GitServerInfo{}, spinner) // Download the git repo to a temporary directory - err := gitCfg.DownloadRepoToTemp(url) + err := gitCfg.DownloadRepoToTemp(ctx, url) if err != nil { return "", fmt.Errorf("unable to download the git repo %s: %w", url, err) } diff --git a/src/pkg/packager/creator/normal.go b/src/pkg/packager/creator/normal.go index b8135a8c2f..3b34b7e846 100644 --- a/src/pkg/packager/creator/normal.go +++ b/src/pkg/packager/creator/normal.go @@ -86,7 +86,7 @@ func (pc *PackageCreator) LoadPackageDefinition(ctx context.Context, src *layout warnings = append(warnings, templateWarnings...) // After templates are filled process any create extensions - pkg.Components, err = pc.processExtensions(pkg.Components, src, pkg.Metadata.YOLO) + pkg.Components, err = pc.processExtensions(ctx, pkg.Components, src, pkg.Metadata.YOLO) if err != nil { return types.ZarfPackage{}, nil, err } @@ -142,7 +142,7 @@ func (pc *PackageCreator) Assemble(ctx context.Context, dst *layout.PackagePaths } } - if err := pc.addComponent(component, dst); err != nil { + if err := pc.addComponent(ctx, component, dst); err != nil { onFailure() return fmt.Errorf("unable to add component %q: %w", component.Name, err) } @@ -327,7 +327,7 @@ func (pc *PackageCreator) Output(ctx context.Context, dst *layout.PackagePaths, return nil } -func (pc *PackageCreator) processExtensions(components []types.ZarfComponent, layout *layout.PackagePaths, isYOLO bool) (processedComponents []types.ZarfComponent, err error) { +func (pc *PackageCreator) processExtensions(ctx context.Context, components []types.ZarfComponent, layout *layout.PackagePaths, isYOLO bool) (processedComponents []types.ZarfComponent, err error) { // Create component paths and process extensions for each component. for _, c := range components { componentPaths, err := layout.Components.Create(c) @@ -337,7 +337,7 @@ func (pc *PackageCreator) processExtensions(components []types.ZarfComponent, la // Big Bang if c.Extensions.BigBang != nil { - if c, err = bigbang.Run(isYOLO, componentPaths, c); err != nil { + if c, err = bigbang.Run(ctx, isYOLO, componentPaths, c); err != nil { return nil, fmt.Errorf("unable to process bigbang extension: %w", err) } } @@ -348,7 +348,7 @@ func (pc *PackageCreator) processExtensions(components []types.ZarfComponent, la return processedComponents, nil } -func (pc *PackageCreator) addComponent(component types.ZarfComponent, dst *layout.PackagePaths) error { +func (pc *PackageCreator) addComponent(ctx context.Context, component types.ZarfComponent, dst *layout.PackagePaths) error { message.HeaderInfof("📦 %s COMPONENT", strings.ToUpper(component.Name)) componentPaths, err := dst.Components.Create(component) @@ -364,7 +364,7 @@ func (pc *PackageCreator) addComponent(component types.ZarfComponent, dst *layou // If any helm charts are defined, process them. for _, chart := range component.Charts { helmCfg := helm.New(chart, componentPaths.Charts, componentPaths.Values) - if err := helmCfg.PackageChart(componentPaths.Charts); err != nil { + if err := helmCfg.PackageChart(ctx, componentPaths.Charts); err != nil { return err } } @@ -514,7 +514,7 @@ func (pc *PackageCreator) addComponent(component types.ZarfComponent, dst *layou for _, url := range component.Repos { // Pull all the references if there is no `@` in the string. gitCfg := git.NewWithSpinner(types.GitServerInfo{}, spinner) - if err := gitCfg.Pull(url, componentPaths.Repos, false); err != nil { + if err := gitCfg.Pull(ctx, url, componentPaths.Repos, false); err != nil { return fmt.Errorf("unable to pull git repo %s: %w", url, err) } } diff --git a/src/pkg/packager/generate.go b/src/pkg/packager/generate.go index 905859bc53..d1fc593033 100644 --- a/src/pkg/packager/generate.go +++ b/src/pkg/packager/generate.go @@ -5,6 +5,7 @@ package packager import ( + "context" "fmt" "os" "path/filepath" @@ -19,7 +20,7 @@ import ( ) // Generate generates a Zarf package definition. -func (p *Packager) Generate() (err error) { +func (p *Packager) Generate(ctx context.Context) (err error) { generatedZarfYAMLPath := filepath.Join(p.cfg.GenerateOpts.Output, layout.ZarfYAML) spinner := message.NewProgressSpinner("Generating package for %q at %s", p.cfg.GenerateOpts.Name, generatedZarfYAMLPath) @@ -61,7 +62,7 @@ func (p *Packager) Generate() (err error) { }, } - images, err := p.findImages() + 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()) diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index c71038b2f8..340ac1c85a 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -70,10 +70,10 @@ func (p *Packager) FindImages(ctx context.Context) (map[string][]string, error) message.Warn(warning) } - return p.findImages() + return p.findImages(ctx) } -func (p *Packager) findImages() (imgMap map[string][]string, err error) { +func (p *Packager) findImages(ctx context.Context) (imgMap map[string][]string, err error) { repoHelmChartPath := p.cfg.FindImagesOpts.RepoHelmChartPath kubeVersionOverride := p.cfg.FindImagesOpts.KubeVersionOverride whyImage := p.cfg.FindImagesOpts.Why @@ -172,7 +172,7 @@ func (p *Packager) findImages() (imgMap map[string][]string, err error) { helm.WithVariableConfig(p.variableConfig), ) - err = helmCfg.PackageChart(component.DeprecatedCosignKeyPath) + err = helmCfg.PackageChart(ctx, component.DeprecatedCosignKeyPath) if err != nil { return nil, fmt.Errorf("unable to package the chart %s: %w", chart.Name, err) }