Skip to content

Commit 8f33e3d

Browse files
feat: Template chart API added (#1692)
* wip * fix * template chart API
1 parent ee81e71 commit 8f33e3d

14 files changed

+1110
-130
lines changed

api/helm-app/HelmAppRestHandler.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"encoding/json"
66
"errors"
77
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
8+
openapi2 "github.com/devtron-labs/devtron/api/openapi/openapiClient"
89
"github.com/devtron-labs/devtron/api/restHandler/common"
910
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
1011
appStoreDeploymentCommon "github.com/devtron-labs/devtron/pkg/appStore/deployment/common"
1112
"github.com/devtron-labs/devtron/pkg/cluster"
13+
"github.com/devtron-labs/devtron/pkg/user"
1214
"github.com/devtron-labs/devtron/pkg/user/casbin"
1315
"github.com/devtron-labs/devtron/util/k8sObjectsUtil"
1416
"github.com/devtron-labs/devtron/util/rbac"
@@ -17,6 +19,7 @@ import (
1719
"net/http"
1820
"strconv"
1921
"strings"
22+
"time"
2023
)
2124

2225
type HelmAppRestHandler interface {
@@ -28,6 +31,7 @@ type HelmAppRestHandler interface {
2831
GetDesiredManifest(w http.ResponseWriter, r *http.Request)
2932
DeleteApplication(w http.ResponseWriter, r *http.Request)
3033
UpdateApplication(w http.ResponseWriter, r *http.Request)
34+
TemplateChart(w http.ResponseWriter, r *http.Request)
3135
}
3236

3337
type HelmAppRestHandlerImpl struct {
@@ -37,18 +41,21 @@ type HelmAppRestHandlerImpl struct {
3741
clusterService cluster.ClusterService
3842
enforcerUtil rbac.EnforcerUtilHelm
3943
appStoreDeploymentCommonService appStoreDeploymentCommon.AppStoreDeploymentCommonService
44+
userAuthService user.UserService
4045
}
4146

4247
func NewHelmAppRestHandlerImpl(logger *zap.SugaredLogger,
4348
helmAppService HelmAppService, enforcer casbin.Enforcer,
44-
clusterService cluster.ClusterService, enforcerUtil rbac.EnforcerUtilHelm, appStoreDeploymentCommonService appStoreDeploymentCommon.AppStoreDeploymentCommonService) *HelmAppRestHandlerImpl {
49+
clusterService cluster.ClusterService, enforcerUtil rbac.EnforcerUtilHelm, appStoreDeploymentCommonService appStoreDeploymentCommon.AppStoreDeploymentCommonService,
50+
userAuthService user.UserService) *HelmAppRestHandlerImpl {
4551
return &HelmAppRestHandlerImpl{
4652
logger: logger,
4753
helmAppService: helmAppService,
4854
enforcer: enforcer,
4955
clusterService: clusterService,
5056
enforcerUtil: enforcerUtil,
5157
appStoreDeploymentCommonService: appStoreDeploymentCommonService,
58+
userAuthService: userAuthService,
5259
}
5360
}
5461

@@ -335,6 +342,33 @@ func (handler *HelmAppRestHandlerImpl) UpdateApplication(w http.ResponseWriter,
335342
common.WriteJsonResp(w, err, res, http.StatusOK)
336343
}
337344

345+
func (handler *HelmAppRestHandlerImpl) TemplateChart(w http.ResponseWriter, r *http.Request) {
346+
userId, err := handler.userAuthService.GetLoggedInUser(r)
347+
if userId == 0 || err != nil {
348+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
349+
return
350+
}
351+
request := &openapi2.TemplateChartRequest{}
352+
decoder := json.NewDecoder(r.Body)
353+
err = decoder.Decode(request)
354+
if err != nil {
355+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
356+
return
357+
}
358+
359+
//making this api rbac free
360+
361+
// template chart starts
362+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
363+
defer cancel()
364+
response, err := handler.helmAppService.TemplateChart(ctx, request)
365+
if err != nil {
366+
handler.logger.Errorw("Error in helm-template", "err", err, "payload", request)
367+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
368+
return
369+
}
370+
common.WriteJsonResp(w, err, response, http.StatusOK)
371+
}
338372

339373
func (handler *HelmAppRestHandlerImpl) checkHelmAuth(token string, object string) bool {
340374
if ok := handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, strings.ToLower(object)); !ok {

api/helm-app/HelmAppRouter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ func (impl *HelmAppRouterImpl) InitAppListRouter(helmRouter *mux.Router) {
3232

3333
helmRouter.Path("/delete").Queries("appId", "{appId}").
3434
HandlerFunc(impl.helmAppRestHandler.DeleteApplication).Methods("DELETE")
35+
36+
helmRouter.Path("/template-chart").HandlerFunc(impl.helmAppRestHandler.TemplateChart).Methods("POST")
3537
}

api/helm-app/HelmAppService.go

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"github.com/devtron-labs/devtron/api/connector"
88
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
9+
openapi2 "github.com/devtron-labs/devtron/api/openapi/openapiClient"
910
"github.com/devtron-labs/devtron/client/k8s/application"
11+
appStoreDiscoverRepository "github.com/devtron-labs/devtron/pkg/appStore/discover/repository"
1012
"github.com/devtron-labs/devtron/pkg/cluster"
1113
serverBean "github.com/devtron-labs/devtron/pkg/server/bean"
1214
serverEnvConfig "github.com/devtron-labs/devtron/pkg/server/config"
@@ -47,31 +49,37 @@ type HelmAppService interface {
4749
GetClusterConf(clusterId int) (*ClusterConfig, error)
4850
GetDevtronHelmAppIdentifier() *AppIdentifier
4951
UpdateApplicationWithChartInfoWithExtraValues(ctx context.Context, appIdentifier *AppIdentifier, chartRepository *ChartRepository, extraValues map[string]interface{}, extraValuesYamlUrl string, useLatestChartVersion bool) (*openapi.UpdateReleaseResponse, error)
52+
TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error)
5053
}
5154

5255
type HelmAppServiceImpl struct {
53-
logger *zap.SugaredLogger
54-
clusterService cluster.ClusterService
55-
helmAppClient HelmAppClient
56-
pump connector.Pump
57-
enforcerUtil rbac.EnforcerUtilHelm
58-
serverDataStore *serverDataStore.ServerDataStore
59-
serverEnvConfig *serverEnvConfig.ServerEnvConfig
56+
logger *zap.SugaredLogger
57+
clusterService cluster.ClusterService
58+
helmAppClient HelmAppClient
59+
pump connector.Pump
60+
enforcerUtil rbac.EnforcerUtilHelm
61+
serverDataStore *serverDataStore.ServerDataStore
62+
serverEnvConfig *serverEnvConfig.ServerEnvConfig
63+
appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository
64+
environmentService cluster.EnvironmentService
6065
}
6166

6267
func NewHelmAppServiceImpl(Logger *zap.SugaredLogger,
6368
clusterService cluster.ClusterService,
6469
helmAppClient HelmAppClient,
6570
pump connector.Pump, enforcerUtil rbac.EnforcerUtilHelm, serverDataStore *serverDataStore.ServerDataStore,
66-
serverEnvConfig *serverEnvConfig.ServerEnvConfig) *HelmAppServiceImpl {
71+
serverEnvConfig *serverEnvConfig.ServerEnvConfig, appStoreApplicationVersionRepository appStoreDiscoverRepository.AppStoreApplicationVersionRepository,
72+
environmentService cluster.EnvironmentService) *HelmAppServiceImpl {
6773
return &HelmAppServiceImpl{
68-
logger: Logger,
69-
clusterService: clusterService,
70-
helmAppClient: helmAppClient,
71-
pump: pump,
72-
enforcerUtil: enforcerUtil,
73-
serverDataStore: serverDataStore,
74-
serverEnvConfig: serverEnvConfig,
74+
logger: Logger,
75+
clusterService: clusterService,
76+
helmAppClient: helmAppClient,
77+
pump: pump,
78+
enforcerUtil: enforcerUtil,
79+
serverDataStore: serverDataStore,
80+
serverEnvConfig: serverEnvConfig,
81+
appStoreApplicationVersionRepository: appStoreApplicationVersionRepository,
82+
environmentService: environmentService,
7583
}
7684
}
7785

@@ -575,6 +583,65 @@ func (impl *HelmAppServiceImpl) UpdateApplicationWithChartInfoWithExtraValues(ct
575583
return response, nil
576584
}
577585

586+
func (impl *HelmAppServiceImpl) TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error) {
587+
appStoreApplicationVersionId := int(*templateChartRequest.AppStoreApplicationVersionId)
588+
environmentId := int(*templateChartRequest.EnvironmentId)
589+
appStoreAppVersion, err := impl.appStoreApplicationVersionRepository.FindById(appStoreApplicationVersionId)
590+
if err != nil {
591+
impl.logger.Errorw("Error in fetching app-store application version", "appStoreApplicationVersionId", appStoreApplicationVersionId, "err", err)
592+
return nil, err
593+
}
594+
595+
if environmentId > 0 {
596+
environment, err := impl.environmentService.FindById(environmentId)
597+
if err != nil {
598+
impl.logger.Errorw("Error in fetching environment", "environmentId", environmentId, "err", err)
599+
return nil, err
600+
}
601+
templateChartRequest.Namespace = &environment.Namespace
602+
clusterIdI32 := int32(environment.ClusterId)
603+
templateChartRequest.ClusterId = &clusterIdI32
604+
}
605+
606+
clusterId := int(*templateChartRequest.ClusterId)
607+
608+
installReleaseRequest := &InstallReleaseRequest{
609+
ChartName: appStoreAppVersion.Name,
610+
ChartVersion: appStoreAppVersion.Version,
611+
ValuesYaml: *templateChartRequest.ValuesYaml,
612+
ChartRepository: &ChartRepository{
613+
Name: appStoreAppVersion.AppStore.ChartRepo.Name,
614+
Url: appStoreAppVersion.AppStore.ChartRepo.Url,
615+
Username: appStoreAppVersion.AppStore.ChartRepo.UserName,
616+
Password: appStoreAppVersion.AppStore.ChartRepo.Password,
617+
},
618+
ReleaseIdentifier: &ReleaseIdentifier{
619+
ReleaseNamespace: *templateChartRequest.Namespace,
620+
ReleaseName: *templateChartRequest.ReleaseName,
621+
},
622+
}
623+
624+
config, err := impl.GetClusterConf(clusterId)
625+
if err != nil {
626+
impl.logger.Errorw("error in fetching cluster detail", "clusterId", clusterId, "err", err)
627+
return nil, err
628+
}
629+
630+
installReleaseRequest.ReleaseIdentifier.ClusterConfig = config
631+
632+
templateChartResponse, err := impl.helmAppClient.TemplateChart(ctx, installReleaseRequest)
633+
if err != nil {
634+
impl.logger.Errorw("error in templating chart", "err", err)
635+
return nil, err
636+
}
637+
638+
response := &openapi2.TemplateChartResponse{
639+
Manifest: &templateChartResponse.GeneratedManifest,
640+
}
641+
642+
return response, nil
643+
}
644+
578645
type AppIdentifier struct {
579646
ClusterId int `json:"clusterId"`
580647
Namespace string `json:"namespace"`

api/helm-app/applicationClient.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type HelmAppClient interface {
2424
UpdateApplicationWithChartInfo(ctx context.Context, in *InstallReleaseRequest) (*UpgradeReleaseResponse, error)
2525
IsReleaseInstalled(ctx context.Context, in *ReleaseIdentifier) (*BooleanResponse, error)
2626
RollbackRelease(ctx context.Context, in *RollbackReleaseRequest) (*BooleanResponse, error)
27+
TemplateChart(ctx context.Context, in *InstallReleaseRequest) (*TemplateChartResponse, error)
2728
}
2829

2930
type HelmAppClientImpl struct {
@@ -247,4 +248,16 @@ func (impl *HelmAppClientImpl) RollbackRelease(ctx context.Context, in *Rollback
247248
return nil, err
248249
}
249250
return response, nil
250-
}
251+
}
252+
253+
func (impl *HelmAppClientImpl) TemplateChart(ctx context.Context, in *InstallReleaseRequest) (*TemplateChartResponse, error) {
254+
applicationClient, err := impl.getApplicationClient()
255+
if err != nil {
256+
return nil, err
257+
}
258+
response, err := applicationClient.TemplateChart(ctx, in)
259+
if err != nil {
260+
return nil, err
261+
}
262+
return response, nil
263+
}

0 commit comments

Comments
 (0)