/
helm_helper.go
109 lines (89 loc) · 3.59 KB
/
helm_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"strings"
"github.com/rook/rook/pkg/util/exec"
"github.com/rook/rook/pkg/util/sys"
"time"
)
//HelmHelper is wrapper for running helm commands
type HelmHelper struct {
executor *exec.CommandExecutor
}
//NewHelmHelper creates a instance of HelmHelper
func NewHelmHelper() *HelmHelper {
executor := &exec.CommandExecutor{}
return &HelmHelper{executor: executor}
}
//Execute is wrapper for executing helm commands
func (h *HelmHelper) Execute(args ...string) (string, error) {
result, err := h.executor.ExecuteCommandWithOutput(false, "", "helm", args...)
if err != nil {
logger.Errorf("Errors Encountered while executing helm command %v: %v", result, err)
return result, fmt.Errorf("Failed to run helm commands on args %v : %v , err -> %v", args, result, err)
}
return result, nil
}
//GetLocalRookHelmChartVersion returns helm chart version for a give chart
func (h *HelmHelper) GetLocalRookHelmChartVersion(chartName string) (string, error) {
cmdArgs := []string{"search", chartName}
result, err := h.Execute(cmdArgs...)
if err != nil {
logger.Errorf("cannot find helm chart %v : %v", chartName, err)
return "", fmt.Errorf("Failed to find helm chart %v : %v", chartName, err)
}
if strings.Contains(result, "No results found") {
return "", fmt.Errorf("Failed to find helm chart %v ", chartName)
}
cd := strings.Replace(sys.Grep(result, chartName), "\t", " ", 2)
return sys.Awk(cd, 2), nil
}
//InstallLocalRookHelmChart installs a give helm chart
func (h *HelmHelper) InstallLocalRookHelmChart(chartName string, deployName string, chartVersion string, namespace string) error {
cmdArgs := []string{"install", chartName, "--name", deployName, "--version", chartVersion}
if namespace != "" {
cmdArgs = append(cmdArgs, "--namespace", namespace)
}
var result string
var err error
inc := 0
for inc < RetryLoop {
result, err = h.Execute(cmdArgs...)
if err == nil {
return nil
}
logger.Infof("helm install for %s failed %v, err ->%v", chartName, result, err)
ls, _ := h.Execute([]string{"ls"}...)
logger.Infof("Helm ls result : %v", ls)
ss, _ := h.Execute([]string{"search"}...)
logger.Infof("Helm search result : %v", ss)
rl, _ := h.Execute([]string{"repo", "list"}...)
logger.Infof("Helm repo list result : %v", rl)
inc++
time.Sleep(RetryInterval * time.Second)
}
logger.Errorf("cannot install helm chart with name : %v, version: %v, namespace: %v - %v , err: %v", chartName, chartVersion, namespace, result, err)
return fmt.Errorf("cannot install helm chart with name : %v, version: %v, namespace: %v - %v, err: %v", chartName, chartVersion, namespace, result, err)
}
//DeleteLocalRookHelmChart uninstalls a give helm deploy
func (h *HelmHelper) DeleteLocalRookHelmChart(deployName string) error {
cmdArgs := []string{"delete", "--purge", deployName}
_, err := h.Execute(cmdArgs...)
if err != nil {
logger.Errorf("cannot delete helm chart with name %v : %v", deployName, err)
return fmt.Errorf("Failed to delete helm chart with name %v : %v", deployName, err)
}
return nil
}