Skip to content

Commit d37a4ae

Browse files
authored
Clean up the chartsCache interfaces (#2270)
* Clean up the chartsCache interfaces - Accept a smaller interface of things we actually require * ProfilesGeneratorCache sounds a bit better than GenerateProfilesCache
1 parent 3b49f8a commit d37a4ae

File tree

6 files changed

+14
-158
lines changed

6 files changed

+14
-158
lines changed

cmd/clusters-service/pkg/server/clusters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var (
6060
type generateProfileFilesParams struct {
6161
helmRepositoryCluster types.NamespacedName
6262
helmRepository types.NamespacedName
63-
chartsCache helm.ChartsCacheReader
63+
chartsCache helm.ProfilesGeneratorCache
6464
profileValues []*capiv1_proto.ProfileValues
6565
parameterValues map[string]string
6666
}

cmd/clusters-service/pkg/server/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func GetFiles(
270270
client client.Client,
271271
log logr.Logger,
272272
estimator estimation.Estimator,
273-
chartsCache helm.ChartsCacheReader,
273+
chartsCache helm.ProfilesGeneratorCache,
274274
profileHelmRepositoryCluster types.NamespacedName,
275275
profileHelmRepository types.NamespacedName,
276276
tmpl templatesv1.Template,

cmd/gitops/app/create/templates/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func generateFilesLocally(tmpl *gapiv1.GitOpsTemplate, params map[string]string,
247247

248248
var helmRepo *sourcev1.HelmRepository
249249
var helmRepoRef types.NamespacedName
250-
var chartsCache helm.ChartsCacheReader = helm.NilCache{}
250+
var chartsCache helm.ProfilesGeneratorCache = helm.NilProfilesGeneratorCache{}
251251
if len(profiles) > 0 || templateHasRequiredProfiles {
252252
entry, index, err := localHelmRepo(helmRepoName, settings)
253253
if err != nil {

pkg/helm/charts_cache.go

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ type ChartsCacheWriter interface {
1919
DeleteAllChartsForCluster(ctx context.Context, clusterRef types.NamespacedName) error
2020
}
2121

22+
// ProfilesGeneratorCache is all that is needed to generate the profiles for a cluster
23+
type ProfilesGeneratorCache interface {
24+
GetLatestVersion(ctx context.Context, clusterRef, repoRef types.NamespacedName, name string) (string, error)
25+
GetLayer(ctx context.Context, clusterRef, repoRef types.NamespacedName, name, version string) (string, error)
26+
}
27+
2228
// ChartsCacheReader is the "reading" interface to the cache, used by api etc
2329
type ChartsCacheReader interface {
30+
ProfilesGeneratorCache
2431
ListChartsByRepositoryAndCluster(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, kind string) ([]Chart, error)
2532
IsKnownChart(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) (bool, error)
2633
GetChartValues(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) ([]byte, error)
2734
UpdateValuesYaml(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart, valuesYaml []byte) error
28-
GetLatestVersion(ctx context.Context, clusterRef, repoRef types.NamespacedName, name string) (string, error)
29-
GetLayer(ctx context.Context, clusterRef, repoRef types.NamespacedName, name, version string) (string, error)
3035
}
3136

3237
type ChartsCache interface {
@@ -50,41 +55,13 @@ type Chart struct {
5055
Layer string
5156
}
5257

53-
// Implementation of ChartsCache that does nothing.
54-
type NilCache struct{}
55-
56-
func (n NilCache) AddChart(ctx context.Context, name, version, kind, layer string, clusterRef types.NamespacedName, repoRef ObjectReference) error {
57-
return nil
58-
}
59-
60-
func (n NilCache) Delete(ctx context.Context, repoRef ObjectReference, clusterRef types.NamespacedName) error {
61-
return nil
62-
}
63-
64-
func (n NilCache) DeleteAllChartsForCluster(ctx context.Context, clusterRef types.NamespacedName) error {
65-
return nil
66-
}
67-
68-
func (n NilCache) ListChartsByRepositoryAndCluster(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, kind string) ([]Chart, error) {
69-
return nil, nil
70-
}
71-
72-
func (n NilCache) IsKnownChart(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) (bool, error) {
73-
return false, nil
74-
}
75-
76-
func (n NilCache) GetChartValues(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) ([]byte, error) {
77-
return nil, nil
78-
}
79-
80-
func (n NilCache) UpdateValuesYaml(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart, valuesYaml []byte) error {
81-
return nil
82-
}
58+
// Implementation of ProfilesGeneratorCache that does nothing.
59+
type NilProfilesGeneratorCache struct{}
8360

84-
func (n NilCache) GetLatestVersion(ctx context.Context, clusterRef, repoRef types.NamespacedName, name string) (string, error) {
61+
func (n NilProfilesGeneratorCache) GetLatestVersion(ctx context.Context, clusterRef, repoRef types.NamespacedName, name string) (string, error) {
8562
return "", nil
8663
}
8764

88-
func (n NilCache) GetLayer(ctx context.Context, clusterRef, repoRef types.NamespacedName, name, version string) (string, error) {
65+
func (n NilProfilesGeneratorCache) GetLayer(ctx context.Context, clusterRef, repoRef types.NamespacedName, name, version string) (string, error) {
8966
return "", nil
9067
}

pkg/helm/helm_index_file_reader.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package helm
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"sort"
86

97
"helm.sh/helm/v3/pkg/repo"
108
"k8s.io/apimachinery/pkg/types"
@@ -24,40 +22,6 @@ func NewHelmIndexFileReader(index *repo.IndexFile) *HelmIndexFileReader {
2422
}
2523
}
2624

27-
// ListChartsByRepositoryAndCluster returns a list of charts from the
28-
// index file. The repoRef and clusterRef parameters are ignored.
29-
func (c HelmIndexFileReader) ListChartsByRepositoryAndCluster(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, kind string) ([]Chart, error) {
30-
var charts []Chart
31-
32-
for _, chart := range c.index.Entries {
33-
for _, version := range chart {
34-
charts = append(charts, Chart{
35-
Name: version.Name,
36-
Version: version.Version,
37-
Layer: version.Annotations[LayerAnnotation],
38-
})
39-
}
40-
}
41-
42-
sort.Slice(charts, func(i, j int) bool {
43-
return charts[i].Name < charts[j].Name
44-
})
45-
46-
return charts, nil
47-
}
48-
49-
// IsKnownChart returns true if the chart is in the index file. The repoRef
50-
// and clusterRef parameters are ignored.
51-
func (c HelmIndexFileReader) IsKnownChart(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) (bool, error) {
52-
for _, version := range c.index.Entries[chart.Name] {
53-
if version.Version == chart.Version {
54-
return true, nil
55-
}
56-
}
57-
58-
return false, nil
59-
}
60-
6125
// GetLatestVersion returns the latest version of the chart. The repoRef
6226
// and clusterRef parameters are ignored.
6327
func (c HelmIndexFileReader) GetLatestVersion(ctx context.Context, clusterRef, repoRef types.NamespacedName, name string) (string, error) {
@@ -87,13 +51,3 @@ func (c HelmIndexFileReader) GetLayer(ctx context.Context, clusterRef, repoRef t
8751

8852
return "", nil
8953
}
90-
91-
// not implmented, this does not support reading values.yaml
92-
func (c HelmIndexFileReader) GetChartValues(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart) ([]byte, error) {
93-
return nil, errors.New("not implemented")
94-
}
95-
96-
// not implmented, this does not support reading values.yaml
97-
func (c HelmIndexFileReader) UpdateValuesYaml(ctx context.Context, clusterRef types.NamespacedName, repoRef ObjectReference, chart Chart, valuesYaml []byte) error {
98-
return errors.New("not implemented")
99-
}

pkg/helm/helm_index_file_reader_test.go

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,11 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/google/go-cmp/cmp"
87
"helm.sh/helm/v3/pkg/chart"
98
"helm.sh/helm/v3/pkg/repo"
109
"k8s.io/apimachinery/pkg/types"
1110
)
1211

13-
func TestListChart(t *testing.T) {
14-
index := makeTestIndex()
15-
cache := NewHelmIndexFileReader(index)
16-
clusterRef := types.NamespacedName{}
17-
repoRef := ObjectReference{}
18-
19-
charts, err := cache.ListChartsByRepositoryAndCluster(context.TODO(), clusterRef, repoRef, "")
20-
if err != nil {
21-
t.Fatal(err)
22-
}
23-
24-
expected := []Chart{
25-
{
26-
Name: "chart1",
27-
Version: "1.0.0",
28-
Layer: "",
29-
},
30-
{
31-
Name: "chart1",
32-
Version: "1.0.1",
33-
Layer: "",
34-
},
35-
{
36-
Name: "chart2",
37-
Version: "1.0.0",
38-
Layer: "layer-0",
39-
},
40-
{
41-
Name: "chart2",
42-
Version: "1.0.1",
43-
Layer: "",
44-
},
45-
}
46-
47-
if diff := cmp.Diff(expected, charts); diff != "" {
48-
t.Fatal("unexpected diff", diff)
49-
}
50-
}
51-
52-
func TestIndexIsKnownChart(t *testing.T) {
53-
index := makeTestIndex()
54-
cache := NewHelmIndexFileReader(index)
55-
clusterRef := types.NamespacedName{}
56-
repoRef := ObjectReference{}
57-
58-
chart := Chart{
59-
Name: "chart1",
60-
Version: "1.0.0",
61-
}
62-
63-
known, err := cache.IsKnownChart(context.TODO(), clusterRef, repoRef, chart)
64-
if err != nil {
65-
t.Fatal(err)
66-
}
67-
68-
if !known {
69-
t.Fatal("chart should be known")
70-
}
71-
72-
chart = Chart{
73-
Name: "chart1",
74-
Version: "1.0.2",
75-
}
76-
77-
known, err = cache.IsKnownChart(context.TODO(), clusterRef, repoRef, chart)
78-
if err != nil {
79-
t.Fatal(err)
80-
}
81-
82-
if known {
83-
t.Fatal("chart should not be known")
84-
}
85-
}
86-
8712
func TestIndexGetLatestVersion(t *testing.T) {
8813
index := makeTestIndex()
8914
cache := NewHelmIndexFileReader(index)

0 commit comments

Comments
 (0)