agent.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package helm
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "github.com/porter-dev/porter/internal/helm/loader"
  6. "github.com/porter-dev/porter/internal/kubernetes"
  7. "github.com/porter-dev/porter/internal/models"
  8. "github.com/porter-dev/porter/internal/repository"
  9. "golang.org/x/oauth2"
  10. "helm.sh/helm/v3/pkg/action"
  11. "helm.sh/helm/v3/pkg/chart"
  12. "helm.sh/helm/v3/pkg/release"
  13. "k8s.io/helm/pkg/chartutil"
  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 *ListFilter,
  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. getDeps bool,
  34. ) (*release.Release, error) {
  35. // Namespace is already known by the RESTClientGetter.
  36. cmd := action.NewGet(a.ActionConfig)
  37. cmd.Version = version
  38. release, err := cmd.Run(name)
  39. if getDeps {
  40. for _, dep := range release.Chart.Metadata.Dependencies {
  41. depExists := false
  42. for _, currDep := range release.Chart.Dependencies() {
  43. // we just case on name for now -- there might be edge cases we're missing
  44. // but this will cover 99% of cases
  45. if dep.Name == currDep.Name() {
  46. depExists = true
  47. break
  48. }
  49. }
  50. if !depExists {
  51. depChart, err := loader.LoadChartPublic(dep.Repository, dep.Name, dep.Version)
  52. if err == nil {
  53. release.Chart.AddDependency(depChart)
  54. }
  55. }
  56. }
  57. }
  58. return release, err
  59. }
  60. // GetReleaseHistory returns a list of charts for a specific release
  61. func (a *Agent) GetReleaseHistory(
  62. name string,
  63. ) ([]*release.Release, error) {
  64. cmd := action.NewHistory(a.ActionConfig)
  65. return cmd.Run(name)
  66. }
  67. type UpgradeReleaseConfig struct {
  68. Name string
  69. Values map[string]interface{}
  70. Cluster *models.Cluster
  71. Repo repository.Repository
  72. Registries []*models.Registry
  73. // Optional, if chart should be overriden
  74. Chart *chart.Chart
  75. }
  76. // UpgradeRelease upgrades a specific release with new values.yaml
  77. func (a *Agent) UpgradeRelease(
  78. conf *UpgradeReleaseConfig,
  79. values string,
  80. doAuth *oauth2.Config,
  81. ) (*release.Release, error) {
  82. valuesYaml, err := chartutil.ReadValues([]byte(values))
  83. if err != nil {
  84. return nil, fmt.Errorf("Values could not be parsed: %v", err)
  85. }
  86. conf.Values = valuesYaml
  87. return a.UpgradeReleaseByValues(conf, doAuth)
  88. }
  89. // UpgradeReleaseByValues upgrades a release by unmarshaled yaml values
  90. func (a *Agent) UpgradeReleaseByValues(
  91. conf *UpgradeReleaseConfig,
  92. doAuth *oauth2.Config,
  93. ) (*release.Release, error) {
  94. // grab the latest release
  95. rel, err := a.GetRelease(conf.Name, 0, true)
  96. if err != nil {
  97. return nil, fmt.Errorf("Could not get release to be upgraded: %v", err)
  98. }
  99. ch := rel.Chart
  100. if conf.Chart != nil {
  101. ch = conf.Chart
  102. }
  103. cmd := action.NewUpgrade(a.ActionConfig)
  104. cmd.Namespace = rel.Namespace
  105. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  106. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  107. conf.Cluster,
  108. conf.Repo,
  109. a.K8sAgent,
  110. rel.Namespace,
  111. conf.Registries,
  112. doAuth,
  113. )
  114. if err != nil {
  115. return nil, err
  116. }
  117. }
  118. res, err := cmd.Run(conf.Name, ch, conf.Values)
  119. if err != nil {
  120. return nil, fmt.Errorf("Upgrade failed: %v", err)
  121. }
  122. return res, nil
  123. }
  124. // InstallChartConfig is the config required to install a chart
  125. type InstallChartConfig struct {
  126. Chart *chart.Chart
  127. Name string
  128. Namespace string
  129. Values map[string]interface{}
  130. Cluster *models.Cluster
  131. Repo repository.Repository
  132. Registries []*models.Registry
  133. }
  134. // InstallChartFromValuesBytes reads the raw values and calls Agent.InstallChart
  135. func (a *Agent) InstallChartFromValuesBytes(
  136. conf *InstallChartConfig,
  137. values []byte,
  138. doAuth *oauth2.Config,
  139. ) (*release.Release, error) {
  140. valuesYaml, err := chartutil.ReadValues(values)
  141. if err != nil {
  142. return nil, fmt.Errorf("Values could not be parsed: %v", err)
  143. }
  144. conf.Values = valuesYaml
  145. return a.InstallChart(conf, doAuth)
  146. }
  147. // InstallChart installs a new chart
  148. func (a *Agent) InstallChart(
  149. conf *InstallChartConfig,
  150. doAuth *oauth2.Config,
  151. ) (*release.Release, error) {
  152. cmd := action.NewInstall(a.ActionConfig)
  153. if cmd.Version == "" && cmd.Devel {
  154. cmd.Version = ">0.0.0-0"
  155. }
  156. cmd.ReleaseName = conf.Name
  157. cmd.Namespace = conf.Namespace
  158. cmd.Timeout = 300
  159. if err := checkIfInstallable(conf.Chart); err != nil {
  160. return nil, err
  161. }
  162. var err error
  163. // only add the postrenderer if required fields exist
  164. if conf.Cluster != nil && a.K8sAgent != nil && conf.Registries != nil && len(conf.Registries) > 0 {
  165. cmd.PostRenderer, err = NewDockerSecretsPostRenderer(
  166. conf.Cluster,
  167. conf.Repo,
  168. a.K8sAgent,
  169. conf.Namespace,
  170. conf.Registries,
  171. doAuth,
  172. )
  173. if err != nil {
  174. return nil, err
  175. }
  176. }
  177. if req := conf.Chart.Metadata.Dependencies; req != nil {
  178. if err := action.CheckDependencies(conf.Chart, req); err != nil {
  179. // TODO: Handle dependency updates.
  180. return nil, err
  181. }
  182. }
  183. return cmd.Run(conf.Chart, conf.Values)
  184. }
  185. // UninstallChart uninstalls a chart
  186. func (a *Agent) UninstallChart(
  187. name string,
  188. ) (*release.UninstallReleaseResponse, error) {
  189. cmd := action.NewUninstall(a.ActionConfig)
  190. return cmd.Run(name)
  191. }
  192. // RollbackRelease rolls a release back to a specified revision/version
  193. func (a *Agent) RollbackRelease(
  194. name string,
  195. version int,
  196. ) error {
  197. cmd := action.NewRollback(a.ActionConfig)
  198. cmd.Version = version
  199. return cmd.Run(name)
  200. }
  201. // ------------------------ Helm agent helper functions ------------------------ //
  202. // checkIfInstallable validates if a chart can be installed
  203. // Application chart type is only installable
  204. func checkIfInstallable(ch *chart.Chart) error {
  205. switch ch.Metadata.Type {
  206. case "", "application":
  207. return nil
  208. }
  209. return errors.Errorf("%s charts are not installable", ch.Metadata.Type)
  210. }