agent.go 6.1 KB

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