Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: infra configuration refactored #6286

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/devtron-labs/devtron/api/externalLink"
fluxApplication "github.com/devtron-labs/devtron/api/fluxApplication"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/api/infraConfig"
"github.com/devtron-labs/devtron/api/k8s"
"github.com/devtron-labs/devtron/api/module"
"github.com/devtron-labs/devtron/api/resourceScan"
Expand Down Expand Up @@ -129,7 +128,8 @@ import (
"github.com/devtron-labs/devtron/pkg/chart/gitOpsConfig"
chartRepoRepository "github.com/devtron-labs/devtron/pkg/chartRepo/repository"
"github.com/devtron-labs/devtron/pkg/commonService"
"github.com/devtron-labs/devtron/pkg/configDiff"
"github.com/devtron-labs/devtron/pkg/config"
"github.com/devtron-labs/devtron/pkg/config/configDiff"
delete2 "github.com/devtron-labs/devtron/pkg/delete"
deployment2 "github.com/devtron-labs/devtron/pkg/deployment"
"github.com/devtron-labs/devtron/pkg/deployment/common"
Expand All @@ -143,17 +143,14 @@ import (
"github.com/devtron-labs/devtron/pkg/generateManifest"
"github.com/devtron-labs/devtron/pkg/gitops"
"github.com/devtron-labs/devtron/pkg/imageDigestPolicy"
repository11 "github.com/devtron-labs/devtron/pkg/infraConfig/repository"
infraConfigService "github.com/devtron-labs/devtron/pkg/infraConfig/service"
"github.com/devtron-labs/devtron/pkg/infraConfig/units"
"github.com/devtron-labs/devtron/pkg/infraConfig"
"github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs"
repository7 "github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs/repository"
"github.com/devtron-labs/devtron/pkg/notifier"
"github.com/devtron-labs/devtron/pkg/pipeline"
"github.com/devtron-labs/devtron/pkg/pipeline/executors"
history3 "github.com/devtron-labs/devtron/pkg/pipeline/history"
repository3 "github.com/devtron-labs/devtron/pkg/pipeline/history/repository"
"github.com/devtron-labs/devtron/pkg/pipeline/infraProviders"
repository5 "github.com/devtron-labs/devtron/pkg/pipeline/repository"
"github.com/devtron-labs/devtron/pkg/pipeline/types"
"github.com/devtron-labs/devtron/pkg/plugin"
Expand Down Expand Up @@ -278,20 +275,6 @@ func InitializeApp() (*App, error) {
wire.Bind(new(dashboardEvent.DashboardTelemetryRouter),
new(*dashboardEvent.DashboardTelemetryRouterImpl)),

repository11.NewInfraProfileRepositoryImpl,
wire.Bind(new(repository11.InfraConfigRepository), new(*repository11.InfraConfigRepositoryImpl)),

units.NewUnits,
infraConfigService.NewInfraConfigServiceImpl,
wire.Bind(new(infraConfigService.InfraConfigService), new(*infraConfigService.InfraConfigServiceImpl)),
infraProviders.NewInfraProviderImpl,
wire.Bind(new(infraProviders.InfraProvider), new(*infraProviders.InfraProviderImpl)),
infraConfig.NewInfraConfigRestHandlerImpl,
wire.Bind(new(infraConfig.InfraConfigRestHandler), new(*infraConfig.InfraConfigRestHandlerImpl)),

infraConfig.NewInfraProfileRouterImpl,
wire.Bind(new(infraConfig.InfraConfigRouter), new(*infraConfig.InfraConfigRouterImpl)),

router.NewMuxRouter,

app2.NewAppRepositoryImpl,
Expand Down Expand Up @@ -526,6 +509,10 @@ func InitializeApp() (*App, error) {
chartConfig.NewConfigMapRepositoryImpl,
wire.Bind(new(chartConfig.ConfigMapRepository), new(*chartConfig.ConfigMapRepositoryImpl)),

config.WireSet,

infraConfig.WireSet,

notifier.NewSESNotificationServiceImpl,
wire.Bind(new(notifier.SESNotificationService), new(*notifier.SESNotificationServiceImpl)),

Expand Down
149 changes: 94 additions & 55 deletions api/infraConfig/restHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ package infraConfig

import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/devtron-labs/devtron/pkg/infraConfig/adapter"
"github.com/devtron-labs/devtron/pkg/infraConfig/bean"
"github.com/devtron-labs/devtron/pkg/infraConfig/bean/v0"
"github.com/devtron-labs/devtron/pkg/infraConfig/bean/v1"
errors2 "github.com/devtron-labs/devtron/pkg/infraConfig/errors"
"github.com/devtron-labs/devtron/pkg/infraConfig/service"
util2 "github.com/devtron-labs/devtron/pkg/infraConfig/util"
"github.com/devtron-labs/devtron/pkg/infraConfig/util"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/go-pg/pg"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand All @@ -42,13 +46,15 @@ type InfraConfigRestHandler interface {
GetProfileV0(w http.ResponseWriter, r *http.Request)
// Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
UpdateInfraProfileV0(w http.ResponseWriter, r *http.Request)

InfraConfigRestHandlerEnt
}

type InfraConfigRestHandlerImpl struct {
logger *zap.SugaredLogger
infraProfileService service.InfraConfigService
userService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
validator *validator.Validate
}

Expand All @@ -58,7 +64,6 @@ func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileServic
infraProfileService: infraProfileService,
userService: userService,
enforcer: enforcer,
enforcerUtil: enforcerUtil,
validator: validator,
}
}
Expand All @@ -76,29 +81,48 @@ func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *
}

identifier := r.URL.Query().Get("name")

if len(identifier) == 0 {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
return
}
profileName := strings.ToLower(identifier)
var profile *bean.ProfileBeanDto
if profileName != bean.GLOBAL_PROFILE_NAME {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
return
var profile *v1.ProfileBeanDto
if profileName != v1.GLOBAL_PROFILE_NAME {
profile, err = handler.infraProfileService.GetProfileByName(profileName)
if err != nil {
statusCode := http.StatusInternalServerError
if errors.Is(err, pg.ErrNoRows) {
err = errors.New(fmt.Sprintf("profile %s not found", profileName))
statusCode = http.StatusNotFound
}
common.WriteJsonResp(w, err, nil, statusCode)
return
}
}
defaultProfile, err := handler.infraProfileService.GetProfileByName(profileName)

defaultProfile, err := handler.infraProfileService.GetProfileByName(v1.GLOBAL_PROFILE_NAME)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
statusCode := http.StatusInternalServerError
if errors.Is(err, pg.ErrNoRows) {
err = errors.New(fmt.Sprintf("profile %s not found", v1.GLOBAL_PROFILE_NAME))
statusCode = http.StatusNotFound
}
common.WriteJsonResp(w, err, nil, statusCode)
return
}
profile = defaultProfile

resp := bean.ProfileResponse{
if profileName == v1.GLOBAL_PROFILE_NAME {
profile = defaultProfile
}
resp := v1.ProfileResponse{
Profile: *profile,
}
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits()
resp.DefaultConfigurations = defaultProfile.Configurations
resp.ConfigurationUnits, err = handler.infraProfileService.GetConfigurationUnits()
if err != nil {
handler.logger.Errorw("error in getting configuration units", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
resp.DefaultConfigurations = defaultProfile.GetConfigurations()
common.WriteJsonResp(w, nil, resp, http.StatusOK)
}

Expand All @@ -117,28 +141,26 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
//vars := mux.Vars(r)
val := r.URL.Query().Get("name")
if len(val) == 0 {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
common.WriteJsonResp(w, errors.New("name is required"), nil, http.StatusBadRequest)
return
}
profileName := strings.ToLower(val)

payload := &bean.ProfileBeanDto{}
if profileName == "" {
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
return
}
payload := &v1.ProfileBeanDto{}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(payload)
if err != nil {
handler.logger.Errorw("error in decoding the request payload", "err", err, "requestBody", r.Body)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
//handler.validator.RegisterValidation("validateValue", ValidateValue)
payload.Name = strings.ToLower(payload.Name)
err = handler.validator.Struct(payload)
if err != nil {
err = errors.Wrap(err, bean.PayloadValidationError)
err = errors.Wrap(err, errors2.PayloadValidationError)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
}
if !util2.IsValidProfileNameRequested(profileName, payload.Name) {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
return
}
err = handler.infraProfileService.UpdateProfile(userId, profileName, payload)
Expand All @@ -150,7 +172,7 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
common.WriteJsonResp(w, nil, nil, http.StatusOK)
}

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

if profileName != bean.DEFAULT_PROFILE_NAME {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
return
var profileV0 *v0.ProfileBeanV0
if profileName != v1.DEFAULT_PROFILE_NAME {
profileV1, err := handler.infraProfileService.GetProfileByName(profileName)
profileV0 = adapter.GetV0ProfileBean(profileV1)
if err != nil {
statusCode := http.StatusInternalServerError
if errors.Is(err, pg.ErrNoRows) {
err = errors.New(fmt.Sprintf("profile %s not found", profileName))
statusCode = http.StatusNotFound
}
common.WriteJsonResp(w, err, nil, statusCode)
return
}
}

profileName = bean.GLOBAL_PROFILE_NAME

var profile *bean.ProfileBeanV0
defaultProfileV1, err := handler.infraProfileService.GetProfileByName(profileName)
defaultProfileV0 := adapter.GetV0ProfileBean(defaultProfileV1)
defaultProfileV1, err := handler.infraProfileService.GetProfileByName(v1.GLOBAL_PROFILE_NAME)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
statusCode := http.StatusInternalServerError
if errors.Is(err, pg.ErrNoRows) {
err = errors.New(fmt.Sprintf("profile %s not found", v1.GLOBAL_PROFILE_NAME))
statusCode = http.StatusNotFound
}
common.WriteJsonResp(w, err, nil, statusCode)
return
}
profile = defaultProfileV0
resp := bean.ProfileResponseV0{
Profile: *profile,
defaultProfileV0 := adapter.GetV0ProfileBean(defaultProfileV1)
if profileName == v1.DEFAULT_PROFILE_NAME {
profileV0 = defaultProfileV0
}
resp := v0.ProfileResponseV0{
Profile: *profileV0,
}
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits()
resp.ConfigurationUnits, err = handler.infraProfileService.GetConfigurationUnits()
if err != nil {
handler.logger.Errorw("error in getting configuration units", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
//returning the default configuration for UI inheriting purpose
resp.DefaultConfigurations = defaultProfileV0.Configurations
common.WriteJsonResp(w, nil, resp, http.StatusOK)
}

// Deprecated
func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
Expand All @@ -209,38 +250,36 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
vars := mux.Vars(r)
profileName := strings.ToLower(vars["name"])
if profileName == "" {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
return
}
payload := &bean.ProfileBeanV0{}
payload := &v0.ProfileBeanV0{}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(payload)
if err != nil {
handler.logger.Errorw("error in decoding the request payload", "err", err, "requestBody", r.Body)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
payload.Name = strings.ToLower(payload.Name)
err = handler.validator.Struct(payload)
if err != nil {
err = errors.Wrap(err, bean.PayloadValidationError)
err = errors.Wrap(err, errors2.PayloadValidationError)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
if !util2.IsValidProfileNameRequestedV0(profileName, payload.Name) {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
if !util.IsValidProfileNameRequestedV0(profileName, payload.GetName()) {
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
return
}
if payload.Name != bean.DEFAULT_PROFILE_NAME {
common.WriteJsonResp(w, errors.New(bean.InvalidProfileName), nil, http.StatusBadRequest)
if profileName == v1.DEFAULT_PROFILE_NAME && payload.GetName() != v1.DEFAULT_PROFILE_NAME {
common.WriteJsonResp(w, errors.New(errors2.InvalidProfileName), nil, http.StatusBadRequest)
return
}

profileName = bean.GLOBAL_PROFILE_NAME
payloadV1 := adapter.GetV1ProfileBean(payload)
err = handler.infraProfileService.UpdateProfile(userId, profileName, payloadV1)
payloadV1 := adapter.ConvertToV1ProfileBean(payload)
err = handler.infraProfileService.UpdateProfileV0(userId, profileName, payloadV1)
if err != nil {
handler.logger.Errorw("error in updating profile and configurations", "profileName", profileName, "payLoad", payload, "err", err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, nil, nil, http.StatusOK)
Expand Down
20 changes: 20 additions & 0 deletions api/infraConfig/restHandler_ent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 infraConfig

type InfraConfigRestHandlerEnt interface {
}
12 changes: 12 additions & 0 deletions api/resourceScan/wire_scan.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 resourceScan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/devtron-labs/devtron/pkg/configDiff"
"github.com/devtron-labs/devtron/pkg/configDiff/bean"
"github.com/devtron-labs/devtron/pkg/config/configDiff"
"github.com/devtron-labs/devtron/pkg/config/configDiff/bean"
util2 "github.com/devtron-labs/devtron/util"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
Expand Down
2 changes: 1 addition & 1 deletion api/restHandler/app/configDiff/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configDiff

import (
"errors"
"github.com/devtron-labs/devtron/pkg/configDiff/bean"
"github.com/devtron-labs/devtron/pkg/config/configDiff/bean"
)

var validConfigCategories = map[string]bool{bean.Secret.ToString(): true, bean.ConfigMap.ToString(): true, bean.DeploymentTemplate.ToString(): true, bean.PipelineStrategy.ToString(): true}
Expand Down
Loading
Loading