Skip to content

Commit

Permalink
chore: move context.TODO to context.Background() (#2742)
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff-du <[email protected]>
Co-authored-by: schristoff-du <[email protected]>
  • Loading branch information
schristoff and schristoff-du authored Jul 21, 2024
1 parent 1e4399e commit 0d04a9f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "."
Expand All @@ -83,7 +83,7 @@ var devGenerateCmd = &cobra.Command{
}
defer pkgClient.ClearTempPaths()

err = pkgClient.Generate()
err = pkgClient.Generate(cmd.Context())
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package bigbang

import (
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions src/internal/packager/git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions src/internal/packager/git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package git

import (
"context"
"fmt"
"path"
"strings"
Expand All @@ -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)
Expand All @@ -26,15 +27,15 @@ 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)
}

return nil
}

// 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
Expand All @@ -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)
}
Expand Down
13 changes: 7 additions & 6 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package helm

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 7 additions & 7 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package packager

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -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)

Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 0d04a9f

Please sign in to comment.