-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathChartRepoRepository.go
More file actions
396 lines (353 loc) · 13 KB
/
ChartRepoRepository.go
File metadata and controls
396 lines (353 loc) · 13 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (c) 2020 Devtron Labs
*
* 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 chartRepoRepository
import (
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"strings"
)
type Chart struct {
tableName struct{} `sql:"charts" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
AppId int `sql:"app_id"`
ChartRepoId int `sql:"chart_repo_id"`
ChartName string `sql:"chart_name"` //use composite key as unique id
ChartVersion string `sql:"chart_version"`
ChartRepo string `sql:"chart_repo"`
ChartRepoUrl string `sql:"chart_repo_url"`
Values string `sql:"values_yaml"` //json format // used at for release. this should be always updated
GlobalOverride string `sql:"global_override"` //json format // global overrides visible to user only
ReleaseOverride string `sql:"release_override"` //json format //image descriptor template used for injecting tigger metadata injection
PipelineOverride string `sql:"pipeline_override"` //json format // pipeline values -> strategy values
Status models.ChartStatus `sql:"status"` //(new , deployment-in-progress, deployed-To-production, error )
Active bool `sql:"active"`
GitRepoUrl string `sql:"git_repo_url"` //git repository where chart is stored
ChartLocation string `sql:"chart_location"` //location within git repo where current chart is pointing
ReferenceTemplate string `sql:"reference_template"`
ImageDescriptorTemplate string `sql:"image_descriptor_template"`
ChartRefId int `sql:"chart_ref_id"`
Latest bool `sql:"latest,notnull"`
Previous bool `sql:"previous,notnull"`
ReferenceChart []byte `sql:"reference_chart"`
sql.AuditLog
}
type ChartRepository interface {
//ChartReleasedToProduction(chartRepo, appName, chartVersion string) (bool, error)
FindOne(chartRepo, appName, chartVersion string) (*Chart, error)
Save(*Chart) error
FindCurrentChartVersion(chartRepo, chartName, chartVersionPattern string) (string, error)
FindActiveChart(appId int) (chart *Chart, err error)
FindLatestByAppId(appId int) (chart *Chart, err error)
FindById(id int) (chart *Chart, err error)
Update(chart *Chart) error
FindActiveChartsByAppId(appId int) (charts []*Chart, err error)
FindLatestChartForAppByAppId(appId int) (chart *Chart, err error)
FindChartByAppIdAndRefId(appId int, chartRefId int) (chart *Chart, err error)
FindNoLatestChartForAppByAppId(appId int) ([]*Chart, error)
FindPreviousChartByAppId(appId int) (chart *Chart, err error)
FindNumberOfAppsWithDeploymentTemplate(appIds []int) (int, error)
FindChartByGitRepoUrl(gitRepoUrl string) (*Chart, error)
}
func NewChartRepository(dbConnection *pg.DB) *ChartRepositoryImpl {
return &ChartRepositoryImpl{dbConnection: dbConnection}
}
type ChartRepositoryImpl struct {
dbConnection *pg.DB
}
func (repositoryImpl ChartRepositoryImpl) FindOne(chartRepo, chartName, chartVersion string) (*Chart, error) {
chart := &Chart{}
err := repositoryImpl.dbConnection.
Model(chart).
Where("chart_name= ?", chartName).
Where("chart_version = ?", chartVersion).
Where("chart_repo = ? ", chartRepo).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindCurrentChartVersion(chartRepo, chartName, chartVersionPattern string) (string, error) {
chart := &Chart{}
err := repositoryImpl.dbConnection.
Model(chart).
Where("chart_name= ?", chartName).
Where("chart_version like ?", chartVersionPattern+"%").
Where("chart_repo = ? ", chartRepo).
Order("id Desc").
Limit(1).
Select()
return chart.ChartVersion, err
}
//Deprecated
func (repositoryImpl ChartRepositoryImpl) FindActiveChart(appId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Where("active =?", true).
Select()
return chart, err
}
//Deprecated
func (repositoryImpl ChartRepositoryImpl) FindLatestByAppId(appId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindActiveChartsByAppId(appId int) (charts []*Chart, err error) {
var activeCharts []*Chart
err = repositoryImpl.dbConnection.
Model(&activeCharts).
Where("app_id= ?", appId).
Where("active= ?", true).
Select()
return activeCharts, err
}
func (repositoryImpl ChartRepositoryImpl) FindLatestChartForAppByAppId(appId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Where("latest= ?", true).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindChartByAppIdAndRefId(appId int, chartRefId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Where("chart_ref_id= ?", chartRefId).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindNoLatestChartForAppByAppId(appId int) ([]*Chart, error) {
var charts []*Chart
err := repositoryImpl.dbConnection.
Model(&charts).
Where("app_id= ?", appId).
Where("latest= ?", false).
Select()
return charts, err
}
func (repositoryImpl ChartRepositoryImpl) FindLatestChartForAppByAppIdAndEnvId(appId int, envId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Where("latest= ?", true).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindPreviousChartByAppId(appId int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.
Model(chart).
Where("app_id= ?", appId).
Where("previous= ?", true).
Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) Save(chart *Chart) error {
return repositoryImpl.dbConnection.Insert(chart)
}
func (repositoryImpl ChartRepositoryImpl) Update(chart *Chart) error {
_, err := repositoryImpl.dbConnection.Model(chart).WherePK().UpdateNotNull()
return err
}
func (repositoryImpl ChartRepositoryImpl) FindById(id int) (chart *Chart, err error) {
chart = &Chart{}
err = repositoryImpl.dbConnection.Model(chart).
Where("id = ?", id).Select()
return chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindChartByGitRepoUrl(gitRepoUrl string) (*Chart, error) {
var chart Chart
err := repositoryImpl.dbConnection.Model(&chart).
Join("INNER JOIN app ON app.id=app_id").
Where("app.active = ?", true).
Where("chart.git_repo_url = ?", gitRepoUrl).
Where("chart.active = ?", true).
Limit(1).
Select()
return &chart, err
}
func (repositoryImpl ChartRepositoryImpl) FindNumberOfAppsWithDeploymentTemplate(appIds []int) (int, error) {
var charts []*Chart
count, err := repositoryImpl.dbConnection.
Model(&charts).
ColumnExpr("DISTINCT app_id").
Where("app_id in (?)", pg.In(appIds)).
Count()
if err != nil {
return 0, err
}
return count, nil
}
//---------------------------chart repository------------------
type ChartRepo struct {
tableName struct{} `sql:"chart_repo"`
Id int `sql:"id,pk"`
Name string `sql:"name"`
Url string `sql:"url"`
Active bool `sql:"active,notnull"`
Default bool `sql:"is_default,notnull"`
UserName string `sql:"user_name"`
Password string `sql:"password"`
SshKey string `sql:"ssh_key"`
AccessToken string `sql:"access_token"`
AuthMode repository.AuthMode `sql:"auth_mode,notnull"`
External bool `sql:"external,notnull"`
Deleted bool `sql:"deleted,notnull"`
sql.AuditLog
}
type ChartRepoRepository interface {
Save(chartRepo *ChartRepo, tx *pg.Tx) error
Update(chartRepo *ChartRepo, tx *pg.Tx) error
GetDefault() (*ChartRepo, error)
FindById(id int) (*ChartRepo, error)
FindAll() ([]*ChartRepo, error)
GetConnection() *pg.DB
MarkChartRepoDeleted(chartRepo *ChartRepo, tx *pg.Tx) error
}
type ChartRepoRepositoryImpl struct {
dbConnection *pg.DB
}
func NewChartRepoRepositoryImpl(dbConnection *pg.DB) *ChartRepoRepositoryImpl {
return &ChartRepoRepositoryImpl{
dbConnection: dbConnection,
}
}
func (impl ChartRepoRepositoryImpl) GetConnection() *pg.DB {
return impl.dbConnection
}
func (impl ChartRepoRepositoryImpl) Save(chartRepo *ChartRepo, tx *pg.Tx) error {
return tx.Insert(chartRepo)
}
func (impl ChartRepoRepositoryImpl) Update(chartRepo *ChartRepo, tx *pg.Tx) error {
return tx.Update(chartRepo)
}
func (impl ChartRepoRepositoryImpl) GetDefault() (*ChartRepo, error) {
repo := &ChartRepo{}
err := impl.dbConnection.Model(repo).
Where("is_default = ?", true).
Where("active = ?", true).
Where("deleted = ?", false).
Select()
return repo, err
}
func (impl ChartRepoRepositoryImpl) FindById(id int) (*ChartRepo, error) {
repo := &ChartRepo{}
err := impl.dbConnection.Model(repo).
Where("id = ?", id).
Where("deleted = ?", false).
Select()
return repo, err
}
func (impl ChartRepoRepositoryImpl) FindAll() ([]*ChartRepo, error) {
var repo []*ChartRepo
err := impl.dbConnection.Model(&repo).
Where("deleted = ?", false).
Select()
return repo, err
}
func (impl ChartRepoRepositoryImpl) MarkChartRepoDeleted(chartRepo *ChartRepo, tx *pg.Tx) error {
chartRepo.Deleted = true
return tx.Update(chartRepo)
}
// ------------------------ CHART REF REPOSITORY ---------------
type RefChartDir string
type ChartRef struct {
tableName struct{} `sql:"chart_ref" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Location string `sql:"location"`
Version string `sql:"version"`
Active bool `sql:"active,notnull"`
Default bool `sql:"is_default,notnull"`
Name string `sql:"name"`
ChartData []byte `sql:"chart_data"`
ChartDescription string `sql:"chart_description"`
UserUploaded bool `sql:"user_uploaded,notnull"`
sql.AuditLog
}
type ChartRefRepository interface {
Save(chartRepo *ChartRef) error
GetDefault() (*ChartRef, error)
FindById(id int) (*ChartRef, error)
GetAll() ([]*ChartRef, error)
CheckIfDataExists(name string, version string) (bool, error)
FetchChart(name string) ([]*ChartRef, error)
FetchChartInfoByUploadFlag(userUploaded bool) ([]*ChartRef, error)
}
type ChartRefRepositoryImpl struct {
dbConnection *pg.DB
}
func NewChartRefRepositoryImpl(dbConnection *pg.DB) *ChartRefRepositoryImpl {
return &ChartRefRepositoryImpl{
dbConnection: dbConnection,
}
}
func (impl ChartRefRepositoryImpl) Save(chartRepo *ChartRef) error {
return impl.dbConnection.Insert(chartRepo)
}
func (impl ChartRefRepositoryImpl) GetDefault() (*ChartRef, error) {
repo := &ChartRef{}
err := impl.dbConnection.Model(repo).
Where("is_default = ?", true).
Where("active = ?", true).Select()
return repo, err
}
func (impl ChartRefRepositoryImpl) FindById(id int) (*ChartRef, error) {
repo := &ChartRef{}
err := impl.dbConnection.Model(repo).
Where("id = ?", id).
Where("active = ?", true).Select()
return repo, err
}
func (impl ChartRefRepositoryImpl) GetAll() ([]*ChartRef, error) {
var chartRefs []*ChartRef
err := impl.dbConnection.Model(&chartRefs).
Where("active = ?", true).Select()
return chartRefs, err
}
func (impl ChartRefRepositoryImpl) CheckIfDataExists(name string, version string) (bool, error) {
repo := &ChartRef{}
return impl.dbConnection.Model(repo).
Where("lower(name) = ?", strings.ToLower(name)).
Where("version = ? ", version).Exists()
}
func (impl ChartRefRepositoryImpl) FetchChart(name string) ([]*ChartRef, error) {
var chartRefs []*ChartRef
err := impl.dbConnection.Model(&chartRefs).Where("lower(name) = ?", strings.ToLower(name)).Select()
if err != nil {
return nil, err
}
return chartRefs, err
}
func (impl ChartRefRepositoryImpl) FetchChartInfoByUploadFlag(userUploaded bool) ([]*ChartRef, error) {
var repo []*ChartRef
err := impl.dbConnection.Model(&repo).
Where("user_uploaded = ?", userUploaded).
Where("active = ?", true).Select()
if err != nil {
return repo, err
}
return repo, err
}