Skip to content

Commit aebe5d3

Browse files
Merge pull request #6518 from devtron-labs/main-sync-develop-14apr
sync: Main sync develop
2 parents 6459d27 + 09fc9f8 commit aebe5d3

File tree

15 files changed

+129
-29
lines changed

15 files changed

+129
-29
lines changed

api/cluster/EnvironmentRestHandler.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const ENV_DELETE_SUCCESS_RESP = "Environment deleted successfully."
5151
type EnvironmentRestHandler interface {
5252
Create(w http.ResponseWriter, r *http.Request)
5353
Get(w http.ResponseWriter, r *http.Request)
54+
GetDataSourceName(w http.ResponseWriter, r *http.Request)
5455
GetAll(w http.ResponseWriter, r *http.Request)
5556
GetAllActive(w http.ResponseWriter, r *http.Request)
5657
Update(w http.ResponseWriter, r *http.Request)
@@ -167,6 +168,34 @@ func (impl EnvironmentRestHandlerImpl) Get(w http.ResponseWriter, r *http.Reques
167168
common.WriteJsonResp(w, err, bean, http.StatusOK)
168169
}
169170

171+
func (impl EnvironmentRestHandlerImpl) GetDataSourceName(w http.ResponseWriter, r *http.Request) {
172+
vars := mux.Vars(r)
173+
environmentName := vars["environmentName"]
174+
175+
bean, err := impl.environmentClusterMappingsService.FindOne(environmentName)
176+
if err != nil {
177+
impl.logger.Errorw("service err, Get", "err", err, "payload", bean)
178+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
179+
return
180+
}
181+
182+
// RBAC enforcer applying
183+
token := r.Header.Get("token")
184+
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobalEnvironment, casbin.ActionGet, bean.EnvironmentIdentifier); !ok {
185+
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
186+
return
187+
}
188+
//RBAC enforcer Ends
189+
190+
resp, err := impl.environmentClusterMappingsService.GetDataSourceName(bean)
191+
if err != nil {
192+
impl.logger.Errorw("service err, Get", "err", err, "env", environmentName)
193+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
194+
return
195+
}
196+
common.WriteJsonResp(w, err, resp, http.StatusOK)
197+
}
198+
170199
func (impl EnvironmentRestHandlerImpl) GetAll(w http.ResponseWriter, r *http.Request) {
171200
environments, err := impl.environmentReadService.GetAll()
172201
if err != nil {

api/cluster/EnvironmentRouter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (impl EnvironmentRouterImpl) InitEnvironmentClusterMappingsRouter(environme
4141
Methods("GET").
4242
Queries("environment", "{environment}").
4343
HandlerFunc(impl.environmentClusterMappingsRestHandler.Get)
44+
environmentClusterMappingsRouter.Path("/data-source-name").
45+
Methods("GET").
46+
Queries("environmentName", "{environmentName}").
47+
HandlerFunc(impl.environmentClusterMappingsRestHandler.GetDataSourceName)
4448
environmentClusterMappingsRouter.Path("").
4549
Methods("GET").
4650
Queries("id", "{id}").

client/grafana/GrafanaClient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (impl *GrafanaClientImpl) CreateDatasource(createDatasourceRequest CreateDa
380380
url = fmt.Sprintf(url, impl.config.GrafanaUsername, impl.config.GrafanaPassword)
381381
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBody))
382382
if err != nil {
383-
// do not log url or req body as they contains sensitive data
383+
// do not log url or req body as they contain sensitive data
384384
impl.logger.Errorw("error while creating http request", "destinationURL", impl.config.DestinationURL, "err", err)
385385
return nil, err
386386
}

cmd/external-app/wire.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import (
5959
"github.com/devtron-labs/devtron/client/argocdServer/repoCredsK8sClient"
6060
"github.com/devtron-labs/devtron/client/argocdServer/session"
6161
"github.com/devtron-labs/devtron/client/dashboard"
62+
"github.com/devtron-labs/devtron/client/grafana"
6263
"github.com/devtron-labs/devtron/client/telemetry"
6364
"github.com/devtron-labs/devtron/internal/sql/repository"
6465
app2 "github.com/devtron-labs/devtron/internal/sql/repository/app"
@@ -145,6 +146,10 @@ func InitializeApp() (*App, error) {
145146
rbac.NewEnforcerUtilImpl,
146147
wire.Bind(new(rbac.EnforcerUtil), new(*rbac.EnforcerUtilImpl)),
147148

149+
grafana.GetGrafanaClientConfig,
150+
grafana.NewGrafanaClientImpl,
151+
wire.Bind(new(grafana.GrafanaClient), new(*grafana.GrafanaClientImpl)),
152+
148153
commonEnforcementFunctionsUtil.NewCommonEnforcementUtilImpl,
149154
wire.Bind(new(commonEnforcementFunctionsUtil.CommonEnforcementUtil), new(*commonEnforcementFunctionsUtil.CommonEnforcementUtilImpl)),
150155

cmd/external-app/wire_gen.go

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ require (
307307

308308
replace (
309309
github.com/argoproj/argo-workflows/v3 v3.5.13 => github.com/devtron-labs/argo-workflows/v3 v3.5.13
310-
github.com/devtron-labs/authenticator => github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250410055937-f65f6e252704
311-
github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250410055937-f65f6e252704
310+
github.com/devtron-labs/authenticator => github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250414075707-6076f6f97fb9
311+
github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250414075707-6076f6f97fb9
312312
github.com/go-check/check => github.com/go-check/check v0.0.0-20180628173108-788fd7840127
313313
github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.5.5
314314
k8s.io/api => k8s.io/api v0.29.7

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,10 @@ github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc h1:VRRKCwnzq
829829
github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
830830
github.com/devtron-labs/argo-workflows/v3 v3.5.13 h1:3pINq0gXOSeTw2z/vYe+j80lRpSN5Rp/8mfQORh8SmU=
831831
github.com/devtron-labs/argo-workflows/v3 v3.5.13/go.mod h1:/vqxcovDPT4zqr4DjR5v7CF8ggpY1l3TSa2CIG3jmjA=
832-
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250410055937-f65f6e252704 h1:xjTvh5f01PARce9TeKjpO6QRM1GaR2fh56/x6+HZcaI=
833-
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250410055937-f65f6e252704/go.mod h1:5lv4Wfj5ERhhvDGXe2IeES6qxjvUVCcohaRwKnWBMNo=
834-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250410055937-f65f6e252704 h1:qA1Ec4flLuUozww0C3MnE3VGpuacBMyT6QxMyqECtrA=
835-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250410055937-f65f6e252704/go.mod h1:ceFKgQ2qm40PR95g5Xp2EClq7nDBKFTcglJ0JdsgClA=
832+
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250414075707-6076f6f97fb9 h1:z72S6KALLrQ2VmW785FuPZpYJTFSzq1QflZJcQv8/O0=
833+
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20250414075707-6076f6f97fb9/go.mod h1:5lv4Wfj5ERhhvDGXe2IeES6qxjvUVCcohaRwKnWBMNo=
834+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250414075707-6076f6f97fb9 h1:o/wKBkZ6aGrFhSsDGSyQVTD3uurGH3db4zdWjRVLkI8=
835+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20250414075707-6076f6f97fb9/go.mod h1:ceFKgQ2qm40PR95g5Xp2EClq7nDBKFTcglJ0JdsgClA=
836836
github.com/devtron-labs/go-bitbucket v0.9.60-beta h1:VEx1jvDgdtDPS6A1uUFoaEi0l1/oLhbr+90xOwr6sDU=
837837
github.com/devtron-labs/go-bitbucket v0.9.60-beta/go.mod h1:GnuiCesvh8xyHeMCb+twm8lBR/kQzJYSKL28ZfObp1Y=
838838
github.com/devtron-labs/protos v0.0.3-0.20250323220609-ecf8a0f7305e h1:U6UdYbW8a7xn5IzFPd8cywjVVPfutGJCudjePAfL/Hs=

internal/sql/repository/chartConfig/EnvConfigOverrideRepository.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ type EnvConfigOverrideRepository interface {
7373
UpdateWithTxn(envConfigOverride *EnvConfigOverride, tx *pg.Tx) (*EnvConfigOverride, error)
7474

7575
GetByAppIdEnvIdAndChartRefId(appId, envId int, chartRefId int) (*EnvConfigOverride, error)
76-
GetAllOverridesForApp(appId int) ([]EnvConfigOverride, error)
76+
// GetAllOverridesForApp will return all overrides []*EnvConfigOverride for an app by appId
77+
// Note:
78+
// EnvConfigOverride.Chart is not populated,
79+
// as the chartRepoRepository.Chart contains the reference chart(in bytes).
80+
GetAllOverridesForApp(appId int) ([]*EnvConfigOverride, error)
7781
}
7882

7983
type EnvConfigOverrideRepositoryImpl struct {
@@ -360,13 +364,19 @@ func (r EnvConfigOverrideRepositoryImpl) GetByAppIdEnvIdAndChartRefId(appId, env
360364
return eco, err
361365
}
362366

363-
func (r EnvConfigOverrideRepositoryImpl) GetAllOverridesForApp(appId int) ([]EnvConfigOverride, error) {
364-
var eco []EnvConfigOverride
367+
// GetAllOverridesForApp will return all overrides EnvConfigOverride for an app by appId
368+
// Note:
369+
// EnvConfigOverride.Chart is not populated,
370+
// as the chartRepoRepository.Chart contains the reference chart(in bytes).
371+
func (r EnvConfigOverrideRepositoryImpl) GetAllOverridesForApp(appId int) ([]*EnvConfigOverride, error) {
372+
var eco []*EnvConfigOverride
365373
err := r.dbConnection.
366374
Model(&eco).
367-
Column("env_config_override.*", "Chart").
375+
Column("env_config_override.*").
376+
Join("INNER JOIN charts").
377+
JoinOn("charts.id = env_config_override.chart_id").
368378
Where("env_config_override.active = ?", true).
369-
Where("Chart.app_id =? ", appId).
379+
Where("charts.app_id = ?", appId).
370380
Select()
371381
return eco, err
372382
}

pkg/chart/ChartService.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func (impl *ChartServiceImpl) Create(templateRequest bean3.TemplateRequest, ctx
315315
return nil, err
316316
}
317317

318-
err = impl.UpdateChartLocationForEnvironmentConfigs(templateRequest.AppId, chart.ChartRefId, templateRequest.UserId, version)
318+
err = impl.updateChartLocationForEnvironmentConfigs(newCtx, templateRequest.AppId, chart.ChartRefId, templateRequest.UserId, version)
319319
if err != nil {
320320
impl.logger.Errorw("error in updating chart location in env overrides", "appId", templateRequest.AppId, "err", err)
321321
return nil, err
@@ -350,8 +350,10 @@ func (impl *ChartServiceImpl) Create(templateRequest bean3.TemplateRequest, ctx
350350
return chartVal, err
351351
}
352352

353-
func (impl *ChartServiceImpl) UpdateChartLocationForEnvironmentConfigs(appId, chartRefId int, userId int32, version string) error {
354-
envOverrides, err := impl.envConfigOverrideReadService.GetAllOverridesForApp(appId)
353+
func (impl *ChartServiceImpl) updateChartLocationForEnvironmentConfigs(ctx context.Context, appId, chartRefId int, userId int32, version string) error {
354+
newCtx, span := otel.Tracer("orchestrator").Start(ctx, "ChartServiceImpl.updateChartLocationForEnvironmentConfigs")
355+
defer span.End()
356+
envOverrides, err := impl.envConfigOverrideReadService.GetAllOverridesForApp(newCtx, appId)
355357
if err != nil {
356358
impl.logger.Errorw("error in getting all overrides for app", "appId", appId, "err", err)
357359
return err
@@ -661,7 +663,7 @@ func (impl *ChartServiceImpl) UpdateAppOverride(ctx context.Context, templateReq
661663
return nil, err
662664
}
663665

664-
err = impl.UpdateChartLocationForEnvironmentConfigs(templateRequest.AppId, templateRequest.ChartRefId, templateRequest.UserId, template.ChartVersion)
666+
err = impl.updateChartLocationForEnvironmentConfigs(newCtx, templateRequest.AppId, templateRequest.ChartRefId, templateRequest.UserId, template.ChartVersion)
665667
if err != nil {
666668
impl.logger.Errorw("error in updating chart location in env overrides", "appId", templateRequest.AppId, "err", err)
667669
return nil, err

pkg/cluster/ClusterServiceExtended.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
repository2 "github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
3737
)
3838

39+
const DataSourceNameFormat = "Prometheus-%s-EnvId-%d"
40+
3941
// extends ClusterServiceImpl and enhances method of ClusterService with full mode specific errors
4042
type ClusterServiceImplExtended struct {
4143
environmentRepository repository.EnvironmentRepository
@@ -268,7 +270,8 @@ func (impl *ClusterServiceImplExtended) CreateGrafanaDataSource(clusterBean *bea
268270
//starts grafana creation
269271
// appending envId to ensure unique datasource name for each environment (ex- env got deleted and created with same name)
270272
// reverting to old name will be done in next release
271-
DataSourceName := "Prometheus-" + env.Name
273+
274+
DataSourceName := fmt.Sprintf(DataSourceNameFormat, env.Name, env.Id)
272275
createDatasourceReq := grafana.CreateDatasourceRequest{
273276
Name: DataSourceName,
274277
Type: "prometheus",

0 commit comments

Comments
 (0)