-
Notifications
You must be signed in to change notification settings - Fork 28
/
controller.go
340 lines (295 loc) · 9.17 KB
/
controller.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
337
338
339
340
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
)
const (
// ChartPackageFileExtension is the file extension used for chart packages
ChartPackageFileExtension = "tgz"
// ChartFolder the folder relative to the `index.yaml` file where we store charts for a repository
ChartFolder = "files"
// DefaultWritePermissions default permissions for new folders
DefaultWritePermissions = 0760
)
// FileController controller which handles the artifacts files serving and updating
type FileController struct {
config Config
cache Storage
cloudStorage Storage
repositories []Repository
chartsPath string
chartsDir string
chartIndexer *ChartIndexer
operationChannel chan string
}
var (
// defaultChartIndex an empty chart index
defaultChartIndex = `apiVersion: v1
generated: "2019-11-01T17:04:16Z"
entries:`
)
// NewFileController creates a new file controller
func NewFileController(cache Storage, storage Storage, repositories []Repository, config Config) (*FileController, error) {
chartsPath := config.HTTP.ChartPath
ctrl := &FileController{
config: config,
cache: cache,
cloudStorage: storage,
repositories: repositories,
chartsPath: chartsPath,
}
if chartsPath != "" {
ctrl.chartsDir = filepath.Join(config.Cache.BaseDir, chartsPath)
ctrl.operationChannel = make(chan string)
err := os.MkdirAll(ctrl.chartsDir, DefaultWritePermissions)
if err != nil {
return nil, errors.Wrapf(err, "failed to create charts dir %s", ctrl.chartsDir)
}
chartIndexFile := filepath.Join(ctrl.chartsDir, "index.yaml")
err = ctrl.updateCache(chartIndexFile)
if err != nil {
log.Warnf("could not find chart index file %s due to: %s", chartIndexFile, err.Error())
}
ctrl.chartIndexer = &ChartIndexer{BaseCacheDir: config.Cache.BaseDir}
go ctrl.backgroundOperations()
log.Debugf("now triggering a background reindex")
ctrl.operationChannel <- "reindex"
}
return ctrl, nil
}
// GetFile handlers which returns an artifacts file either from the local file cache, cloud storage or
// central repository
func (ctrl *FileController) GetFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filename := ps.ByName("filepath")
log.Debugf("GetFile, filename: %s", filename)
err := ctrl.readFileFromCache(w, filename, r.Method != "HEAD")
if err == nil {
return
}
err = ctrl.updateCache(filename)
if err != nil {
w.WriteHeader(404)
msg := fmt.Sprintf("Error when downloading the file: %s", err)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
err = ctrl.readFileFromCache(w, filename, r.Method != "HEAD")
if err != nil {
w.WriteHeader(404)
msg := fmt.Sprintf("Error when serving the file from cache: %s", err)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
}
// PutFile handler which stores an artifact file either into a local file cache or cloud storage
func (ctrl *FileController) PutFile(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
filename := ps.ByName("filepath")
log.Debugf("PutFile, filename: %s\n", filename)
err := ctrl.writeFileToCache(filename, r.Body)
if err != nil {
msg := fmt.Sprintf("Error when saving the file into cache: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
err = ctrl.updateCloudStorage(filename)
if err != nil {
msg := fmt.Sprintf("Error when saving the file into cloud storage: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
w.WriteHeader(200)
}
// PostChart handler which stores an artifact file either into a local file cache or cloud storage
func (ctrl *FileController) PostChart(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
repo := ps.ByName("repo")
log.Debugf("PostChart, repo: %s\n", repo)
content, err := ioutil.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("Failed to load payload: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
filename, err := ctrl.chartPackageFilenameFromContent(content)
if err != nil {
msg := fmt.Sprintf("Failed to extract chart from payload: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
if path.Base(filename) != filename {
// Name wants to break out of current directory
msg := fmt.Sprintf("%s is improperly formattedd ", filename)
w.WriteHeader(400)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
folder := ctrl.chartsPath
if repo != "" {
folder = filepath.Join(folder, repo)
}
filename = filepath.Join(folder, ChartFolder, filename)
log.Debugf("PostChart, filename: %s\n", filename)
err = ctrl.writeFileToCache(filename, ioutil.NopCloser(bytes.NewReader(content)))
if err != nil {
msg := fmt.Sprintf("Error when saving the file into cache: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
return
}
err = ctrl.updateCloudStorage(filename)
if err != nil {
msg := fmt.Sprintf("Error when saving the file into cloud storage: %s", err)
w.WriteHeader(500)
log.Error(msg)
fmt.Fprint(w, msg)
}
// trigger a reindex
ctrl.operationChannel <- "reindex"
w.WriteHeader(200)
}
func (ctrl *FileController) updateCache(filepath string) error {
file, err := ctrl.downloadFile(filepath)
updateCloud := false
if err != nil {
file, err = ctrl.readFileFromCloudStorage(filepath)
if err != nil {
return fmt.Errorf("reading file from cloud storage: %v", err)
}
updateCloud = true
}
defer file.Close()
err = ctrl.writeFileToCache(filepath, file)
if err != nil {
return fmt.Errorf("writing file to cache: %v", err)
}
if updateCloud && ctrl.cloudStorage != nil {
file, err := ctrl.cache.ReadFile(filepath)
if err != nil {
return fmt.Errorf("reading file from cache: %v", err)
}
defer file.Close()
err = ctrl.writeFileToCloudStorage(filepath, file)
if err != nil {
log.Warnf("Error when storing the file into cloud storage: %s", err)
return nil
}
}
return nil
}
func (ctrl *FileController) updateCloudStorage(filepath string) error {
file, err := ctrl.cache.ReadFile(filepath)
if err != nil {
return fmt.Errorf("reading file from cache: %v", err)
}
defer file.Close()
return ctrl.writeFileToCloudStorage(filepath, file)
}
func (ctrl *FileController) readFileFromCache(w io.Writer, filepath string, writeBody bool) error {
log.Debugf("Read file from cache: %s\n", filepath)
file, err := ctrl.cache.ReadFile(filepath)
if err != nil {
return fmt.Errorf("reading file from cache: %v", err)
}
defer file.Close()
if !writeBody {
return nil
}
_, err = io.Copy(w, file)
if err != nil {
return fmt.Errorf("copying file from cache: %v", err)
}
return nil
}
func (ctrl *FileController) writeFileToCache(filepath string, file io.ReadCloser) error {
log.Debugf("Write file to cache: %s\n", filepath)
return ctrl.cache.WriteFile(filepath, file)
}
func (ctrl *FileController) readFileFromCloudStorage(filepath string) (io.ReadCloser, error) {
if ctrl.cloudStorage != nil {
log.Debugf("Read file from cloud storage: %s", filepath)
return ctrl.cloudStorage.ReadFile(filepath)
}
return nil, errors.New("no cloud storage available")
}
func (ctrl *FileController) writeFileToCloudStorage(filepath string, file io.ReadCloser) error {
if ctrl.cloudStorage != nil {
log.Debugf("Write file to cloud storage: %s", filepath)
return ctrl.cloudStorage.WriteFile(filepath, file)
}
return nil
}
func (ctrl *FileController) downloadFile(filepath string) (io.ReadCloser, error) {
log.Debugf("Read file from repository: %s", filepath)
for _, r := range ctrl.repositories {
log.Debugf("Trying to download from repository: %s", r.BaseURL())
b, err := r.DownloadFile(filepath)
if err == nil {
return b, nil
}
}
return nil, fmt.Errorf("unable to download %s from any configured repository", filepath)
}
func (ctrl *FileController) chartReindex() error {
dir := ctrl.chartsDir
filename := filepath.Join(dir, "index.yaml")
err := ctrl.updateCache(filename)
if err != nil {
if ctrl.cloudStorage == nil {
log.Debugf("no cloud storage so failed to update cache for %s: %s", filename, err.Error())
} else {
log.Errorf("failed to update cache for %s: %s", filename, err.Error())
}
}
err = ctrl.chartIndexer.Reindex(dir, filename, ctrl.cache, ctrl.cloudStorage)
if err != nil {
log.Errorf("failed to reindex the charts: %s", err.Error())
return err
}
return nil
}
// ChartPackageFilenameFromContent returns a chart filename from binary content
func (ctrl *FileController) chartPackageFilenameFromContent(content []byte) (string, error) {
chart, err := chartFromContent(content)
if err != nil {
return "", err
}
meta := chart.Metadata
filename := fmt.Sprintf("%s-%s.%s", meta.Name, meta.Version, ChartPackageFileExtension)
return filename, nil
}
func (ctrl *FileController) backgroundOperations() {
for {
<-ctrl.operationChannel
log.Debugf("reindexing charts")
err := ctrl.chartReindex()
if err != nil {
log.Errorf("failed to reindex charts: %s", err.Error())
}
}
}
func chartFromContent(content []byte) (*chart.Chart, error) {
chart, err := loader.LoadArchive(bytes.NewBuffer(content))
return chart, err
}