-
Notifications
You must be signed in to change notification settings - Fork 85
/
repositories.go
241 lines (206 loc) · 4.95 KB
/
repositories.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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/Masterminds/semver"
"github.com/gin-gonic/gin"
"github.com/gofrs/flock"
"github.com/golang/glog"
"github.com/pkg/errors"
"helm.sh/helm/v3/cmd/helm/search"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/repo"
"sigs.k8s.io/yaml"
)
const searchMaxScore = 25
type repoChartElement struct {
Name string `json:"name"`
Version string `json:"version"`
AppVersion string `json:"app_version"`
Description string `json:"description"`
}
type repoChartList []repoChartElement
func applyConstraint(version string, versions bool, res []*search.Result) ([]*search.Result, error) {
if len(version) == 0 {
return res, nil
}
constraint, err := semver.NewConstraint(version)
if err != nil {
return res, errors.Wrap(err, "an invalid version/constraint format")
}
data := res[:0]
foundNames := map[string]bool{}
for _, r := range res {
if _, found := foundNames[r.Name]; found {
continue
}
v, err := semver.NewVersion(r.Chart.Version)
if err != nil || constraint.Check(v) {
data = append(data, r)
if !versions {
foundNames[r.Name] = true // If user hasn't requested all versions, only show the latest that matches
}
}
}
return data, nil
}
func buildSearchIndex(version string) (*search.Index, error) {
i := search.NewIndex()
for _, re := range helmConfig.HelmRepos {
n := re.Name
f := filepath.Join(settings.RepositoryCache, helmpath.CacheIndexFile(n))
ind, err := repo.LoadIndexFile(f)
if err != nil {
glog.Warningf("WARNING: Repo %q is corrupt or missing. Try 'helm repo update'.", n)
continue
}
i.AddRepo(n, ind, len(version) > 0)
}
return i, nil
}
func initRepos(c *repo.Entry) error {
// Ensure the file directory exists as it is required for file locking
err := os.MkdirAll(filepath.Dir(settings.RepositoryConfig), os.ModePerm)
if err != nil && !os.IsExist(err) {
return err
}
// Acquire a file lock for process synchronization
fileLock := flock.New(strings.Replace(settings.RepositoryConfig, filepath.Ext(settings.RepositoryConfig), ".lock", 1))
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
locked, err := fileLock.TryLockContext(lockCtx, time.Second)
if err == nil && locked {
SafeCloser(fileLock, &err)
}
if err != nil {
return err
}
b, err := os.ReadFile(settings.RepositoryConfig)
if err != nil && !os.IsNotExist(err) {
return err
}
var f repo.File
if err := yaml.Unmarshal(b, &f); err != nil {
return err
}
r, err := repo.NewChartRepository(c, getter.All(settings))
if err != nil {
return err
}
if _, err := r.DownloadIndexFile(); err != nil {
return err
}
f.Update(c)
if err := f.WriteFile(settings.RepositoryConfig, 0644); err != nil {
return err
}
return nil
}
func updateChart(c *repo.Entry) error {
r, err := repo.NewChartRepository(c, getter.All(settings))
if err != nil {
return err
}
_, err = r.DownloadIndexFile()
if err != nil {
return err
}
return nil
}
func updateRepos(c *gin.Context) {
type errRepo struct {
Name string
Err string
}
errRepoList := []errRepo{}
var wg sync.WaitGroup
for _, c := range helmConfig.HelmRepos {
wg.Add(1)
go func(c *repo.Entry) {
defer wg.Done()
err := updateChart(c)
if err != nil {
errRepoList = append(errRepoList, errRepo{
Name: c.Name,
Err: err.Error(),
})
}
}(c)
}
wg.Wait()
if len(errRepoList) > 0 {
respErr(c, fmt.Errorf("error list: %v", errRepoList))
return
}
respOK(c, nil)
}
func listRepos(c *gin.Context) {
type repo struct {
Name string `json:"name"`
URL string `json:"url"`
}
repos := []repo{}
for _, r := range helmConfig.HelmRepos {
repos = append(repos, repo{
r.Name,
r.URL,
})
}
respOK(c, repos)
}
func listRepoCharts(c *gin.Context) {
version := c.Query("version") // chart version
versions := c.Query("versions") // if "true", all versions
keyword := c.Query("keyword") // search keyword
// default stable
if version == "" {
version = ">0.0.0"
}
index, err := buildSearchIndex(version)
if err != nil {
respErr(c, err)
return
}
var res []*search.Result
if keyword == "" {
res = index.All()
} else {
res, err = index.Search(keyword, searchMaxScore, false)
if err != nil {
respErr(c, err)
return
}
}
search.SortScore(res)
var versionsB bool
if versions == "true" {
versionsB = true
}
data, err := applyConstraint(version, versionsB, res)
if err != nil {
respErr(c, err)
return
}
chartList := make(repoChartList, 0, len(data))
for _, v := range data {
chartList = append(chartList, repoChartElement{
Name: v.Name,
Version: v.Chart.Version,
AppVersion: v.Chart.AppVersion,
Description: v.Chart.Description,
})
}
respOK(c, chartList)
}
func SafeCloser(fileLock *flock.Flock, err *error) {
if fileErr := fileLock.Unlock(); fileErr != nil && *err == nil {
*err = fileErr
glog.Error(fileErr)
}
}