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

pkg/repo: inject chart metadata(name/version) into storage object #362

Merged
merged 3 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions acceptance_tests/helm.robot
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Test Helm integration
Able to fetch and verify test charts
Delete test charts from ChartMuseum
Able to update ChartMuseum repo
Ensure test charts deleted
Helm search does not return test charts
Unable to fetch and verify test charts

Expand All @@ -82,6 +83,9 @@ Upload bad provenance files to ChartMuseum
Delete test charts from ChartMuseum
ChartMuseum.delete test charts

Ensure test charts deleted
ChartMuseum.ensure charts deleted

Able to add ChartMuseum as Helm chart repo
Helm.add chart repo
Helm.return code should be 0
Expand All @@ -98,6 +102,8 @@ Helm search returns test charts
Helm search does not return test charts
Helm.search for chart mychart
Helm.output does not contain mychart
Helm.search for chart mychart2
Helm.output does not contain mychart2

Able to fetch and verify test charts
Helm.fetch and verify chart mychart
Expand Down
20 changes: 20 additions & 0 deletions acceptance_tests/lib/ChartMuseum.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def delete_test_charts(self):
testcharts_dir = os.path.join(self.rootdir, common.TESTCHARTS_DIR)
os.chdir(testcharts_dir)
for d in os.listdir('.'):
# delete all charts inside /mychart (also includes mychart2)
if not os.path.isdir(d):
continue
os.chdir(d)
Expand All @@ -167,3 +168,22 @@ def delete_test_charts(self):
print(('HTTP CONTENT: %s' % response.content))
self.http_status_code_should_be(200, response.status_code)
os.chdir('../')

def ensure_charts_deleted(self):
endpoint = '%s/api/charts' % common.HELM_REPO_URL
testcharts_dir = os.path.join(self.rootdir, common.TESTCHARTS_DIR)
os.chdir(testcharts_dir)
for d in os.listdir('.'):
if not os.path.isdir(d):
continue
os.chdir(d)
tgzs = glob.glob('*.tgz')
for tgz in tgzs:
tmp = tgz[:-4].rsplit('-', 1)
name = tmp[0]
version = tmp[1]
with open(tgz):
epoint = '%s/%s/%s' % (endpoint, name, version)
response = requests.get(url=epoint)
self.http_status_code_should_be(404, response.status_code)
os.chdir('../')
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ replace (
go.etcd.io/etcd => github.com/eddycjy/etcd v0.5.0-alpha.5.0.20200218102753-4258cdd2efdf
)

replace github.com/chartmuseum/storage => github.com/chartmuseum/storage v0.9.2-0.20201014231951-6dd385da21c3

require (
github.com/Masterminds/semver/v3 v3.1.0
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5/go.mod h1:/iP1qXHoty45bqomnu2LM+VVyAEdWN+vtSHGlQgyxbw=
github.com/chartmuseum/auth v0.4.2 h1:Ch7sh2BpoYLN20zc6U6NQyB3JPEdBJ0quWH6+NQHcQ0=
github.com/chartmuseum/auth v0.4.2/go.mod h1:nO+nNadtlcIQrnVdL2QxI+XYoe/4sd8kypYT956miQM=
github.com/chartmuseum/storage v0.9.1 h1:RgnG4jC6Xhvw0sMS8A+tA0pO5xg+NU0rNz/X+iPEG/8=
github.com/chartmuseum/storage v0.9.1/go.mod h1:rEOwJVPHU569BC22tUhl6g64sYPxFzZKrxxzjz+zvWQ=
github.com/chartmuseum/storage v0.9.2-0.20201014231951-6dd385da21c3 h1:+CGbrnh/pOLmE50kU5KIjzwSMNTcXAAXvVVpYW7/1bk=
github.com/chartmuseum/storage v0.9.2-0.20201014231951-6dd385da21c3/go.mod h1:rEOwJVPHU569BC22tUhl6g64sYPxFzZKrxxzjz+zvWQ=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
15 changes: 13 additions & 2 deletions pkg/chartmuseum/server/multitenant/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ import (
"context"
"encoding/json"
"errors"
"go.uber.org/zap"
"fmt"
pathutil "path"
"sync"
"time"

"go.uber.org/zap"

cm_logger "helm.sh/chartmuseum/pkg/chartmuseum/logger"
cm_repo "helm.sh/chartmuseum/pkg/repo"

Expand Down Expand Up @@ -201,6 +203,11 @@ func (server *MultiTenantServer) fetchChartsInStorage(log cm_logger.LoggingFn, r
filteredObjects := []cm_storage.Object{}
for _, object := range allObjects {
if object.HasExtension(cm_repo.ChartPackageFileExtension) {
// Since ListObject cannot fetch the content from file list
object, err = server.StorageBackend.GetObject(object.Path)
if err != nil {
return nil, fmt.Errorf("backend storage: chart not found: %q", err)
}
filteredObjects = append(filteredObjects, object)
}
}
Expand Down Expand Up @@ -506,7 +513,11 @@ func (server *MultiTenantServer) startEventListener() {
}
index := entry.RepoIndex

tenant := server.Tenants[e.RepoName]
tenant, ok := server.Tenants[e.RepoName]
if !ok {
log(cm_logger.ErrorLevel, "Error find tenants repo name", zap.Error(err), zap.String("repo", repo))
continue
}
tenant.RegenerationLock.Lock()

if e.ChartVersion == nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/repo/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func ChartPackageFilenameFromContent(content []byte) (string, error) {
// ChartVersionFromStorageObject returns a chart version from a storage object
func ChartVersionFromStorageObject(object storage.Object) (*helm_repo.ChartVersion, error) {
if len(object.Content) == 0 {
if object.Meta.Version != "" && object.Meta.Name != "" {
return &helm_repo.ChartVersion{
Metadata: &helm_chart.Metadata{Name: object.Meta.Name, Version: object.Meta.Version},
}, nil
}
chartVersion := emptyChartVersionFromPackageFilename(object.Path)
if chartVersion.Name == "" || chartVersion.Version == "" {
return nil, ErrorInvalidChartPackage
Expand All @@ -86,7 +91,13 @@ func ChartVersionFromStorageObject(object storage.Object) (*helm_repo.ChartVersi

// StorageObjectFromChartVersion returns a storage object from a chart version (empty content)
func StorageObjectFromChartVersion(chartVersion *helm_repo.ChartVersion) storage.Object {
meta := storage.Metadata{}
if chartVersion.Metadata != nil {
meta.Name = chartVersion.Name
meta.Version = chartVersion.Version
}
object := storage.Object{
Meta: meta,
Path: pathutil.Base(chartVersion.URLs[0]),
Content: []byte{},
LastModified: chartVersion.Created,
Expand Down
40 changes: 40 additions & 0 deletions pkg/repo/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/chartmuseum/storage"
"github.com/stretchr/testify/suite"
"helm.sh/helm/v3/pkg/chart"
helm_repo "helm.sh/helm/v3/pkg/repo"
)

Expand Down Expand Up @@ -83,6 +84,39 @@ func (suite *ChartTestSuite) TestChartVersionFromStorageObject() {
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT", chartVersion.Version, "chart version as expected")

snapshotObject2 := storage.Object{
Meta: storage.Metadata{Name: "mychart", Version: "1.0.4-SNAPSHOT-1"},
Path: "mychart-1.0.4-SNAPSHOT-1.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(snapshotObject2)
suite.Nil(err)
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1.0.4-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

snapshotObject3 := storage.Object{
Meta: storage.Metadata{Name: "mychart", Version: "1.0-SNAPSHOT-1"},
Path: "mychart-1.0-SNAPSHOT-1.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(snapshotObject3)
suite.Nil(err)
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1.0-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

snapshotObject4 := storage.Object{
Meta: storage.Metadata{Name: "mychart", Version: "1-SNAPSHOT-1"},
Path: "mychart-1-SNAPSHOT-1.tgz",
Content: []byte{},
LastModified: time.Now(),
}
chartVersion, err = ChartVersionFromStorageObject(snapshotObject4)
suite.Nil(err)
suite.Equal("mychart", chartVersion.Name, "chart name as expected")
suite.Equal("1-SNAPSHOT-1", chartVersion.Version, "chart version as expected")

multiHyphenObject := storage.Object{
Path: "my-long-hyphenated-chart-name-1.0.4.tgz",
Content: []byte{},
Expand Down Expand Up @@ -140,12 +174,18 @@ func (suite *ChartTestSuite) TestChartVersionFromStorageObject() {
func (suite *ChartTestSuite) TestStorageObjectFromChartVersion() {
now := time.Now()
chartVersion := &helm_repo.ChartVersion{
Metadata: &chart.Metadata{
Name: "mychart",
Version: "0.1.0",
},
URLs: []string{"charts/mychart-0.1.0.tgz"},
Created: now,
}
object := StorageObjectFromChartVersion(chartVersion)
suite.Equal(now, object.LastModified, "object last modified as expected")
suite.Equal("mychart-0.1.0.tgz", object.Path, "object path as expected")
suite.Equal("mychart", object.Meta.Name, "object chart name as expected")
suite.Equal("0.1.0", object.Meta.Version, "object chart version as expected")
suite.Equal([]byte{}, object.Content, "object content as expected")
}

Expand Down
2 changes: 2 additions & 0 deletions testdata/charts/mychart2/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tgz
*.prov
2 changes: 2 additions & 0 deletions testdata/charts/mychart2/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: mychart2
version: 0.1.0-SNAPSHOT-1
9 changes: 9 additions & 0 deletions testdata/charts/mychart2/templates/pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Pod
metadata:
name: '{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" -}}'
spec:
containers:
- image: busybox
name: '{{ .Chart.Name }}'
command: ['/bin/sh', '-c', 'while true; do echo {{ .Release.Name }}; sleep 5; done']