37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
common.go 4.97 KB
一键复制 编辑 原始数据 按行查看 历史
Erik Wilson 提交于 2018-11-06 18:04 . Store templateContent cache on disk
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)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.0-rc8

搜索帮助

0d507c66 1850385 C8b1a773 1850385