helm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/api/compose"
  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. // NewActions new helm action
  31. func NewActions(getter genericclioptions.RESTClientGetter) (*Actions, error) {
  32. if getter == nil {
  33. settings := env.New()
  34. getter = settings.RESTClientGetter()
  35. }
  36. namespace := "default"
  37. if ns, _, err := getter.ToRawKubeConfigLoader().Namespace(); err == nil {
  38. namespace = ns
  39. }
  40. actions := &Actions{
  41. Config: &action.Configuration{
  42. RESTClientGetter: getter,
  43. },
  44. Namespace: namespace,
  45. }
  46. actions.initialize = func(f func(format string, v ...interface{})) error {
  47. err := actions.Config.Init(getter, namespace, "configmap", f)
  48. if err != nil {
  49. return err
  50. }
  51. return actions.Config.KubeClient.IsReachable()
  52. }
  53. return actions, nil
  54. }
  55. // InstallChart installs chart
  56. func (hc *Actions) InstallChart(name string, chart *chart.Chart, logger func(format string, v ...interface{})) error {
  57. err := hc.initialize(logger)
  58. if err != nil {
  59. return err
  60. }
  61. actInstall := action.NewInstall(hc.Config)
  62. actInstall.ReleaseName = name
  63. actInstall.Namespace = hc.Namespace
  64. _, err = actInstall.Run(chart, map[string]interface{}{})
  65. return err
  66. }
  67. // Uninstall uninstall chart
  68. func (hc *Actions) Uninstall(name string, logger func(format string, v ...interface{})) error {
  69. err := hc.initialize(logger)
  70. if err != nil {
  71. return err
  72. }
  73. release, err := hc.Get(name)
  74. if err != nil {
  75. return err
  76. }
  77. if release == nil {
  78. return errors.New("no release found with the name provided")
  79. }
  80. actUninstall := action.NewUninstall(hc.Config)
  81. _, err = actUninstall.Run(name)
  82. return err
  83. }
  84. // Get get released object for a named chart
  85. func (hc *Actions) Get(name string) (*release.Release, error) {
  86. actGet := action.NewGet(hc.Config)
  87. return actGet.Run(name)
  88. }
  89. // ListReleases lists chart releases
  90. func (hc *Actions) ListReleases() ([]compose.Stack, error) {
  91. err := hc.initialize(nil)
  92. if err != nil {
  93. return nil, err
  94. }
  95. actList := action.NewList(hc.Config)
  96. releases, err := actList.Run()
  97. if err != nil {
  98. return nil, err
  99. }
  100. result := []compose.Stack{}
  101. for _, rel := range releases {
  102. result = append(result, compose.Stack{
  103. ID: rel.Name,
  104. Name: rel.Name,
  105. Status: string(rel.Info.Status),
  106. })
  107. }
  108. return result, nil
  109. }