Skip to content

Commit fa2693a

Browse files
committed
main sync develop
2 parents 6459d27 + 1815677 commit fa2693a

File tree

12 files changed

+119
-19
lines changed

12 files changed

+119
-19
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.

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",

pkg/cluster/environment/EnvironmentService.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package environment
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"github.com/devtron-labs/devtron/client/grafana"
2223
bean3 "github.com/devtron-labs/devtron/pkg/attributes/bean"
2324
"github.com/devtron-labs/devtron/pkg/cluster"
2425
bean4 "github.com/devtron-labs/devtron/pkg/cluster/bean"
@@ -45,6 +46,7 @@ import (
4546

4647
type EnvironmentService interface {
4748
FindOne(environment string) (*bean2.EnvironmentBean, error)
49+
GetDataSourceName(*bean2.EnvironmentBean) (dataSourceData *bean2.DataSourceMetaData, err error)
4850
Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
4951
Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
5052
GetAllActive() ([]bean2.EnvironmentBean, error)
@@ -80,14 +82,16 @@ type EnvironmentServiceImpl struct {
8082
userAuthService user.UserAuthService
8183
attributesRepository repository2.AttributesRepository
8284
clusterReadService read.ClusterReadService
85+
grafanaClient grafana.GrafanaClient
8386
}
8487

8588
func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepository,
8689
clusterService cluster.ClusterService, logger *zap.SugaredLogger,
8790
K8sUtil *util2.K8sServiceImpl, k8sInformerFactory informer.K8sInformerFactory,
8891
// propertiesConfigService pipeline.PropertiesConfigService,
8992
userAuthService user.UserAuthService, attributesRepository repository2.AttributesRepository,
90-
clusterReadService read.ClusterReadService) *EnvironmentServiceImpl {
93+
clusterReadService read.ClusterReadService,
94+
grafanaClient grafana.GrafanaClient) *EnvironmentServiceImpl {
9195
return &EnvironmentServiceImpl{
9296
environmentRepository: environmentRepository,
9397
logger: logger,
@@ -98,6 +102,25 @@ func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepos
98102
userAuthService: userAuthService,
99103
attributesRepository: attributesRepository,
100104
clusterReadService: clusterReadService,
105+
grafanaClient: grafanaClient,
106+
}
107+
}
108+
109+
func (impl EnvironmentServiceImpl) GetDataSourceName(bean *bean2.EnvironmentBean) (*bean2.DataSourceMetaData, error) {
110+
datasource := &bean2.DataSourceMetaData{}
111+
if bean.DataSourceId == 0 || bean.PrometheusEndpoint == "" {
112+
impl.logger.Debugw("grafana data source not configured for given", "dataSourceId", bean.DataSourceId)
113+
return datasource, nil
114+
} else {
115+
impl.logger.Debugw("environment datasource", "datasource", bean.DataSourceId)
116+
data, err := impl.grafanaClient.GetDatasource(bean.DataSourceId)
117+
if err != nil {
118+
impl.logger.Errorw("error in fetching datasource", "err", err)
119+
return datasource, err
120+
}
121+
datasource.Name = data.Name
122+
datasource.Id = bean.DataSourceId
123+
return datasource, nil
101124
}
102125
}
103126

@@ -183,6 +206,7 @@ func (impl EnvironmentServiceImpl) FindOne(environment string) (*bean2.Environme
183206
Default: model.Default,
184207
EnvironmentIdentifier: model.EnvironmentIdentifier,
185208
Description: model.Description,
209+
DataSourceId: model.GrafanaDatasourceId,
186210
}
187211
return bean, nil
188212
}

pkg/cluster/environment/bean/bean.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type EnvironmentBean struct {
3939
ClusterCAData string `json:"-"`
4040
ClusterKeyData string `json:"-"`
4141
ClusterCertData string `json:"-"`
42+
DataSourceId int `json:"-"`
4243
}
4344

4445
type EnvDto struct {
@@ -66,3 +67,8 @@ const (
6667
PIPELINE_DEPLOYMENT_TYPE_HELM = "helm"
6768
PIPELINE_DEPLOYMENT_TYPE_ACD = "argo_cd"
6869
)
70+
71+
type DataSourceMetaData struct {
72+
Id int `json:"id"`
73+
Name string `json:"name"`
74+
}

0 commit comments

Comments
 (0)