Skip to content

Commit

Permalink
feat: support build infra configurations properties configurable from…
Browse files Browse the repository at this point in the history
… ui (#4583)

* intial commit

* wip

* default related crud

* wip

* update oss code

* refactoring

* add int to string conversions for cpu units

* units loading fix

* refactoring

* refactoring

* fix

* const variable names updated

* pointer fix

* test: temporary fix for empty unit

* wip

* configurations key fix

* update spec

* unit conversion method update

* add type param in profile bean

* move constants to constants file

* move constants to constants file

* fix

* change time units from s,m,h to seconds,minutes,hours

* code refactoring around default configurations loading

* add get infraConfig by scope method

* folder structure refactoring

* improve logging

* sql script number update

* resolving build infra configuration values form infraConfig service

* script fix

* remove readonly check from id

* refactor

* query fix

* validation logic fix

* validations added

* update units

* move infra getter logic to seperate service

* script update

* refactoring  wip

* refactoring done

* rebase local

* rebase local

* wire

* pointer fix

* wire

* script number update

* safe check added

* remove test code

* refactoring

* remove cd infra getter

* add params in err log

* minimize diff w.r.t ent

* minor updates

* support values upto 2 decimal points only

* int max as upper limit on timeout

* int max as upper limit on timeout

* infra providers architecture refactoring

* unique constraint remove

* script number update
  • Loading branch information
gireesh-devtron committed Feb 12, 2024
1 parent b615f0d commit 52c3e35
Show file tree
Hide file tree
Showing 27 changed files with 1,999 additions and 117 deletions.
60 changes: 39 additions & 21 deletions Wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/devtron-labs/devtron/api/deployment"
"github.com/devtron-labs/devtron/api/externalLink"
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/restHandler"
Expand Down Expand Up @@ -125,13 +126,16 @@ import (
"github.com/devtron-labs/devtron/pkg/git"
"github.com/devtron-labs/devtron/pkg/gitops"
"github.com/devtron-labs/devtron/pkg/imageDigestPolicy"
infraConfigService "github.com/devtron-labs/devtron/pkg/infraConfig"
"github.com/devtron-labs/devtron/pkg/infraConfig/units"
"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 @@ -181,20 +185,20 @@ func InitializeApp() (*App, error) {
deployment2.DeploymentWireSet,

// -------wireset end ----------
//-------
// -------
gitSensor.GetConfig,
gitSensor.NewGitSensorClient,
wire.Bind(new(gitSensor.Client), new(*gitSensor.ClientImpl)),
//-------
// -------
helper.NewAppListingRepositoryQueryBuilder,
//sql.GetConfig,
// sql.GetConfig,
eClient.GetEventClientConfig,
util2.GetGlobalEnvVariables,
//sql.NewDbConnection,
//app.GetACDAuthConfig,
// sql.NewDbConnection,
// app.GetACDAuthConfig,
util3.GetACDAuthConfig,
connection.SettingsManager,
//auth.GetConfig,
// auth.GetConfig,

connection.GetConfig,
wire.Bind(new(session2.ServiceClient), new(*middleware.LoginService)),
Expand All @@ -203,13 +207,13 @@ func InitializeApp() (*App, error) {
trigger2.NewPipelineTriggerRouter,
wire.Bind(new(trigger2.PipelineTriggerRouter), new(*trigger2.PipelineTriggerRouterImpl)),

//---- pprof start ----
// ---- pprof start ----
restHandler.NewPProfRestHandler,
wire.Bind(new(restHandler.PProfRestHandler), new(*restHandler.PProfRestHandlerImpl)),

router.NewPProfRouter,
wire.Bind(new(router.PProfRouter), new(*router.PProfRouterImpl)),
//---- pprof end ----
// ---- pprof end ----

trigger.NewPipelineRestHandler,
wire.Bind(new(trigger.PipelineTriggerRestHandler), new(*trigger.PipelineTriggerRestHandlerImpl)),
Expand Down Expand Up @@ -238,6 +242,20 @@ func InitializeApp() (*App, error) {
wire.Bind(new(dashboardEvent.DashboardTelemetryRouter),
new(*dashboardEvent.DashboardTelemetryRouterImpl)),

infraConfigService.NewInfraProfileRepositoryImpl,
wire.Bind(new(infraConfigService.InfraConfigRepository), new(*infraConfigService.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 @@ -293,7 +311,7 @@ func InitializeApp() (*App, error) {
util.NewChartTemplateServiceImpl,
wire.Bind(new(util.ChartTemplateService), new(*util.ChartTemplateServiceImpl)),

//scoped variables start
// scoped variables start
variables.NewScopedVariableServiceImpl,
wire.Bind(new(variables.ScopedVariableService), new(*variables.ScopedVariableServiceImpl)),

Expand All @@ -315,7 +333,7 @@ func InitializeApp() (*App, error) {
variables.NewScopedVariableCMCSManagerImpl,
wire.Bind(new(variables.ScopedVariableCMCSManager), new(*variables.ScopedVariableCMCSManagerImpl)),

//end
// end

chart.NewChartServiceImpl,
wire.Bind(new(chart.ChartService), new(*chart.ChartServiceImpl)),
Expand Down Expand Up @@ -395,10 +413,10 @@ func InitializeApp() (*App, error) {
//app.GetConfig,

pipeline.GetEcrConfig,
//otel.NewOtelTracingServiceImpl,
//wire.Bind(new(otel.OtelTracingService), new(*otel.OtelTracingServiceImpl)),
// otel.NewOtelTracingServiceImpl,
// wire.Bind(new(otel.OtelTracingService), new(*otel.OtelTracingServiceImpl)),
NewApp,
//session.NewK8sClient,
// session.NewK8sClient,
repository8.NewImageTaggingRepositoryImpl,
wire.Bind(new(repository8.ImageTaggingRepository), new(*repository8.ImageTaggingRepositoryImpl)),
pipeline.NewImageTaggingServiceImpl,
Expand Down Expand Up @@ -581,7 +599,7 @@ func InitializeApp() (*App, error) {
restHandler.NewPubSubClientRestHandlerImpl,
wire.Bind(new(restHandler.PubSubClientRestHandler), new(*restHandler.PubSubClientRestHandlerImpl)),

//Batch actions
// Batch actions
batch.NewWorkflowActionImpl,
wire.Bind(new(batch.WorkflowAction), new(*batch.WorkflowActionImpl)),
batch.NewDeploymentActionImpl,
Expand Down Expand Up @@ -784,9 +802,9 @@ func InitializeApp() (*App, error) {

history3.NewDeployedConfigurationHistoryServiceImpl,
wire.Bind(new(history3.DeployedConfigurationHistoryService), new(*history3.DeployedConfigurationHistoryServiceImpl)),
//history ends
// history ends

//plugin starts
// plugin starts
repository6.NewGlobalPluginRepository,
wire.Bind(new(repository6.GlobalPluginRepository), new(*repository6.GlobalPluginRepositoryImpl)),

Expand All @@ -804,7 +822,7 @@ func InitializeApp() (*App, error) {

pipeline.NewPipelineStageService,
wire.Bind(new(pipeline.PipelineStageService), new(*pipeline.PipelineStageServiceImpl)),
//plugin ends
// plugin ends

connection.NewArgoCDConnectionManagerImpl,
wire.Bind(new(connection.ArgoCDConnectionManager), new(*connection.ArgoCDConnectionManagerImpl)),
Expand All @@ -816,12 +834,12 @@ func InitializeApp() (*App, error) {
cron.NewCdApplicationStatusUpdateHandlerImpl,
wire.Bind(new(cron.CdApplicationStatusUpdateHandler), new(*cron.CdApplicationStatusUpdateHandlerImpl)),

//app_status
// app_status
appStatusRepo.NewAppStatusRepositoryImpl,
wire.Bind(new(appStatusRepo.AppStatusRepository), new(*appStatusRepo.AppStatusRepositoryImpl)),
appStatus.NewAppStatusServiceImpl,
wire.Bind(new(appStatus.AppStatusService), new(*appStatus.AppStatusServiceImpl)),
//app_status ends
// app_status ends

cron.GetCiWorkflowStatusUpdateConfig,
cron.NewCiStatusUpdateCronImpl,
Expand Down Expand Up @@ -866,8 +884,8 @@ func InitializeApp() (*App, error) {
repository.NewGlobalCMCSRepositoryImpl,
wire.Bind(new(repository.GlobalCMCSRepository), new(*repository.GlobalCMCSRepositoryImpl)),

//chartRepoRepository.NewGlobalStrategyMetadataRepositoryImpl,
//wire.Bind(new(chartRepoRepository.GlobalStrategyMetadataRepository), new(*chartRepoRepository.GlobalStrategyMetadataRepositoryImpl)),
// chartRepoRepository.NewGlobalStrategyMetadataRepositoryImpl,
// wire.Bind(new(chartRepoRepository.GlobalStrategyMetadataRepository), new(*chartRepoRepository.GlobalStrategyMetadataRepositoryImpl)),
chartRepoRepository.NewGlobalStrategyMetadataChartRefMappingRepositoryImpl,
wire.Bind(new(chartRepoRepository.GlobalStrategyMetadataChartRefMappingRepository), new(*chartRepoRepository.GlobalStrategyMetadataChartRefMappingRepositoryImpl)),

Expand Down
120 changes: 120 additions & 0 deletions api/infraConfig/restHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package infraConfig

import (
"encoding/json"
"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"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strings"
)

type InfraConfigRestHandler interface {
UpdateInfraProfile(w http.ResponseWriter, r *http.Request)
GetProfile(w http.ResponseWriter, r *http.Request)
}
type InfraConfigRestHandlerImpl struct {
logger *zap.SugaredLogger
infraProfileService infraConfig.InfraConfigService
userService user.UserService
enforcer casbin.Enforcer
enforcerUtil rbac.EnforcerUtil
validator *validator.Validate
}

func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileService infraConfig.InfraConfigService, userService user.UserService, enforcer casbin.Enforcer, enforcerUtil rbac.EnforcerUtil, validator *validator.Validate) *InfraConfigRestHandlerImpl {
return &InfraConfigRestHandlerImpl{
logger: logger,
infraProfileService: infraProfileService,
userService: userService,
enforcer: enforcer,
enforcerUtil: enforcerUtil,
validator: validator,
}
}

func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
token := r.Header.Get("token")
if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}

vars := mux.Vars(r)
profileName := strings.ToLower(vars["name"])
if profileName == "" {
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest)
return
}
payload := &infraConfig.ProfileBean{}
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, infraConfig.PayloadValidationError)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
}
if profileName == "" || (profileName == infraConfig.DEFAULT_PROFILE_NAME && payload.Name != infraConfig.DEFAULT_PROFILE_NAME) {
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest)
}
err = handler.infraProfileService.UpdateProfile(userId, profileName, payload)
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)
return
}
common.WriteJsonResp(w, nil, nil, http.StatusOK)
}

func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
token := r.Header.Get("token")
if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}

vars := mux.Vars(r)
profileName := strings.ToLower(vars["name"])
if profileName == "" {
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest)
return
}

var profile *infraConfig.ProfileBean
defaultProfile, err := handler.infraProfileService.GetProfileByName(profileName)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
if profileName == infraConfig.DEFAULT_PROFILE_NAME {
profile = defaultProfile
}
resp := infraConfig.ProfileResponse{
Profile: *profile,
}
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits()
resp.DefaultConfigurations = defaultProfile.Configurations
common.WriteJsonResp(w, nil, resp, http.StatusOK)
}
27 changes: 27 additions & 0 deletions api/infraConfig/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package infraConfig

import "github.com/gorilla/mux"

type InfraConfigRouter interface {
InitInfraConfigRouter(configRouter *mux.Router)
}

type InfraConfigRouterImpl struct {
infraConfigRestHandler InfraConfigRestHandler
}

func NewInfraProfileRouterImpl(infraConfigRestHandler InfraConfigRestHandler) *InfraConfigRouterImpl {
return &InfraConfigRouterImpl{
infraConfigRestHandler: infraConfigRestHandler,
}
}

func (impl *InfraConfigRouterImpl) InitInfraConfigRouter(configRouter *mux.Router) {
configRouter.Path("/profile/{name}").
HandlerFunc(impl.infraConfigRestHandler.GetProfile).
Methods("GET")

configRouter.Path("/profile/{name}").
HandlerFunc(impl.infraConfigRestHandler.UpdateInfraProfile).
Methods("PUT")
}
17 changes: 12 additions & 5 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/devtron-labs/devtron/api/deployment"
"github.com/devtron-labs/devtron/api/externalLink"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/api/infraConfig"
"github.com/devtron-labs/devtron/api/k8s/application"
"github.com/devtron-labs/devtron/api/k8s/capacity"
"github.com/devtron-labs/devtron/api/module"
Expand Down Expand Up @@ -118,6 +119,7 @@ type MuxRouter struct {
rbacRoleRouter user.RbacRoleRouter
scopedVariableRouter ScopedVariableRouter
ciTriggerCron cron.CiTriggerCron
infraConfigRouter infraConfig.InfraConfigRouter
}

func NewMuxRouter(logger *zap.SugaredLogger,
Expand Down Expand Up @@ -148,7 +150,8 @@ func NewMuxRouter(logger *zap.SugaredLogger,
rbacRoleRouter user.RbacRoleRouter,
scopedVariableRouter ScopedVariableRouter,
ciTriggerCron cron.CiTriggerCron,
proxyRouter proxy.ProxyRouter) *MuxRouter {
proxyRouter proxy.ProxyRouter,
infraConfigRouter infraConfig.InfraConfigRouter) *MuxRouter {
r := &MuxRouter{
Router: mux.NewRouter(),
EnvironmentClusterMappingsRouter: EnvironmentClusterMappingsRouter,
Expand Down Expand Up @@ -213,6 +216,7 @@ func NewMuxRouter(logger *zap.SugaredLogger,
rbacRoleRouter: rbacRoleRouter,
scopedVariableRouter: scopedVariableRouter,
ciTriggerCron: ciTriggerCron,
infraConfigRouter: infraConfigRouter,
}
return r
}
Expand All @@ -222,8 +226,8 @@ func (r MuxRouter) Init() {

r.Router.StrictSlash(true)
r.Router.Handle("/metrics", promhttp.Handler())
//prometheus.MustRegister(pipeline.CiTriggerCounter)
//prometheus.MustRegister(app.CdTriggerCounter)
// prometheus.MustRegister(pipeline.CiTriggerCounter)
// prometheus.MustRegister(app.CdTriggerCounter)
r.Router.Path("/health").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(200)
Expand Down Expand Up @@ -375,10 +379,10 @@ func (r MuxRouter) Init() {
r.dashboardTelemetryRouter.Init(dashboardTelemetryRouter)
// dashboard event router ends

//GitOps,Acd + HelmCLi both apps deployment related api's
// GitOps,Acd + HelmCLi both apps deployment related api's
applicationSubRouter := r.Router.PathPrefix("/orchestrator/application").Subrouter()
r.commonDeploymentRouter.Init(applicationSubRouter)
//this router must placed after commonDeploymentRouter
// this router must placed after commonDeploymentRouter
r.helmAppRouter.InitAppListRouter(applicationSubRouter)

externalLinkRouter := r.Router.PathPrefix("/orchestrator/external-links").Subrouter()
Expand Down Expand Up @@ -411,4 +415,7 @@ func (r MuxRouter) Init() {

rbacRoleRouter := r.Router.PathPrefix("/orchestrator/rbac/role").Subrouter()
r.rbacRoleRouter.InitRbacRoleRouter(rbacRoleRouter)

infraConfigRouter := r.Router.PathPrefix("/orchestrator/infra-config").Subrouter()
r.infraConfigRouter.InitInfraConfigRouter(infraConfigRouter)
}

0 comments on commit 52c3e35

Please sign in to comment.