Skip to content

Commit

Permalink
Cache Helm indexes, release 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluies committed Aug 30, 2019
1 parent 0a92477 commit 44f42cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dependencies/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func RemoteCheck(dependencyFilePath string) error {
if updateAvailable {
log.Infof("Update available for dependency %v: %v (current: %v)\n", dep.Name, latestVersion.Version, currentVersion.Version)
} else {
log.Infof("No update available for dependency %v: %v (latest: %v)\n", dep.Name, currentVersion.Version, latestVersion.Version)
log.Debugf("No update available for dependency %v: %v (latest: %v)\n", dep.Name, currentVersion.Version, latestVersion.Version)
}
}
return nil
Expand Down
64 changes: 45 additions & 19 deletions upstreams/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,47 @@ type Helm struct {
CAFile string
}

// Cache remote repositories locally to prevent unnecessary network round-trips
var cache map[string]*repo.IndexFile

// getIndex returns the index for the given repository, and caches it for subsequent calls
func getIndex(c repo.Entry) (*repo.IndexFile, error) {
// Check cache first
if cache == nil {
// No cache: initialise it
cache = make(map[string]*repo.IndexFile)
} else {
index, cacheHit := cache[c.URL]
if cacheHit {
log.Debugf("Using cached index for %s", c.URL)
return index, nil
}
}

// Download and write the index file to a temporary location
tempIndexFile, err := ioutil.TempFile("", "tmp-repo-file")
if err != nil {
return nil, fmt.Errorf("cannot write index file for repository requested")
}
defer os.Remove(tempIndexFile.Name())

r, err := repo.NewChartRepository(&c, getter.All(environment.EnvSettings{}))
if err != nil {
return nil, err
}
if err := r.DownloadIndexFile(tempIndexFile.Name()); err != nil {
return nil, fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", c.URL, err)
}
index, err := repo.LoadIndexFile(tempIndexFile.Name())
if err != nil {
return nil, err
}

// Found: add to cache
cache[c.URL] = index
return index, nil
}

// LatestVersion returns the latest version of a Helm chart.
//
// Returns the latest chart version in the given repository.
Expand All @@ -45,37 +86,22 @@ func (upstream Helm) LatestVersion() (string, error) {
repoURL = "https://kubernetes-charts.storage.googleapis.com/"
}

// Download and write the index file to a temporary location
// TODO: cache this file to prevent unnecessary network round-trips during a single invocation
tempIndexFile, err := ioutil.TempFile("", "tmp-repo-file")
if err != nil {
return "", fmt.Errorf("cannot write index file for repository requested")
}
defer os.Remove(tempIndexFile.Name())

c := repo.Entry{
entry := repo.Entry{
URL: repoURL,
Username: upstream.Username,
Password: upstream.Password,
CertFile: upstream.CertFile,
KeyFile: upstream.KeyFile,
CAFile: upstream.CAFile,
}
r, err := repo.NewChartRepository(&c, getter.All(environment.EnvSettings{}))
if err != nil {
return "", err
}
if err := r.DownloadIndexFile(tempIndexFile.Name()); err != nil {
return "", fmt.Errorf("Looks like %q is not a valid chart repository or cannot be reached: %s", repoURL, err)
}

// Read the index file for the repository to get chart information and return chart URL
repo, err := repo.LoadIndexFile(tempIndexFile.Name())
// Get the index
index, err := getIndex(entry)
if err != nil {
return "", err
}

cv, err := repo.Get(upstream.Name, upstream.Constraints)
cv, err := index.Get(upstream.Name, upstream.Constraints)
if err != nil {
if upstream.Constraints != "" {
return "", fmt.Errorf("%s not found in %s repository (with constraints: %s)", upstream.Name, repoURL, upstream.Constraints)
Expand Down
2 changes: 1 addition & 1 deletion zeitgeist.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
app := cli.NewApp()
app.Name = "zeitgeist"
app.Usage = "Manage your external dependencies"
app.Version = "0.0.3"
app.Version = "0.0.4"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Expand Down

0 comments on commit 44f42cf

Please sign in to comment.