refactor(filesystem): add CopyDir method and optimize Fetch function (#2111)

* refactor(filesystem): add CopyDir method and optimize Fetch function

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix(state): conditionally prepare charts for local helmfile command

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix(state): conditionally prepare charts for local helmfile command

Signed-off-by: yxxhero <aiopsclub@163.com>

* refactor(state): optimize chart path generation and update dependencies

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix(test): update path in fetch-forl-local-chart test

Signed-off-by: yxxhero <aiopsclub@163.com>

* add more test cases

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2025-07-29 04:10:25 +08:00 committed by GitHub
parent b0911ab1a2
commit a76bec234c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 47 additions and 23 deletions

View File

@ -45,7 +45,7 @@ linters:
lines: 280
statements: 140
gocognit:
min-complexity: 100
min-complexity: 110
goconst:
min-len: 3
min-occurrences: 8

19
Vagrantfile vendored
View File

@ -1,19 +0,0 @@
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.hostname = "minikube.box"
config.vm.provision :shell, privileged: false,
inline: <<-EOS
set -e
sudo apt-get update
sudo apt-get install -y make docker.io
sudo systemctl start docker
sudo usermod -G docker $USER
cd /vagrant/.circleci
make all
EOS
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end
end

2
go.mod
View File

@ -84,7 +84,7 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/otiai10/copy v1.14.1 // indirect
github.com/otiai10/copy v1.14.1
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect

View File

@ -345,8 +345,7 @@ func (a *App) Fetch(c FetchConfigProvider) error {
OutputDir: c.OutputDir(),
OutputDirTemplate: c.OutputDirTemplate(),
Concurrency: c.Concurrency(),
}, func() {
})
}, func() {})
if prepErr != nil {
errs = append(errs, prepErr)

View File

@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"time"
copyDir "github.com/otiai10/copy"
)
type fileStat struct {
@ -36,6 +38,7 @@ type FileSystem struct {
Chdir func(string) error
Abs func(string) (string, error)
EvalSymlinks func(string) (string, error)
CopyDir func(src, dst string) error
}
func DefaultFileSystem() *FileSystem {
@ -55,6 +58,7 @@ func DefaultFileSystem() *FileSystem {
dfs.DirectoryExistsAt = dfs.directoryExistsDefault
dfs.FileExists = dfs.fileExistsDefault
dfs.Abs = dfs.absDefault
dfs.CopyDir = dfs.copyDirDefault
return &dfs
}
@ -100,6 +104,9 @@ func FromFileSystem(params FileSystem) *FileSystem {
if params.Dir != nil {
dfs.Dir = params.Dir
}
if params.CopyDir != nil {
dfs.CopyDir = params.CopyDir
}
return dfs
}
@ -180,3 +187,8 @@ func (filesystem *FileSystem) absDefault(path string) (string, error) {
}
return filepath.Abs(path)
}
// copyDirDefault recursively copies a directory tree, preserving permissions.
func (filesystem *FileSystem) copyDirDefault(src string, dst string) error {
return copyDir.Copy(src, dst, copyDir.Options{Sync: true})
}

View File

@ -1372,6 +1372,18 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre
// 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 {
chartAbsPath := strings.TrimSuffix(filepath.Clean(normalizedChart), "/")
chartPath, err = generateChartPath(filepath.Base(chartAbsPath), dir, release, opts.OutputDirTemplate)
if err != nil {
results <- &chartPrepareResult{err: err}
return
}
if err := st.fs.CopyDir(normalizedChart, filepath.Clean(chartPath)); err != nil {
results <- &chartPrepareResult{err: err}
return
}
}
buildDeps = !skipDeps
} else if !opts.ForceDownload {

View File

@ -80,6 +80,7 @@ ${kubectl} create namespace ${test_ns} || fail "Could not create namespace ${tes
# TEST CASES----------------------------------------------------------------------------------------------------------
. ${dir}/test-cases/fetch-forl-local-chart.sh
. ${dir}/test-cases/suppress-output-line-regex.sh
. ${dir}/test-cases/chartify-jsonPatches-and-strategicMergePatches.sh
. ${dir}/test-cases/include-template-func.sh

View File

@ -0,0 +1,15 @@
fetch_forl_local_chart_input_dir="${cases_dir}/fetch-forl-local-chart/input"
fetch_forl_local_chart_tmp=$(mktemp -d)
case_title="fetch for local chart"
test_start "$case_title"
info "Comparing fetch-forl-local-chart diff log #$i"
${helmfile} -f ${fetch_forl_local_chart_input_dir}/helmfile.yaml.gotmpl fetch --output-dir ${fetch_forl_local_chart_tmp} || fail "\"helmfile fetch\" shouldn't fail"
cat ${fetch_forl_local_chart_tmp}/helmfile-tests/local-chart/raw/latest/Chart.yaml || fail "Chart.yaml should exist in the fetched local chart directory"
cat ${fetch_forl_local_chart_tmp}/helmfile-tests/local-chart/raw/latest/templates/resources.yaml || fail "templates/resources.yaml should exist in the fetched local chart directory"
echo code=$?
test_pass "$case_title"

View File

@ -0,0 +1,4 @@
releases:
- name: local-chart
chart: ../../../charts/raw
namespace: local-chart