2
0

urlcache.go 927 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package urlcache
  2. import "github.com/porter-dev/porter/internal/helm/loader"
  3. // ChartLookupURLs contains an in-memory store of Porter chart names matched with
  4. // a repo URL, so that finding a chart does not involve multiple lookups to our
  5. // chart repo's index.yaml file
  6. type ChartURLCache struct {
  7. cache map[string]string
  8. urls []string
  9. }
  10. func Init(urls ...string) *ChartURLCache {
  11. res := &ChartURLCache{
  12. cache: make(map[string]string),
  13. urls: urls,
  14. }
  15. res.Update()
  16. return res
  17. }
  18. func (c *ChartURLCache) Update() {
  19. newCharts := make(map[string]string)
  20. for _, chartRepo := range c.urls {
  21. indexFile, err := loader.LoadRepoIndexPublic(chartRepo)
  22. if err != nil {
  23. continue
  24. }
  25. for chartName := range indexFile.Entries {
  26. newCharts[chartName] = chartRepo
  27. }
  28. }
  29. c.cache = newCharts
  30. }
  31. func (c *ChartURLCache) GetURL(chartName string) (string, bool) {
  32. res, ok := c.cache[chartName]
  33. return res, ok
  34. }