This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Support import of repositories for Helm v2 _and_ v3 #141
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package v2 | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"k8s.io/helm/pkg/getter" | ||
"k8s.io/helm/pkg/repo" | ||
) | ||
|
||
var ( | ||
repositoryConfigLock sync.RWMutex | ||
getters = getter.Providers{{ | ||
Schemes: []string{"http", "https"}, | ||
New: func(URL, CertFile, KeyFile, CAFile string) (getter.Getter, error) { | ||
return getter.NewHTTPGetter(URL, CertFile, KeyFile, CAFile) | ||
}, | ||
}} | ||
) | ||
|
||
func (h *HelmV2) RepositoryIndex() error { | ||
repositoryConfigLock.RLock() | ||
defer repositoryConfigLock.RUnlock() | ||
|
||
f, err := loadRepositoryConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var wg sync.WaitGroup | ||
for _, c := range f.Repositories { | ||
r, err := repo.NewChartRepository(c, getters) | ||
if err != nil { | ||
return err | ||
} | ||
wg.Add(1) | ||
go func(r *repo.ChartRepository) { | ||
if err := r.DownloadIndexFile(repositoryCache); err != nil { | ||
h.logger.Log("error", "unable to get an update from the chart repository", "url", r.Config.URL, "err", err) | ||
} else { | ||
h.logger.Log("info", "successfully got an update from the chart repository", "url", r.Config.URL) | ||
} | ||
wg.Done() | ||
}(r) | ||
} | ||
wg.Wait() | ||
return nil | ||
} | ||
|
||
func (h *HelmV2) RepositoryAdd(name, url, username, password, certFile, keyFile, caFile string) error { | ||
repositoryConfigLock.Lock() | ||
defer repositoryConfigLock.Unlock() | ||
|
||
f, err := loadRepositoryConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c := &repo.Entry{ | ||
Name: name, | ||
URL: url, | ||
Username: username, | ||
Password: password, | ||
CertFile: certFile, | ||
KeyFile: keyFile, | ||
CAFile: caFile, | ||
} | ||
f.Add(c) | ||
|
||
if f.Has(name) { | ||
return errors.New("chart repository with name %s already exists") | ||
} | ||
|
||
r, err := repo.NewChartRepository(c, getters) | ||
if err != nil { | ||
return err | ||
} | ||
if err = r.DownloadIndexFile(repositoryCache); err != nil { | ||
return err | ||
} | ||
|
||
return f.WriteFile(repositoryConfig, 0644) | ||
} | ||
|
||
func (h *HelmV2) RepositoryRemove(name string) error { | ||
repositoryConfigLock.Lock() | ||
defer repositoryConfigLock.Unlock() | ||
|
||
f, err := repo.LoadRepositoriesFile(repositoryConfig) | ||
if err != nil { | ||
return err | ||
} | ||
f.Remove(name) | ||
|
||
return f.WriteFile(repositoryConfig, 0644) | ||
} | ||
|
||
func (h *HelmV2) RepositoryImport(path string) error { | ||
s, err := repo.LoadRepositoriesFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repositoryConfigLock.Lock() | ||
defer repositoryConfigLock.Unlock() | ||
|
||
t, err := loadRepositoryConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, c := range s.Repositories { | ||
if t.Has(c.Name) { | ||
h.logger.Log("error", "repository with name already exists", "name", c.Name, "url", c.URL) | ||
continue | ||
} | ||
r, err := repo.NewChartRepository(c, getters) | ||
if err != nil { | ||
h.logger.Log("error", err, "name", c.Name, "url", c.URL) | ||
continue | ||
} | ||
if err := r.DownloadIndexFile(repositoryCache); err != nil { | ||
h.logger.Log("error", err, "name", c.Name, "url", c.URL) | ||
continue | ||
} | ||
|
||
t.Add(c) | ||
h.logger.Log("info", "successfully imported repository", "name", c.Name, "url", c.URL) | ||
} | ||
|
||
return t.WriteFile(repositoryConfig, 0644) | ||
} | ||
|
||
func loadRepositoryConfig() (*repo.RepoFile, error) { | ||
r, err := repo.LoadRepositoriesFile(repositoryConfig) | ||
if err != nil && !os.IsNotExist(errors.Cause(err)) { | ||
return nil, err | ||
} | ||
return r, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a good reason to have this as a global variable? Can't it be part of
Helm2
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There can theoretically be multiple versions of
Helm2
while they all speak with the same repository index file (this is not completely true, as they can have differentHELM_HOME
s and are thus working with a different file, but this is the simplest secure option).