agent.go 5.3 KB

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