Skip to content

Commit abbe2ff

Browse files
fix: not update helm repo if there are no repos (#64)
* fix: not update helm repo if there are no repos * chore: fix syntax
1 parent e1d1e9b commit abbe2ff

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

helm.go

+28-22
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ type ChartInfo struct {
2828
}
2929
type HelmUpdater struct {
3030
registryHelper *RegistryHelper
31-
config *HelmUpdateConfig
31+
config *HelmUpdateConfig
3232
}
3333

3434
type HelmUpdateConfig struct {
35-
SkipRepoOverwrite bool // ENV: HELM_DEPS_SKIP_REPO_OVERWRITE
36-
SkipDepdencyRefresh bool // ENV: HELM_DEPS_SKIP_REFRESH
35+
SkipRepoOverwrite bool // ENV: HELM_DEPS_SKIP_REPO_OVERWRITE
36+
SkipDepdencyRefresh bool // ENV: HELM_DEPS_SKIP_REFRESH
3737
FetchArgocdRepoSecrets bool // ENV: HELM_DEPS_FETCH_ARGOCD_REPO_SECRETS
38-
UseRandomHelmCacheDir bool // ENV: HELM_DEPS_RANDOM_CACHE_DIR
38+
UseRandomHelmCacheDir bool // ENV: HELM_DEPS_RANDOM_CACHE_DIR
3939
}
4040

4141
func (c *ChartInfo) AddDependencyUrl(depdencyUrl string) error {
@@ -80,10 +80,16 @@ func (updater *HelmUpdater) UpdateChart(chartPath string) error {
8080
}
8181
}
8282
// update helm repo after adding all registries
83-
err := runHelmCommand("repo", "update")
83+
_, reposAvailable, err := helmRepoExists(&RegistryInfo{Hostname: ""}, updater.config)
8484
if err != nil {
8585
return err
8686
}
87+
if reposAvailable {
88+
err := runHelmCommand("repo", "update")
89+
if err != nil {
90+
return err
91+
}
92+
}
8793
}
8894
return updater.updateDependencies(chartInfo)
8995
}
@@ -177,30 +183,30 @@ func runHelmCommand(args ...string) error {
177183
}
178184

179185
// helmRepoExists checks if a helm repository already exists with helm repo ls command
180-
func helmRepoExists(registry *RegistryInfo, config *HelmUpdateConfig) (bool, error) {
186+
func helmRepoExists(registry *RegistryInfo, config *HelmUpdateConfig) (bool, bool, error) {
181187
if !config.SkipRepoOverwrite {
182-
return false, nil
188+
return false, false, nil
183189
}
184190
cmd := exec.Command("helm", "repo", "ls")
185191
var out bytes.Buffer
186192
cmd.Stdout = &out
187193
cmd.Stderr = &out
188194
err := cmd.Run()
189-
if err != nil {
190-
// Check if the error is due to no repositories existing
191-
if strings.Contains(out.String(), "no repositories to show") {
192-
return false, nil
193-
}
194-
return false, fmt.Errorf("failed to run helm repo ls: %w", err)
195-
}
196-
output := out.String()
197-
lines := strings.Split(output, "\n")
198-
for _, line := range lines {
199-
if strings.Contains(line, registry.Hostname) {
200-
return true, nil
201-
}
202-
}
203-
return false, nil
195+
if err != nil {
196+
// Check if the error is due to no repositories existing
197+
if strings.Contains(out.String(), "no repositories to show") {
198+
return false, false, nil
199+
}
200+
return false, false, fmt.Errorf("failed to run helm repo ls: %w", err)
201+
}
202+
output := out.String()
203+
lines := strings.Split(output, "\n")
204+
for _, line := range lines {
205+
if strings.Contains(line, registry.Hostname) {
206+
return true, true, nil
207+
}
208+
}
209+
return false, true, nil
204210
}
205211

206212
func (updater *HelmUpdater) helmDepUpdate(chartPath string) error {

kube.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (r *RegistryHelper) LoginIfExists(registry *RegistryInfo) error {
152152
if registry == nil {
153153
return errors.New("registry can not be empty")
154154
}
155-
exists, err := helmRepoExists(registry, r.config)
155+
exists, _, err := helmRepoExists(registry, r.config)
156156
if err != nil {
157157
return err
158158
}

0 commit comments

Comments
 (0)