fixed uninstall cmd

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2020-04-06 17:49:57 +02:00
parent 896b9fd47e
commit 814d4ca832
3 changed files with 68 additions and 90 deletions

View File

@ -1,70 +0,0 @@
package helm
import (
"gopkg.in/yaml.v2"
action "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
loader "helm.sh/helm/v3/pkg/chart/loader"
)
type HelmChart struct {
chart *chart.Chart
actionConfig *HelmConfig
Path string
Name string
}
func NewChart(name, chartpath string) *HelmChart {
chart, err := loader.Load(chartpath)
if err != nil {
return nil
}
return &HelmChart{
chart: chart,
Path: chartpath,
Name: name,
}
}
func (chart *HelmChart) SetActionConfig(config *HelmConfig) error {
chart.actionConfig = config
return nil
}
func (chart *HelmChart) Validate() error {
_, err := yaml.Marshal(chart.chart.Metadata)
if err != nil {
return nil
}
return nil
}
func (chart *HelmChart) Install() error {
err := chart.actionConfig.InitKubeClient()
if err != nil {
return err
}
actInstall := action.NewInstall(chart.actionConfig.Config)
actInstall.ReleaseName = chart.Name
actInstall.Namespace = chart.actionConfig.Settings.Namespace()
release, err := actInstall.Run(chart.chart, map[string]interface{}{})
if err != nil {
return err
}
println("Release status: ", release.Info.Status)
println("Release description: ", release.Info.Description)
return chart.actionConfig.Config.Releases.Update(release)
}
func (chart *HelmChart) Uninstall() error {
err := chart.actionConfig.InitKubeClient()
if err != nil {
return err
}
actUninstall := action.NewUninstall(chart.actionConfig.Config)
_, err = actUninstall.Run(chart.Name)
return err
}

View File

@ -1,34 +1,43 @@
package helm package helm
import ( import (
"errors"
"log" "log"
"os"
action "helm.sh/helm/v3/pkg/action" action "helm.sh/helm/v3/pkg/action"
loader "helm.sh/helm/v3/pkg/chart/loader"
env "helm.sh/helm/v3/pkg/cli" env "helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/release"
) )
type HelmConfig struct { type HelmActions struct {
Config *action.Configuration Config *action.Configuration
Settings *env.EnvSettings Settings *env.EnvSettings
kube_conn_init bool kube_conn_init bool
} }
func NewHelmConfig(settings *env.EnvSettings) *HelmConfig { func NewHelmActions(settings *env.EnvSettings) *HelmActions {
if settings == nil { if settings == nil {
settings = env.New() settings = env.New()
} }
return &HelmConfig{ return &HelmActions{
Config: new(action.Configuration), Config: new(action.Configuration),
Settings: settings, Settings: settings,
kube_conn_init: false, kube_conn_init: false,
} }
} }
func (hc *HelmConfig) InitKubeClient() error { func (hc *HelmActions) InitKubeClient() error {
if hc.kube_conn_init { if hc.kube_conn_init {
return nil return nil
} }
if err := hc.Config.Init(hc.Settings.RESTClientGetter(), hc.Settings.Namespace(), "memory", log.Printf); err != nil { if err := hc.Config.Init(
hc.Settings.RESTClientGetter(),
hc.Settings.Namespace(),
"configmap",
log.Printf,
); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := hc.Config.KubeClient.IsReachable(); err != nil { if err := hc.Config.KubeClient.IsReachable(); err != nil {
@ -37,3 +46,52 @@ func (hc *HelmConfig) InitKubeClient() error {
hc.kube_conn_init = true hc.kube_conn_init = true
return nil return nil
} }
func (hc *HelmActions) Install(name, chartpath string) error {
hc.InitKubeClient()
if chartpath == "" {
cwd, err := os.Getwd()
if err != nil {
return nil
}
chartpath = cwd
}
chart, err := loader.Load(chartpath)
if err != nil {
return nil
}
actInstall := action.NewInstall(hc.Config)
actInstall.ReleaseName = name
actInstall.Namespace = hc.Settings.Namespace()
release, err := actInstall.Run(chart, map[string]interface{}{})
if err != nil {
return err
}
println("Release status: ", release.Info.Status)
println("Release description: ", release.Info.Description)
return hc.Config.Releases.Update(release)
}
func (hc *HelmActions) Uninstall(name string) error {
hc.InitKubeClient()
release, err := hc.Get(name)
if err != nil {
return err
}
if release == nil {
return errors.New("No release found with the name provided.")
}
actUninstall := action.NewUninstall(hc.Config)
_, err = actUninstall.Run(name)
return err
}
func (hc *HelmActions) Get(name string) (*release.Release, error) {
hc.InitKubeClient()
actGet := action.NewGet(hc.Config)
return actGet.Run(name)
}

View File

@ -39,8 +39,7 @@ func GetDefault() *Engine {
type Project struct { type Project struct {
Config *types.Config Config *types.Config
HelmConfig *helm.HelmConfig Helm *helm.HelmActions
HelmChart *helm.HelmChart
ProjectDir string ProjectDir string
Name string `yaml:"-" json:"-"` Name string `yaml:"-" json:"-"`
} }
@ -53,8 +52,7 @@ func NewProject(config types.ConfigDetails, name string) (*Project, error) {
p := Project{ p := Project{
Config: model, Config: model,
HelmConfig: helm.NewHelmConfig(nil), Helm: helm.NewHelmActions(nil),
HelmChart: nil,
ProjectDir: config.WorkingDir, ProjectDir: config.WorkingDir,
Name: name, Name: name,
} }
@ -95,18 +93,10 @@ func (p *Project) ExportToCharts(path string) error {
} }
func (p *Project) Install(name, path string) error { func (p *Project) Install(name, path string) error {
if p.HelmChart == nil { return p.Helm.Install(name, path)
chart := helm.NewChart(name, path)
chart.SetActionConfig(p.HelmConfig)
p.HelmChart = chart
}
return p.HelmChart.Install()
} }
func (p *Project) Uninstall(name string) error { func (p *Project) Uninstall(name string) error {
if p.HelmChart == nil {
p.HelmChart = helm.NewChart(name, "") return p.Helm.Uninstall(name)
p.HelmChart.SetActionConfig(p.HelmConfig)
}
return p.HelmChart.Uninstall()
} }