Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt hd-rk helm 3 support #34

Merged
merged 17 commits into from
Dec 24, 2019
Merged
Prev Previous commit
Next Next commit
ref: optimize removal of chart version
  • Loading branch information
hd-rk committed Sep 24, 2019
commit 20b0212a0340666a8dafa49c03d2a44e89bfb02a
22 changes: 10 additions & 12 deletions pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"cloud.google.com/go/storage"
"github.com/ghodss/yaml"
Expand Down Expand Up @@ -79,7 +80,6 @@ func Load(name string, gcs *storage.Client) (*Repo, error) {
// Create creates a new repository on GCS by uploading a blank index.yaml file.
// This function is idempotent.
func Create(r *Repo) error {
// log := logger()
log.Debugf("create a repository with index file at %s", r.indexFileURL)

o, err := gcs.Object(r.gcs, r.indexFileURL)
Expand All @@ -105,7 +105,6 @@ func Create(r *Repo) error {
// The push will fail if the repository is updated at the same time, use "retry" to automatically reload
// the index of the repository.
func (r Repo) PushChart(chartpath string, force, retry bool, public bool, publicURL string) error {
// log := logger()
i, err := r.indexFile()
if err != nil {
return errors.Wrap(err, "load index file")
Expand Down Expand Up @@ -135,8 +134,6 @@ func (r Repo) PushChart(chartpath string, force, retry bool, public bool, public
if err != nil {
return errors.Wrap(err, "update index file")
}
// if !i.Has(chart.Metadata.Name, chart.Metadata.Version) {
// }

log.Debugf("upload file to GCS")
err = r.uploadChart(chartpath)
Expand All @@ -149,7 +146,6 @@ func (r Repo) PushChart(chartpath string, force, retry bool, public bool, public
// RemoveChart removes a chart from the repository
// If version is empty, all version will be deleted.
func (r Repo) RemoveChart(name, version string, retry bool) error {
// log := logger()
log.Debugf("removing chart %s-%s", name, version)

removeChart:
Expand All @@ -171,7 +167,9 @@ removeChart:
urls = append(urls, chartURL)
}
if version == v.Version {
index.Entries[name] = append(vs[:i], vs[i+1:]...)
vs[i] = vs[len(vs)-1]
vs[len(vs)-1] = nil
index.Entries[name] = vs[:len(vs)-1]
break
}
}
Expand Down Expand Up @@ -206,9 +204,11 @@ removeChart:

// uploadIndexFile update the index file on GCS.
func (r Repo) uploadIndexFile(i *repo.IndexFile) error {
// log := logger()
log.Debugf("push index file")

i.SortEntries()
i.Generated = time.Now()

o, err := gcs.Object(r.gcs, r.indexFileURL)
if r.indexFileGeneration != 0 {
log.Debugf("update condition: if generation = %d", r.indexFileGeneration)
Expand Down Expand Up @@ -241,7 +241,6 @@ func (r Repo) uploadIndexFile(i *repo.IndexFile) error {
// indexFile retrieves the index file from GCS.
// It will also retrieve the generation number of the file, for optimistic locking.
func (r *Repo) indexFile() (*repo.IndexFile, error) {
// log := logger()
log.Debugf("load index file \"%s\"", r.indexFileURL)

// retrieve index file generation
Expand Down Expand Up @@ -277,7 +276,6 @@ func (r *Repo) indexFile() (*repo.IndexFile, error) {

// uploadChart pushes a chart into the repository.
func (r Repo) uploadChart(chartpath string) error {
// log := logger()
f, err := os.Open(chartpath)
if err != nil {
return errors.Wrap(err, "open")
Expand Down Expand Up @@ -305,7 +303,6 @@ func (r Repo) uploadChart(chartpath string) error {
}

func (r Repo) updateIndexFile(i *repo.IndexFile, chartpath string, chart *chart.Chart, public bool, publicURL string) error {
// log := logger()
hash, err := provenance.DigestFile(chartpath)
if err != nil {
return errors.Wrap(err, "generate chart file digest")
Expand All @@ -325,7 +322,9 @@ func (r Repo) updateIndexFile(i *repo.IndexFile, chartpath string, chart *chart.
chartVersions := i.Entries[chart.Metadata.Name]
for idx, ver := range chartVersions {
if ver.Version == currentChart.Version {
i.Entries[chart.Metadata.Name] = append(chartVersions[:idx], chartVersions[idx+1:]...)
chartVersions[idx] = chartVersions[len(chartVersions)-1]
chartVersions[len(chartVersions)-1] = nil
i.Entries[chart.Metadata.Name] = chartVersions[:len(chartVersions)-1]
break
}
}
Expand Down Expand Up @@ -358,7 +357,6 @@ func resolveReference(base, p string) (string, error) {
}

func retrieveRepositoryEntry(name string) (*repo.Entry, error) {
// log := logger()
repoFilePath := helmpath.ConfigPath("repositories.yaml")
log.Debugf("helm repo file: %s", repoFilePath)

Expand Down