Skip to content

sync: Main sync rc33 9apr #6507

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

Merged
merged 11 commits into from
Apr 9, 2025
29 changes: 29 additions & 0 deletions api/cluster/EnvironmentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const ENV_DELETE_SUCCESS_RESP = "Environment deleted successfully."
type EnvironmentRestHandler interface {
Create(w http.ResponseWriter, r *http.Request)
Get(w http.ResponseWriter, r *http.Request)
GetDataSourceName(w http.ResponseWriter, r *http.Request)
GetAll(w http.ResponseWriter, r *http.Request)
GetAllActive(w http.ResponseWriter, r *http.Request)
Update(w http.ResponseWriter, r *http.Request)
Expand Down Expand Up @@ -167,6 +168,34 @@ func (impl EnvironmentRestHandlerImpl) Get(w http.ResponseWriter, r *http.Reques
common.WriteJsonResp(w, err, bean, http.StatusOK)
}

func (impl EnvironmentRestHandlerImpl) GetDataSourceName(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
environmentName := vars["environmentName"]

bean, err := impl.environmentClusterMappingsService.FindOne(environmentName)
if err != nil {
impl.logger.Errorw("service err, Get", "err", err, "payload", bean)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}

// RBAC enforcer applying
token := r.Header.Get("token")
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobalEnvironment, casbin.ActionGet, bean.EnvironmentIdentifier); !ok {
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
return
}
//RBAC enforcer Ends

resp, err := impl.environmentClusterMappingsService.GetDataSourceName(bean)
if err != nil {
impl.logger.Errorw("service err, Get", "err", err, "env", environmentName)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, resp, http.StatusOK)
}

func (impl EnvironmentRestHandlerImpl) GetAll(w http.ResponseWriter, r *http.Request) {
environments, err := impl.environmentReadService.GetAll()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions api/cluster/EnvironmentRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (impl EnvironmentRouterImpl) InitEnvironmentClusterMappingsRouter(environme
Methods("GET").
Queries("environment", "{environment}").
HandlerFunc(impl.environmentClusterMappingsRestHandler.Get)
environmentClusterMappingsRouter.Path("/data-source-name").
Methods("GET").
Queries("environmentName", "{environmentName}").
HandlerFunc(impl.environmentClusterMappingsRestHandler.GetDataSourceName)
environmentClusterMappingsRouter.Path("").
Methods("GET").
Queries("id", "{id}").
Expand Down
2 changes: 1 addition & 1 deletion client/grafana/GrafanaClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (impl *GrafanaClientImpl) CreateDatasource(createDatasourceRequest CreateDa
url = fmt.Sprintf(url, impl.config.GrafanaUsername, impl.config.GrafanaPassword)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBody))
if err != nil {
// do not log url or req body as they contains sensitive data
// do not log url or req body as they contain sensitive data
impl.logger.Errorw("error while creating http request", "destinationURL", impl.config.DestinationURL, "err", err)
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/external-app/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/devtron-labs/devtron/client/argocdServer/repoCredsK8sClient"
"github.com/devtron-labs/devtron/client/argocdServer/session"
"github.com/devtron-labs/devtron/client/dashboard"
"github.com/devtron-labs/devtron/client/grafana"
"github.com/devtron-labs/devtron/client/telemetry"
"github.com/devtron-labs/devtron/internal/sql/repository"
app2 "github.com/devtron-labs/devtron/internal/sql/repository/app"
Expand Down Expand Up @@ -145,6 +146,10 @@ func InitializeApp() (*App, error) {
rbac.NewEnforcerUtilImpl,
wire.Bind(new(rbac.EnforcerUtil), new(*rbac.EnforcerUtilImpl)),

grafana.GetGrafanaClientConfig,
grafana.NewGrafanaClientImpl,
wire.Bind(new(grafana.GrafanaClient), new(*grafana.GrafanaClientImpl)),

commonEnforcementFunctionsUtil.NewCommonEnforcementUtilImpl,
wire.Bind(new(commonEnforcementFunctionsUtil.CommonEnforcementUtil), new(*commonEnforcementFunctionsUtil.CommonEnforcementUtilImpl)),

Expand Down
14 changes: 10 additions & 4 deletions cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pkg/cluster/ClusterServiceExtended.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
repository2 "github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
)

const DataSourceNameFormat = "Prometheus-%s-EnvId-%d"

// extends ClusterServiceImpl and enhances method of ClusterService with full mode specific errors
type ClusterServiceImplExtended struct {
environmentRepository repository.EnvironmentRepository
Expand Down Expand Up @@ -268,7 +270,8 @@ func (impl *ClusterServiceImplExtended) CreateGrafanaDataSource(clusterBean *bea
//starts grafana creation
// appending envId to ensure unique datasource name for each environment (ex- env got deleted and created with same name)
// reverting to old name will be done in next release
DataSourceName := "Prometheus-" + env.Name

DataSourceName := fmt.Sprintf(DataSourceNameFormat, env.Name, env.Id)
createDatasourceReq := grafana.CreateDatasourceRequest{
Name: DataSourceName,
Type: "prometheus",
Expand Down
26 changes: 25 additions & 1 deletion pkg/cluster/environment/EnvironmentService.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package environment
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/client/grafana"
bean3 "github.com/devtron-labs/devtron/pkg/attributes/bean"
"github.com/devtron-labs/devtron/pkg/cluster"
bean4 "github.com/devtron-labs/devtron/pkg/cluster/bean"
Expand All @@ -45,6 +46,7 @@ import (

type EnvironmentService interface {
FindOne(environment string) (*bean2.EnvironmentBean, error)
GetDataSourceName(*bean2.EnvironmentBean) (dataSourceData *bean2.DataSourceMetaData, err error)
Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
GetAllActive() ([]bean2.EnvironmentBean, error)
Expand Down Expand Up @@ -80,14 +82,16 @@ type EnvironmentServiceImpl struct {
userAuthService user.UserAuthService
attributesRepository repository2.AttributesRepository
clusterReadService read.ClusterReadService
grafanaClient grafana.GrafanaClient
}

func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepository,
clusterService cluster.ClusterService, logger *zap.SugaredLogger,
K8sUtil *util2.K8sServiceImpl, k8sInformerFactory informer.K8sInformerFactory,
// propertiesConfigService pipeline.PropertiesConfigService,
userAuthService user.UserAuthService, attributesRepository repository2.AttributesRepository,
clusterReadService read.ClusterReadService) *EnvironmentServiceImpl {
clusterReadService read.ClusterReadService,
grafanaClient grafana.GrafanaClient) *EnvironmentServiceImpl {
return &EnvironmentServiceImpl{
environmentRepository: environmentRepository,
logger: logger,
Expand All @@ -98,6 +102,25 @@ func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepos
userAuthService: userAuthService,
attributesRepository: attributesRepository,
clusterReadService: clusterReadService,
grafanaClient: grafanaClient,
}
}

func (impl EnvironmentServiceImpl) GetDataSourceName(bean *bean2.EnvironmentBean) (*bean2.DataSourceMetaData, error) {
datasource := &bean2.DataSourceMetaData{}
if bean.DataSourceId == 0 || bean.PrometheusEndpoint == "" {
impl.logger.Debugw("grafana data source not configured for given", "dataSourceId", bean.DataSourceId)
return datasource, nil
} else {
impl.logger.Debugw("environment datasource", "datasource", bean.DataSourceId)
data, err := impl.grafanaClient.GetDatasource(bean.DataSourceId)
if err != nil {
impl.logger.Errorw("error in fetching datasource", "err", err)
return datasource, err
}
datasource.Name = data.Name
datasource.Id = bean.DataSourceId
return datasource, nil
}
}

Expand Down Expand Up @@ -183,6 +206,7 @@ func (impl EnvironmentServiceImpl) FindOne(environment string) (*bean2.Environme
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
DataSourceId: model.GrafanaDatasourceId,
}
return bean, nil
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/cluster/environment/bean/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type EnvironmentBean struct {
ClusterCAData string `json:"-"`
ClusterKeyData string `json:"-"`
ClusterCertData string `json:"-"`
DataSourceId int `json:"-"`
}

type EnvDto struct {
Expand Down Expand Up @@ -66,3 +67,8 @@ const (
PIPELINE_DEPLOYMENT_TYPE_HELM = "helm"
PIPELINE_DEPLOYMENT_TYPE_ACD = "argo_cd"
)

type DataSourceMetaData struct {
Id int `json:"id"`
Name string `json:"name"`
}
4 changes: 2 additions & 2 deletions wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.