writer.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package helm
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/porter-dev/porter/internal/helm"
  6. "github.com/stefanmcshane/helm/pkg/chart"
  7. )
  8. // TemplateWriter upgrades and installs charts by setting Helm values
  9. type TemplateWriter struct {
  10. // The object to read from, identified by its group-version-kind
  11. Agent *helm.Agent
  12. // Chart that gets installed
  13. Chart *chart.Chart
  14. // ReleaseName for upgrading the chart or installing
  15. ReleaseName string
  16. // Namespace it gets installed to
  17. Namespace string
  18. }
  19. // Transform does nothing, since Helm handles the transforms internally
  20. func (w *TemplateWriter) Transform() error {
  21. return nil
  22. }
  23. // Create installs a new chart, ChartPath must be set
  24. func (w *TemplateWriter) Create(
  25. vals map[string]interface{},
  26. ) (map[string]interface{}, error) {
  27. if w.Chart == nil {
  28. return nil, fmt.Errorf("chart must be set")
  29. }
  30. conf := &helm.InstallChartConfig{
  31. Chart: w.Chart,
  32. Name: w.ReleaseName,
  33. Namespace: w.Namespace,
  34. Values: vals,
  35. }
  36. _, err := w.Agent.InstallChart(context.Background(), conf, nil, false)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return vals, nil
  41. }
  42. // Update upgrades a chart, ReleaseName must be set
  43. func (w *TemplateWriter) Update(
  44. vals map[string]interface{},
  45. ) (map[string]interface{}, error) {
  46. if w.ReleaseName != "" {
  47. return nil, fmt.Errorf("release not set")
  48. }
  49. conf := &helm.UpgradeReleaseConfig{
  50. Name: w.ReleaseName,
  51. Values: vals,
  52. }
  53. _, err := w.Agent.UpgradeReleaseByValues(context.Background(), conf, nil, false, false)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return vals, nil
  58. }