agent.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. cmd.Namespace = rel.Namespace
  78. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  79. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  80. conf.Cluster,
  81. conf.Repo,
  82. a.K8sAgent,
  83. rel.Namespace,
  84. conf.Registries,
  85. doAuth,
  86. )
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. res, err := cmd.Run(conf.Name, ch, conf.Values)
  92. if err != nil {
  93. return nil, fmt.Errorf("Upgrade failed: %v", err)
  94. }
  95. return res, nil
  96. }
  97. // InstallChartConfig is the config required to install a chart
  98. type InstallChartConfig struct {
  99. Chart *chart.Chart
  100. Name string
  101. Namespace string
  102. Values map[string]interface{}
  103. Cluster *models.Cluster
  104. Repo repository.Repository
  105. Registries []*models.Registry
  106. }
  107. // InstallChartFromValuesBytes reads the raw values and calls Agent.InstallChart
  108. func (a *Agent) InstallChartFromValuesBytes(
  109. conf *InstallChartConfig,
  110. values []byte,
  111. doAuth *oauth2.Config,
  112. ) (*release.Release, error) {
  113. valuesYaml, err := chartutil.ReadValues(values)
  114. if err != nil {
  115. return nil, fmt.Errorf("Values could not be parsed: %v", err)
  116. }
  117. conf.Values = valuesYaml
  118. return a.InstallChart(conf, doAuth)
  119. }
  120. // InstallChart installs a new chart
  121. func (a *Agent) InstallChart(
  122. conf *InstallChartConfig,
  123. doAuth *oauth2.Config,
  124. ) (*release.Release, error) {
  125. cmd := action.NewInstall(a.ActionConfig)
  126. if cmd.Version == "" && cmd.Devel {
  127. cmd.Version = ">0.0.0-0"
  128. }
  129. cmd.ReleaseName = conf.Name
  130. cmd.Namespace = conf.Namespace
  131. if err := checkIfInstallable(conf.Chart); err != nil {
  132. return nil, err
  133. }
  134. var err error
  135. // only add the postrenderer if required fields exist
  136. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  137. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  138. conf.Cluster,
  139. conf.Repo,
  140. a.K8sAgent,
  141. conf.Namespace,
  142. conf.Registries,
  143. doAuth,
  144. )
  145. if err != nil {
  146. return nil, err
  147. }
  148. }
  149. if req := conf.Chart.Metadata.Dependencies; req != nil {
  150. if err := action.CheckDependencies(conf.Chart, req); err != nil {
  151. // TODO: Handle dependency updates.
  152. return nil, err
  153. }
  154. }
  155. return cmd.Run(conf.Chart, conf.Values)
  156. }
  157. // UninstallChart uninstalls a chart
  158. func (a *Agent) UninstallChart(
  159. name string,
  160. ) (*release.UninstallReleaseResponse, error) {
  161. cmd := action.NewUninstall(a.ActionConfig)
  162. return cmd.Run(name)
  163. }
  164. // RollbackRelease rolls a release back to a specified revision/version
  165. func (a *Agent) RollbackRelease(
  166. name string,
  167. version int,
  168. ) error {
  169. cmd := action.NewRollback(a.ActionConfig)
  170. cmd.Version = version
  171. return cmd.Run(name)
  172. }
  173. // ------------------------ Helm agent helper functions ------------------------ //
  174. // checkIfInstallable validates if a chart can be installed
  175. // Application chart type is only installable
  176. func checkIfInstallable(ch *chart.Chart) error {
  177. switch ch.Metadata.Type {
  178. case "", "application":
  179. return nil
  180. }
  181. return errors.Errorf("%s charts are not installable", ch.Metadata.Type)
  182. }