/
index_v2.go
162 lines (135 loc) · 3.49 KB
/
index_v2.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package helmutil
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/Masterminds/semver"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/repo"
"k8s.io/helm/pkg/urlutil"
)
type IndexV2 struct {
index *repo.IndexFile
}
func (idx *IndexV2) Add(metadata interface{}, filename, baseURL, digest string) error {
md, ok := metadata.(*chart.Metadata)
if !ok {
return errors.New("metadata is not *chart.Metadata")
}
idx.index.Add(md, filename, baseURL, digest)
return nil
}
func (idx *IndexV2) AddOrReplace(metadata interface{}, filename, baseURL, digest string) error {
// TODO: this looks like a workaround.
// Think how we can rework this in the future.
// Ref: https://github.com/kubernetes/helm/issues/3230
// TODO: this code is the same as for Helm v3, only chart.Medata struct is from Helm v2 SDK.
// We probably should reduce duplicate code .
md, ok := metadata.(*chart.Metadata)
if !ok {
return errors.New("md is not *chart.Metadata")
}
u := filename
if baseURL != "" {
var err error
_, file := filepath.Split(filename)
u, err = urlutil.URLJoin(baseURL, file)
if err != nil {
u = filepath.Join(baseURL, file)
}
}
cr := &repo.ChartVersion{
URLs: []string{u},
Metadata: md,
Digest: digest,
Created: time.Now(),
}
// If no chart with such name exists in the index, just create a new
// list of versions.
entry, ok := idx.index.Entries[md.Name]
if !ok {
idx.index.Entries[md.Name] = repo.ChartVersions{cr}
return nil
}
chartSemVer, err := semver.NewVersion(md.Version)
if err != nil {
return err
}
// If such version exists, replace it.
for i, v := range entry {
itemSemVer, err := semver.NewVersion(v.Version)
if err != nil {
return err
}
if chartSemVer.Equal(itemSemVer) {
idx.index.Entries[md.Name][i] = cr
return nil
}
}
// Otherwise just add to the list of versions
idx.index.Entries[md.Name] = append(entry, cr)
return nil
}
func (idx *IndexV2) Delete(name, version string) (url string, err error) {
for chartName, chartVersions := range idx.index.Entries {
if chartName != name {
continue
}
for i, chartVersion := range chartVersions {
if chartVersion.Version == version {
idx.index.Entries[chartName] = append(
idx.index.Entries[chartName][:i],
idx.index.Entries[chartName][i+1:]...,
)
if len(chartVersion.URLs) > 0 {
return chartVersion.URLs[0], nil
}
return "", nil
}
}
}
return "", fmt.Errorf("chart %s version %s not found in index", name, version)
}
func (idx *IndexV2) Has(name, version string) bool {
return idx.index.Has(name, version)
}
func (idx *IndexV2) SortEntries() {
idx.index.SortEntries()
}
func (idx *IndexV2) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
func (idx *IndexV2) UnmarshalBinary(data []byte) error {
i := &repo.IndexFile{}
if err := yaml.Unmarshal(data, i); err != nil {
return err
}
i.SortEntries()
*idx = IndexV2{index: i}
return nil
}
func (idx *IndexV2) Reader() (io.Reader, error) {
b, err := idx.MarshalBinary()
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func (idx *IndexV2) WriteFile(dest string, mode os.FileMode) error {
return idx.index.WriteFile(dest, mode)
}
func newIndexV2() *IndexV2 {
return &IndexV2{index: repo.NewIndexFile()}
}
func loadIndexV2(fpath string) (*IndexV2, error) {
idx, err := repo.LoadIndexFile(fpath)
if err != nil {
return nil, err
}
return &IndexV2{index: idx}, nil
}