helm_helper.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package utils
  14. import (
  15. "fmt"
  16. "os"
  17. "path"
  18. "path/filepath"
  19. "gopkg.in/yaml.v2"
  20. "github.com/pkg/errors"
  21. "github.com/rook/rook/pkg/util/exec"
  22. )
  23. // HelmHelper is wrapper for running helm commands
  24. type HelmHelper struct {
  25. executor *exec.CommandExecutor
  26. HelmPath string
  27. }
  28. // NewHelmHelper creates a instance of HelmHelper
  29. func NewHelmHelper(helmPath string) *HelmHelper {
  30. executor := &exec.CommandExecutor{}
  31. return &HelmHelper{executor: executor, HelmPath: helmPath}
  32. }
  33. // Execute is wrapper for executing helm commands
  34. func (h *HelmHelper) Execute(args ...string) (string, error) {
  35. result, err := h.executor.ExecuteCommandWithOutput(h.HelmPath, args...)
  36. if err != nil {
  37. logger.Errorf("Errors Encountered while executing helm command %v: %v", result, err)
  38. return result, fmt.Errorf("Failed to run helm command on args %v : %v , err -> %v", args, result, err)
  39. }
  40. return result, nil
  41. }
  42. func createValuesFile(path string, values map[string]interface{}) error {
  43. f, err := os.Create(path)
  44. if err != nil {
  45. return fmt.Errorf("could not create values file: %v", err)
  46. }
  47. defer func() {
  48. closeErr := f.Close()
  49. if err == nil {
  50. err = closeErr
  51. }
  52. }()
  53. output, err := yaml.Marshal(values)
  54. if err != nil {
  55. return fmt.Errorf("could not serialize values file: %v", err)
  56. }
  57. logger.Debugf("Writing values file %v: \n%v", path, string(output))
  58. if _, err := f.Write(output); err != nil {
  59. return fmt.Errorf("could not write values file: %v", err)
  60. }
  61. if err := f.Sync(); err != nil {
  62. return fmt.Errorf("could not flush values file")
  63. }
  64. return nil
  65. }
  66. // InstallLocalHelmChart installs a give helm chart
  67. func (h *HelmHelper) InstallLocalHelmChart(upgrade bool, namespace, chart string, values map[string]interface{}) error {
  68. rootDir, err := FindRookRoot()
  69. if err != nil {
  70. return errors.Wrap(err, "failed to find rook root")
  71. }
  72. var cmdArgs []string
  73. chartDir := path.Join(rootDir, fmt.Sprintf("deploy/charts/%s/", chart))
  74. if upgrade {
  75. cmdArgs = []string{"upgrade"}
  76. } else {
  77. cmdArgs = []string{"install", "--create-namespace"}
  78. }
  79. cmdArgs = append(cmdArgs, chart, chartDir)
  80. if namespace != "" {
  81. cmdArgs = append(cmdArgs, "--namespace", namespace)
  82. }
  83. err = h.installChart(cmdArgs, values)
  84. if err != nil {
  85. return fmt.Errorf("failed to install local helm chart %s with in namespace: %v, err=%v", chart, namespace, err)
  86. }
  87. return nil
  88. }
  89. func (h *HelmHelper) InstallVersionedChart(namespace, chart, version string, values map[string]interface{}) error {
  90. logger.Infof("adding rook-release helm repo")
  91. cmdArgs := []string{"repo", "add", "rook-release", "https://charts.rook.io/release"}
  92. _, err := h.Execute(cmdArgs...)
  93. if err != nil {
  94. // Continue on error in case the repo already was added
  95. logger.Warningf("failed to add repo rook-release, err=%v", err)
  96. }
  97. logger.Infof("installing helm chart %s with version %s", chart, version)
  98. cmdArgs = []string{"install", "--create-namespace", chart, "rook-release/" + chart, "--version=" + version}
  99. if namespace != "" {
  100. cmdArgs = append(cmdArgs, "--namespace", namespace)
  101. }
  102. err = h.installChart(cmdArgs, values)
  103. if err != nil {
  104. return fmt.Errorf("failed to install helm chart %s with version %s in namespace: %v, err=%v", chart, version, namespace, err)
  105. }
  106. return nil
  107. }
  108. func (h *HelmHelper) installChart(cmdArgs []string, values map[string]interface{}) error {
  109. if values != nil {
  110. testValuesPath := "values-test.yaml"
  111. if err := createValuesFile(testValuesPath, values); err != nil {
  112. return fmt.Errorf("error creating values file: %v", err)
  113. }
  114. defer func() {
  115. _ = os.Remove(testValuesPath)
  116. }()
  117. cmdArgs = append(cmdArgs, "-f", testValuesPath)
  118. }
  119. result, err := h.Execute(cmdArgs...)
  120. if err != nil {
  121. logger.Errorf("failed to install chart. result=%s, err=%v", result, err)
  122. return err
  123. }
  124. return nil
  125. }
  126. // DeleteLocalRookHelmChart uninstalls a give helm deploy
  127. func (h *HelmHelper) DeleteLocalRookHelmChart(namespace, deployName string) error {
  128. cmdArgs := []string{"delete", "-n", namespace, deployName}
  129. _, err := h.Execute(cmdArgs...)
  130. if err != nil {
  131. logger.Errorf("could not delete helm chart with name %v : %v", deployName, err)
  132. return fmt.Errorf("Failed to delete helm chart with name %v : %v", deployName, err)
  133. }
  134. return nil
  135. }
  136. func FindRookRoot() (string, error) {
  137. const folderToFind = "tests"
  138. workingDirectory, err := os.Getwd()
  139. if err != nil {
  140. return "", fmt.Errorf("failed to find current working directory. %v", err)
  141. }
  142. parentPath := workingDirectory
  143. userHome, err := os.UserHomeDir()
  144. if err != nil {
  145. return "", fmt.Errorf("failed to find user home directory. %v", err)
  146. }
  147. for parentPath != userHome {
  148. fmt.Printf("parent path = %s\n", parentPath)
  149. _, err := os.Stat(path.Join(parentPath, folderToFind))
  150. if os.IsNotExist(err) {
  151. parentPath = filepath.Dir(parentPath)
  152. continue
  153. }
  154. return parentPath, nil
  155. }
  156. return "", fmt.Errorf("rook root not found above directory %s", workingDirectory)
  157. }