passing install configuration to service

pull/8330/head
devdinu 5 years ago
parent 2b515a08b6
commit 87abc3b47f

@ -6,6 +6,7 @@ import (
"helm.sh/helm/v3/pkg/http/api"
"helm.sh/helm/v3/pkg/http/api/list"
"helm.sh/helm/v3/pkg/http/api/logger"
"helm.sh/helm/v3/pkg/http/api/ping"
"helm.sh/helm/v3/pkg/servercontext"
)
@ -17,9 +18,11 @@ func main() {
func startServer(appconfig *servercontext.Application) {
router := http.NewServeMux()
//TODO: use gorilla mux and add middleware to write content type and other headers
cfg := servercontext.App().Config
service := api.NewService(cfg)
app := servercontext.App()
logger.Setup("debug")
service := api.NewService(app.Config, app.ActionConfig)
router.Handle("/ping", ping.Handler())
router.Handle("/list", list.Handler())
router.Handle("/install", api.Install(service))

@ -31,7 +31,8 @@ func Install(svc Service) http.Handler {
}
defer r.Body.Close()
var response InstallResponse
res, err := svc.Install(r.Context(), req.Chart, req.Values)
cfg := InstallConfig{ChartName: req.Chart, Name: req.Name, Namespace: req.Namespace}
res, err := svc.Install(r.Context(), cfg, req.Values)
if err != nil {
response.Error = err.Error()
logger.Errorf("[Install] error while installing chart: %v", err)
@ -41,6 +42,7 @@ func Install(svc Service) http.Handler {
response.Status = res.status
if err := json.NewEncoder(w).Encode(response); err != nil {
logger.Errorf("[Install] error writing response %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
})

@ -10,12 +10,21 @@ import (
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/http/api/logger"
"helm.sh/helm/v3/pkg/servercontext"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type Service struct {
config *cli.EnvSettings
install *action.Install
settings *cli.EnvSettings
actionConfig *action.Configuration
chartloader *action.ChartPathOptions
}
type InstallConfig struct {
Name string
Namespace string
ChartName string
}
type chartValues map[string]interface{}
@ -27,31 +36,25 @@ type installResult struct {
func (s Service) getValues(vals chartValues) (chartValues, error) {
valueOpts := &values.Options{}
//valueOpts.Values = append(valueOpts.Values, vals)
//TODO: we need to make this as Provider, so it'll be able to merge
return valueOpts.MergeValues(getter.All(servercontext.App().Config))
}
func (s Service) Install(ctx context.Context, chartName string, values chartValues) (installResult, error) {
var result installResult
chart, err := s.loadChart(chartName)
func (s Service) Install(ctx context.Context, cfg InstallConfig, values chartValues) (*installResult, error) {
chart, err := s.loadChart(cfg.ChartName)
if err != nil {
return result, err
return nil, err
}
vals, err := s.getValues(values)
if err != nil {
return result, fmt.Errorf("error merging values: %v", err)
return nil, fmt.Errorf("error merging values: %v", err)
}
release, err := s.install.Run(chart, vals)
if err != nil {
return result, fmt.Errorf("error in installing chart: %v", err)
}
if release.Info != nil {
result.status = release.Info.Status.String()
}
return result, nil
return s.installChart(cfg, chart, vals)
}
func (s Service) loadChart(chartName string) (*chart.Chart, error) {
cp, err := s.install.ChartPathOptions.LocateChart(chartName, s.config)
logger.Debugf("[Install] chart name: %s", chartName)
cp, err := s.chartloader.LocateChart(chartName, s.settings)
if err != nil {
return nil, fmt.Errorf("error in locating chart: %v", err)
}
@ -62,10 +65,27 @@ func (s Service) loadChart(chartName string) (*chart.Chart, error) {
return requestedChart, nil
}
func NewService(cfg *cli.EnvSettings) Service {
func (s Service) installChart(icfg InstallConfig, ch *chart.Chart, vals chartValues) (*installResult, error) {
install := action.NewInstall(s.actionConfig)
install.Namespace = icfg.Namespace
install.ReleaseName = icfg.Name
release, err := install.Run(ch, vals)
if err != nil {
return nil, fmt.Errorf("error in installing chart: %v", err)
}
result := new(installResult)
fmt.Println(result)
if release.Info != nil {
result.status = release.Info.Status.String()
}
return result, nil
}
func NewService(settings *cli.EnvSettings, actionConfig *action.Configuration) Service {
return Service{
config: cfg,
//TODO: not sure why this's needed, but we can refactor later,could be passed as param
install: action.NewInstall(servercontext.App().ActionConfig),
settings: settings,
actionConfig: actionConfig,
chartloader: new(action.ChartPathOptions),
}
}

@ -89,7 +89,12 @@ func (h UpgradeHandler) UpgradeRelease(releaseName, releaseNamespace, chartPath
fmt.Printf("Release %q does not exist. Installing it now.\n", releaseName)
//TODO: yet to accomodate namespace and releasename, just refactoring
release, err := h.service.Install(context.TODO(), chartPath, vals)
icfg := InstallConfig{
Namespace: releaseNamespace,
Name: releaseName,
ChartName: chartPath,
}
release, err := h.service.Install(context.TODO(), icfg, vals)
if err != nil {
fmt.Printf("error in request: %v", err)
return false, "", err

Loading…
Cancel
Save