helm.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package helm
  15. import (
  16. "errors"
  17. "github.com/docker/compose-cli/pkg/api"
  18. "helm.sh/helm/v3/pkg/action"
  19. "helm.sh/helm/v3/pkg/chart"
  20. env "helm.sh/helm/v3/pkg/cli"
  21. "helm.sh/helm/v3/pkg/release"
  22. "k8s.io/cli-runtime/pkg/genericclioptions"
  23. )
  24. // Actions implements charts installation management
  25. type Actions struct {
  26. Config *action.Configuration
  27. Namespace string
  28. initialize func(f func(format string, v ...interface{})) error
  29. }
  30. const helmDriver = "configmap"
  31. // NewActions new helm action
  32. func NewActions(getter genericclioptions.RESTClientGetter) (*Actions, error) {
  33. if getter == nil {
  34. settings := env.New()
  35. getter = settings.RESTClientGetter()
  36. }
  37. namespace := "default"
  38. if ns, _, err := getter.ToRawKubeConfigLoader().Namespace(); err == nil {
  39. namespace = ns
  40. }
  41. actions := &Actions{
  42. Config: &action.Configuration{
  43. RESTClientGetter: getter,
  44. },
  45. Namespace: namespace,
  46. }
  47. actions.initialize = func(f func(format string, v ...interface{})) error {
  48. err := actions.Config.Init(getter, namespace, helmDriver, f)
  49. if err != nil {
  50. return err
  51. }
  52. return actions.Config.KubeClient.IsReachable()
  53. }
  54. err := actions.initialize(nil) // by default no logger, users might re-initialize with another logger function
  55. if err != nil {
  56. return nil, err
  57. }
  58. return actions, nil
  59. }
  60. // InstallChart installs chart
  61. func (hc *Actions) InstallChart(name string, chart *chart.Chart, logger func(format string, v ...interface{})) error {
  62. err := hc.initialize(logger)
  63. if err != nil {
  64. return err
  65. }
  66. actInstall := action.NewInstall(hc.Config)
  67. actInstall.ReleaseName = name
  68. actInstall.Namespace = hc.Namespace
  69. _, err = actInstall.Run(chart, map[string]interface{}{})
  70. return err
  71. }
  72. // UpdateChart upgrades chart
  73. func (hc *Actions) UpdateChart(name string, chart *chart.Chart, logger func(format string, v ...interface{})) error {
  74. err := hc.initialize(logger)
  75. if err != nil {
  76. return err
  77. }
  78. actUpgrade := action.NewUpgrade(hc.Config)
  79. actUpgrade.Namespace = hc.Namespace
  80. _, err = actUpgrade.Run(name, chart, map[string]interface{}{})
  81. return err
  82. }
  83. // Uninstall uninstall chart
  84. func (hc *Actions) Uninstall(name string, logger func(format string, v ...interface{})) error {
  85. err := hc.initialize(logger)
  86. if err != nil {
  87. return err
  88. }
  89. release, err := hc.Get(name)
  90. if err != nil {
  91. return err
  92. }
  93. if release == nil {
  94. return errors.New("no release found with the name provided")
  95. }
  96. actUninstall := action.NewUninstall(hc.Config)
  97. _, err = actUninstall.Run(name)
  98. return err
  99. }
  100. // Get get released object for a named chart
  101. func (hc *Actions) Get(name string) (*release.Release, error) {
  102. actGet := action.NewGet(hc.Config)
  103. return actGet.Run(name)
  104. }
  105. // ListReleases lists chart releases
  106. func (hc *Actions) ListReleases() ([]api.Stack, error) {
  107. actList := action.NewList(hc.Config)
  108. releases, err := actList.Run()
  109. if err != nil {
  110. return nil, err
  111. }
  112. result := []api.Stack{}
  113. for _, rel := range releases {
  114. result = append(result, api.Stack{
  115. ID: rel.Name,
  116. Name: rel.Name,
  117. Status: string(rel.Info.Status),
  118. })
  119. }
  120. return result, nil
  121. }