agent.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package helm
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "github.com/porter-dev/porter/internal/kubernetes"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/internal/repository"
  8. "golang.org/x/oauth2"
  9. "helm.sh/helm/v3/pkg/action"
  10. "helm.sh/helm/v3/pkg/chart"
  11. "helm.sh/helm/v3/pkg/release"
  12. "k8s.io/helm/pkg/chartutil"
  13. )
  14. // Agent is a Helm agent for performing helm operations
  15. type Agent struct {
  16. ActionConfig *action.Configuration
  17. K8sAgent *kubernetes.Agent
  18. }
  19. // ListReleases lists releases based on a ListFilter
  20. func (a *Agent) ListReleases(
  21. namespace string,
  22. filter *ListFilter,
  23. ) ([]*release.Release, error) {
  24. cmd := action.NewList(a.ActionConfig)
  25. filter.apply(cmd)
  26. return cmd.Run()
  27. }
  28. // GetRelease returns the info of a release.
  29. func (a *Agent) GetRelease(
  30. name string,
  31. version int,
  32. ) (*release.Release, error) {
  33. // Namespace is already known by the RESTClientGetter.
  34. cmd := action.NewGet(a.ActionConfig)
  35. cmd.Version = version
  36. return cmd.Run(name)
  37. }
  38. // GetReleaseHistory returns a list of charts for a specific release
  39. func (a *Agent) GetReleaseHistory(
  40. name string,
  41. ) ([]*release.Release, error) {
  42. cmd := action.NewHistory(a.ActionConfig)
  43. return cmd.Run(name)
  44. }
  45. type UpgradeReleaseConfig struct {
  46. Name string
  47. Values map[string]interface{}
  48. Cluster *models.Cluster
  49. Repo repository.Repository
  50. Registries []*models.Registry
  51. }
  52. // UpgradeRelease upgrades a specific release with new values.yaml
  53. func (a *Agent) UpgradeRelease(
  54. conf *UpgradeReleaseConfig,
  55. values string,
  56. doAuth *oauth2.Config,
  57. ) (*release.Release, error) {
  58. valuesYaml, err := chartutil.ReadValues([]byte(values))
  59. if err != nil {
  60. return nil, fmt.Errorf("Values could not be parsed: %v", err)
  61. }
  62. conf.Values = valuesYaml
  63. return a.UpgradeReleaseByValues(conf, doAuth)
  64. }
  65. // UpgradeReleaseByValues upgrades a release by unmarshaled yaml values
  66. func (a *Agent) UpgradeReleaseByValues(
  67. conf *UpgradeReleaseConfig,
  68. doAuth *oauth2.Config,
  69. ) (*release.Release, error) {
  70. // grab the latest release
  71. rel, err := a.GetRelease(conf.Name, 0)
  72. if err != nil {
  73. return nil, fmt.Errorf("Could not get release to be upgraded: %v", err)
  74. }
  75. ch := rel.Chart
  76. cmd := action.NewUpgrade(a.ActionConfig)
  77. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  78. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  79. conf.Cluster,
  80. conf.Repo,
  81. a.K8sAgent,
  82. rel.Namespace,
  83. conf.Registries,
  84. doAuth,
  85. )
  86. if err != nil {
  87. return nil, err
  88. }
  89. }
  90. res, err := cmd.Run(conf.Name, ch, conf.Values)
  91. if err != nil {
  92. return nil, fmt.Errorf("Upgrade failed: %v", err)
  93. }
  94. return res, nil
  95. }
  96. // InstallChartConfig is the config required to install a chart
  97. type InstallChartConfig struct {
  98. Chart *chart.Chart
  99. Name string
  100. Namespace string
  101. Values map[string]interface{}
  102. Cluster *models.Cluster
  103. Repo repository.Repository
  104. Registries []*models.Registry
  105. }
  106. // InstallChartFromValuesBytes reads the raw values and calls Agent.InstallChart
  107. func (a *Agent) InstallChartFromValuesBytes(
  108. conf *InstallChartConfig,
  109. values []byte,
  110. doAuth *oauth2.Config,
  111. ) (*release.Release, error) {
  112. valuesYaml, err := chartutil.ReadValues(values)
  113. if err != nil {
  114. return nil, fmt.Errorf("Values could not be parsed: %v", err)
  115. }
  116. conf.Values = valuesYaml
  117. return a.InstallChart(conf, doAuth)
  118. }
  119. // InstallChart installs a new chart
  120. func (a *Agent) InstallChart(
  121. conf *InstallChartConfig,
  122. doAuth *oauth2.Config,
  123. ) (*release.Release, error) {
  124. cmd := action.NewInstall(a.ActionConfig)
  125. if cmd.Version == "" && cmd.Devel {
  126. cmd.Version = ">0.0.0-0"
  127. }
  128. cmd.ReleaseName = conf.Name
  129. cmd.Namespace = conf.Namespace
  130. if err := checkIfInstallable(conf.Chart); err != nil {
  131. return nil, err
  132. }
  133. var err error
  134. // only add the postrenderer if required fields exist
  135. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  136. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  137. conf.Cluster,
  138. conf.Repo,
  139. a.K8sAgent,
  140. conf.Namespace,
  141. conf.Registries,
  142. doAuth,
  143. )
  144. if err != nil {
  145. return nil, err
  146. }
  147. }
  148. if req := conf.Chart.Metadata.Dependencies; req != nil {
  149. if err := action.CheckDependencies(conf.Chart, req); err != nil {
  150. // TODO: Handle dependency updates.
  151. return nil, err
  152. }
  153. }
  154. return cmd.Run(conf.Chart, conf.Values)
  155. }
  156. // UninstallChart uninstalls a chart
  157. func (a *Agent) UninstallChart(
  158. name string,
  159. ) (*release.UninstallReleaseResponse, error) {
  160. cmd := action.NewUninstall(a.ActionConfig)
  161. return cmd.Run(name)
  162. }
  163. // RollbackRelease rolls a release back to a specified revision/version
  164. func (a *Agent) RollbackRelease(
  165. name string,
  166. version int,
  167. ) error {
  168. cmd := action.NewRollback(a.ActionConfig)
  169. cmd.Version = version
  170. return cmd.Run(name)
  171. }
  172. // ------------------------ Helm agent helper functions ------------------------ //
  173. // checkIfInstallable validates if a chart can be installed
  174. // Application chart type is only installable
  175. func checkIfInstallable(ch *chart.Chart) error {
  176. switch ch.Metadata.Type {
  177. case "", "application":
  178. return nil
  179. }
  180. return errors.Errorf("%s charts are not installable", ch.Metadata.Type)
  181. }