1919package helm
2020
2121import (
22+ "context"
2223 "errors"
2324 "log"
2425
@@ -28,44 +29,38 @@ import (
2829 loader "helm.sh/helm/v3/pkg/chart/loader"
2930 env "helm.sh/helm/v3/pkg/cli"
3031 "helm.sh/helm/v3/pkg/release"
32+ "k8s.io/cli-runtime/pkg/genericclioptions"
3133)
3234
3335// Actions helm actions
3436type Actions struct {
35- Config * action.Configuration
36- Settings * env.EnvSettings
37- kubeConnInit bool
37+ Config * action.Configuration
38+ Namespace string
3839}
3940
4041// NewHelmActions new helm action
41- func NewHelmActions (settings * env.EnvSettings ) * Actions {
42- if settings == nil {
43- settings = env .New ()
42+ func NewHelmActions (ctx context.Context , getter genericclioptions.RESTClientGetter ) (* Actions , error ) {
43+ if getter == nil {
44+ settings := env .New ()
45+ getter = settings .RESTClientGetter ()
4446 }
45- return & Actions {
46- Config : new (action.Configuration ),
47- Settings : settings ,
48- kubeConnInit : false ,
49- }
50- }
5147
52- func ( hc * Actions ) initKubeClient () error {
53- if hc . kubeConnInit {
54- return nil
48+ namespace := "default"
49+ if ns , _ , err := getter . ToRawKubeConfigLoader (). Namespace (); err == nil {
50+ namespace = ns
5551 }
56- if err := hc .Config .Init (
57- hc .Settings .RESTClientGetter (),
58- hc .Settings .Namespace (),
59- "configmap" ,
60- log .Printf ,
61- ); err != nil {
62- log .Fatal (err )
52+ actions := & Actions {
53+ Config : & action.Configuration {
54+ RESTClientGetter : getter ,
55+ },
56+ Namespace : namespace ,
6357 }
64- if err := hc .Config .KubeClient .IsReachable (); err != nil {
65- return err
58+ err := actions .Config .Init (getter , namespace , "configmap" , log .Printf )
59+ if err != nil {
60+ return nil , err
6661 }
67- hc . kubeConnInit = true
68- return nil
62+
63+ return actions , actions . Config . KubeClient . IsReachable ()
6964}
7065
7166//InstallChartFromDir install from dir
@@ -79,14 +74,9 @@ func (hc *Actions) InstallChartFromDir(name string, chartpath string) error {
7974
8075// InstallChart instal chart
8176func (hc * Actions ) InstallChart (name string , chart * chart.Chart ) error {
82- err := hc .initKubeClient ()
83- if err != nil {
84- return err
85- }
86-
8777 actInstall := action .NewInstall (hc .Config )
8878 actInstall .ReleaseName = name
89- actInstall .Namespace = hc .Settings . Namespace ()
79+ actInstall .Namespace = hc .Namespace
9080
9181 release , err := actInstall .Run (chart , map [string ]interface {}{})
9282 if err != nil {
@@ -99,11 +89,6 @@ func (hc *Actions) InstallChart(name string, chart *chart.Chart) error {
9989
10090// Uninstall uninstall chart
10191func (hc * Actions ) Uninstall (name string ) error {
102- err := hc .initKubeClient ()
103- if err != nil {
104- return err
105- }
106-
10792 release , err := hc .Get (name )
10893 if err != nil {
10994 return err
@@ -122,20 +107,12 @@ func (hc *Actions) Uninstall(name string) error {
122107
123108// Get get released object for a named chart
124109func (hc * Actions ) Get (name string ) (* release.Release , error ) {
125- err := hc .initKubeClient ()
126- if err != nil {
127- return nil , err
128- }
129110 actGet := action .NewGet (hc .Config )
130111 return actGet .Run (name )
131112}
132113
133114// ListReleases lists chart releases
134115func (hc * Actions ) ListReleases () ([]compose.Stack , error ) {
135- err := hc .initKubeClient ()
136- if err != nil {
137- return nil , err
138- }
139116 actList := action .NewList (hc .Config )
140117 releases , err := actList .Run ()
141118 if err != nil {
0 commit comments