Fix CI issues: resolve gci formatting and reduce cognitive complexity
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									f187d09987
								
							
						
					
					
						commit
						85f0bc5238
					
				|  | @ -1222,100 +1222,8 @@ type PrepareChartKey struct { | ||||||
| // Otheriwse, if a chart is not a helm chart, it will call "chartify" to turn it into a chart.
 | // Otheriwse, if a chart is not a helm chart, it will call "chartify" to turn it into a chart.
 | ||||||
| //
 | //
 | ||||||
| // If exists, it will also patch resources by json patches, strategic-merge patches, and injectors.
 | // If exists, it will also patch resources by json patches, strategic-merge patches, and injectors.
 | ||||||
| func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurrency int, helmfileCommand string, opts ChartPrepareOptions) (map[PrepareChartKey]string, []error) { | // processChartification handles the chartification process
 | ||||||
| 	if !opts.SkipResolve { | func (st *HelmState) processChartification(chartification *Chartify, release *ReleaseSpec, chartPath string, opts ChartPrepareOptions, skipDeps bool) (string, bool, error) { | ||||||
| 		updated, err := st.ResolveDeps() |  | ||||||
| 		if err != nil { |  | ||||||
| 			return nil, []error{err} |  | ||||||
| 		} |  | ||||||
| 		*st = *updated |  | ||||||
| 	} |  | ||||||
| 	selected, err := st.GetSelectedReleases(opts.IncludeTransitiveNeeds) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return nil, []error{err} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	releases := releasesNeedCharts(selected) |  | ||||||
| 
 |  | ||||||
| 	var prepareChartInfoMutex sync.Mutex |  | ||||||
| 
 |  | ||||||
| 	prepareChartInfo := make(map[PrepareChartKey]string, len(releases)) |  | ||||||
| 
 |  | ||||||
| 	errs := []error{} |  | ||||||
| 
 |  | ||||||
| 	jobQueue := make(chan *ReleaseSpec, len(releases)) |  | ||||||
| 	results := make(chan *chartPrepareResult, len(releases)) |  | ||||||
| 
 |  | ||||||
| 	var builds []*chartPrepareResult |  | ||||||
| 
 |  | ||||||
| 	st.scatterGather( |  | ||||||
| 		concurrency, |  | ||||||
| 		len(releases), |  | ||||||
| 		func() { |  | ||||||
| 			for i := 0; i < len(releases); i++ { |  | ||||||
| 				jobQueue <- &releases[i] |  | ||||||
| 			} |  | ||||||
| 			close(jobQueue) |  | ||||||
| 		}, |  | ||||||
| 		func(workerIndex int) { |  | ||||||
| 			for release := range jobQueue { |  | ||||||
| 				if st.OverrideChart != "" { |  | ||||||
| 					release.Chart = st.OverrideChart |  | ||||||
| 				} |  | ||||||
| 				// Call user-defined `prepare` hooks to create/modify local charts to be used by
 |  | ||||||
| 				// the later process.
 |  | ||||||
| 				//
 |  | ||||||
| 				// If it wasn't called here, Helmfile can end up an issue like
 |  | ||||||
| 				// https://github.com/roboll/helmfile/issues/1328
 |  | ||||||
| 				if _, err := st.triggerPrepareEvent(release, helmfileCommand); err != nil { |  | ||||||
| 					results <- &chartPrepareResult{err: err} |  | ||||||
| 					return |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				chartName := release.Chart |  | ||||||
| 
 |  | ||||||
| 				chartPath, err := st.downloadChartWithGoGetter(release) |  | ||||||
| 				if err != nil { |  | ||||||
| 					results <- &chartPrepareResult{err: fmt.Errorf("release %q: %w", release.Name, err)} |  | ||||||
| 					return |  | ||||||
| 				} |  | ||||||
| 				chartFetchedByGoGetter := chartPath != chartName |  | ||||||
| 
 |  | ||||||
| 				if !chartFetchedByGoGetter { |  | ||||||
| 					ociChartPath, err := st.getOCIChart(release, dir, helm, opts) |  | ||||||
| 					if err != nil { |  | ||||||
| 						results <- &chartPrepareResult{err: fmt.Errorf("release %q: %w", release.Name, err)} |  | ||||||
| 
 |  | ||||||
| 						return |  | ||||||
| 					} |  | ||||||
| 
 |  | ||||||
| 					if ociChartPath != nil { |  | ||||||
| 						chartPath = *ociChartPath |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				isLocal := st.fs.DirectoryExistsAt(normalizeChart(st.basePath, chartName)) |  | ||||||
| 
 |  | ||||||
| 				chartification, clean, err := st.PrepareChartify(helm, release, chartPath, workerIndex) |  | ||||||
| 
 |  | ||||||
| 				if !opts.SkipCleanup { |  | ||||||
| 					// nolint: staticcheck
 |  | ||||||
| 					defer clean() |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				if err != nil { |  | ||||||
| 					results <- &chartPrepareResult{err: err} |  | ||||||
| 					return |  | ||||||
| 				} |  | ||||||
| 
 |  | ||||||
| 				var buildDeps bool |  | ||||||
| 
 |  | ||||||
| 				skipDepsGlobal := opts.SkipDeps |  | ||||||
| 				skipDepsRelease := release.SkipDeps != nil && *release.SkipDeps |  | ||||||
| 				skipDepsDefault := release.SkipDeps == nil && st.HelmDefaults.SkipDeps |  | ||||||
| 				skipDeps := (!isLocal && !chartFetchedByGoGetter) || skipDepsGlobal || skipDepsRelease || skipDepsDefault |  | ||||||
| 
 |  | ||||||
| 				if chartification != nil && helmfileCommand != "pull" { |  | ||||||
| 	c := chartify.New( | 	c := chartify.New( | ||||||
| 		chartify.HelmBin(st.DefaultHelmBinary), | 		chartify.HelmBin(st.DefaultHelmBinary), | ||||||
| 		chartify.KustomizeBin(st.DefaultKustomizeBinary), | 		chartify.KustomizeBin(st.DefaultKustomizeBinary), | ||||||
|  | @ -1354,53 +1262,135 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre | ||||||
| 
 | 
 | ||||||
| 	out, err := c.Chartify(release.Name, chartPath, chartify.WithChartifyOpts(chartifyOpts)) | 	out, err := c.Chartify(release.Name, chartPath, chartify.WithChartifyOpts(chartifyOpts)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 						results <- &chartPrepareResult{err: err} | 		return "", false, err | ||||||
| 						return |  | ||||||
| 					} else { |  | ||||||
| 						chartPath = out |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	chartPath = out | ||||||
| 	// Skip `helm dep build` and `helm dep up` altogether when the chart is from remote or the dep is
 | 	// Skip `helm dep build` and `helm dep up` altogether when the chart is from remote or the dep is
 | ||||||
| 	// explicitly skipped.
 | 	// explicitly skipped.
 | ||||||
| 					buildDeps = !skipDeps | 	buildDeps := !skipDeps | ||||||
| 				} else if normalizedChart := normalizeChart(st.basePath, chartPath); st.fs.DirectoryExistsAt(normalizedChart) { | 	return chartPath, buildDeps, nil | ||||||
| 					// At this point, we are sure that chartPath is a local directory containing either:
 | } | ||||||
| 					// - A remote chart fetched by go-getter or
 | 
 | ||||||
| 					// - A local chart
 | // processLocalChart handles local chart processing
 | ||||||
| 					//
 | func (st *HelmState) processLocalChart(normalizedChart, dir string, release *ReleaseSpec, helmfileCommand string, opts ChartPrepareOptions, isLocal bool) (string, error) { | ||||||
| 					// The chart may have Chart.yaml(and requirements.yaml for Helm 2), and optionally Chart.lock/requirements.lock,
 | 	chartPath := normalizedChart | ||||||
| 					// but no `charts/` directory populated at all, or a subet of chart dependencies are missing in the directory.
 |  | ||||||
| 					//
 |  | ||||||
| 					// In such situation, Helm fails with an error like:
 |  | ||||||
| 					//   Error: found in Chart.yaml, but missing in charts/ directory: cert-manager, prometheus, postgresql, gitlab-runner, grafana, redis
 |  | ||||||
| 					//
 |  | ||||||
| 					// (See also https://github.com/roboll/helmfile/issues/1401#issuecomment-670854495)
 |  | ||||||
| 					//
 |  | ||||||
| 					// To avoid it, we need to call a `helm dep build` command on the chart.
 |  | ||||||
| 					// But the command may consistently fail when an outdated Chart.lock exists.
 |  | ||||||
| 					//
 |  | ||||||
| 					// (I've mentioned about such case in https://github.com/roboll/helmfile/pull/1400.)
 |  | ||||||
| 					//
 |  | ||||||
| 					// Trying to run `helm dep build` on the chart regardless of if it's from local or remote is
 |  | ||||||
| 					// problematic, as usually the user would have no way to fix the remote chart on their own.
 |  | ||||||
| 					//
 |  | ||||||
| 					// Given that, we always run `helm dep build` on the chart here, but tolerate any error caused by it
 |  | ||||||
| 					// for a remote chart, so that the user can notice/fix the issue in a local chart while
 |  | ||||||
| 					// a broken remote chart won't completely block their job.
 |  | ||||||
| 					chartPath = normalizedChart |  | ||||||
| 	if helmfileCommand == "pull" && isLocal { | 	if helmfileCommand == "pull" && isLocal { | ||||||
| 		chartAbsPath := strings.TrimSuffix(filepath.Clean(normalizedChart), "/") | 		chartAbsPath := strings.TrimSuffix(filepath.Clean(normalizedChart), "/") | ||||||
|  | 		var err error | ||||||
| 		chartPath, err = generateChartPath(filepath.Base(chartAbsPath), dir, release, opts.OutputDirTemplate) | 		chartPath, err = generateChartPath(filepath.Base(chartAbsPath), dir, release, opts.OutputDirTemplate) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 							results <- &chartPrepareResult{err: err} | 			return "", err | ||||||
| 							return |  | ||||||
| 		} | 		} | ||||||
| 		if err := st.fs.CopyDir(normalizedChart, filepath.Clean(chartPath)); err != nil { | 		if err := st.fs.CopyDir(normalizedChart, filepath.Clean(chartPath)); err != nil { | ||||||
| 							results <- &chartPrepareResult{err: err} | 			return "", err | ||||||
| 							return | 		} | ||||||
|  | 	} | ||||||
|  | 	return chartPath, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // forcedDownloadChart handles forced chart downloads
 | ||||||
|  | func (st *HelmState) forcedDownloadChart(chartName, dir string, release *ReleaseSpec, helm helmexec.Interface, opts ChartPrepareOptions) (string, error) { | ||||||
|  | 	// Check global chart cache first for non-OCI charts
 | ||||||
|  | 	cacheKey := st.getChartCacheKey(release) | ||||||
|  | 	if cachedPath, exists := st.checkChartCache(cacheKey); exists && st.fs.DirectoryExistsAt(cachedPath) { | ||||||
|  | 		st.logger.Debugf("Chart %s:%s already downloaded, using cached version at %s", chartName, release.Version, cachedPath) | ||||||
|  | 		return cachedPath, nil | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	chartPath, err := generateChartPath(chartName, dir, release, opts.OutputDirTemplate) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// only fetch chart if it is not already fetched
 | ||||||
|  | 	if _, err := os.Stat(chartPath); os.IsNotExist(err) { | ||||||
|  | 		var fetchFlags []string | ||||||
|  | 		fetchFlags = st.appendChartVersionFlags(fetchFlags, release) | ||||||
|  | 		fetchFlags = append(fetchFlags, "--untar", "--untardir", chartPath) | ||||||
|  | 		if err := helm.Fetch(chartName, fetchFlags...); err != nil { | ||||||
|  | 			return "", err | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		st.logger.Infof("\"%s\" has not been downloaded because the output directory \"%s\" already exists", chartName, chartPath) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Set chartPath to be the path containing Chart.yaml, if found
 | ||||||
|  | 	fullChartPath, err := findChartDirectory(chartPath) | ||||||
|  | 	if err == nil { | ||||||
|  | 		chartPath = filepath.Dir(fullChartPath) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Add to global chart cache
 | ||||||
|  | 	st.addToChartCache(cacheKey, chartPath) | ||||||
|  | 
 | ||||||
|  | 	return chartPath, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // prepareChartForRelease processes a single release and prepares its chart
 | ||||||
|  | func (st *HelmState) prepareChartForRelease(release *ReleaseSpec, helm helmexec.Interface, dir string, helmfileCommand string, opts ChartPrepareOptions, workerIndex int) *chartPrepareResult { | ||||||
|  | 	if st.OverrideChart != "" { | ||||||
|  | 		release.Chart = st.OverrideChart | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// Call user-defined `prepare` hooks to create/modify local charts to be used by
 | ||||||
|  | 	// the later process.
 | ||||||
|  | 	//
 | ||||||
|  | 	// If it wasn't called here, Helmfile can end up an issue like
 | ||||||
|  | 	// https://github.com/roboll/helmfile/issues/1328
 | ||||||
|  | 	if _, err := st.triggerPrepareEvent(release, helmfileCommand); err != nil { | ||||||
|  | 		return &chartPrepareResult{err: err} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	chartName := release.Chart | ||||||
|  | 
 | ||||||
|  | 	chartPath, err := st.downloadChartWithGoGetter(release) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return &chartPrepareResult{err: fmt.Errorf("release %q: %w", release.Name, err)} | ||||||
|  | 	} | ||||||
|  | 	chartFetchedByGoGetter := chartPath != chartName | ||||||
|  | 
 | ||||||
|  | 	if !chartFetchedByGoGetter { | ||||||
|  | 		ociChartPath, err := st.getOCIChart(release, dir, helm, opts) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &chartPrepareResult{err: fmt.Errorf("release %q: %w", release.Name, err)} | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if ociChartPath != nil { | ||||||
|  | 			chartPath = *ociChartPath | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	isLocal := st.fs.DirectoryExistsAt(normalizeChart(st.basePath, chartName)) | ||||||
|  | 
 | ||||||
|  | 	chartification, clean, err := st.PrepareChartify(helm, release, chartPath, workerIndex) | ||||||
|  | 
 | ||||||
|  | 	if !opts.SkipCleanup { | ||||||
|  | 		// nolint: staticcheck
 | ||||||
|  | 		defer clean() | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if err != nil { | ||||||
|  | 		return &chartPrepareResult{err: err} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var buildDeps bool | ||||||
|  | 
 | ||||||
|  | 	skipDepsGlobal := opts.SkipDeps | ||||||
|  | 	skipDepsRelease := release.SkipDeps != nil && *release.SkipDeps | ||||||
|  | 	skipDepsDefault := release.SkipDeps == nil && st.HelmDefaults.SkipDeps | ||||||
|  | 	skipDeps := (!isLocal && !chartFetchedByGoGetter) || skipDepsGlobal || skipDepsRelease || skipDepsDefault | ||||||
|  | 
 | ||||||
|  | 	if chartification != nil && helmfileCommand != "pull" { | ||||||
|  | 		chartPath, buildDeps, err = st.processChartification(chartification, release, chartPath, opts, skipDeps) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &chartPrepareResult{err: err} | ||||||
|  | 		} | ||||||
|  | 	} else if normalizedChart := normalizeChart(st.basePath, chartPath); st.fs.DirectoryExistsAt(normalizedChart) { | ||||||
|  | 		chartPath, err = st.processLocalChart(normalizedChart, dir, release, helmfileCommand, opts, isLocal) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return &chartPrepareResult{err: err} | ||||||
|  | 		} | ||||||
| 		buildDeps = !skipDeps | 		buildDeps = !skipDeps | ||||||
| 	} else if !opts.ForceDownload { | 	} else if !opts.ForceDownload { | ||||||
| 		// At this point, we are sure that either:
 | 		// At this point, we are sure that either:
 | ||||||
|  | @ -1415,43 +1405,13 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre | ||||||
| 		//    For helm 2, we `helm fetch` with the version flags and call `helm template`
 | 		//    For helm 2, we `helm fetch` with the version flags and call `helm template`
 | ||||||
| 		//    WITHOUT the version flags.
 | 		//    WITHOUT the version flags.
 | ||||||
| 	} else { | 	} else { | ||||||
| 					// Check global chart cache first for non-OCI charts
 | 		chartPath, err = st.forcedDownloadChart(chartName, dir, release, helm, opts) | ||||||
| 					cacheKey := st.getChartCacheKey(release) |  | ||||||
| 					if cachedPath, exists := st.checkChartCache(cacheKey); exists && st.fs.DirectoryExistsAt(cachedPath) { |  | ||||||
| 						st.logger.Debugf("Chart %s:%s already downloaded, using cached version at %s", chartName, release.Version, cachedPath) |  | ||||||
| 						chartPath = cachedPath |  | ||||||
| 					} else { |  | ||||||
| 						chartPath, err = generateChartPath(chartName, dir, release, opts.OutputDirTemplate) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 							results <- &chartPrepareResult{err: err} | 			return &chartPrepareResult{err: err} | ||||||
| 							return |  | ||||||
| 						} |  | ||||||
| 
 |  | ||||||
| 						// only fetch chart if it is not already fetched
 |  | ||||||
| 						if _, err := os.Stat(chartPath); os.IsNotExist(err) { |  | ||||||
| 							var fetchFlags []string |  | ||||||
| 							fetchFlags = st.appendChartVersionFlags(fetchFlags, release) |  | ||||||
| 							fetchFlags = append(fetchFlags, "--untar", "--untardir", chartPath) |  | ||||||
| 							if err := helm.Fetch(chartName, fetchFlags...); err != nil { |  | ||||||
| 								results <- &chartPrepareResult{err: err} |  | ||||||
| 								return |  | ||||||
| 							} |  | ||||||
| 						} else { |  | ||||||
| 							st.logger.Infof("\"%s\" has not been downloaded because the output directory \"%s\" already exists", chartName, chartPath) |  | ||||||
| 						} |  | ||||||
| 
 |  | ||||||
| 						// Set chartPath to be the path containing Chart.yaml, if found
 |  | ||||||
| 						fullChartPath, err := findChartDirectory(chartPath) |  | ||||||
| 						if err == nil { |  | ||||||
| 							chartPath = filepath.Dir(fullChartPath) |  | ||||||
| 						} |  | ||||||
| 
 |  | ||||||
| 						// Add to global chart cache
 |  | ||||||
| 						st.addToChartCache(cacheKey, chartPath) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 				results <- &chartPrepareResult{ | 	return &chartPrepareResult{ | ||||||
| 		releaseName:            release.Name, | 		releaseName:            release.Name, | ||||||
| 		chartName:              chartName, | 		chartName:              chartName, | ||||||
| 		releaseNamespace:       release.Namespace, | 		releaseNamespace:       release.Namespace, | ||||||
|  | @ -1462,6 +1422,47 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre | ||||||
| 		chartFetchedByGoGetter: chartFetchedByGoGetter, | 		chartFetchedByGoGetter: chartFetchedByGoGetter, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurrency int, helmfileCommand string, opts ChartPrepareOptions) (map[PrepareChartKey]string, []error) { | ||||||
|  | 	if !opts.SkipResolve { | ||||||
|  | 		updated, err := st.ResolveDeps() | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, []error{err} | ||||||
|  | 		} | ||||||
|  | 		*st = *updated | ||||||
|  | 	} | ||||||
|  | 	selected, err := st.GetSelectedReleases(opts.IncludeTransitiveNeeds) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, []error{err} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	releases := releasesNeedCharts(selected) | ||||||
|  | 
 | ||||||
|  | 	var prepareChartInfoMutex sync.Mutex | ||||||
|  | 
 | ||||||
|  | 	prepareChartInfo := make(map[PrepareChartKey]string, len(releases)) | ||||||
|  | 
 | ||||||
|  | 	errs := []error{} | ||||||
|  | 
 | ||||||
|  | 	jobQueue := make(chan *ReleaseSpec, len(releases)) | ||||||
|  | 	results := make(chan *chartPrepareResult, len(releases)) | ||||||
|  | 
 | ||||||
|  | 	var builds []*chartPrepareResult | ||||||
|  | 
 | ||||||
|  | 	st.scatterGather( | ||||||
|  | 		concurrency, | ||||||
|  | 		len(releases), | ||||||
|  | 		func() { | ||||||
|  | 			for i := 0; i < len(releases); i++ { | ||||||
|  | 				jobQueue <- &releases[i] | ||||||
|  | 			} | ||||||
|  | 			close(jobQueue) | ||||||
|  | 		}, | ||||||
|  | 		func(workerIndex int) { | ||||||
|  | 			for release := range jobQueue { | ||||||
|  | 				result := st.prepareChartForRelease(release, helm, dir, helmfileCommand, opts, workerIndex) | ||||||
|  | 				results <- result | ||||||
|  | 			} | ||||||
| 		}, | 		}, | ||||||
| 		func() { | 		func() { | ||||||
| 			for i := 0; i < len(releases); i++ { | 			for i := 0; i < len(releases); i++ { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue