forked from kyma-project/kyma
/
populator.go
126 lines (107 loc) · 3.77 KB
/
populator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package ybundle
import (
"io"
"io/ioutil"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/helm/pkg/proto/hapi/chart"
"github.com/kyma-project/kyma/components/helm-broker/internal"
)
type (
// BundleName represents name of the Bundle
BundleName string
// BundleVersion represents version of the Bundle
BundleVersion string
)
type indexDTO struct {
Entries map[BundleName][]entryDTO `yaml:"entries"`
}
type entryDTO struct {
Name BundleName `yaml:"name"`
Description string `yaml:"description"`
Version BundleVersion `yaml:"version"`
}
// Populator is responsible for populating bundles and charts into storage.
// Source data is provided by bundleLoader.
type Populator struct {
repo repository
bundleLoader bundleLoader
bundleInterface bundleUpserter
chartInterface chartUpserter
log *logrus.Entry
}
// NewPopulator creates new instance of Populator.
func NewPopulator(p repository, bundleLoader bundleLoader, bundleInterface bundleUpserter, chartInterface chartUpserter, log *logrus.Entry) *Populator {
return &Populator{
repo: p,
bundleLoader: bundleLoader,
bundleInterface: bundleInterface,
chartInterface: chartInterface,
log: log.WithField("service", "populator"),
}
}
// Init triggers population process.
func (b *Populator) Init() error {
idx, err := b.getIndex()
if err != nil {
return err
}
for entryName, versions := range idx.Entries {
for _, v := range versions {
bundleReader, bundleCloser, err := b.repo.BundleReader(entryName, v.Version)
if err != nil {
return errors.Wrapf(err, "while reading bundle archive for name [%s] and version [%v]", entryName, v.Version)
}
defer bundleCloser()
bundle, charts, err := b.bundleLoader.Load(bundleReader)
if err != nil {
return errors.Wrapf(err, "while loading bundle and charts for bundle [%s] and version [%s]", entryName, v.Version)
}
for _, ch := range charts {
if _, err := b.chartInterface.Upsert(ch); err != nil {
return errors.Wrapf(err, "while storing chart [%s] for bundle [%s] with version [%s]", ch.String(), entryName, v.Version)
}
}
if _, err := b.bundleInterface.Upsert(bundle); err != nil {
return errors.Wrapf(err, "while storing bundle [%s] with version [%s]", entryName, v.Version)
}
b.log.Infof("Bundle with name [%s] and version [%s] successfully stored", entryName, v.Version)
}
}
return nil
}
func (b *Populator) getIndex() (*indexDTO, error) {
idxReader, idxCloser, err := b.repo.IndexReader()
if err != nil {
return nil, errors.Wrap(err, "while getting index.yaml")
}
defer idxCloser()
bytes, err := ioutil.ReadAll(idxReader)
if err != nil {
return nil, errors.Wrap(err, "while reading all index.yaml")
}
idx := indexDTO{}
if err = yaml.Unmarshal(bytes, &idx); err != nil {
return nil, errors.Wrap(err, "while unmarshaling index")
}
return &idx, nil
}
// be aware that after regenerating mocks, manual steps are required
//go:generate mockery -name=repository -output=automock -outpkg=automock -case=underscore
type repository interface {
IndexReader() (r io.Reader, closer func(), err error)
BundleReader(name BundleName, version BundleVersion) (r io.Reader, closer func(), err error)
}
//go:generate mockery -name=bundleUpserter -output=automock -outpkg=automock -case=underscore
type bundleUpserter interface {
Upsert(*internal.Bundle) (replace bool, err error)
}
//go:generate mockery -name=chartUpserter -output=automock -outpkg=automock -case=underscore
type chartUpserter interface {
Upsert(c *chart.Chart) (replace bool, err error)
}
//go:generate mockery -name=bundleLoader -output=automock -outpkg=automock -case=underscore
type bundleLoader interface {
Load(io.Reader) (*internal.Bundle, []*chart.Chart, error)
}