agent.go 6.0 KB

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