Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/helm-app/HelmAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type HelmAppRestHandler interface {
DeleteApplication(w http.ResponseWriter, r *http.Request)
UpdateApplication(w http.ResponseWriter, r *http.Request)
TemplateChart(w http.ResponseWriter, r *http.Request)
GetManifestForDeploymentTemplate(w http.ResponseWriter, r *http.Request)
SaveHelmAppDetailsViewedTelemetryData(w http.ResponseWriter, r *http.Request)
}

Expand Down Expand Up @@ -347,6 +348,37 @@ func (handler *HelmAppRestHandlerImpl) UpdateApplication(w http.ResponseWriter,
common.WriteJsonResp(w, err, res, http.StatusOK)
}

func (handler *HelmAppRestHandlerImpl) GetManifestForDeploymentTemplate(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
return
}
vars := mux.Vars(r)
chartRefId, err := strconv.Atoi(vars["chartRefId"])
if err != nil {
handler.logger.Error(err)
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
request := &openapi2.TemplateChartResponse{}
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(request)
if err != nil {
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
defer cancel()
response, err := handler.helmAppService.GetManifest(ctx, chartRefId, *request.Manifest)
if err != nil {
handler.logger.Errorw("Error in helm-template", "err", err)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, response, http.StatusOK)
}

func (handler *HelmAppRestHandlerImpl) TemplateChart(w http.ResponseWriter, r *http.Request) {
userId, err := handler.userAuthService.GetLoggedInUser(r)
if userId == 0 || err != nil {
Expand Down
1 change: 1 addition & 0 deletions api/helm-app/HelmAppRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ func (impl *HelmAppRouterImpl) InitAppListRouter(helmRouter *mux.Router) {
HandlerFunc(impl.helmAppRestHandler.DeleteApplication).Methods("DELETE")

helmRouter.Path("/template-chart").HandlerFunc(impl.helmAppRestHandler.TemplateChart).Methods("POST")
helmRouter.Path("/manifest/{chartRefId}").HandlerFunc(impl.helmAppRestHandler.GetManifestForDeploymentTemplate).Methods("POST")
}
73 changes: 72 additions & 1 deletion api/helm-app/HelmAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/devtron-labs/devtron/pkg/chart"
"github.com/devtron-labs/devtron/util/k8s"
"net/http"
"reflect"
Expand Down Expand Up @@ -59,6 +60,7 @@ type HelmAppService interface {
GetClusterConf(clusterId int) (*ClusterConfig, error)
GetDevtronHelmAppIdentifier() *AppIdentifier
UpdateApplicationWithChartInfoWithExtraValues(ctx context.Context, appIdentifier *AppIdentifier, chartRepository *ChartRepository, extraValues map[string]interface{}, extraValuesYamlUrl string, useLatestChartVersion bool) (*openapi.UpdateReleaseResponse, error)
GetManifest(ctx context.Context, chartRefId int, valuesYaml string) (*openapi2.TemplateChartResponse, error)
TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error)
GetNotes(ctx context.Context, request *InstallReleaseRequest) (string, error)
GetRevisionHistoryMaxValue(appType SourceAppType) int32
Expand All @@ -80,6 +82,8 @@ type HelmAppServiceImpl struct {
clusterRepository clusterRepository.ClusterRepository
K8sUtil *k8s.K8sUtil
helmReleaseConfig *HelmReleaseConfig
chartService chart.ChartService
chartTemplateServiceImpl util.ChartTemplateService
}

func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.ClusterService,
Expand All @@ -89,7 +93,9 @@ func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.Clu
environmentService cluster.EnvironmentService, pipelineRepository pipelineConfig.PipelineRepository,
installedAppRepository repository.InstalledAppRepository, appRepository app.AppRepository,
clusterRepository clusterRepository.ClusterRepository, K8sUtil *k8s.K8sUtil,
helmReleaseConfig *HelmReleaseConfig) *HelmAppServiceImpl {
helmReleaseConfig *HelmReleaseConfig,
chartService chart.ChartService,
chartTemplateServiceImpl util.ChartTemplateService) *HelmAppServiceImpl {
return &HelmAppServiceImpl{
logger: Logger,
clusterService: clusterService,
Expand All @@ -106,6 +112,8 @@ func NewHelmAppServiceImpl(Logger *zap.SugaredLogger, clusterService cluster.Clu
clusterRepository: clusterRepository,
K8sUtil: K8sUtil,
helmReleaseConfig: helmReleaseConfig,
chartService: chartService,
chartTemplateServiceImpl: chartTemplateServiceImpl,
}
}

Expand Down Expand Up @@ -684,6 +692,69 @@ func (impl *HelmAppServiceImpl) UpdateApplicationWithChartInfoWithExtraValues(ct
return response, nil
}

func (impl HelmAppServiceImpl) GetManifest(ctx context.Context, chartRefId int, valuesYaml string) (*openapi2.TemplateChartResponse, error) {
refChart, template, err, version, _ := impl.chartService.GetRefChart(chart.TemplateRequest{ChartRefId: chartRefId})
if err != nil {
impl.logger.Errorw("error in getting refChart", "err", err, "chartRefId", chartRefId)
return nil, err
}

//valuesYaml, err := ioutil.ReadFile(filepath.Clean(filepath.Join(refChart, "values.yaml")))
//if err != nil {
// impl.logger.Errorw("error in reading schema.json file for refChart", "err", err, "chartRefId", chartRefId)
//}

chartBytes, chartZipPath, err := impl.chartTemplateServiceImpl.LoadChartInBytes(refChart, false, "", "")
if err != nil {
impl.logger.Errorw("error in getting chart", "err", err)
return nil, err
}
defer impl.chartTemplateServiceImpl.CleanDir(chartZipPath)

k8sServerVersion, err := impl.K8sUtil.GetKubeVersion()
if err != nil {
impl.logger.Errorw("exception caught in getting k8sServerVersion", "err", err)
return nil, err
}
installReleaseRequest := &InstallReleaseRequest{
ChartName: template,
ChartVersion: version,
ValuesYaml: valuesYaml,
K8SVersion: k8sServerVersion.String(),
ChartRepository: &ChartRepository{
Name: "repo",
Url: "http://localhost:8080/",
Username: "admin",
Password: "password",
},
ReleaseIdentifier: &ReleaseIdentifier{
ReleaseNamespace: "devtron-demo",
ReleaseName: "release-name",
},
ChartContent: &ChartContent{
Content: chartBytes,
},
}
config, err := impl.GetClusterConf(1)
if err != nil {
impl.logger.Errorw("error in fetching cluster detail", "clusterId", 1, "err", err)
return nil, err
}

installReleaseRequest.ReleaseIdentifier.ClusterConfig = config

templateChartResponse, err := impl.helmAppClient.TemplateChart(ctx, installReleaseRequest)
if err != nil {
impl.logger.Errorw("error in templating chart", "err", err)
return nil, err
}
response := &openapi2.TemplateChartResponse{
Manifest: &templateChartResponse.GeneratedManifest,
}

return response, nil
}

func (impl *HelmAppServiceImpl) TemplateChart(ctx context.Context, templateChartRequest *openapi2.TemplateChartRequest) (*openapi2.TemplateChartResponse, error) {
appStoreApplicationVersionId := int(*templateChartRequest.AppStoreApplicationVersionId)
environmentId := int(*templateChartRequest.EnvironmentId)
Expand Down
Loading