-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathEnvConfigOverrideRepository.go
More file actions
289 lines (266 loc) · 11.4 KB
/
EnvConfigOverrideRepository.go
File metadata and controls
289 lines (266 loc) · 11.4 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
/*
* 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 chartConfig
import (
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/devtron-labs/devtron/internal/sql/repository/cluster"
"github.com/go-pg/pg"
"github.com/juju/errors"
)
type EnvConfigOverride struct {
tableName struct{} `sql:"chart_env_config_override" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
ChartId int `sql:"chart_id,notnull"`
TargetEnvironment int `sql:"target_environment,notnull"` //target environment
EnvOverrideValues string `sql:"env_override_yaml,notnull"`
Status models.ChartStatus `sql:"status,notnull"` //new, deployment-in-progress, error, rollbacked, su
ManualReviewed bool `sql:"reviewed,notnull"`
Active bool `sql:"active,notnull"`
Namespace string `sql:"namespace,notnull"`
Chart *Chart
Environment *cluster.Environment `sql:"-"`
Latest bool `sql:"latest,notnull"`
Previous bool `sql:"previous,notnull"`
IsOverride bool `sql:"is_override,notnull"`
models.AuditLog
}
type EnvConfigOverrideRepository interface {
Save(*EnvConfigOverride) error
GetByChartAndEnvironment(chartId, targetEnvironmentId int) (*EnvConfigOverride, error)
ActiveEnvConfigOverride(appId, environmentId int) (*EnvConfigOverride, error) //successful env config
Get(id int) (*EnvConfigOverride, error)
//this api updates only EnvOverrideValues, EnvMergedValues, Status, ManualReviewed, active based on id
UpdateProperties(config *EnvConfigOverride) error
GetByEnvironment(targetEnvironmentId int) ([]EnvConfigOverride, error)
GetEnvConfigByChartId(chartId int) ([]EnvConfigOverride, error)
UpdateEnvConfigStatus(config *EnvConfigOverride) error
Delete(envConfigOverride *EnvConfigOverride) error
FindLatestChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error)
FindChartByAppIdAndEnvIdAndChartRefId(appId, targetEnvironmentId int, chartRefId int) (*EnvConfigOverride, error)
Update(envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error)
FindChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error)
}
type EnvConfigOverrideRepositoryImpl struct {
dbConnection *pg.DB
}
func NewEnvConfigOverrideRepository(dbConnection *pg.DB) *EnvConfigOverrideRepositoryImpl {
return &EnvConfigOverrideRepositoryImpl{dbConnection: dbConnection}
}
func (r EnvConfigOverrideRepositoryImpl) Save(override *EnvConfigOverride) error {
err := r.dbConnection.Insert(override)
return err
}
func (r EnvConfigOverrideRepositoryImpl) ActiveEnvConfigOverride(appId, environmentId int) (*EnvConfigOverride, error) {
var environmentConfig struct {
Id int `sql:"id,pk"`
ChartId int `sql:"chart_id,notnull"`
TargetEnvironment int `sql:"target_environment,notnull"` //target environment
EnvOverrideValues string `sql:"env_override_yaml,notnull"`
Status models.ChartStatus `sql:"status,notnull"` //new, deployment-in-progress, error, rollbacked, su
ManualReviewed bool `sql:"reviewed,notnull"`
Active bool `sql:"active,notnull"`
Namespace string `sql:"namespace"`
ChartName string `sql:"chart_name"`
ChartLocation string `sql:"chart_location"` //location within git repo where current chart is pointing
GlobalOverride string `sql:"global_override"` //json format
ImageDescriptorTemplate string `sql:"image_descriptor_template"`
EnvironmentName string `sql:"environment_name"`
Latest bool `sql:"latest,notnull"`
AppName string `sql:"app_name"`
IsOverride bool `sql:"is_override"`
ChartRefId int `sql:"chart_ref_id,notnull"`
ChartVersion string `sql:"chart_version,notnull"`
GitRepoUrl string `sql:"git_repo_url"`
}
query := "SELECT " +
" ec.id as id, ec.chart_id as chart_id," +
" ec.target_environment as target_environment, ec.env_override_yaml as env_override_yaml, ec.status as status, ec.reviewed as reviewed," +
" ec.active as active, ec.namespace as namespace, ec.latest as latest," +
" ch.chart_name as chart_name," +
" ch.chart_location as chart_location," +
" ch.git_repo_url as git_repo_url, " +
" ch.global_override as global_override, ch.chart_version as chart_version," +
" ch.image_descriptor_template as image_descriptor_template," +
" en.environment_name as environment_name, ec.is_override, ch.chart_ref_id" +
" FROM chart_env_config_override ec" +
" LEFT JOIN charts ch on ec.chart_id=ch.id" +
" LEFT JOIN environment en on en.id=ec.target_environment" +
" WHERE ec.target_environment=? and ec.active = ? and ch.app_id =? and ec.latest = ? AND en.active = TRUE;"
_, err := r.dbConnection.Query(&environmentConfig, query, environmentId, true, appId, true)
if err != nil {
return nil, err
}
chart := &Chart{
ChartName: environmentConfig.ChartName,
ChartLocation: environmentConfig.ChartLocation,
GlobalOverride: environmentConfig.GlobalOverride,
ImageDescriptorTemplate: environmentConfig.ImageDescriptorTemplate,
ChartRefId: environmentConfig.ChartRefId,
ChartVersion: environmentConfig.ChartVersion,
GitRepoUrl: environmentConfig.GitRepoUrl,
}
env := &cluster.Environment{
Name: environmentConfig.EnvironmentName,
}
eco := &EnvConfigOverride{
Id: environmentConfig.Id,
ChartId: environmentConfig.ChartId,
TargetEnvironment: environmentConfig.TargetEnvironment,
EnvOverrideValues: environmentConfig.EnvOverrideValues,
Status: environmentConfig.Status,
ManualReviewed: environmentConfig.ManualReviewed,
Active: environmentConfig.Active,
Namespace: environmentConfig.Namespace,
Chart: chart,
Environment: env,
Latest: environmentConfig.Latest,
IsOverride: environmentConfig.IsOverride,
//AppMetricsOverride: environmentConfig.AppMetricsOverride,
}
return eco, err
}
func (r EnvConfigOverrideRepositoryImpl) GetByChartAndEnvironment(chartId, targetEnvironmentId int) (*EnvConfigOverride, error) {
eco := &EnvConfigOverride{}
err := r.dbConnection.
Model(eco).
Where("env_config_override.target_environment = ?", targetEnvironmentId).
Where("env_config_override.active = ?", true).
Where("Chart.id =? ", chartId).
Column("env_config_override.*", "Chart").
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return eco, err
}
func (r EnvConfigOverrideRepositoryImpl) Get(id int) (*EnvConfigOverride, error) {
eco := &EnvConfigOverride{}
err := r.dbConnection.
Model(eco).
Where("env_config_override.id = ?", id).
Column("env_config_override.*", "Chart").
Select()
return eco, err
}
//this api updates only EnvOverrideValues, EnvMergedValues, Status, ManualReviewed, active
// based on id
func (r EnvConfigOverrideRepositoryImpl) UpdateProperties(config *EnvConfigOverride) error {
_, err := r.dbConnection.Model(config).
Set("env_override_yaml = ?", config.EnvOverrideValues).
Set("status =?", config.Status).
Set("reviewed =?", config.ManualReviewed).
Set("active =?", config.Active).
Set("updated_by =?", config.UpdatedBy).
Set("updated_on =? ", config.UpdatedOn).
Set("previous =?", config.Previous).
Set("is_override =?", config.IsOverride).
Set("namespace =?", config.Namespace).
Set("latest =?", config.Latest).
//Set("app_metrics_override =?", config.AppMetricsOverride).
WherePK().
Update()
return err
}
func (r EnvConfigOverrideRepositoryImpl) GetByEnvironment(targetEnvironmentId int) ([]EnvConfigOverride, error) {
var envConfigs []EnvConfigOverride
err := r.dbConnection.
Model(&envConfigs).
Where("env_config_override.target_environment = ?", targetEnvironmentId).
Where("env_config_override.active = ?", true).
Column("env_config_override.*").
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return envConfigs, err
}
func (r EnvConfigOverrideRepositoryImpl) GetEnvConfigByChartId(chartId int) ([]EnvConfigOverride, error) {
var envConfigs []EnvConfigOverride
err := r.dbConnection.
Model(&envConfigs).
Where("chart_id = ?", chartId).
Where("active = ?", true).
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return envConfigs, err
}
func (r EnvConfigOverrideRepositoryImpl) UpdateEnvConfigStatus(config *EnvConfigOverride) error {
_, err := r.dbConnection.Model(config).
Set("latest =?", config.Latest).
Set("status =?", config.Status).
Set("reviewed =?", config.ManualReviewed).
Set("active =?", config.Active).
Set("updated_by =?", config.UpdatedBy).
Set("updated_on =? ", config.UpdatedOn).
Set("previous =?", config.Previous).
WherePK().
Update()
return err
}
func (r EnvConfigOverrideRepositoryImpl) Delete(envConfigOverride *EnvConfigOverride) error {
err := r.dbConnection.Delete(envConfigOverride)
return err
}
func (r EnvConfigOverrideRepositoryImpl) FindLatestChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error) {
eco := &EnvConfigOverride{}
err := r.dbConnection.
Model(eco).
Where("env_config_override.target_environment = ?", targetEnvironmentId).
Where("env_config_override.latest = ?", true).
Where("Chart.app_id =? ", appId).
Column("env_config_override.*", "Chart").
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return eco, err
}
func (r EnvConfigOverrideRepositoryImpl) FindChartByAppIdAndEnvIdAndChartRefId(appId, targetEnvironmentId int, chartRefId int) (*EnvConfigOverride, error) {
eco := &EnvConfigOverride{}
err := r.dbConnection.
Model(eco).
Where("env_config_override.target_environment = ?", targetEnvironmentId).
//Where("env_config_override.latest = ?", true).
Where("Chart.app_id =? ", appId).
Where("Chart.chart_ref_id =? ", chartRefId).
Column("env_config_override.*", "Chart").
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return eco, err
}
func (r EnvConfigOverrideRepositoryImpl) Update(envConfigOverride *EnvConfigOverride) (*EnvConfigOverride, error) {
err := r.dbConnection.Update(envConfigOverride)
return envConfigOverride, err
}
func (r EnvConfigOverrideRepositoryImpl) FindChartForAppByAppIdAndEnvId(appId, targetEnvironmentId int) (*EnvConfigOverride, error) {
eco := &EnvConfigOverride{}
err := r.dbConnection.
Model(eco).
Where("env_config_override.target_environment = ?", targetEnvironmentId).
Where("env_config_override.active = ?", true).
Where("Chart.app_id =? ", appId).
Column("env_config_override.*", "Chart").
Select()
if pg.ErrNoRows == err {
return nil, errors.NotFoundf(err.Error())
}
return eco, err
}