Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Chart dependencies stored in etcd #109

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 51 additions & 16 deletions internal/storage/driver/etcd/entity_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ func (s *Chart) Upsert(namespace internal.Namespace, c *chart.Chart) (replaced b
if err != nil {
return false, err
}

buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
if err := enc.Encode(c); err != nil {
return false, errors.Wrap(err, "while encoding entity")
encoded, err := s.encodeChart(c)
if err != nil {
return false, errors.Wrap(err, "while encoding chart")
}

resp, err := s.kv.Put(context.TODO(), s.key(namespace, nv), buf.String(), clientv3.WithPrevKV())
resp, err := s.kv.Put(context.TODO(), s.key(namespace, nv), encoded, clientv3.WithPrevKV())
if err != nil {
return false, errors.Wrap(err, "while calling database")
}
Expand Down Expand Up @@ -93,16 +91,6 @@ func (s *Chart) Get(namespace internal.Namespace, name internal.ChartName, ver s
return c, nil
}

func (s *Chart) decodeChart(raw []byte) (*chart.Chart, error) {
dec := json.NewDecoder(bytes.NewReader(raw))
var c chart.Chart
if err := dec.Decode(&c); err != nil {
return nil, err
}

return &c, nil
}

// Remove is removing chart with given name and version from storage
func (s *Chart) Remove(namespace internal.Namespace, name internal.ChartName, ver semver.Version) error {
nv, err := s.nameVersion(name, ver)
Expand Down Expand Up @@ -166,3 +154,50 @@ func (*Chart) key(namespace internal.Namespace, nv chartNameVersion) string {
}
return fmt.Sprintf("%s|%s", prefix, string(nv))
}

type dto struct {
Main *chart.Chart `json:"main"`
Deps []*dto `json:"dependencies"`
}

func (s *Chart) toDto(c *chart.Chart) *dto {
var deps []*dto
for _, d := range c.Dependencies() {
deps = append(deps, s.toDto(d))
}
return &dto{
Main: c,
Deps: deps,
}
}

func (s *Chart) fromDto(obj *dto) *chart.Chart {
chrt := obj.Main

var deps []*chart.Chart
for _, d := range obj.Deps {
deps = append(deps, s.fromDto(d))
}
chrt.SetDependencies(deps...)
return chrt
}

func (s *Chart) encodeChart(c *chart.Chart) (string, error) {
obj := s.toDto(c)
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
if err := enc.Encode(obj); err != nil {
return "", errors.Wrap(err, "while encoding entity")
}
return buf.String(), nil
}

func (s *Chart) decodeChart(raw []byte) (*chart.Chart, error) {
dec := json.NewDecoder(bytes.NewReader(raw))
var obj dto
if err := dec.Decode(&obj); err != nil {
return nil, err
}

return s.fromDto(&obj), nil
}
18 changes: 18 additions & 0 deletions internal/storage/testing/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kyma-project/helm-broker/internal"
"github.com/kyma-project/helm-broker/internal/storage"
"github.com/stretchr/testify/require"
"helm.sh/helm/v3/pkg/chart/loader"
)

func TestChartGet(t *testing.T) {
Expand Down Expand Up @@ -92,6 +93,23 @@ func TestChartUpsert(t *testing.T) {
})
}

func TestHappyPath(t *testing.T) {
tRunDrivers(t, "Upsert and get preserves dependencies", func(t *testing.T, sf storage.Factory) {
// given
ts := newChartTestSuite(t, sf)
chrt, err := loader.LoadDir("testing")
require.NoError(t, err)

// when
ts.s.Upsert("ns1", chrt)

// then
got, err := ts.s.Get("ns1", internal.ChartName(chrt.Name()), *semver.MustParse(chrt.Metadata.Version))
require.NoError(t, err)
assert.Len(t, chrt.Dependencies(), len(got.Dependencies()))
})
}

func TestChartRemove(t *testing.T) {
tRunDrivers(t, "Found", func(t *testing.T, sf storage.Factory) {
// GIVEN:
Expand Down
21 changes: 21 additions & 0 deletions internal/storage/testing/testing/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*~
# Various IDEs
.project
.idea/
*.tmproj
5 changes: 5 additions & 0 deletions internal/storage/testing/testing/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: Testing config map
name: testing
version: 0.0.1
5 changes: 5 additions & 0 deletions internal/storage/testing/testing/charts/subch/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: Subchart config map
name: subch
version: 0.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "testing.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "testing.fullname" . }}-subchart
data:
someKey: "someValue"
8 changes: 8 additions & 0 deletions internal/storage/testing/testing/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "testing.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
9 changes: 9 additions & 0 deletions internal/storage/testing/testing/templates/cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "testing.fullname" . }}
data:
planName: {{ .Values.planName | quote }}
{{- if .Values.additionalData }}
additionalData: {{ .Values.additionalData | quote }}
{{- end }}
Empty file.