forked from kubesphere/kubesphere
/
repo_index.go
336 lines (287 loc) · 9.25 KB
/
repo_index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Copyright 2020 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package helmrepoindex
import (
"bytes"
"compress/zlib"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strings"
"time"
helmrepo "helm.sh/helm/v3/pkg/repo"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
"kubesphere.io/api/application/v1alpha1"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/utils/idutils"
)
const IndexYaml = "index.yaml"
func LoadRepoIndex(ctx context.Context, u string, cred *v1alpha1.HelmRepoCredential) (*helmrepo.IndexFile, error) {
if !strings.HasSuffix(u, "/") {
u = fmt.Sprintf("%s/%s", u, IndexYaml)
} else {
u = fmt.Sprintf("%s%s", u, IndexYaml)
}
resp, err := loadData(ctx, u, cred)
if err != nil {
return nil, err
}
indexFile, err := loadIndex(resp.Bytes())
if err != nil {
return nil, err
}
return indexFile, nil
}
// loadIndex loads an index file and does minimal validity checking.
//
// This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails.
func loadIndex(data []byte) (*helmrepo.IndexFile, error) {
i := &helmrepo.IndexFile{}
if err := yaml.Unmarshal(data, i); err != nil {
return i, err
}
i.SortEntries()
if i.APIVersion == "" {
return i, helmrepo.ErrNoAPIVersion
}
return i, nil
}
var empty = struct{}{}
// MergeRepoIndex merge new index with index from crd
func MergeRepoIndex(repo *v1alpha1.HelmRepo, index *helmrepo.IndexFile, existsSavedIndex *SavedIndex) *SavedIndex {
saved := &SavedIndex{}
if index == nil {
return existsSavedIndex
}
saved.Applications = make(map[string]*Application)
if existsSavedIndex != nil {
for name := range existsSavedIndex.Applications {
saved.Applications[name] = existsSavedIndex.Applications[name]
}
}
// just copy fields from index
saved.APIVersion = index.APIVersion
saved.Generated = index.Generated
saved.PublicKeys = index.PublicKeys
allAppNames := make(map[string]struct{}, len(index.Entries))
for name, versions := range index.Entries {
if len(versions) == 0 {
continue
}
// add new applications
if application, exists := saved.Applications[name]; !exists {
application = &Application{
Name: name,
Description: versions[0].Description,
Icon: versions[0].Icon,
Created: time.Now(),
}
// The app id will be added to the labels of the helm release.
// But the apps in the repos which are created by the user may contain malformed text, so we generate a random name for them.
// The apps in the system repo have been audited by the admin, so the name of the charts should not include malformed text.
// Then we can add the name string to the labels of the k8s object.
if IsBuiltInRepo(repo.Name) {
application.ApplicationId = fmt.Sprintf("%s%s-%s", v1alpha1.HelmApplicationIdPrefix, repo.Name, name)
} else {
application.ApplicationId = idutils.GetUuid36(v1alpha1.HelmApplicationIdPrefix)
}
charts := make([]*ChartVersion, 0, len(versions))
for ind := range versions {
chart := &ChartVersion{
ApplicationId: application.ApplicationId,
ChartVersion: *versions[ind],
}
chart.ApplicationVersionId = generateAppVersionId(repo, versions[ind].Name, versions[ind].Version)
charts = append(charts, chart)
// Use the creation time of the oldest chart as the creation time of the app.
if versions[ind].Created.Before(application.Created) {
application.Created = versions[ind].Created
}
}
application.Charts = charts
saved.Applications[name] = application
} else {
// update exists applications
savedChartVersion := make(map[string]struct{})
for _, ver := range application.Charts {
savedChartVersion[ver.Version] = struct{}{}
}
charts := application.Charts
var newVersion = make(map[string]struct{}, len(versions))
for _, ver := range versions {
// add new chart version
if _, exists := savedChartVersion[ver.Version]; !exists {
chart := &ChartVersion{
ApplicationId: application.ApplicationId,
ChartVersion: *ver,
}
chart.ApplicationVersionId = generateAppVersionId(repo, ver.Name, ver.Version)
charts = append(charts, chart)
}
newVersion[ver.Version] = empty
}
// delete not exists chart version
for last, curr := 0, 0; curr < len(charts); {
chart := charts[curr]
version := chart.Version
if _, exists := newVersion[version]; !exists {
// version not exists, check next one
curr++
} else {
// If last and curr point to the same place, there is nothing to do, just move to next.
if last != curr {
charts[last] = charts[curr]
}
last++
curr++
}
}
application.Charts = charts[:len(newVersion)]
saved.Applications[name] = application
}
allAppNames[name] = empty
}
for name := range saved.Applications {
if _, exists := allAppNames[name]; !exists {
delete(saved.Applications, name)
}
}
return saved
}
func (i *SavedIndex) GetApplicationVersion(appId, versionId string) *v1alpha1.HelmApplicationVersion {
for _, app := range i.Applications {
if app.ApplicationId == appId {
for _, ver := range app.Charts {
if ver.ApplicationVersionId == versionId {
version := &v1alpha1.HelmApplicationVersion{
ObjectMeta: metav1.ObjectMeta{
Name: versionId,
Labels: map[string]string{
constants.ChartApplicationIdLabelKey: appId,
},
},
Spec: v1alpha1.HelmApplicationVersionSpec{
URLs: ver.URLs,
Digest: ver.Digest,
Metadata: &v1alpha1.Metadata{
Name: ver.Name,
AppVersion: ver.AppVersion,
Version: ver.Version,
Annotations: ver.Annotations,
},
},
}
return version
}
}
}
}
return nil
}
// The app version id will be added to the labels of the helm release.
// But the apps in the repos which are created by the user may contain malformed text, so we generate a random name for them.
// The apps in the system repo have been audited by the admin, so the name of the charts should not include malformed text.
// Then we can add the name string to the labels of the k8s object.
func generateAppVersionId(repo *v1alpha1.HelmRepo, chartName, version string) string {
if IsBuiltInRepo(repo.Name) {
return fmt.Sprintf("%s%s-%s-%s", v1alpha1.HelmApplicationIdPrefix, repo.Name, chartName, version)
} else {
return idutils.GetUuid36(v1alpha1.HelmApplicationVersionIdPrefix)
}
}
// IsBuiltInRepo checks whether a repo is a built-in repo.
// All the built-in repos are located in the workspace system-workspace and the name starts with 'built-in'
// to differentiate from the repos created by the user.
func IsBuiltInRepo(repoName string) bool {
return strings.HasPrefix(repoName, v1alpha1.BuiltinRepoPrefix)
}
type SavedIndex struct {
APIVersion string `json:"apiVersion"`
Generated time.Time `json:"generated"`
Applications map[string]*Application `json:"apps"`
PublicKeys []string `json:"publicKeys,omitempty"`
// Annotations are additional mappings uninterpreted by Helm. They are made available for
// other applications to add information to the index file.
Annotations map[string]string `json:"annotations,omitempty"`
}
func ByteArrayToSavedIndex(data []byte) (*SavedIndex, error) {
ret := &SavedIndex{}
if len(data) == 0 {
return ret, nil
}
enc := base64.URLEncoding
buf := make([]byte, enc.DecodedLen(len(data)))
n, err := enc.Decode(buf, data)
if err != nil {
return nil, err
}
buf = buf[:n]
r, err := zlib.NewReader(bytes.NewBuffer(buf))
if err != nil {
return nil, err
}
r.Close()
b, err := io.ReadAll(r)
if err != nil && err != io.EOF {
return nil, err
}
err = json.Unmarshal(b, ret)
if err != nil {
return nil, err
}
return ret, nil
}
func (i *SavedIndex) Bytes() ([]byte, error) {
d, err := json.Marshal(i)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
w := zlib.NewWriter(buf)
_, err = w.Write(d)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
encSrc := buf.Bytes()
enc := base64.URLEncoding
ret := make([]byte, enc.EncodedLen(len(encSrc)))
enc.Encode(ret, encSrc)
return ret, nil
}
// chart version with app id and app version id
type ChartVersion struct {
// Do not save ApplicationId into crd
ApplicationId string `json:"-"`
ApplicationVersionId string `json:"verId"`
helmrepo.ChartVersion `json:",inline"`
}
type Application struct {
// application name
Name string `json:"name"`
ApplicationId string `json:"appId"`
// chart description
Description string `json:"desc"`
// application status
Status string `json:"status"`
// The URL to an icon file.
Icon string `json:"icon,omitempty"`
Created time.Time `json:"created"`
Charts []*ChartVersion `json:"charts"`
}