From 21325c1272626bb6d520f11091a1f6e2448a398f Mon Sep 17 00:00:00 2001 From: Anirudh M Date: Wed, 17 Jun 2020 14:25:38 +0530 Subject: [PATCH] Add install chart endpoint --- cmd/endpoints/install/installcontract.go | 13 +++++ cmd/endpoints/install/installhandler.go | 67 ++++++++++++++++++++++++ cmd/endpoints/list/listhandler.go | 4 +- cmd/service/service.go | 2 + go.sum | 1 + pkg/cli/values/options.go | 1 + pkg/http/install.go | 42 --------------- pkg/http/repo_update.go | 54 ------------------- 8 files changed, 86 insertions(+), 98 deletions(-) create mode 100644 cmd/endpoints/install/installcontract.go create mode 100644 cmd/endpoints/install/installhandler.go delete mode 100644 pkg/http/install.go delete mode 100644 pkg/http/repo_update.go diff --git a/cmd/endpoints/install/installcontract.go b/cmd/endpoints/install/installcontract.go new file mode 100644 index 0000000..9b5047a --- /dev/null +++ b/cmd/endpoints/install/installcontract.go @@ -0,0 +1,13 @@ +package install + +type InstallRequest struct { + RequestID string + ReleaseName string + ReleaseNamespace string + ChartPath string +} + +type InstallReponse struct { + Status bool + ReleaseStatus string +} diff --git a/cmd/endpoints/install/installhandler.go b/cmd/endpoints/install/installhandler.go new file mode 100644 index 0000000..53b234a --- /dev/null +++ b/cmd/endpoints/install/installhandler.go @@ -0,0 +1,67 @@ +package install + +import ( + "encoding/json" + "fmt" + "net/http" + + "helm.sh/helm/v3/cmd/servercontext" + "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/chart" + "helm.sh/helm/v3/pkg/chart/loader" +) + +func Handler() http.Handler { + return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + + res.Header().Set("Content-Type", "application/json") + defer req.Body.Close() + + var request InstallRequest + decoder := json.NewDecoder(req.Body) + decoder.UseNumber() + + if err := decoder.Decode(&request); err != nil { + fmt.Printf("error in request: %v", err) + return + } + + request.RequestID = req.Header.Get("Request-Id") + request.ReleaseName = req.Header.Get("Release-Name") + request.ReleaseNamespace = req.Header.Get("Release-Namespace") + request.ChartPath = req.Header.Get("Chart-Path") + + install := action.NewInstall(servercontext.App().ActionConfig) + install.ReleaseName = request.ReleaseName + install.Namespace = request.ReleaseNamespace + + cp, err := install.ChartPathOptions.LocateChart(request.ChartPath, servercontext.App().Config) + if err != nil { + fmt.Printf("error in locating chart: %v", err) + return + } + + var requestedChart *chart.Chart + if requestedChart, err = loader.Load(cp); err != nil { + fmt.Printf("error in loading chart: %v", err) + return + } + + var vals map[string]interface{} + release, err := install.Run(requestedChart, vals) + if err != nil { + fmt.Printf("error in installing chart: %v", err) + return + } + + response := InstallReponse{Status: true, ReleaseStatus: string(release.Info.Status)} + + payload, err := json.Marshal(response) + if err != nil { + fmt.Printf("error parsing response %v", err) + return + } + + res.Write(payload) + }) +} diff --git a/cmd/endpoints/list/listhandler.go b/cmd/endpoints/list/listhandler.go index a0f7dcd..59c875b 100644 --- a/cmd/endpoints/list/listhandler.go +++ b/cmd/endpoints/list/listhandler.go @@ -5,8 +5,8 @@ import ( "fmt" "net/http" + "helm.sh/helm/v3/cmd/servercontext" "helm.sh/helm/v3/pkg/action" - "helm.sh/helm/v3/pkg/servercontext" ) func Handler() http.Handler { @@ -31,7 +31,7 @@ func Handler() http.Handler { list.SetStateMask() results, err := list.Run() if err != nil { - fmt.Print("error while running helm list") + fmt.Printf("error while running helm list %v", err) } var helmReleases []HelmRelease diff --git a/cmd/service/service.go b/cmd/service/service.go index af2fcfa..19fe281 100644 --- a/cmd/service/service.go +++ b/cmd/service/service.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + "helm.sh/helm/v3/cmd/endpoints/install" "helm.sh/helm/v3/cmd/endpoints/list" "helm.sh/helm/v3/cmd/endpoints/ping" "helm.sh/helm/v3/cmd/servercontext" @@ -18,6 +19,7 @@ func startServer(appconfig *servercontext.Application) { router := http.NewServeMux() router.Handle("/ping", ping.Handler()) router.Handle("/list", list.Handler()) + router.Handle("/install", install.Handler()) err := http.ListenAndServe(fmt.Sprintf(":%d", 8080), router) if err != nil { diff --git a/go.sum b/go.sum index e4fc771..2cacc86 100644 --- a/go.sum +++ b/go.sum @@ -665,6 +665,7 @@ gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= helm.sh/helm v1.2.1 h1:Jrn7kKQqQ/hnFWZEX+9pMFvYqFexkzrBnGqYBmIph7c= helm.sh/helm v2.16.7+incompatible h1:3Hp8GLmr6uuBNYGmTQ4p3J4N6xKqxvFDswxHAOposlM= +helm.sh/helm v2.16.9+incompatible h1:31XFG6KPAbh2A/oDgIaQFR8L63vtyxN7tOt64URUJvY= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/cli/values/options.go b/pkg/cli/values/options.go index e6ad717..ebb74ae 100644 --- a/pkg/cli/values/options.go +++ b/pkg/cli/values/options.go @@ -39,6 +39,7 @@ type Options struct { // MergeValues merges values from files specified via -f/--values and directly // via --set, --set-string, or --set-file, marshaling them to YAML func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, error) { + base := map[string]interface{}{} // User specified a values files via -f/--values diff --git a/pkg/http/install.go b/pkg/http/install.go deleted file mode 100644 index 8ad1402..0000000 --- a/pkg/http/install.go +++ /dev/null @@ -1,42 +0,0 @@ -package http - -import ( - "fmt" - - "helm.sh/helm/v3/pkg/action" - "helm.sh/helm/v3/pkg/chart" - "helm.sh/helm/v3/pkg/chart/loader" - "helm.sh/helm/v3/pkg/cli" - "helm.sh/helm/v3/pkg/release" -) - -var ( - settings = cli.New() -) - -func Install(cfg *action.Configuration, chartName, relName string) (*release.Release, error) { - settings := cli.New() - - opts := make(map[string]interface{}) - - install := action.NewInstall(cfg) - install.ReleaseName = relName - install.Namespace = "default" - - fmt.Printf("%+v %#v\n", install, settings) - cp, err := install.ChartPathOptions.LocateChart(chartName, settings) - if err != nil { - return nil, err - } - var requestedChart *chart.Chart - if requestedChart, err = loader.Load(cp); err != nil { - return nil, err - } - - release, err := install.Run(requestedChart, opts) - if err != nil { - return nil, err - } - // deal with dependent charts later - return release, nil -} diff --git a/pkg/http/repo_update.go b/pkg/http/repo_update.go deleted file mode 100644 index 5e3bc1e..0000000 --- a/pkg/http/repo_update.go +++ /dev/null @@ -1,54 +0,0 @@ -package http - -import ( - "fmt" - "io" - "os" - "sync" - - "helm.sh/helm/v3/pkg/getter" - "helm.sh/helm/v3/pkg/repo" -) - -type repoUpdateOptions struct { - update func([]*repo.ChartRepository, io.Writer) - repoFile string -} - -func HelmRepoUpdate() error { - o := &repoUpdateOptions{update: updateCharts} - o.repoFile = settings.RepositoryConfig - f, err := repo.LoadFile(o.repoFile) - if err != nil { - return err - } - var repos []*repo.ChartRepository - for _, cfg := range f.Repositories { - r, err := repo.NewChartRepository(cfg, getter.All(settings)) - if err != nil { - return err - } - repos = append(repos, r) - } - - o.update(repos, os.Stdout) - return nil -} - -func updateCharts(repos []*repo.ChartRepository, out io.Writer) { - fmt.Fprintln(out, "Hang tight while we grab the latest from your chart repositories...") - var wg sync.WaitGroup - for _, re := range repos { - wg.Add(1) - go func(re *repo.ChartRepository) { - defer wg.Done() - if _, err := re.DownloadIndexFile(); err != nil { - fmt.Fprintf(out, "...Unable to get an update from the %q chart repository (%s):\n\t%s\n", re.Config.Name, re.Config.URL, err) - } else { - fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) - } - }(re) - } - wg.Wait() - fmt.Fprintln(out, "Update Complete. ⎈ Happy Helming!⎈ ") -}