Skip to content

Commit

Permalink
fix: not update helm repo if there are no repos (#64)
Browse files Browse the repository at this point in the history
* fix: not update helm repo if there are no repos

* chore: fix syntax
  • Loading branch information
karlderkaefer authored Dec 15, 2024
1 parent e1d1e9b commit abbe2ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 28 additions & 22 deletions helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ type ChartInfo struct {
}
type HelmUpdater struct {
registryHelper *RegistryHelper
config *HelmUpdateConfig
config *HelmUpdateConfig
}

type HelmUpdateConfig struct {
SkipRepoOverwrite bool // ENV: HELM_DEPS_SKIP_REPO_OVERWRITE
SkipDepdencyRefresh bool // ENV: HELM_DEPS_SKIP_REFRESH
SkipRepoOverwrite bool // ENV: HELM_DEPS_SKIP_REPO_OVERWRITE
SkipDepdencyRefresh bool // ENV: HELM_DEPS_SKIP_REFRESH
FetchArgocdRepoSecrets bool // ENV: HELM_DEPS_FETCH_ARGOCD_REPO_SECRETS
UseRandomHelmCacheDir bool // ENV: HELM_DEPS_RANDOM_CACHE_DIR
UseRandomHelmCacheDir bool // ENV: HELM_DEPS_RANDOM_CACHE_DIR
}

func (c *ChartInfo) AddDependencyUrl(depdencyUrl string) error {
Expand Down Expand Up @@ -80,10 +80,16 @@ func (updater *HelmUpdater) UpdateChart(chartPath string) error {
}
}
// update helm repo after adding all registries
err := runHelmCommand("repo", "update")
_, reposAvailable, err := helmRepoExists(&RegistryInfo{Hostname: ""}, updater.config)
if err != nil {
return err
}
if reposAvailable {
err := runHelmCommand("repo", "update")
if err != nil {
return err
}
}
}
return updater.updateDependencies(chartInfo)
}
Expand Down Expand Up @@ -177,30 +183,30 @@ func runHelmCommand(args ...string) error {
}

// helmRepoExists checks if a helm repository already exists with helm repo ls command
func helmRepoExists(registry *RegistryInfo, config *HelmUpdateConfig) (bool, error) {
func helmRepoExists(registry *RegistryInfo, config *HelmUpdateConfig) (bool, bool, error) {
if !config.SkipRepoOverwrite {
return false, nil
return false, false, nil
}
cmd := exec.Command("helm", "repo", "ls")
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
// Check if the error is due to no repositories existing
if strings.Contains(out.String(), "no repositories to show") {
return false, nil
}
return false, fmt.Errorf("failed to run helm repo ls: %w", err)
}
output := out.String()
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, registry.Hostname) {
return true, nil
}
}
return false, nil
if err != nil {
// Check if the error is due to no repositories existing
if strings.Contains(out.String(), "no repositories to show") {
return false, false, nil
}
return false, false, fmt.Errorf("failed to run helm repo ls: %w", err)
}
output := out.String()
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, registry.Hostname) {
return true, true, nil
}
}
return false, true, nil
}

func (updater *HelmUpdater) helmDepUpdate(chartPath string) error {
Expand Down
2 changes: 1 addition & 1 deletion kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *RegistryHelper) LoginIfExists(registry *RegistryInfo) error {
if registry == nil {
return errors.New("registry can not be empty")
}
exists, err := helmRepoExists(registry, r.config)
exists, _, err := helmRepoExists(registry, r.config)
if err != nil {
return err
}
Expand Down

0 comments on commit abbe2ff

Please sign in to comment.