forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
166 lines (147 loc) · 4.97 KB
/
common.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package helm
import (
"bytes"
"context"
"encoding/base64"
"io/ioutil"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
helmlib "github.com/rancher/rancher/pkg/catalog/helm"
"github.com/rancher/rancher/pkg/controllers/user/helm/common"
"github.com/rancher/types/apis/project.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
helmName = "helm"
appLabel = "io.cattle.field/appId"
failedLabel = "io.cattle.field/failed-revision"
)
func writeTempDir(rootDir string, files map[string]string) error {
for name, content := range files {
fp := filepath.Join(rootDir, name)
if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil {
return err
}
if err := ioutil.WriteFile(fp, []byte(content), 0644); err != nil {
return err
}
}
return nil
}
func getAppSubDir(files map[string]string) string {
var minLen = math.MaxInt32
var appSubDir string
for filename := range files {
dir, file := filepath.Split(filename)
if strings.EqualFold(file, "Chart.yaml") {
pathLen := len(filepath.SplitList(dir))
if minLen > pathLen {
appSubDir = dir
minLen = pathLen
}
}
}
return appSubDir
}
func helmInstall(templateDir, kubeconfigPath string, app *v3.App) error {
cont, cancel := context.WithCancel(context.Background())
defer cancel()
addr := common.GenerateRandomPort()
probeAddr := common.GenerateRandomPort()
go common.StartTiller(cont, addr, probeAddr, app.Spec.TargetNamespace, kubeconfigPath)
return common.InstallCharts(templateDir, addr, app)
}
func helmDelete(kubeconfigPath string, app *v3.App) error {
cont, cancel := context.WithCancel(context.Background())
defer cancel()
addr := common.GenerateRandomPort()
probeAddr := common.GenerateRandomPort()
go common.StartTiller(cont, addr, probeAddr, app.Spec.TargetNamespace, kubeconfigPath)
return common.DeleteCharts(addr, app)
}
func (l *Lifecycle) generateTemplates(obj *v3.App) (string, string, string, string, error) {
var appSubDir string
files := map[string]string{}
if obj.Spec.ExternalID != "" {
templateVersionID, templateVersionNamespace, err := common.ParseExternalID(obj.Spec.ExternalID)
if err != nil {
return "", "", "", "", err
}
templateVersion, err := l.TemplateVersionClient.GetNamespaced(templateVersionNamespace, templateVersionID, metav1.GetOptions{})
if err != nil {
return "", "", "", "", err
}
namespace, catalogName, catalogType, _, _, err := common.SplitExternalID(templateVersion.Spec.ExternalID)
catalog, err := helmlib.GetCatalog(catalogType, namespace, catalogName, l.CatalogLister, l.ClusterCatalogLister, l.ProjectCatalogLister)
if err != nil {
return "", "", "", "", err
}
helm, err := helmlib.New(catalog)
if err != nil {
return "", "", "", "", err
}
files, err = helm.LoadChart(&templateVersion.Spec, nil)
if err != nil {
return "", "", "", "", err
}
appSubDir = templateVersion.Spec.VersionName
} else {
for k, v := range obj.Spec.Files {
content, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return "", "", "", "", err
}
files[k] = string(content)
}
appSubDir = getAppSubDir(files)
}
tempDir, err := ioutil.TempDir("", "helm-")
if err != nil {
return "", "", "", "", err
}
if err := writeTempDir(tempDir, files); err != nil {
return "", "", "", tempDir, err
}
appDir := filepath.Join(tempDir, appSubDir)
common.InjectDefaultRegistry(obj)
setValues, err := common.GenerateAnswerSetValues(obj, tempDir)
if err != nil {
return "", "", "", tempDir, err
}
commands := append([]string{"template", appDir, "--name", obj.Name, "--namespace", obj.Spec.TargetNamespace}, setValues...)
cmd := exec.Command(helmName, commands...)
sbOut := &bytes.Buffer{}
sbErr := &bytes.Buffer{}
cmd.Stdout = sbOut
cmd.Stderr = sbErr
if err := cmd.Start(); err != nil {
return "", "", "", tempDir, errors.Wrapf(err, "helm template failed. %s", filterErrorMessage(sbErr.String(), appDir, "template-dir"))
}
if err := cmd.Wait(); err != nil {
return "", "", "", tempDir, errors.Wrapf(err, "helm template failed. %s", filterErrorMessage(sbErr.String(), appDir, "template-dir"))
}
// notes.txt
commands = append([]string{"template", appDir, "--name", obj.Name, "--namespace", obj.Spec.TargetNamespace, "--notes"}, setValues...)
cmd = exec.Command(helmName, commands...)
noteOut := &bytes.Buffer{}
sbErr = &bytes.Buffer{}
cmd.Stdout = noteOut
cmd.Stderr = sbErr
if err := cmd.Start(); err != nil {
return "", "", "", tempDir, errors.Wrapf(err, "helm template --notes failed. %s", filterErrorMessage(sbErr.String(), appDir, "template-dir"))
}
if err := cmd.Wait(); err != nil {
return "", "", "", tempDir, errors.Wrapf(err, "helm template --notes failed. %s", filterErrorMessage(sbErr.String(), appDir, "template-dir"))
}
template := sbOut.String()
notes := noteOut.String()
return template, notes, appDir, tempDir, nil
}
// filter error message, replace old with new
func filterErrorMessage(msg, old, new string) string {
return strings.Replace(msg, old, new, -1)
}