writer.go 1.5 KB

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