-
Notifications
You must be signed in to change notification settings - Fork 217
/
helm.go
213 lines (188 loc) · 5.75 KB
/
helm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package utils
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/ghodss/yaml"
"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"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // Need this for gke clusters
"github.com/spaceuptech/space-cloud/space-cli/cmd/model"
)
// HelmInstall install helm chart
func HelmInstall(chartReleaseName, chartLocation, downloadURL, namespace string, valuesFileObj map[string]interface{}) (*chart.Chart, error) {
actionConfig, helmChart, err := CreateChart(chartLocation, downloadURL)
if err != nil {
return nil, err
}
iCli := action.NewInstall(actionConfig)
iCli.ReleaseName = chartReleaseName
iCli.CreateNamespace = namespace != model.HelmSpaceCloudNamespace // don't create namespace while setting up space cloud cluster
iCli.Namespace = namespace
iCli.Devel = true
rel, err := iCli.Run(helmChart, valuesFileObj)
if err != nil {
return nil, err
}
LogInfo(fmt.Sprintf("Successfully installed chart: (%s)", rel.Name))
return rel.Chart, nil
}
// HelmUninstall uninstall helm chart
func HelmUninstall(releaseName string) error {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
return err
}
uCli := action.NewUninstall(actionConfig)
rel, err := uCli.Run(releaseName)
if err != nil {
return err
}
LogInfo(fmt.Sprintf("Successfully removed chart: (%s)", rel.Release.Name))
return nil
}
// HelmShow executes the helm show command
func HelmShow(chartLocation, downloadURL, releaseArg string) (string, error) {
_, _, err := CreateChart(chartLocation, downloadURL)
if err != nil {
return "", err
}
sCli := action.NewShow(action.ShowValues)
info, err := sCli.Run("")
if err != nil {
return "", err
}
return info, nil
}
// HelmGet gets chart info
func HelmGet(releaseName string) (*release.Release, error) {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
return nil, err
}
iCli := action.NewGet(actionConfig)
info, err := iCli.Run(releaseName)
if err != nil {
return nil, err
}
return info, nil
}
// HelmList uninstall helm chart
func HelmList(filterRegex string) ([]*release.Release, error) {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
return nil, err
}
lCli := action.NewList(actionConfig)
list, err := lCli.Run()
if err != nil {
return nil, err
}
filteredList := make([]*release.Release, 0)
for _, item := range list {
if strings.Contains(item.Namespace, filterRegex) {
filteredList = append(filteredList, item)
}
}
return filteredList, nil
}
// HelmUpgrade upgrade space cloud chart
func HelmUpgrade(releaseName, chartLocation, downloadURL, namespace string, valuesFileObj map[string]interface{}) (*chart.Chart, error) {
actionConfig, helmChart, err := CreateChart(chartLocation, downloadURL)
if err != nil {
return nil, err
}
uCli := action.NewUpgrade(actionConfig)
uCli.Namespace = namespace
rel, err := uCli.Run(releaseName, helmChart, valuesFileObj)
if err != nil {
return nil, err
}
LogInfo(fmt.Sprintf("Successfully upgraded chart: (%s)", rel.Name))
return rel.Chart, nil
}
// CreateChart returns chart object, which describe the provide chart
func CreateChart(chartLocation, downloadURL string) (*action.Configuration, *chart.Chart, error) {
settings := cli.New()
actionConfig := new(action.Configuration)
if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), os.Getenv("HELM_DRIVER"), log.Printf); err != nil {
return nil, nil, err
}
var helmChart *chart.Chart
var err error
if chartLocation == "" {
res, err := http.Get(downloadURL)
if err != nil {
return nil, nil, err
}
if res.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("received invalid status code (%s)", res.Status)
}
helmChart, err = loader.LoadArchive(res.Body)
if err != nil {
return nil, nil, err
}
} else {
helmChart, err = loader.Load(chartLocation)
if err != nil {
return nil, nil, err
}
}
return actionConfig, helmChart, err
}
// ExtractValuesObj extract chart values from yaml file & cli flags
func ExtractValuesObj(setValuesFlag, valuesYamlFile string) (map[string]interface{}, error) {
valuesFileObj := map[string]interface{}{}
if valuesYamlFile != "" {
var bodyInBytes []byte
var err error
if strings.HasPrefix(valuesYamlFile, "http") {
// download file from the internet
resp, err := http.Get(valuesYamlFile)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("")
}
bodyInBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
} else {
// read locally available file
bodyInBytes, err = ioutil.ReadFile(valuesYamlFile)
if err != nil {
return nil, err
}
}
if err := yaml.Unmarshal(bodyInBytes, &valuesFileObj); err != nil {
return nil, err
}
}
setValuesObj := map[string]interface{}{}
if setValuesFlag != "" {
arr := strings.Split(setValuesFlag, ",")
for _, element := range arr {
tempArr := strings.Split(element, "=")
if len(tempArr) != 2 {
return nil, fmt.Errorf("invalid value (%s) provided for flag --set, it should be in format foo1=bar1,foo2=bar2", tempArr)
}
setValuesObj[tempArr[0]] = tempArr[1]
}
}
// override values of yaml file
for key, value := range setValuesObj {
valuesFileObj[key] = value
}
return valuesFileObj, nil
}