agent.go 5.3 KB

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