37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generator.go 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
package generator
import (
"bytes"
"fmt"
"strings"
"text/template"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/utils"
"github.com/rancher/rancher/pkg/project"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/pkg/errors"
k8scorev1 "k8s.io/api/core/v1"
)
func generateConfig(templateM, tempalteName string, conf map[string]interface{}) ([]byte, error) {
tp, err := template.New(tempalteName).Parse(templateM)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
if err = tp.Execute(buf, conf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func GenerateClusterConfig(logging mgmtv3.ClusterLoggingSpec, excludeNamespaces, certDir string) ([]byte, error) {
wl, err := utils.NewWrapClusterLogging(logging, excludeNamespaces)
if err != nil {
return nil, errors.Wrap(err, "to wraper cluster logging failed")
}
if wl == nil {
return []byte{}, nil
}
conf := map[string]interface{}{
"clusterTarget": wl,
"clusterName": logging.ClusterName,
"certDir": certDir,
}
buf, err := generateConfig(ClusterTemplate, loggingconfig.ClusterLevel, conf)
if err != nil {
return nil, errors.Wrap(err, "generate cluster config file failed")
}
return buf, nil
}
func GenerateProjectConfig(projectLoggings []*mgmtv3.ProjectLogging, namespaces []*k8scorev1.Namespace, systemProjectID, certDir string) ([]byte, error) {
var wl []utils.ProjectLoggingTemplateWrap
for _, v := range projectLoggings {
var grepNamespace []string
for _, v2 := range namespaces {
if nsProjectName, ok := v2.Annotations[project.ProjectIDAnn]; ok && nsProjectName == v.Spec.ProjectName {
namespacePattern := loggingconfig.GetNamespacePattern(v2.Name)
grepNamespace = append(grepNamespace, namespacePattern)
}
}
if len(grepNamespace) == 0 {
continue
}
formatgrepNamespace := fmt.Sprintf("(%s)", strings.Join(grepNamespace, "|"))
isSystemProject := v.Spec.ProjectName == systemProjectID
wpl, err := utils.NewWrapProjectLogging(v.Spec, formatgrepNamespace, isSystemProject)
if err != nil {
return nil, err
}
if wpl == nil {
continue
}
wl = append(wl, *wpl)
}
conf := map[string]interface{}{
"projectTargets": wl,
"certDir": certDir,
}
buf, err := generateConfig(ProjectTemplate, loggingconfig.ProjectLevel, conf)
if err != nil {
return nil, errors.Wrap(err, "generate project config file failed")
}
return buf, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.3.0-alpha1

搜索帮助

0d507c66 1850385 C8b1a773 1850385