Skip to content

Commit

Permalink
修复install指定命名空间部署到了default
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmin committed Jun 23, 2020
1 parent e460f9b commit 713934f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 43 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BINARY_NAME=helm-api
INSTALL_DIR=/opt/helm-api/

build:
go build -o ${BINARY_NAME}
install:
install -D ${BINARY_NAME} ${INSTALL_DIR}/${BINARY_NAME}
cp conf /opt/helm-api/ -R
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# helm-api

**支持多集群、多命名空间的helm部署http接口**

## 安装
git clone https://github.com/OojaxoO/helm-api.git
cd helm-api
make
make install

## 配置
vim /opt/helm-api/conf/app.ini
[database]
Type = mysql
User = root
Password = test123
Host = 127.0.0.1:3306
Name = nvwa
TablePrefix = kube_

[http]
Port = 10000

## 运行
/opt/helm-api/helm-api
5 changes: 4 additions & 1 deletion conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ User = root
Password = test123
Host = 127.0.0.1:3306
Name = nvwa
TablePrefix = kube_
TablePrefix = kube_

[http]
Port = 10000
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ func main() {
"message": "pong",
})
})
r.Run(":10000")
port := setting.HttpSetting.Port
r.Run(":" + port)
}
4 changes: 1 addition & 3 deletions models/cluster.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package models

import (
// "fmt"

"github.com/jinzhu/gorm"
)

Expand All @@ -17,7 +15,7 @@ func GetCluster(id int) (*Cluster, error) {
var cluster Cluster
err := db.Where("id = ?", id).First(&cluster).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
return nil, err
}
return &cluster, nil
}
10 changes: 8 additions & 2 deletions setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ type Database struct {
TablePrefix string
}

type Http struct {
Port string
}

var DatabaseSetting = &Database{}
var HttpSetting = &Http{}

var cfg *ini.File

func Setup() {
var err error
cfg, err = ini.Load("conf/app.ini")
if err != nil {
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
}
mapTo("database", DatabaseSetting)
mapTo("http", HttpSetting)
}

// mapTo map section
func mapTo(section string, v interface{}) {
err := cfg.Section(section).MapTo(v)
if err != nil {
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
log.Fatalf("Cfg.MapTo %s err: %v", section, err)
}
}
59 changes: 23 additions & 36 deletions views/charts/chats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"io"
"strconv"
"fmt"


"github.com/golang/glog"
Expand All @@ -18,9 +17,8 @@ import (
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/downloader"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/strvals"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/storage/driver"
"sigs.k8s.io/yaml"

"helm-api/util"
"helm-api/models"
Expand All @@ -35,16 +33,11 @@ type releaseElement struct {
Chart string `json:"chart"`
ChartVersion string `json:"chart_version"`
AppVersion string `json:"app_version"`

Notes string `json:"notes,omitempty"`

// TODO: Test Suite?
}

type releaseOptions struct {
Values string `json:"values"`
SetValues []string `json:"set"`
SetStringValues []string `json:"set_string"`
Values map[string]interface {} `json:"values"`
}


Expand All @@ -61,8 +54,9 @@ func actionConfigInit(cluster int, namespace string) (action.Configuration, erro
kubeConfig := "/tmp/config"
kubeContext := kubeCluster.Config
util.WriteFile(kubeConfig, kubeContext)
settings.KubeConfig = kubeConfig
err = actionConfig.Init(settings.RESTClientGetter(), namespace, os.Getenv("HELM_DRIVER"), glog.Infof)
settings.KubeConfig = kubeConfig
clientConfig := kube.GetConfig(settings.KubeConfig, settings.KubeContext, namespace)
err = actionConfig.Init(clientConfig, namespace, os.Getenv("HELM_DRIVER"), glog.Infof)
if err != nil {
glog.Errorf("%+v", err)
return *actionConfig, err
Expand Down Expand Up @@ -135,29 +129,6 @@ func Retrieve(c *gin.Context) {
util.RespOK(c, results)
}

func mergeValues(options releaseOptions) (map[string]interface{}, error) {
vals := map[string]interface{}{}
err := yaml.Unmarshal([]byte(options.Values), &vals)
if err != nil {
return vals, fmt.Errorf("failed parsing values")
}

for _, value := range options.SetValues {
if err := strvals.ParseInto(value, vals); err != nil {
return vals, fmt.Errorf("failed parsing set data")
}
}

for _, value := range options.SetStringValues {
if err := strvals.ParseIntoString(value, vals); err != nil {
return vals, fmt.Errorf("failed parsing set_string data")
}
}

return vals, nil
}


func Update(c *gin.Context) {
namespace := c.Param("namespace")
name := c.Param("name")
Expand All @@ -174,12 +145,13 @@ func Update(c *gin.Context) {
util.RespErr(c, err)
return
}
vals, err := mergeValues(options)
vals := options.Values
if err != nil {
util.RespErr(c, err)
return
}
client := action.NewUpgrade(&actionConfig)
client.Namespace = namespace
histClient := action.NewHistory(&actionConfig)
histClient.Max = 1
if _, err := histClient.Run(name); err == driver.ErrReleaseNotFound {
Expand All @@ -191,6 +163,7 @@ func Update(c *gin.Context) {
return
}
util.RespOK(c, rel)
return
} else if err != nil {
util.RespErr(c, err)
return
Expand Down Expand Up @@ -280,5 +253,19 @@ func runInstall(name string, chart string, client *action.Install, vals map[stri


func Delete(c *gin.Context) {

namespace := c.Param("namespace")
name := c.Param("name")
cluster := com.StrTo(c.Param("cluster")).MustInt()
actionConfig, err := actionConfigInit(cluster, namespace)
if err != nil {
util.RespErr(c, err)
return
}
client := action.NewUninstall(&actionConfig)
results, err := client.Run(name)
if err != nil {
util.RespErr(c, err)
return
}
util.RespOK(c, results)
}

0 comments on commit 713934f

Please sign in to comment.