/
helm_workload.go
760 lines (679 loc) · 25.9 KB
/
helm_workload.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
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package v0
import (
"errors"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"github.com/threeport/threeport/internal/agent"
"github.com/threeport/threeport/internal/helm-workload/status"
workload_status "github.com/threeport/threeport/internal/workload/status"
v0 "github.com/threeport/threeport/pkg/api/v0"
client "github.com/threeport/threeport/pkg/client/v0"
util "github.com/threeport/threeport/pkg/util/v0"
)
// HelmWorkloadConfig contains the config for a helm workload which is an abstraction of
// a helm workload definition and helm workload instance.
type HelmWorkloadConfig struct {
HelmWorkload HelmWorkloadValues `yaml:"HelmWorkload"`
}
// HelmWorkloadValues contains the attributes needed to manage a helm workload
// definition and helm workload instance.
type HelmWorkloadValues struct {
Name string `yaml:"Name"`
Repo string `yaml:"Repo"`
Chart string `yaml:"Chart"`
ChartVersion string `yaml:"ChartVersion"`
DefinitionValues string `yaml:"DefinitionValues"`
DefinitionValuesDocument string `yaml:"DefinitionValuesDocument"`
InstanceValues string `yaml:"InstanceValues"`
InstanceValuesDocument string `yaml:"InstanceValuesDocument"`
HelmWorkloadConfigPath string `yaml:"HelmWorkloadConfigPath"`
KubernetesRuntimeInstance *KubernetesRuntimeInstanceValues `yaml:"KubernetesRuntimeInstance"`
DomainName *DomainNameDefinitionValues `yaml:"DomainName"`
Gateway *GatewayDefinitionValues `yaml:"Gateway"`
AwsRelationalDatabase *AwsRelationalDatabaseValues `yaml:"AwsRelationalDatabase"`
AwsObjectStorageBucket *AwsObjectStorageBucketValues `yaml:"AwsObjectStorageBucket"`
}
// HelmWorkloadDefinitionConfig contains the config for a helm workload definition.
type HelmWorkloadDefinitionConfig struct {
HelmWorkloadDefinition HelmWorkloadDefinitionValues `yaml:"HelmWorkloadDefinition"`
}
// HelmWorkloadDefinitionValues contains the attributes needed to manage a helm workload
// definition.
type HelmWorkloadDefinitionValues struct {
Name string `yaml:"Name"`
Repo string `yaml:"Repo"`
Chart string `yaml:"Chart"`
ChartVersion string `yaml:"ChartVersion"`
Values string `yaml:"Values"`
ValuesDocument string `yaml:"ValuesDocument"`
HelmWorkloadConfigPath string `yaml:"HelmWorkloadConfigPath"`
}
// HelmWorkloadInstanceConfig contains the config for a helm workload instance.
type HelmWorkloadInstanceConfig struct {
HelmWorkloadInstance HelmWorkloadInstanceValues `yaml:"HelmWorkloadInstance"`
}
// HelmWorkloadInstanceValues contains the attributes needed to manage a helm workload
// instance.
type HelmWorkloadInstanceValues struct {
Name string `yaml:"Name"`
Values string `yaml:"Values"`
ValuesDocument string `yaml:"ValuesDocument"`
KubernetesRuntimeInstance *KubernetesRuntimeInstanceValues `yaml:"KubernetesRuntimeInstance"`
HelmWorkloadDefinition HelmWorkloadDefinitionValues `yaml:"HelmWorkloadDefinition"`
HelmWorkloadConfigPath string `yaml:"HelmWorkloadConfigPath"`
}
// Create creates a helm workload definition and instance in the Threeport API.
func (h *HelmWorkloadValues) Create(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadDefinition, *v0.HelmWorkloadInstance, error) {
// validate required fields
if err := h.ValidateCreate(); err != nil {
return nil, nil, fmt.Errorf(
"failed to validate values for helm workload with name %s: %w",
h.Name,
err,
)
}
// get operations
operations, createdHelmWorkloadDefinition, createdHelmWorkloadInstance, err := h.GetOperations(
apiClient,
apiEndpoint,
)
if err != nil {
return nil, nil, fmt.Errorf(
"failed to get operations for helm workload with name %s: %w",
h.Name,
err,
)
}
// execute create operations
if err := operations.Create(); err != nil {
return nil, nil, fmt.Errorf(
"failed to execute create operations for helm workload defined instance with name %s: %w",
h.Name,
err,
)
}
return createdHelmWorkloadDefinition, createdHelmWorkloadInstance, nil
}
// ValidateCreate validates the helm workload values before creating a helm workload
func (h *HelmWorkloadValues) ValidateCreate() error {
multiError := util.MultiError{}
// ensure name is set
if h.Name == "" {
multiError.AppendError(errors.New("missing required field in config: Name"))
}
// ensure repo is set
if h.Repo == "" {
multiError.AppendError(errors.New("missing required field in config: Repo"))
}
// ensure chart is set
if h.Chart == "" {
multiError.AppendError(errors.New("missing required field in config: Chart"))
}
// ensure definition values or definition values document is set
if h.DefinitionValues != "" && h.DefinitionValuesDocument != "" {
multiError.AppendError(errors.New("cannot set both DefinitionValues and DefinitionValuesDocument"))
}
// ensure instance values or instance values document is set
if h.InstanceValues != "" && h.InstanceValuesDocument != "" {
multiError.AppendError(errors.New("cannot set both InstanceValues and InstanceValuesDocument"))
}
return multiError.Error()
}
// Delete deletes a helm workload definition, helm workload instance,
// domain name definition, domain name instance,
// gateway definition, and gateway instance from the Threeport API.
func (h *HelmWorkloadValues) Delete(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadDefinition, *v0.HelmWorkloadInstance, error) {
// get operation
operations, _, _, err := h.GetOperations(apiClient, apiEndpoint)
if err != nil {
return nil, nil, fmt.Errorf(
"failed to get operations for helm workload with name %s: %w",
h.Name,
err,
)
}
// execute delete operations
if err := operations.Delete(); err != nil {
return nil, nil, fmt.Errorf(
"failed to execute delete operations for helm workload defined instance with name %s: %w",
h.Name,
err,
)
}
return nil, nil, nil
}
// Create creates a helm workload definition in the Threeport API.
func (h *HelmWorkloadDefinitionValues) Create(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadDefinition, error) {
// validate required fields
if h.Name == "" || h.Repo == "" || h.Chart == "" {
return nil, errors.New("missing required field/s in config - required fields: Name, Repo, Chart")
}
// construct helm workload definition object
helmWorkloadDefinition := v0.HelmWorkloadDefinition{
Definition: v0.Definition{
Name: &h.Name,
},
Repo: &h.Repo,
Chart: &h.Chart,
}
// set helm values if present
values, err := GetValuesFromDocumentOrInline(h.Values, h.ValuesDocument, h.HelmWorkloadConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get values document from path: %w", err)
}
helmWorkloadDefinition.ValuesDocument = values
// create helm workload definition
createdHelmWorkloadDefinition, err := client.CreateHelmWorkloadDefinition(
apiClient,
apiEndpoint,
&helmWorkloadDefinition,
)
if err != nil {
return nil, fmt.Errorf("failed to create helm workload definition in threeport API: %w", err)
}
return createdHelmWorkloadDefinition, nil
}
// ValidateCreate validates the helm workload definition values before creating a helm
// workload definition
func (h *HelmWorkloadDefinitionValues) ValidateCreate() error {
multiError := util.MultiError{}
// ensure name is set
if h.Name == "" {
multiError.AppendError(errors.New("missing required field in config: Name"))
}
// ensure repo is set
if h.Repo == "" {
multiError.AppendError(errors.New("missing required field in config: Repo"))
}
// ensure chart is set
if h.Chart == "" {
multiError.AppendError(errors.New("missing required field in config: Chart"))
}
// ensure values or values document is set
if h.Values != "" && h.ValuesDocument != "" {
multiError.AppendError(errors.New("cannot set both Values and ValuesDocument"))
}
return multiError.Error()
}
// Describe returns details related to a helm workload definition.
func (wd *HelmWorkloadDefinitionValues) Describe(
apiClient *http.Client,
apiEndpoint string,
) (*status.HelmWorkloadDefinitionStatusDetail, error) {
// get helm workload definition by name
helmWorkloadDefinition, err := client.GetHelmWorkloadDefinitionByName(apiClient, apiEndpoint, wd.Name)
if err != nil {
return nil, fmt.Errorf("failed to find helm workload definition with name %s: %w", wd.Name, err)
}
// get helm workload definition status
statusDetail, err := status.GetHelmWorkloadDefinitionStatus(
apiClient,
apiEndpoint,
*helmWorkloadDefinition.ID,
)
if err != nil {
return nil, fmt.Errorf("failed to get status for helm workload definition with name %s: %w", wd.Name, err)
}
return statusDetail, nil
}
// Delete deletes a helm workload definition from the Threeport API.
func (h *HelmWorkloadDefinitionValues) Delete(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadDefinition, error) {
// get helm workload definition by name
helmWorkloadDefinition, err := client.GetHelmWorkloadDefinitionByName(apiClient, apiEndpoint, h.Name)
if err != nil {
return nil, fmt.Errorf("failed to find helm workload definition with name %s: %w", h.Name, err)
}
// delete helm workload definition
deletedHelmWorkloadDefinition, err := client.DeleteHelmWorkloadDefinition(
apiClient,
apiEndpoint,
*helmWorkloadDefinition.ID,
)
if err != nil {
return nil, fmt.Errorf("failed to delete helm workload definition from threeport API: %w", err)
}
return deletedHelmWorkloadDefinition, nil
}
// Create creates a helm workload instance in the Threeport API.
func (h *HelmWorkloadInstanceValues) Create(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadInstance, error) {
// validate required fields
if h.Name == "" || h.HelmWorkloadDefinition.Name == "" {
return nil, errors.New("missing required field/s in config - required fields: Name, HelmWorkloadDefinition.Name")
}
// get kubernetes runtime instance API object
kubernetesRuntimeInstance, err := setKubernetesRuntimeInstanceForConfig(
h.KubernetesRuntimeInstance,
apiClient,
apiEndpoint,
)
if err != nil {
return nil, fmt.Errorf("failed to set kubernetes runtime instance: %w", err)
}
// get helm workload definition by name
helmWorkloadDefinition, err := client.GetHelmWorkloadDefinitionByName(
apiClient,
apiEndpoint,
h.HelmWorkloadDefinition.Name,
)
if err != nil {
return nil, fmt.Errorf(
"failed to find helm workload definition with name %s: %w",
h.HelmWorkloadDefinition.Name,
err,
)
}
// construct helm workload instance object
helmWorkloadInstance := v0.HelmWorkloadInstance{
Instance: v0.Instance{
Name: &h.Name,
},
KubernetesRuntimeInstanceID: kubernetesRuntimeInstance.ID,
HelmWorkloadDefinitionID: helmWorkloadDefinition.ID,
}
// get helm instance values
values, err := GetValuesFromDocumentOrInline(h.Values, h.ValuesDocument, h.HelmWorkloadConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get helm instance values document from path: %w", err)
}
helmWorkloadInstance.ValuesDocument = values
// create helm workload instance
createdHelmWorkloadInstance, err := client.CreateHelmWorkloadInstance(
apiClient,
apiEndpoint,
&helmWorkloadInstance,
)
if err != nil {
return nil, fmt.Errorf("failed to create helm workload instance in threeport API: %w", err)
}
return createdHelmWorkloadInstance, nil
}
// ValidateCreate validates the helm workload instance values before creating a helm
// workload instance
func (h *HelmWorkloadInstanceValues) ValidateCreate() error {
multiError := util.MultiError{}
// ensure name is set
if h.Name == "" {
multiError.AppendError(errors.New("missing required field in config: Name"))
}
// ensure kubernetes runtime instance is set
if h.KubernetesRuntimeInstance == nil {
multiError.AppendError(errors.New("missing required field in config: KubernetesRuntimeInstance"))
}
// ensure kubernetes runtime instance name is set
if h.KubernetesRuntimeInstance.Name == "" {
multiError.AppendError(errors.New("missing required field in config: KubernetesRuntimeInstance.Name"))
}
// ensure helm workload definition name is set
if h.HelmWorkloadDefinition.Name == "" {
multiError.AppendError(errors.New("missing required field in config: HelmWorkloadDefinition.Name"))
}
// ensure values or values document is set
if h.Values != "" && h.ValuesDocument != "" {
multiError.AppendError(errors.New("cannot set both Values and ValuesDocument"))
}
return multiError.Error()
}
// Describe returns important failure events related to a helm workload instance.
func (h *HelmWorkloadInstanceValues) Describe(apiClient *http.Client, apiEndpoint string) (*workload_status.WorkloadInstanceStatusDetail, error) {
// get helm workload instance by name
helmWorkloadInstance, err := client.GetHelmWorkloadInstanceByName(apiClient, apiEndpoint, h.Name)
if err != nil {
return nil, fmt.Errorf("failed to find helm workload instance with name %s: %w", h.Name, err)
}
// get helm workload instance status
statusDetail := workload_status.GetWorkloadInstanceStatus(
apiClient,
apiEndpoint,
agent.HelmWorkloadInstanceType,
*helmWorkloadInstance.ID,
*helmWorkloadInstance.Reconciled,
)
if statusDetail.Error != nil {
return nil, fmt.Errorf("failed to get status for helm workload instance with name %s: %w", h.Name, statusDetail.Error)
}
return statusDetail, nil
}
// Delete deletes a helm workload instance from the Threeport API.
func (h *HelmWorkloadInstanceValues) Delete(
apiClient *http.Client,
apiEndpoint string,
) (*v0.HelmWorkloadInstance, error) {
// get helm workload instance by name
helmWorkloadInstance, err := client.GetHelmWorkloadInstanceByName(apiClient, apiEndpoint, h.Name)
if err != nil {
return nil, fmt.Errorf("failed to find helm workload instance with name %s: %w", h.Name, err)
}
// delete helm workload instance
deletedHelmWorkloadInstance, err := client.DeleteHelmWorkloadInstance(
apiClient,
apiEndpoint,
*helmWorkloadInstance.ID,
)
if err != nil {
return nil, fmt.Errorf("failed to delete helm workload instance from threeport API: %w", err)
}
// wait for helm workload instance to be deleted
util.Retry(60, 1, func() error {
if _, err := client.GetHelmWorkloadInstanceByName(apiClient, apiEndpoint, h.Name); err == nil {
return errors.New("helm workload instance not deleted")
}
return nil
})
return deletedHelmWorkloadInstance, nil
}
// GetOperations returns a slice of operations used to
// create, update, or delete a helm workload.
func (h *HelmWorkloadValues) GetOperations(
apiClient *http.Client,
apiEndpoint string,
) (*util.Operations, *v0.HelmWorkloadDefinition, *v0.HelmWorkloadInstance, error) {
var err error
var createdHelmWorkloadInstance v0.HelmWorkloadInstance
var createdHelmWorkloadDefinition v0.HelmWorkloadDefinition
operations := util.Operations{}
// get helm definition values
helmDefinitionValues, err := GetValuesFromDocumentOrInline(h.DefinitionValues, h.DefinitionValuesDocument, h.HelmWorkloadConfigPath)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get helm instance values document from path: %w", err)
}
// add helm workload definition operation
helmWorkloadDefinitionValues := HelmWorkloadDefinitionValues{
Name: h.Name,
Repo: h.Repo,
Chart: h.Chart,
ChartVersion: h.ChartVersion,
Values: *helmDefinitionValues,
HelmWorkloadConfigPath: h.HelmWorkloadConfigPath,
}
operations.AppendOperation(util.Operation{
Name: "helm workload definition",
Create: func() error {
helmWorkloadDefinition, err := helmWorkloadDefinitionValues.Create(apiClient, apiEndpoint)
if err != nil {
return fmt.Errorf(
"failed to create helm workload definition with name %s: %w",
h.Name,
err,
)
}
createdHelmWorkloadDefinition = *helmWorkloadDefinition
return nil
},
Delete: func() error {
_, err = helmWorkloadDefinitionValues.Delete(apiClient, apiEndpoint)
if err != nil {
return fmt.Errorf("failed to delete helm workload definition with name %s: %w", h.Name, err)
}
return nil
},
})
// get helm instance values
helmInstanceValues, err := GetValuesFromDocumentOrInline(h.InstanceValues, h.InstanceValuesDocument, h.HelmWorkloadConfigPath)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get helm instance values document from path: %w", err)
}
// add helm workload instance operation
helmWorkloadInstanceValues := HelmWorkloadInstanceValues{
Name: h.Name,
Values: *helmInstanceValues,
HelmWorkloadConfigPath: h.HelmWorkloadConfigPath,
KubernetesRuntimeInstance: h.KubernetesRuntimeInstance,
HelmWorkloadDefinition: HelmWorkloadDefinitionValues{
Name: h.Name,
},
}
operations.AppendOperation(util.Operation{
Name: "helm workload instance",
Create: func() error {
helmWorkloadInstance, err := helmWorkloadInstanceValues.Create(apiClient, apiEndpoint)
if err != nil {
return fmt.Errorf("failed to create helm workload instance with name %s: %w", h.Name, err)
}
createdHelmWorkloadInstance = *helmWorkloadInstance
return nil
},
Delete: func() error {
_, err = helmWorkloadInstanceValues.Delete(apiClient, apiEndpoint)
if err != nil {
return fmt.Errorf("failed to delete helm workload instance with name %s: %w", h.Name, err)
}
return nil
},
})
//// add domain name and gateway if provided
//if h.DomainName != nil && h.Gateway != nil {
// // add domain name definition operation
// domainNameDefinitionValues := DomainNameDefinitionValues{
// Domain: h.DomainName.Domain,
// Zone: h.DomainName.Zone,
// AdminEmail: h.DomainName.AdminEmail,
// }
// operations.AppendOperation(util.Operation{
// Name: "domain name definition",
// Create: func() error {
// _, err = domainNameDefinitionValues.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create domain name definition with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, err = domainNameDefinitionValues.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete domain name definition with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
// // add domain name instance operation
// domainNameInstanceValues := DomainNameInstanceValues{
// DomainNameDefinition: domainNameDefinitionValues,
// KubernetesRuntimeInstance: h.KubernetesRuntimeInstance,
// HelmWorkloadInstance: helmWorkloadInstanceValues,
// }
// operations.AppendOperation(util.Operation{
// Name: "domain name instance",
// Create: func() error {
// _, err = domainNameInstanceValues.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create domain name instance with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, err = domainNameInstanceValues.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete domain name instance with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
// // add gateway definition operation
// gatewayDefinitionValues := GatewayDefinitionValues{
// Name: h.Name,
// HttpPorts: h.Gateway.HttpPorts,
// TcpPorts: h.Gateway.TcpPorts,
// ServiceName: h.Gateway.ServiceName,
// SubDomain: h.Gateway.SubDomain,
// DomainNameDefinition: domainNameDefinitionValues,
// }
// operations.AppendOperation(util.Operation{
// Name: "gateway definition",
// Create: func() error {
// _, err = gatewayDefinitionValues.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create gateway definition with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, err = gatewayDefinitionValues.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete gateway definition with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
// // add gateway instance operation
// gatewayInstanceValues := GatewayInstanceValues{
// GatewayDefinition: gatewayDefinitionValues,
// KubernetesRuntimeInstance: h.KubernetesRuntimeInstance,
// HelmWorkloadInstance: helmWorkloadInstanceValues,
// }
// operations.AppendOperation(util.Operation{
// Name: "gateway instance",
// Create: func() error {
// _, err = gatewayInstanceValues.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create gateway instance with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, err = gatewayInstanceValues.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete gateway instance with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
//}
//// add AWS relational database operation
//if h.AwsRelationalDatabase != nil {
// awsRelationalDatabase := AwsRelationalDatabaseValues{
// Name: h.AwsRelationalDatabase.Name,
// AwsAccountName: h.AwsRelationalDatabase.AwsAccountName,
// Engine: h.AwsRelationalDatabase.Engine,
// EngineVersion: h.AwsRelationalDatabase.EngineVersion,
// DatabaseName: h.AwsRelationalDatabase.DatabaseName,
// DatabasePort: h.AwsRelationalDatabase.DatabasePort,
// BackupDays: h.AwsRelationalDatabase.BackupDays,
// MachineSize: h.AwsRelationalDatabase.MachineSize,
// StorageGb: h.AwsRelationalDatabase.StorageGb,
// HelmWorkloadSecretName: h.AwsRelationalDatabase.HelmWorkloadSecretName,
// HelmWorkloadInstance: &HelmWorkloadInstanceValues{
// Name: h.Name,
// },
// }
// operations.AppendOperation(util.Operation{
// Name: "aws relational database",
// Create: func() error {
// _, _, err := awsRelationalDatabase.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create aws relational database with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, _, err = awsRelationalDatabase.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete aws relational database with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
//}
//// add AWS object storage bucket operation
//if h.AwsObjectStorageBucket != nil {
// awsObjectStorageBucket := AwsObjectStorageBucketValues{
// Name: h.AwsObjectStorageBucket.Name,
// AwsAccountName: h.AwsObjectStorageBucket.AwsAccountName,
// PublicReadAccess: h.AwsObjectStorageBucket.PublicReadAccess,
// HelmWorkloadServiceAccountName: h.AwsObjectStorageBucket.HelmWorkloadServiceAccountName,
// HelmWorkloadBucketEnvVar: h.AwsObjectStorageBucket.HelmWorkloadBucketEnvVar,
// HelmWorkloadInstance: &HelmWorkloadInstanceValues{
// Name: h.Name,
// },
// }
// operations.AppendOperation(util.Operation{
// Name: "aws object storage bucket",
// Create: func() error {
// _, _, err := awsObjectStorageBucket.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create aws object storage bucket with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, _, err := awsObjectStorageBucket.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete aws object storage bucket with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
//}
// // add secret operation
// if h.Secret != nil {
// secret := SecretValues{
// Name: h.Secret.Name,
// AwsAccountName: h.Secret.AwsAccountName,
// Data: h.Secret.Data,
// KubernetesRuntimeInstance: h.KubernetesRuntimeInstance,
// WorkloadInstance: &workloadInstanceValues,
// }
// operations.AppendOperation(util.Operation{
// Name: "secret",
// Create: func() error {
// _, _, err := secret.Create(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to create secret with name %s: %w", h.Name, err)
// }
// return nil
// },
// Delete: func() error {
// _, _, err := secret.Delete(apiClient, apiEndpoint)
// if err != nil {
// return fmt.Errorf("failed to delete secret with name %s: %w", h.Name, err)
// }
// return nil
// },
// })
// }
return &operations, &createdHelmWorkloadDefinition, &createdHelmWorkloadInstance, nil
}
// GetValuesDocumentFromPath returns the values document content from the path
// provided.
func GetValuesDocumentFromPath(valuesDocument, helmWorkloadConfigPath string) (*string, error) {
// set helm values if present
if valuesDocument != "" {
// build the path to the values document relative to the user's working
// directory
configPath, _ := filepath.Split(helmWorkloadConfigPath)
relativeValuesPath := path.Join(configPath, valuesDocument)
// load vaules document
valuesContent, err := os.ReadFile(relativeValuesPath)
if err != nil {
return nil, fmt.Errorf("failed to read definition ValuesDocument file with name %s: %w", valuesDocument, err)
}
stringContent := string(valuesContent)
return &stringContent, nil
}
return nil, nil
}
// GetValuesFromDocumentOrInline returns the values document content from the
// inline value provided first, then from the document provided
func GetValuesFromDocumentOrInline(inline, valuesDocument, helmWorkloadConfigPath string) (*string, error) {
if inline != "" {
return &inline, nil
}
return GetValuesDocumentFromPath(valuesDocument, helmWorkloadConfigPath)
}