-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathprovider.go
More file actions
732 lines (687 loc) · 24.8 KB
/
provider.go
File metadata and controls
732 lines (687 loc) · 24.8 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
package helm
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/storage/driver"
)
var _ provider.Provider = &HelmProvider{}
func New() func() provider.Provider {
return func() provider.Provider {
return &HelmProvider{}
}
}
type Meta struct {
providerData *HelmProvider
Data *HelmProviderModel
Settings *cli.EnvSettings
RegistryClient *registry.Client
HelmDriver string
sync.Mutex
// Experimental feature toggles
Experiments map[string]bool
}
// Models for our provider helm block
type HelmProviderModel struct {
Debug types.Bool `tfsdk:"debug"`
PluginsPath types.String `tfsdk:"plugins_path"`
RegistryConfigPath types.String `tfsdk:"registry_config_path"`
RepositoryConfigPath types.String `tfsdk:"repository_config_path"`
RepositoryCache types.String `tfsdk:"repository_cache"`
HelmDriver types.String `tfsdk:"helm_driver"`
BurstLimit types.Int64 `tfsdk:"burst_limit"`
Kubernetes types.List `tfsdk:"kubernetes"`
Registry types.List `tfsdk:"registry"`
Experiments types.List `tfsdk:"experiments"`
}
type ExperimentsConfigModel struct {
Manifest types.Bool `tfsdk:"manifest"`
}
type RegistryConfigModel struct {
URL types.String `tfsdk:"url"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
}
type KubernetesConfigModel struct {
Host types.String `tfsdk:"host"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
Insecure types.Bool `tfsdk:"insecure"`
TLSServerName types.String `tfsdk:"tls_server_name"`
ClientCertificate types.String `tfsdk:"client_certificate"`
ClientKey types.String `tfsdk:"client_key"`
ClusterCACertificate types.String `tfsdk:"cluster_ca_certificate"`
ConfigPaths types.List `tfsdk:"config_paths"`
ConfigPath types.String `tfsdk:"config_path"`
ConfigContext types.String `tfsdk:"config_context"`
ConfigContextAuthInfo types.String `tfsdk:"config_context_auth_info"`
ConfigContextCluster types.String `tfsdk:"config_context_cluster"`
Token types.String `tfsdk:"token"`
ProxyURL types.String `tfsdk:"proxy_url"`
// Exec types.List `tfsdk:"exec"`
}
type ExecConfigModel struct {
APIVersion types.String `tfsdk:"api_version"`
Command types.String `tfsdk:"command"`
Env types.Map `tfsdk:"env"`
Args types.List `tfsdk:"args"`
}
type HelmProvider struct {
meta *Meta
}
func (p *HelmProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "helm"
}
// /////////////////////// START OF SCHEMA CREATION ///////////////////////////////
// Defines attributes that are avaiable in the provider
func (p *HelmProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Schema to define attributes that are available in the provider",
Attributes: map[string]schema.Attribute{
"debug": schema.BoolAttribute{
Description: "Debug indicates whether or not Helm is running in Debug mode.",
Optional: true,
},
"plugins_path": schema.StringAttribute{
Description: "The path to the helm plugins directory",
Optional: true,
},
"registry_config_path": schema.StringAttribute{
Description: "The path to the registry config file",
Optional: true,
},
"repository_config_path": schema.StringAttribute{
Description: "The path to the file containing repository names and URLs",
Optional: true,
},
"repository_cache": schema.StringAttribute{
Description: "The path to the file containing cached repository indexes",
Optional: true,
},
"helm_driver": schema.StringAttribute{
Description: "The backend storage driver. Values are: configmap, secret, memory, sql",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf(strings.ToLower(driver.MemoryDriverName),
strings.ToLower(driver.ConfigMapsDriverName),
strings.ToLower(driver.SecretsDriverName),
strings.ToLower(driver.SQLDriverName)),
},
},
"burst_limit": schema.Int64Attribute{
Optional: true,
Description: "Helm burst limit. Increase this if you have a cluster with many CRDs",
},
"kubernetes": schema.ListNestedAttribute{
Optional: true,
Validators: []validator.List{
listvalidator.SizeAtMost(1),
},
Description: "Kubernetes Configuration",
NestedObject: schema.NestedAttributeObject{
Attributes: kubernetesResourceSchema(),
},
},
"registry": schema.ListNestedAttribute{
Optional: true,
Description: "RegistryClient configuration.",
NestedObject: schema.NestedAttributeObject{
Attributes: registryResourceSchema(),
},
},
"experiments": schema.ListNestedAttribute{
Optional: true,
Description: "Enable and disable experimental features.",
NestedObject: schema.NestedAttributeObject{
Attributes: experimentsSchema(),
},
},
},
}
}
func experimentsSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"manifest": schema.BoolAttribute{
Optional: true,
Description: "Enable full diff by storing the rendered manifest in the state.",
},
}
}
func registryResourceSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"url": schema.StringAttribute{
Required: true,
Description: "OCI URL in form of oci://host:port or oci://host",
},
"username": schema.StringAttribute{
Required: true,
Description: "The username to use for the OCI HTTP basic authentication when accessing the Kubernetes master endpoint.",
},
"password": schema.StringAttribute{
Required: true,
Description: "The password to use for the OCI HTTP basic authentication when accessing the Kubernetes master endpoint.",
},
}
}
func kubernetesResourceSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"host": schema.StringAttribute{
Optional: true,
Description: "The hostname (in form of URI) of kubernetes master",
},
"username": schema.StringAttribute{
Optional: true,
Description: "The username to use for HTTP basic authentication when accessing the Kubernetes master endpoint",
},
"password": schema.StringAttribute{
Optional: true,
Description: "The password to use for HTTP basic authentication when accessing the Kubernetes master endpoint.",
},
"insecure": schema.BoolAttribute{
Optional: true,
Description: "Whether server should be accessed without verifying the TLS certificate.",
},
"tls_server_name": schema.StringAttribute{
Optional: true,
Description: "Server name passed to the server for SNI and is used in the client to check server certificates against.",
},
"client_certificate": schema.StringAttribute{
Optional: true,
Description: "PEM-encoded client certificate for TLS authentication.",
},
"client_key": schema.StringAttribute{
Optional: true,
Description: "PEM-encoded client certificate key for TLS authentication.",
},
"cluster_ca_certificate": schema.StringAttribute{
Optional: true,
Description: "PEM-encoded root certificates bundle for TLS authentication.",
},
"config_paths": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
Description: "A list of paths to kube config files. Can be set with KUBE_CONFIG_PATHS environment variable.",
},
"config_path": schema.StringAttribute{
Optional: true,
Description: "Path to the kube config file. Can be set with KUBE_CONFIG_PATH.",
Validators: []validator.String{
stringvalidator.ConflictsWith(
path.Root("kubernetes").AtListIndex(0).AtName("config_paths").Expression(),
),
},
},
"config_context": schema.StringAttribute{
Optional: true,
Description: "Context to use for Kubernetes config.",
},
"config_context_auth_info": schema.StringAttribute{
Optional: true,
// TODO REFERENCE THE DEFAULT DOCUMENTATI
Description: "AuthInfo to use for Kubernetes config context.",
},
"config_context_cluster": schema.StringAttribute{
Optional: true,
Description: "Cluster to use for Kubernetes config context.",
},
"token": schema.StringAttribute{
Optional: true,
Description: "Token to authenticate a service account.",
},
"proxy_url": schema.StringAttribute{
Optional: true,
Description: "URL to the proxy to be used for all API requests.",
},
// "exec": schema.ListNestedAttribute{
// Optional: true,
// Validators: []validator.List{
// listvalidator.SizeAtMost(1),
// },
// Description: "Exec configuration for Kubernetes authentication",
// NestedObject: schema.NestedAttributeObject{
// Attributes: execSchema(),
// },
// },
}
}
func execSchema() map[string]schema.Attribute {
return map[string]schema.Attribute{
"api_version": schema.StringAttribute{
Required: true,
Description: "API version for the exec plugin.",
},
"command": schema.StringAttribute{
Required: true,
Description: "Command to run for Kubernetes exec plugin",
},
"env": schema.MapAttribute{
Optional: true,
ElementType: types.StringType,
Description: "Environment variables for the exec plugin",
},
"args": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
Description: "Arguments for the exec plugin",
},
}
}
///////////////////// END OF SCHEMA CREATION ///////////////////////////////
// Setting up the provider, anything we need to get the provider running, probbaly authentication. like the api
func (p *HelmProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
pluginsPath := os.Getenv("HELM_PLUGINS_PATH")
registryConfigPath := os.Getenv("HELM_REGISTRY_CONFIG_PATH")
repositoryConfigPath := os.Getenv("HELM_REPOSITORY_CONFIG_PATH")
repositoryCache := os.Getenv("HELM_REPOSITORY_CACHE")
helmDriver := os.Getenv("HELM_DRIVER")
burstLimitStr := os.Getenv("HELM_BURST_LIMIT")
kubeHost := os.Getenv("KUBE_HOST")
kubeUser := os.Getenv("KUBE_USER")
kubePassword := os.Getenv("KUBE_PASSWORD")
kubeInsecureStr := os.Getenv("KUBE_INSECURE")
kubeTLSServerName := os.Getenv("KUBE_TLS_SERVER_NAME")
kubeClientCert := os.Getenv("KUBE_CLIENT_CERT_DATA")
kubeClientKey := os.Getenv("KUBE_CLIENT_KEY_DATA")
kubeCaCert := os.Getenv("KUBE_CLUSTER_CA_CERT_DATA")
kubeConfigPaths := os.Getenv("KUBE_CONFIG_PATHS")
kubeConfigPath := os.Getenv("KUBE_CONFIG_PATH")
kubeConfigContext := os.Getenv("KUBE_CTX")
kubeConfigContextAuthInfo := os.Getenv("KUBE_CTX_AUTH_INFO")
kubeConfigContextCluster := os.Getenv("KUBE_CTX_CLUSTER")
kubeToken := os.Getenv("KUBE_TOKEN")
kubeProxy := os.Getenv("KUBE_PROXY")
// Initialize the HelmProviderModel with values from the config
var config HelmProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
debug := os.Getenv("HELM_DEBUG") == "true" || config.Debug.ValueBool()
if !config.PluginsPath.IsNull() {
pluginsPath = config.PluginsPath.ValueString()
}
if !config.RegistryConfigPath.IsNull() {
registryConfigPath = config.RegistryConfigPath.ValueString()
}
if !config.RepositoryConfigPath.IsNull() {
repositoryConfigPath = config.RepositoryConfigPath.ValueString()
}
if !config.RepositoryCache.IsNull() {
repositoryCache = config.RepositoryCache.ValueString()
}
if !config.HelmDriver.IsNull() {
helmDriver = config.HelmDriver.ValueString()
}
var burstLimit int64
if burstLimitStr != "" {
var err error
burstLimit, err = strconv.ParseInt(burstLimitStr, 10, 64)
if err != nil {
resp.Diagnostics.AddError(
"Invalid burst limit",
fmt.Sprintf("Invalid burst limit value: %s", burstLimitStr),
)
return
}
}
if !config.BurstLimit.IsNull() {
burstLimit = config.BurstLimit.ValueInt64()
}
var kubeInsecure bool
if kubeInsecureStr != "" {
var err error
kubeInsecure, err = strconv.ParseBool(kubeInsecureStr)
if err != nil {
resp.Diagnostics.AddError(
"Invalid insecure value",
fmt.Sprintf("Invalid insecure value: %s", kubeInsecureStr),
)
return
}
}
var kubernetesConfig KubernetesConfigModel
if !config.Kubernetes.IsNull() && !config.Kubernetes.IsUnknown() {
var kubernetesConfigs []KubernetesConfigModel
diags := config.Kubernetes.ElementsAs(ctx, &kubernetesConfigs, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if len(kubernetesConfigs) > 0 {
kubernetesConfig = kubernetesConfigs[0]
}
}
if !kubernetesConfig.Insecure.IsNull() {
kubeInsecure = kubernetesConfig.Insecure.ValueBool()
}
var kubeConfigPathsList []attr.Value
if !kubernetesConfig.Host.IsNull() {
kubeHost = kubernetesConfig.Host.ValueString()
}
if !kubernetesConfig.Username.IsNull() {
kubeUser = kubernetesConfig.Username.ValueString()
}
if !kubernetesConfig.Password.IsNull() {
kubePassword = kubernetesConfig.Password.ValueString()
}
if !kubernetesConfig.TLSServerName.IsNull() {
kubeTLSServerName = kubernetesConfig.TLSServerName.ValueString()
}
if !kubernetesConfig.ClientCertificate.IsNull() {
kubeClientCert = kubernetesConfig.ClientCertificate.ValueString()
}
if !kubernetesConfig.ClientKey.IsNull() {
kubeClientKey = kubernetesConfig.ClientKey.ValueString()
}
if !kubernetesConfig.ClusterCACertificate.IsNull() {
kubeCaCert = kubernetesConfig.ClusterCACertificate.ValueString()
}
if kubeConfigPaths != "" {
for _, path := range filepath.Split(kubeConfigPaths) {
kubeConfigPathsList = append(kubeConfigPathsList, types.StringValue(path))
}
}
if !kubernetesConfig.ConfigPaths.IsNull() {
var paths []string
diags = kubernetesConfig.ConfigPaths.ElementsAs(ctx, &paths, false)
resp.Diagnostics.Append(diags...)
for _, path := range paths {
kubeConfigPathsList = append(kubeConfigPathsList, types.StringValue(path))
}
}
if !kubernetesConfig.ConfigPath.IsNull() {
kubeConfigPath = kubernetesConfig.ConfigPath.ValueString()
}
if !kubernetesConfig.ConfigContext.IsNull() {
kubeConfigContext = kubernetesConfig.ConfigContext.ValueString()
}
if !kubernetesConfig.ConfigContextAuthInfo.IsNull() {
kubeConfigContextAuthInfo = kubernetesConfig.ConfigContextAuthInfo.ValueString()
}
if !kubernetesConfig.ConfigContextCluster.IsNull() {
kubeConfigContextCluster = kubernetesConfig.ConfigContextCluster.ValueString()
}
if !kubernetesConfig.Token.IsNull() {
kubeToken = kubernetesConfig.Token.ValueString()
}
if !kubernetesConfig.ProxyURL.IsNull() {
kubeProxy = kubernetesConfig.ProxyURL.ValueString()
}
tflog.Debug(ctx, "Config values after overrides", map[string]interface{}{
"config": config,
})
settings := cli.New()
settings.Debug = debug
if pluginsPath != "" {
settings.PluginsDirectory = pluginsPath
}
if registryConfigPath != "" {
settings.RegistryConfig = registryConfigPath
}
if repositoryConfigPath != "" {
settings.RepositoryConfig = repositoryConfigPath
}
if repositoryCache != "" {
settings.RepositoryCache = repositoryCache
}
tflog.Debug(ctx, "Helm settings initialized", map[string]interface{}{
"settings": settings,
})
kubeConfigPathsListValue, diags := types.ListValue(types.StringType, kubeConfigPathsList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
var experimentsConfig ExperimentsConfigModel
if !config.Experiments.IsNull() && !config.Experiments.IsUnknown() {
var experimentsConfigs []ExperimentsConfigModel
diags := config.Experiments.ElementsAs(ctx, &experimentsConfigs, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
if len(experimentsConfigs) > 0 {
experimentsConfig = experimentsConfigs[0]
}
}
manifestExperiment := false
if !experimentsConfig.Manifest.IsNull() {
manifestExperiment = experimentsConfig.Manifest.ValueBool()
}
// Create a list of KubernetesConfigModel
kubernetesConfigList := []attr.Value{
types.ObjectValueMust(map[string]attr.Type{
"host": types.StringType,
"username": types.StringType,
"password": types.StringType,
"insecure": types.BoolType,
"tls_server_name": types.StringType,
"client_certificate": types.StringType,
"client_key": types.StringType,
"cluster_ca_certificate": types.StringType,
"config_paths": types.ListType{ElemType: types.StringType},
"config_path": types.StringType,
"config_context": types.StringType,
"config_context_auth_info": types.StringType,
"config_context_cluster": types.StringType,
"token": types.StringType,
"proxy_url": types.StringType,
}, map[string]attr.Value{
"host": types.StringValue(kubeHost),
"username": types.StringValue(kubeUser),
"password": types.StringValue(kubePassword),
"insecure": types.BoolValue(kubeInsecure),
"tls_server_name": types.StringValue(kubeTLSServerName),
"client_certificate": types.StringValue(kubeClientCert),
"client_key": types.StringValue(kubeClientKey),
"cluster_ca_certificate": types.StringValue(kubeCaCert),
"config_paths": kubeConfigPathsListValue,
"config_path": types.StringValue(kubeConfigPath),
"config_context": types.StringValue(kubeConfigContext),
"config_context_auth_info": types.StringValue(kubeConfigContextAuthInfo),
"config_context_cluster": types.StringValue(kubeConfigContextCluster),
"token": types.StringValue(kubeToken),
"proxy_url": types.StringValue(kubeProxy),
}),
}
kubernetesConfigListValue, diags := types.ListValue(types.ObjectType{
AttrTypes: map[string]attr.Type{
"host": types.StringType,
"username": types.StringType,
"password": types.StringType,
"insecure": types.BoolType,
"tls_server_name": types.StringType,
"client_certificate": types.StringType,
"client_key": types.StringType,
"cluster_ca_certificate": types.StringType,
"config_paths": types.ListType{ElemType: types.StringType},
"config_path": types.StringType,
"config_context": types.StringType,
"config_context_auth_info": types.StringType,
"config_context_cluster": types.StringType,
"token": types.StringType,
"proxy_url": types.StringType,
},
}, kubernetesConfigList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Create a list of ExperimentsConfigModel
experimentsConfigList := []attr.Value{
types.ObjectValueMust(map[string]attr.Type{
"manifest": types.BoolType,
}, map[string]attr.Value{
"manifest": types.BoolValue(manifestExperiment),
}),
}
experimentsConfigListValue, diags := types.ListValue(types.ObjectType{
AttrTypes: map[string]attr.Type{
"manifest": types.BoolType,
},
}, experimentsConfigList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
meta := &Meta{
Data: &HelmProviderModel{
Debug: types.BoolValue(debug),
PluginsPath: types.StringValue(pluginsPath),
RegistryConfigPath: types.StringValue(registryConfigPath),
RepositoryConfigPath: types.StringValue(repositoryConfigPath),
RepositoryCache: types.StringValue(repositoryCache),
HelmDriver: types.StringValue(helmDriver),
BurstLimit: types.Int64Value(burstLimit),
Kubernetes: kubernetesConfigListValue,
Experiments: experimentsConfigListValue,
},
Settings: settings,
HelmDriver: helmDriver,
Experiments: map[string]bool{
"manifest": manifestExperiment,
},
}
registryClient, err := registry.NewClient()
if err != nil {
resp.Diagnostics.AddError(
"Registry client initialization failed",
fmt.Sprintf("Unable to create Helm registry client: %s", err),
)
return
}
meta.RegistryClient = registryClient
if !config.Registry.IsUnknown() {
var registryConfigs []RegistryConfigModel
diags := config.Registry.ElementsAs(ctx, ®istryConfigs, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
for _, r := range registryConfigs {
if r.URL.IsNull() || r.Username.IsNull() || r.Password.IsNull() {
resp.Diagnostics.AddError(
"OCI Registry login failed",
"Registry URL, Username, or Password is null",
)
return
}
err := OCIRegistryPerformLogin(ctx, meta.RegistryClient, r.URL.ValueString(), r.Username.ValueString(), r.Password.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"OCI Registry login failed",
err.Error(),
)
return
}
}
} else {
tflog.Debug(ctx, "No registry configurations found")
}
resp.DataSourceData = meta
resp.ResourceData = meta
tflog.Debug(ctx, "Configure method completed successfully")
}
func (p *HelmProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewDataTemplate,
}
}
func (p *HelmProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewHelmReleaseResource,
}
}
var (
OCILoginMutex sync.Mutex
loggedInOCIRegistries = make(map[string]string)
)
func OCIRegistryLogin(ctx context.Context, actionConfig *action.Configuration, registryClient *registry.Client, repository, chartName, username, password string) diag.Diagnostics {
var diags diag.Diagnostics
actionConfig.RegistryClient = registryClient
var ociURL string
if registry.IsOCI(repository) {
ociURL = repository
} else if registry.IsOCI(chartName) {
ociURL = chartName
}
if ociURL == "" {
return diags
}
if username != "" && password != "" {
err := OCIRegistryPerformLogin(ctx, registryClient, ociURL, username, password)
if err != nil {
diags.AddError(
"OCI Registry Login Failed",
fmt.Sprintf("Failed to log in to OCI registry %q: %s", ociURL, err.Error()),
)
}
}
return diags
}
// registryClient = client used to comm with the registry, oci urls, un, and pw used for authentication
func OCIRegistryPerformLogin(ctx context.Context, registryClient *registry.Client, ociURL, username, password string) error {
// getting the oci url, and extracting the host.
u, err := url.Parse(ociURL)
if err != nil {
return fmt.Errorf("could not parse OCI registry URL: %v", err)
}
OCILoginMutex.Lock()
defer OCILoginMutex.Unlock()
if _, ok := loggedInOCIRegistries[u.Host]; ok {
tflog.Info(ctx, fmt.Sprintf("Already logged into OCI registry %q", u.Host))
return nil
}
// Now we perform the login, with the provided username and password by calling the login method
err = registryClient.Login(u.Host, registry.LoginOptBasicAuth(username, password))
if err != nil {
return fmt.Errorf("could not login to OCI registry %q: %v", u.Host, err)
}
loggedInOCIRegistries[u.Host] = ""
tflog.Info(ctx, fmt.Sprintf("Logged into OCI registry %q", u.Host))
return nil
}
// GetHelmConfiguration retrieves the Helm configuration for a given namespace
func (m *Meta) GetHelmConfiguration(ctx context.Context, namespace string) (*action.Configuration, error) {
if m == nil {
tflog.Error(ctx, "Meta is nil")
return nil, fmt.Errorf("Meta is nil")
}
m.Lock()
defer m.Unlock()
tflog.Info(context.Background(), "[INFO] GetHelmConfiguration start")
actionConfig := new(action.Configuration)
kc, err := m.NewKubeConfig(ctx, namespace)
if err != nil {
return nil, err
}
if err := actionConfig.Init(kc, namespace, m.HelmDriver, func(format string, v ...interface{}) {
tflog.Info(context.Background(), fmt.Sprintf(format, v...))
}); err != nil {
return nil, err
}
tflog.Info(context.Background(), "[INFO] GetHelmConfiguration success")
// returning the initializing action.Configuration object
return actionConfig, nil
}