37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
validator.go 3.51 KB
一键复制 编辑 原始数据 按行查看 历史
Aiwantaozi 提交于 2019-04-08 18:10 . Refactoring logging template
package generator
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/vmware/kube-fluentd-operator/config-reloader/fluentd"
)
var (
fluentdForwardType = "forward"
recordTransformerType = "record_transformer"
rubyCodeBlockReg = regexp.MustCompile(`#\{.*\}`)
generalAllowFragnent = map[string]int{"buffer": 1}
filterAllowFragments = map[string]int{"record": 1}
forwardAllowFragments = map[string]int{
"buffer": 1,
"security": 1,
"server": -1,
}
)
func ValidateCustomTags(data interface{}) error {
return validateFragments("filter-custom-tags", "filter", data)
}
func ValidateSyslogToken(data interface{}) error {
return validateFragments("filter-sumo", "filter", data)
}
func ValidateCustomTarget(data interface{}) error {
return validateFragments("store-target", "store", data)
}
func validateFragments(templateName, fragmentName string, data interface{}) error {
fragments, err := generateFragments(templateName, data)
if err != nil {
return errors.Wrapf(err, "generate configure from template %s failed", templateName)
}
if err = validateFragmentExist(fragmentName, fragments); err != nil {
return err
}
fragment := fragments[0]
var allow map[string]int
switch fragment.Type() {
case recordTransformerType:
allow = filterAllowFragments
case fluentdForwardType:
allow = forwardAllowFragments
default:
allow = generalAllowFragnent
}
return validateFragmentsMatchExpected(fragment.Nested, allow)
}
func validateFragmentsMatchExpected(fragments fluentd.Fragment, expected map[string]int) error {
actual := make(map[string]int)
for _, v := range fragments {
actualNum := actual[v.Name] + 1
expectedNum, ok := expected[v.Name]
if !ok {
return errors.New("unexpected configure element: " + v.Name)
}
if expectedNum < 0 {
continue
}
if actualNum > expectedNum {
return errors.New("unexpected configure element: expected " + fmt.Sprint(expectedNum) + " configure element " + v.Name + ", but got " + fmt.Sprint(actualNum))
}
actual[v.Name] = actualNum
}
return nil
}
func validateFragmentExist(expectedName string, fragments fluentd.Fragment) error {
if len(fragments) == 0 {
return errors.New("not configure element found")
}
if len(fragments) > 1 {
return errors.New("expected configure element: " + expectedName + ", detected more than one elements: " + fragments[0].Name + ", " + fragments[1].Name + "...")
}
if fragments[0].Name != expectedName {
return errors.New(fragments[0].Name + "isn't expected configure element" + expectedName)
}
return nil
}
func generateFragments(templateName string, data interface{}) (fluentd.Fragment, error) {
buf, err := GenerateConfig(templateName, data)
if err != nil {
return nil, errors.Wrap(err, "generate fluentd configure failed")
}
configStr := string(buf)
if err = filterRubyCode(configStr); err != nil {
return nil, err
}
fragments, err := fluentd.ParseString(configStr)
if err != nil {
return nil, errors.Wrap(err, "parse fluentd configure failed")
}
return fragments, nil
}
func filterRubyCode(s string) error {
rubyCodeBlocks := rubyCodeBlockReg.FindStringSubmatch(s)
if len(rubyCodeBlocks) > 0 {
return errors.New("not allow embedded Ruby code: " + fmt.Sprintf("%v", rubyCodeBlocks))
}
return nil
}
func escapeString(postDoc string) string {
var escapeReplacer = strings.NewReplacer(
"\t", `\\t`,
"\n", `\\n`,
"\r", `\\r`,
"\f", `\\f`,
"\b", `\\b`,
"\"", `\\\"`,
"\\", `\\\\`,
)
escapeString := escapeReplacer.Replace(postDoc)
return fmt.Sprintf(`"%s"`, escapeString)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc6

搜索帮助

0d507c66 1850385 C8b1a773 1850385