Skip to content

Commit 822fa30

Browse files
authored
chore: infra configuration refactored (#6286)
* chore: oss ent restructuring * chore: oss ent restructuring * updated wire_gen file * duplicate declaration of pg_query_duration_seconds * chore: oss ent restructuring * chore: oss ent restructuring * upadted licensing * updated licensing * chore: oss ent restructuring * chore: oss ent restructuring * chore: oss ent restructuring * removed dead code * fix: update validations * updated missing key logic * updated errors * feat: mark runner failed with msg * updated profile bean * updated cm/cs name for conflict * updated migration number
1 parent 45e9738 commit 822fa30

File tree

89 files changed

+6850
-1937
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+6850
-1937
lines changed

Wire.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
"github.com/devtron-labs/devtron/api/externalLink"
4444
fluxApplication "github.com/devtron-labs/devtron/api/fluxApplication"
4545
client "github.com/devtron-labs/devtron/api/helm-app"
46-
"github.com/devtron-labs/devtron/api/infraConfig"
4746
"github.com/devtron-labs/devtron/api/k8s"
4847
"github.com/devtron-labs/devtron/api/module"
4948
"github.com/devtron-labs/devtron/api/resourceScan"
@@ -82,7 +81,7 @@ import (
8281
"github.com/devtron-labs/devtron/client/argocdServer/bean"
8382
"github.com/devtron-labs/devtron/client/argocdServer/certificate"
8483
cluster2 "github.com/devtron-labs/devtron/client/argocdServer/cluster"
85-
"github.com/devtron-labs/devtron/client/argocdServer/config"
84+
acdConfig "github.com/devtron-labs/devtron/client/argocdServer/config"
8685
"github.com/devtron-labs/devtron/client/argocdServer/connection"
8786
"github.com/devtron-labs/devtron/client/argocdServer/repoCredsK8sClient"
8887
repocreds "github.com/devtron-labs/devtron/client/argocdServer/repocreds"
@@ -131,7 +130,8 @@ import (
131130
"github.com/devtron-labs/devtron/pkg/chart/gitOpsConfig"
132131
chartRepoRepository "github.com/devtron-labs/devtron/pkg/chartRepo/repository"
133132
"github.com/devtron-labs/devtron/pkg/commonService"
134-
"github.com/devtron-labs/devtron/pkg/configDiff"
133+
"github.com/devtron-labs/devtron/pkg/config"
134+
"github.com/devtron-labs/devtron/pkg/config/configDiff"
135135
delete2 "github.com/devtron-labs/devtron/pkg/delete"
136136
deployment2 "github.com/devtron-labs/devtron/pkg/deployment"
137137
"github.com/devtron-labs/devtron/pkg/deployment/common"
@@ -145,17 +145,14 @@ import (
145145
"github.com/devtron-labs/devtron/pkg/generateManifest"
146146
"github.com/devtron-labs/devtron/pkg/gitops"
147147
"github.com/devtron-labs/devtron/pkg/imageDigestPolicy"
148-
repository11 "github.com/devtron-labs/devtron/pkg/infraConfig/repository"
149-
infraConfigService "github.com/devtron-labs/devtron/pkg/infraConfig/service"
150-
"github.com/devtron-labs/devtron/pkg/infraConfig/units"
148+
"github.com/devtron-labs/devtron/pkg/infraConfig"
151149
"github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs"
152150
repository7 "github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs/repository"
153151
"github.com/devtron-labs/devtron/pkg/notifier"
154152
"github.com/devtron-labs/devtron/pkg/pipeline"
155153
"github.com/devtron-labs/devtron/pkg/pipeline/executors"
156154
history3 "github.com/devtron-labs/devtron/pkg/pipeline/history"
157155
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
158-
"github.com/devtron-labs/devtron/pkg/pipeline/infraProviders"
159156
repository5 "github.com/devtron-labs/devtron/pkg/pipeline/repository"
160157
"github.com/devtron-labs/devtron/pkg/pipeline/types"
161158
"github.com/devtron-labs/devtron/pkg/plugin"
@@ -280,20 +277,6 @@ func InitializeApp() (*App, error) {
280277
wire.Bind(new(dashboardEvent.DashboardTelemetryRouter),
281278
new(*dashboardEvent.DashboardTelemetryRouterImpl)),
282279

283-
repository11.NewInfraProfileRepositoryImpl,
284-
wire.Bind(new(repository11.InfraConfigRepository), new(*repository11.InfraConfigRepositoryImpl)),
285-
286-
units.NewUnits,
287-
infraConfigService.NewInfraConfigServiceImpl,
288-
wire.Bind(new(infraConfigService.InfraConfigService), new(*infraConfigService.InfraConfigServiceImpl)),
289-
infraProviders.NewInfraProviderImpl,
290-
wire.Bind(new(infraProviders.InfraProvider), new(*infraProviders.InfraProviderImpl)),
291-
infraConfig.NewInfraConfigRestHandlerImpl,
292-
wire.Bind(new(infraConfig.InfraConfigRestHandler), new(*infraConfig.InfraConfigRestHandlerImpl)),
293-
294-
infraConfig.NewInfraProfileRouterImpl,
295-
wire.Bind(new(infraConfig.InfraConfigRouter), new(*infraConfig.InfraConfigRouterImpl)),
296-
297280
router.NewMuxRouter,
298281

299282
app2.NewAppRepositoryImpl,
@@ -528,6 +511,10 @@ func InitializeApp() (*App, error) {
528511
chartConfig.NewConfigMapRepositoryImpl,
529512
wire.Bind(new(chartConfig.ConfigMapRepository), new(*chartConfig.ConfigMapRepositoryImpl)),
530513

514+
config.WireSet,
515+
516+
infraConfig.WireSet,
517+
531518
notifier.NewSESNotificationServiceImpl,
532519
wire.Bind(new(notifier.SESNotificationService), new(*notifier.SESNotificationServiceImpl)),
533520

@@ -962,8 +949,8 @@ func InitializeApp() (*App, error) {
962949
dbMigration.NewDbMigrationServiceImpl,
963950
wire.Bind(new(dbMigration.DbMigration), new(*dbMigration.DbMigrationServiceImpl)),
964951

965-
config.NewArgoCDConfigGetter,
966-
wire.Bind(new(config.ArgoCDConfigGetter), new(*config.ArgoCDConfigGetterImpl)),
952+
acdConfig.NewArgoCDConfigGetter,
953+
wire.Bind(new(acdConfig.ArgoCDConfigGetter), new(*acdConfig.ArgoCDConfigGetterImpl)),
967954
)
968955
return &App{}, nil
969956
}

api/infraConfig/restHandler.go

Lines changed: 94 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ package infraConfig
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"github.com/devtron-labs/devtron/api/restHandler/common"
2223
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
2324
"github.com/devtron-labs/devtron/pkg/auth/user"
2425
"github.com/devtron-labs/devtron/pkg/infraConfig/adapter"
25-
"github.com/devtron-labs/devtron/pkg/infraConfig/bean"
26+
"github.com/devtron-labs/devtron/pkg/infraConfig/bean/v0"
27+
"github.com/devtron-labs/devtron/pkg/infraConfig/bean/v1"
28+
errors2 "github.com/devtron-labs/devtron/pkg/infraConfig/errors"
2629
"github.com/devtron-labs/devtron/pkg/infraConfig/service"
27-
util2 "github.com/devtron-labs/devtron/pkg/infraConfig/util"
30+
"github.com/devtron-labs/devtron/pkg/infraConfig/util"
2831
"github.com/devtron-labs/devtron/util/rbac"
32+
"github.com/go-pg/pg"
2933
"github.com/gorilla/mux"
3034
"github.com/pkg/errors"
3135
"go.uber.org/zap"
@@ -42,13 +46,15 @@ type InfraConfigRestHandler interface {
4246
GetProfileV0(w http.ResponseWriter, r *http.Request)
4347
// Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
4448
UpdateInfraProfileV0(w http.ResponseWriter, r *http.Request)
49+
50+
InfraConfigRestHandlerEnt
4551
}
52+
4653
type InfraConfigRestHandlerImpl struct {
4754
logger *zap.SugaredLogger
4855
infraProfileService service.InfraConfigService
4956
userService user.UserService
5057
enforcer casbin.Enforcer
51-
enforcerUtil rbac.EnforcerUtil
5258
validator *validator.Validate
5359
}
5460

@@ -58,7 +64,6 @@ func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileServic
5864
infraProfileService: infraProfileService,
5965
userService: userService,
6066
enforcer: enforcer,
61-
enforcerUtil: enforcerUtil,
6267
validator: validator,
6368
}
6469
}
@@ -76,29 +81,48 @@ func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *
7681
}
7782

7883
identifier := r.URL.Query().Get("name")
79-
8084
if len(identifier) == 0 {
81-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
85+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
8286
return
8387
}
8488
profileName := strings.ToLower(identifier)
85-
var profile *bean.ProfileBeanDto
86-
if profileName != bean.GLOBAL_PROFILE_NAME {
87-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
88-
return
89+
var profile *v1.ProfileBeanDto
90+
if profileName != v1.GLOBAL_PROFILE_NAME {
91+
profile, err = handler.infraProfileService.GetProfileByName(profileName)
92+
if err != nil {
93+
statusCode := http.StatusInternalServerError
94+
if errors.Is(err, pg.ErrNoRows) {
95+
err = errors.New(fmt.Sprintf("profile %s not found", profileName))
96+
statusCode = http.StatusNotFound
97+
}
98+
common.WriteJsonResp(w, err, nil, statusCode)
99+
return
100+
}
89101
}
90-
defaultProfile, err := handler.infraProfileService.GetProfileByName(profileName)
102+
103+
defaultProfile, err := handler.infraProfileService.GetProfileByName(v1.GLOBAL_PROFILE_NAME)
91104
if err != nil {
92-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
105+
statusCode := http.StatusInternalServerError
106+
if errors.Is(err, pg.ErrNoRows) {
107+
err = errors.New(fmt.Sprintf("profile %s not found", v1.GLOBAL_PROFILE_NAME))
108+
statusCode = http.StatusNotFound
109+
}
110+
common.WriteJsonResp(w, err, nil, statusCode)
93111
return
94112
}
95-
profile = defaultProfile
96-
97-
resp := bean.ProfileResponse{
113+
if profileName == v1.GLOBAL_PROFILE_NAME {
114+
profile = defaultProfile
115+
}
116+
resp := v1.ProfileResponse{
98117
Profile: *profile,
99118
}
100-
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits()
101-
resp.DefaultConfigurations = defaultProfile.Configurations
119+
resp.ConfigurationUnits, err = handler.infraProfileService.GetConfigurationUnits()
120+
if err != nil {
121+
handler.logger.Errorw("error in getting configuration units", "err", err)
122+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
123+
return
124+
}
125+
resp.DefaultConfigurations = defaultProfile.GetConfigurations()
102126
common.WriteJsonResp(w, nil, resp, http.StatusOK)
103127
}
104128

@@ -117,28 +141,26 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
117141
//vars := mux.Vars(r)
118142
val := r.URL.Query().Get("name")
119143
if len(val) == 0 {
120-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
144+
common.WriteJsonResp(w, errors.New("name is required"), nil, http.StatusBadRequest)
121145
return
122146
}
123147
profileName := strings.ToLower(val)
124-
125-
payload := &bean.ProfileBeanDto{}
148+
if profileName == "" {
149+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
150+
return
151+
}
152+
payload := &v1.ProfileBeanDto{}
126153
decoder := json.NewDecoder(r.Body)
127154
err = decoder.Decode(payload)
128155
if err != nil {
129156
handler.logger.Errorw("error in decoding the request payload", "err", err, "requestBody", r.Body)
130157
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
131158
return
132159
}
133-
//handler.validator.RegisterValidation("validateValue", ValidateValue)
134-
payload.Name = strings.ToLower(payload.Name)
135160
err = handler.validator.Struct(payload)
136161
if err != nil {
137-
err = errors.Wrap(err, bean.PayloadValidationError)
162+
err = errors.Wrap(err, errors2.PayloadValidationError)
138163
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
139-
}
140-
if !util2.IsValidProfileNameRequested(profileName, payload.Name) {
141-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
142164
return
143165
}
144166
err = handler.infraProfileService.UpdateProfile(userId, profileName, payload)
@@ -150,7 +172,7 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
150172
common.WriteJsonResp(w, nil, nil, http.StatusOK)
151173
}
152174

153-
// Deprecated
175+
// Deprecated: GetProfileV0 is deprecated in favour of GetProfile
154176
func (handler *InfraConfigRestHandlerImpl) GetProfileV0(w http.ResponseWriter, r *http.Request) {
155177
userId, err := handler.userService.GetLoggedInUser(r)
156178
if userId == 0 || err != nil {
@@ -166,34 +188,53 @@ func (handler *InfraConfigRestHandlerImpl) GetProfileV0(w http.ResponseWriter, r
166188
vars := mux.Vars(r)
167189
profileName := strings.ToLower(vars["name"])
168190
if profileName == "" {
169-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
191+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
170192
return
171193
}
172194

173-
if profileName != bean.DEFAULT_PROFILE_NAME {
174-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
175-
return
195+
var profileV0 *v0.ProfileBeanV0
196+
if profileName != v1.DEFAULT_PROFILE_NAME {
197+
profileV1, err := handler.infraProfileService.GetProfileByName(profileName)
198+
profileV0 = adapter.GetV0ProfileBean(profileV1)
199+
if err != nil {
200+
statusCode := http.StatusInternalServerError
201+
if errors.Is(err, pg.ErrNoRows) {
202+
err = errors.New(fmt.Sprintf("profile %s not found", profileName))
203+
statusCode = http.StatusNotFound
204+
}
205+
common.WriteJsonResp(w, err, nil, statusCode)
206+
return
207+
}
176208
}
177209

178-
profileName = bean.GLOBAL_PROFILE_NAME
179-
180-
var profile *bean.ProfileBeanV0
181-
defaultProfileV1, err := handler.infraProfileService.GetProfileByName(profileName)
182-
defaultProfileV0 := adapter.GetV0ProfileBean(defaultProfileV1)
210+
defaultProfileV1, err := handler.infraProfileService.GetProfileByName(v1.GLOBAL_PROFILE_NAME)
183211
if err != nil {
184-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
212+
statusCode := http.StatusInternalServerError
213+
if errors.Is(err, pg.ErrNoRows) {
214+
err = errors.New(fmt.Sprintf("profile %s not found", v1.GLOBAL_PROFILE_NAME))
215+
statusCode = http.StatusNotFound
216+
}
217+
common.WriteJsonResp(w, err, nil, statusCode)
185218
return
186219
}
187-
profile = defaultProfileV0
188-
resp := bean.ProfileResponseV0{
189-
Profile: *profile,
220+
defaultProfileV0 := adapter.GetV0ProfileBean(defaultProfileV1)
221+
if profileName == v1.DEFAULT_PROFILE_NAME {
222+
profileV0 = defaultProfileV0
223+
}
224+
resp := v0.ProfileResponseV0{
225+
Profile: *profileV0,
190226
}
191-
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits()
227+
resp.ConfigurationUnits, err = handler.infraProfileService.GetConfigurationUnits()
228+
if err != nil {
229+
handler.logger.Errorw("error in getting configuration units", "err", err)
230+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
231+
return
232+
}
233+
//returning the default configuration for UI inheriting purpose
192234
resp.DefaultConfigurations = defaultProfileV0.Configurations
193235
common.WriteJsonResp(w, nil, resp, http.StatusOK)
194236
}
195237

196-
// Deprecated
197238
func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseWriter, r *http.Request) {
198239
userId, err := handler.userService.GetLoggedInUser(r)
199240
if userId == 0 || err != nil {
@@ -209,38 +250,36 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
209250
vars := mux.Vars(r)
210251
profileName := strings.ToLower(vars["name"])
211252
if profileName == "" {
212-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
253+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
213254
return
214255
}
215-
payload := &bean.ProfileBeanV0{}
256+
payload := &v0.ProfileBeanV0{}
216257
decoder := json.NewDecoder(r.Body)
217258
err = decoder.Decode(payload)
218259
if err != nil {
219260
handler.logger.Errorw("error in decoding the request payload", "err", err, "requestBody", r.Body)
220261
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
221262
return
222263
}
223-
payload.Name = strings.ToLower(payload.Name)
224264
err = handler.validator.Struct(payload)
225265
if err != nil {
226-
err = errors.Wrap(err, bean.PayloadValidationError)
266+
err = errors.Wrap(err, errors2.PayloadValidationError)
227267
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
268+
return
228269
}
229-
if !util2.IsValidProfileNameRequestedV0(profileName, payload.Name) {
230-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
270+
if !util.IsValidProfileNameRequestedV0(profileName, payload.GetName()) {
271+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
231272
return
232273
}
233-
if payload.Name != bean.DEFAULT_PROFILE_NAME {
234-
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
274+
if profileName == v1.DEFAULT_PROFILE_NAME && payload.GetName() != v1.DEFAULT_PROFILE_NAME {
275+
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
235276
return
236277
}
237-
238-
profileName = bean.GLOBAL_PROFILE_NAME
239-
payloadV1 := adapter.GetV1ProfileBean(payload)
240-
err = handler.infraProfileService.UpdateProfile(userId, profileName, payloadV1)
278+
payloadV1 := adapter.ConvertToV1ProfileBean(payload)
279+
err = handler.infraProfileService.UpdateProfileV0(userId, profileName, payloadV1)
241280
if err != nil {
242281
handler.logger.Errorw("error in updating profile and configurations", "profileName", profileName, "payLoad", payload, "err", err)
243-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
282+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
244283
return
245284
}
246285
common.WriteJsonResp(w, nil, nil, http.StatusOK)

api/infraConfig/restHandler_ent.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package infraConfig
18+
19+
type InfraConfigRestHandlerEnt interface {
20+
}

api/resourceScan/wire_scan.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
/*
22
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
315
*/
416

517
package resourceScan

0 commit comments

Comments
 (0)