add list and ping http endpoints

pull/8330/head
ys.achinta 5 years ago
parent 37d9bfa79c
commit a8a85edd5c

@ -0,0 +1,15 @@
package list
type ListRequest struct {
RequestID string
}
type ListRespose struct {
Status bool
Data []HelmRelease
}
type HelmRelease struct {
Release string `json:"release"`
Namespace string `json:"namespace"`
}

@ -0,0 +1,52 @@
package list
import (
"encoding/json"
"fmt"
"net/http"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/servercontext"
)
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 ListRequest
decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
if err := decoder.Decode(&request); err != nil {
fmt.Println("error in request")
return
}
request.RequestID = req.Header.Get("Request-Id")
list := action.NewList(servercontext.App().ActionConfig)
list.SetStateMask()
results, err := list.Run()
if err != nil {
fmt.Print("error while running helm list")
}
var helmReleases []HelmRelease
for _, res := range results {
r := HelmRelease{Release: res.Name, Namespace: res.Namespace}
helmReleases = append(helmReleases, r)
}
response := ListRespose{Status: true, Data: helmReleases}
payload, err := json.Marshal(response)
if err != nil {
fmt.Println("error parsing response")
return
}
res.Write(payload)
})
}

@ -0,0 +1,10 @@
package ping
type PingReq struct {
RequestID string
}
type PingResponse struct {
Status bool
Data string
}

@ -0,0 +1,35 @@
package ping
import (
"encoding/json"
"fmt"
"net/http"
)
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 PingReq
decoder := json.NewDecoder(req.Body)
decoder.UseNumber()
if err := decoder.Decode(&request); err != nil {
fmt.Println("error in request")
return
}
request.RequestID = req.Header.Get("Request-Id")
response := PingResponse{Status: true, Data: "pong"}
payload, err := json.Marshal(response)
if err != nil {
fmt.Println("error parsing response")
return
}
res.Write(payload)
})
}

@ -0,0 +1,49 @@
package servercontext
import (
"fmt"
"log"
"os"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
)
var app Application
type Application struct {
Config *cli.EnvSettings
ActionConfig *action.Configuration
}
func App() *Application {
return &app
}
func NewApp() *Application {
app.Config = envSettings()
app.ActionConfig = boostrapActionConfig()
return &app
}
func envSettings() *cli.EnvSettings {
envSettings := cli.New()
for k, v := range envSettings.EnvVars() {
fmt.Println(k, v)
}
return envSettings
}
func debug(format string, v ...interface{}) {
format = fmt.Sprintf("[debug] %s\n", format)
log.Output(2, fmt.Sprintf(format, v...))
}
func boostrapActionConfig() *action.Configuration {
actionConfig := new(action.Configuration)
if err := actionConfig.Init(app.Config.RESTClientGetter(), app.Config.Namespace(), os.Getenv("HELM_DRIVER"), debug); err != nil {
log.Fatalf("error getting configuration: %v", err)
return nil
}
return actionConfig
}

@ -2,70 +2,25 @@ package main
import ( import (
"fmt" "fmt"
"log" "net/http"
"os"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/cmd/endpoints/list"
"helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/cmd/endpoints/ping"
"helm.sh/helm/v3/pkg/gates" "helm.sh/helm/v3/cmd/servercontext"
"helm.sh/helm/v3/pkg/http"
_ "k8s.io/client-go/plugin/pkg/client/auth"
) )
var (
settings = cli.New()
)
func debug(format string, v ...interface{}) {
format = fmt.Sprintf("[debug] %s\n", format)
log.Output(2, fmt.Sprintf(format, v...))
}
const FeatureGateOCI = gates.Gate("HELM_EXPERIMENTAL_OCI")
// Input: repositories.yaml, repositories cache (optional), chart location
func main() { func main() {
actionConfig := new(action.Configuration) app := servercontext.NewApp()
for k, v := range settings.EnvVars() { startServer(app)
fmt.Println(k, v)
}
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), debug); err != nil {
log.Fatalf("error getting configuration: %v", err)
return
}
if _, err := actionConfig.KubernetesClientSet(); err != nil {
log.Fatalf("error initilizing kubernetes client configuration: %v", err)
return
}
listReleases(actionConfig)
helmRepoUpdate()
// this has to be added in repositories: https://charts.bitnami.com/bitnami
installRelease(actionConfig, "bitnami/redis", "bitnami-redis-2")
}
func listReleases(cfg *action.Configuration) {
releases, err := http.List(cfg)
if err != nil {
panic(err)
}
fmt.Println(string(releases))
} }
func helmRepoUpdate() { func startServer(appconfig *servercontext.Application) {
err := http.HelmRepoUpdate() router := http.NewServeMux()
if err != nil { router.Handle("/ping", ping.Handler())
panic(err) router.Handle("/list", list.Handler())
}
}
func installRelease(cfg *action.Configuration, chartPath string, releaseName string) { err := http.ListenAndServe(fmt.Sprintf(":%d", 8080), router)
fmt.Printf("installing chart: %s. release name: %s ", chartPath, releaseName)
_, err := http.Install(cfg, chartPath, releaseName)
if err != nil { if err != nil {
fmt.Println("error installing chart", err) fmt.Println("error starting server", err)
return
} }
fmt.Printf("installed chart: %s release: %s successfully", chartPath, releaseName)
} }

@ -1,24 +0,0 @@
package main
import (
"fmt"
"net/http"
)
func main() {
startServer()
}
func startServer() {
router := http.NewServeMux()
router.HandleFunc("/ping", pingFunc)
err := http.ListenAndServe(fmt.Sprintf(":%d", 8080), router)
if err != nil {
fmt.Println("error starting server", err)
}
}
func pingFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println("pong")
}
Loading…
Cancel
Save