-
Notifications
You must be signed in to change notification settings - Fork 374
/
provider.go
377 lines (336 loc) · 10.6 KB
/
provider.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package helm
import (
"context"
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/storage/driver"
// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
// Meta is the meta information structure for the provider
type Meta struct {
data *schema.ResourceData
Settings *cli.EnvSettings
HelmDriver string
// Used to lock some operations
sync.Mutex
}
// Provider returns the provider schema to Terraform.
func Provider() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"debug": {
Type: schema.TypeBool,
Optional: true,
Description: "Debug indicates whether or not Helm is running in Debug mode.",
DefaultFunc: schema.EnvDefaultFunc("HELM_DEBUG", false),
},
"plugins_path": {
Type: schema.TypeString,
Optional: true,
Description: "The path to the helm plugins directory",
DefaultFunc: schema.EnvDefaultFunc("HELM_PLUGINS", helmpath.DataPath("plugins")),
},
"registry_config_path": {
Type: schema.TypeString,
Optional: true,
Description: "The path to the registry config file",
DefaultFunc: schema.EnvDefaultFunc("HELM_REGISTRY_CONFIG", helmpath.ConfigPath("registry.json")),
},
"repository_config_path": {
Type: schema.TypeString,
Optional: true,
Description: "The path to the file containing repository names and URLs",
DefaultFunc: schema.EnvDefaultFunc("HELM_REPOSITORY_CONFIG", helmpath.ConfigPath("repositories.yaml")),
},
"repository_cache": {
Type: schema.TypeString,
Optional: true,
Description: "The path to the file containing cached repository indexes",
DefaultFunc: schema.EnvDefaultFunc("HELM_REPOSITORY_CACHE", helmpath.CachePath("repository")),
},
"helm_driver": {
Type: schema.TypeString,
Optional: true,
Description: "The backend storage driver. Values are: configmap, secret, memory, sql",
DefaultFunc: schema.EnvDefaultFunc("HELM_DRIVER", "secret"),
ValidateDiagFunc: func(val interface{}, key cty.Path) (diags diag.Diagnostics) {
drivers := []string{
strings.ToLower(driver.MemoryDriverName),
strings.ToLower(driver.ConfigMapsDriverName),
strings.ToLower(driver.SecretsDriverName),
strings.ToLower(driver.SQLDriverName),
}
v := strings.ToLower(val.(string))
for _, d := range drivers {
if d == v {
return
}
}
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: fmt.Sprintf("Invalid storage driver: %v used for helm_driver", v),
Detail: fmt.Sprintf("Helm backend storage driver must be set to one of the following values: %v", strings.Join(drivers, ", ")),
},
}
},
},
"kubernetes": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Kubernetes configuration.",
Elem: kubernetesResource(),
},
},
ResourcesMap: map[string]*schema.Resource{
"helm_release": resourceRelease(),
},
}
p.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
return providerConfigure(d, p.TerraformVersion)
}
return p
}
func kubernetesResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_HOST", ""),
Description: "The hostname (in form of URI) of Kubernetes master.",
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_USER", ""),
Description: "The username to use for HTTP basic authentication when accessing the Kubernetes master endpoint.",
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_PASSWORD", ""),
Description: "The password to use for HTTP basic authentication when accessing the Kubernetes master endpoint.",
},
"insecure": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_INSECURE", false),
Description: "Whether server should be accessed without verifying the TLS certificate.",
},
"client_certificate": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CLIENT_CERT_DATA", ""),
Description: "PEM-encoded client certificate for TLS authentication.",
},
"client_key": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CLIENT_KEY_DATA", ""),
Description: "PEM-encoded client certificate key for TLS authentication.",
},
"cluster_ca_certificate": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CLUSTER_CA_CERT_DATA", ""),
Description: "PEM-encoded root certificates bundle for TLS authentication.",
},
"config_paths": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "A list of paths to kube config files. Can be set with KUBE_CONFIG_PATHS environment variable.",
},
"config_path": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CONFIG_PATH", nil),
Description: "Path to the kube config file. Can be set with KUBE_CONFIG_PATH.",
ConflictsWith: []string{"kubernetes.0.config_paths"},
},
"config_context": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CTX", ""),
},
"config_context_auth_info": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CTX_AUTH_INFO", ""),
Description: "",
},
"config_context_cluster": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_CTX_CLUSTER", ""),
Description: "",
},
"token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_TOKEN", ""),
Description: "Token to authenticate an service account",
},
"exec": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_version": {
Type: schema.TypeString,
Required: true,
},
"command": {
Type: schema.TypeString,
Required: true,
},
"env": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"args": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
Description: "",
},
},
}
}
var apiTokenMountPath = "/var/run/secrets/kubernetes.io/serviceaccount"
func inCluster() bool {
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if host == "" || port == "" {
return false
}
if _, err := os.Stat(apiTokenMountPath); err != nil {
return false
}
return true
}
var authDocumentationURL = "https://registry.terraform.io/providers/hashicorp/helm/latest/docs#authentication"
func checkKubernetesConfigurationValid(d *schema.ResourceData) error {
if inCluster() {
log.Printf("[DEBUG] Terraform appears to be running inside the Kubernetes cluster")
return nil
}
if os.Getenv("KUBE_CONFIG_PATHS") != "" {
return nil
}
atLeastOneOf := []string{
"host",
"config_path",
"config_paths",
"client_certificate",
"token",
"exec",
}
for _, a := range atLeastOneOf {
if _, ok := k8sGetOk(d, a); ok {
return nil
}
}
return fmt.Errorf(`provider not configured: you must configure a path to your kubeconfig
or explicitly supply credentials via the provider block or environment variables.
See our authentication documentation at: %s`, authDocumentationURL)
}
func providerConfigure(d *schema.ResourceData, terraformVersion string) (interface{}, diag.Diagnostics) {
m := &Meta{data: d}
settings := cli.New()
settings.Debug = d.Get("debug").(bool)
if v, ok := d.GetOk("plugins_path"); ok {
settings.PluginsDirectory = v.(string)
}
if v, ok := d.GetOk("registry_config_path"); ok {
settings.RegistryConfig = v.(string)
}
if v, ok := d.GetOk("repository_config_path"); ok {
settings.RepositoryConfig = v.(string)
}
if v, ok := d.GetOk("repository_cache"); ok {
settings.RepositoryCache = v.(string)
}
m.Settings = settings
if v, ok := d.GetOk("helm_driver"); ok {
m.HelmDriver = v.(string)
}
return m, nil
}
var k8sPrefix = "kubernetes.0."
func k8sGetOk(d *schema.ResourceData, key string) (interface{}, bool) {
value, ok := d.GetOk(k8sPrefix + key)
// For boolean attributes the zero value is Ok
switch value.(type) {
case bool:
// TODO: replace deprecated GetOkExists with SDK v2 equivalent
// https://github.com/hashicorp/terraform-plugin-sdk/pull/350
value, ok = d.GetOkExists(k8sPrefix + key)
}
// fix: DefaultFunc is not being triggered on TypeList
s := kubernetesResource().Schema[key]
if !ok && s.DefaultFunc != nil {
value, _ = s.DefaultFunc()
switch v := value.(type) {
case string:
ok = len(v) != 0
case bool:
ok = v
}
}
return value, ok
}
func k8sGet(d *schema.ResourceData, key string) interface{} {
value, _ := k8sGetOk(d, key)
return value
}
func expandStringSlice(s []interface{}) []string {
result := make([]string, len(s), len(s))
for k, v := range s {
// Handle the Terraform parser bug which turns empty strings in lists to nil.
if v == nil {
result[k] = ""
} else {
result[k] = v.(string)
}
}
return result
}
// GetHelmConfiguration will return a new Helm configuration
func (m *Meta) GetHelmConfiguration(namespace string) (*action.Configuration, error) {
m.Lock()
defer m.Unlock()
debug("[INFO] GetHelmConfiguration start")
actionConfig := new(action.Configuration)
if err := checkKubernetesConfigurationValid(m.data); err != nil {
return nil, err
}
kc, err := newKubeConfig(m.data, &namespace)
if err != nil {
return nil, err
}
if err := actionConfig.Init(kc, namespace, m.HelmDriver, debug); err != nil {
return nil, err
}
debug("[INFO] GetHelmConfiguration success")
return actionConfig, nil
}
func debug(format string, a ...interface{}) {
log.Printf("[DEBUG] %s", fmt.Sprintf(format, a...))
}