-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm.go
219 lines (170 loc) · 5.51 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
214
215
216
217
218
219
package provisioner
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/apex/log"
"gopkg.in/yaml.v2"
"github.com/little-angry-clouds/particle/internal/cmd"
"github.com/little-angry-clouds/particle/internal/config"
customError "github.com/little-angry-clouds/particle/internal/error"
)
// Helm is an implementation of the provisioner interface. It uses helm to manage the kubernetes cluster:
// https://helm.sh/
type Helm struct {
Logger *log.Entry
}
// Converge ensures that the deployment is executed on the kubernetes cluster.
func (h *Helm) Converge(configuration config.ParticleConfiguration, cmd cmd.Cmd) error {
var err error
var logger *log.Entry = h.Logger
var values map[string]interface{} = configuration.Provisioner.Values
err = h.helmInstall(logger, cmd, ".", "", values)
return err
}
// Cleanup ensures that there's no rest of the main chart nor of its dependencies.
func (h *Helm) Cleanup(configuration config.ParticleConfiguration, cmd cmd.Cmd) error {
var logger *log.Entry = h.Logger
var err error
var name string
var prepare []config.Prepare = configuration.Prepare
for _, chart := range prepare {
if strings.Contains(chart.Name, "/") {
chart.Name = strings.Split(chart.Name, "/")[1]
}
err = h.helmDelete(cmd, chart.Name)
err = isRealError(logger, err)
if err != nil {
return err
}
}
// Delete main chart
path, err := os.Getwd()
if err != nil {
return err
}
name = filepath.Base(path)
err = h.helmDelete(cmd, name)
if err != nil {
return err
}
return err
}
// Dependency locally adds all the helm repositories. It basically executes "helm repo add $whatever".
func (h *Helm) Dependency(configuration config.ParticleConfiguration, cmd cmd.Cmd) error {
var logger *log.Entry = h.Logger
var err error
var args []string
var dependencies []config.Dependencies = configuration.Dependency.Charts
// If there's no dependencies, or is set but not a list, just exit
if dependencies == nil {
return nil
}
for _, dependency := range dependencies {
args = []string{"helm", "repo", "add", dependency.RepositoryName, dependency.RepositoryURL}
err = cmd.Initialize(logger, args)
if err != nil {
return err
}
err = cmd.Run()
stderr := cmd.GetStderr()
if strings.Contains(stderr, "already exists, please specify a different name") {
err = &helmRepoExists{Name: dependency.RepositoryName}
} else if strings.Contains(stderr, "exit status 1") {
err = customError.FormatGenericGolangOutput(stderr)
}
err = isRealError(logger, err)
if err != nil {
return err
}
}
return err
}
// Prepare installs the dependencies.
func (h *Helm) Prepare(configuration config.ParticleConfiguration, cmd cmd.Cmd) error {
var logger *log.Entry = h.Logger
var err error
var dependencies []config.Prepare = configuration.Prepare
// If there's no dependencies, or is set but not a list, just exit
if dependencies == nil {
return nil
}
for _, chart := range dependencies {
err = h.helmInstall(logger, cmd, chart.Name, chart.Version, chart.Values)
}
return err
}
// helmInstall installs the helm chart, whenever is from Converge or from Prepare. It should not be used, it's only for internal usage.
func (h *Helm) helmInstall(logger *log.Entry, cmd cmd.Cmd, chart string, version string, values map[string]interface{}) error {
var err error
var chartName string
// If the chart is local, use the directory's name as chart name
// If not, use the chart's name
// The chart is "." because it installs the chart we're developing
if chart == "." {
path, err := os.Getwd()
if err != nil {
return err
}
chartName = filepath.Base(path)
} else {
chartName = strings.Split(chart, "/")[1]
}
args := []string{"helm", "upgrade", "--install", chartName, "--wait", chart}
if version != "" {
args = append(args, "--version", version)
}
// If the chart has some configuration, write it on a temporary file for helm to use it and destroy it when finished
if values != nil {
// Create temporary file with the defined values
file, err := ioutil.TempFile("/tmp/", "particle-helm-"+chartName)
if err != nil {
return err
}
defer os.Remove(file.Name())
f, err := yaml.Marshal(&values)
if err != nil {
return err
}
err = ioutil.WriteFile(file.Name(), f, 0644)
if err != nil {
return err
}
args = append(args, "-f", file.Name())
}
err = cmd.Initialize(logger, args)
if err != nil {
return err
}
err = cmd.Run()
stderr := cmd.GetStderr()
if strings.Contains(stderr, "Kubernetes cluster unreachable: Get \"http://localhost:8080/version?timeout=32s\": dial tcp 127.0.0.1:8080: connect: connection refused") {
err = &chartCantInstall{Name: chart}
} else if strings.Contains(stderr, "exit status 1") {
err = customError.FormatGenericGolangOutput(stderr)
}
return isRealError(logger, err)
}
// helmDelete deletes the helm chart, whenever is from Converge or from Prepare. It should not be used, it's only for internal usage.
func (h *Helm) helmDelete(cmd cmd.Cmd, chart string) error {
var logger *log.Entry = h.Logger
var err error
args := []string{"helm", "delete", chart}
err = cmd.Initialize(logger, args)
if err != nil {
return err
}
err = cmd.Run()
stderr := cmd.GetStderr()
switch {
case strings.Contains(stderr, "Release not loaded"):
err = &chartNotInstalled{Name: chart}
case strings.Contains(stderr, "Kubernetes cluster unreachable"):
err = &chartCantDelete{Name: chart}
case strings.Contains(stderr, "exit status 1"):
err = customError.FormatGenericGolangOutput(stderr)
}
err = isRealError(logger, err)
return err
}