This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
124 lines (101 loc) · 3.33 KB
/
generator.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
package generator
import (
"bytes"
"fmt"
"strings"
"text/template"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"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"
)
var tmplCache = template.New("template")
func init() {
tmplCache = tmplCache.Funcs(template.FuncMap{"escapeString": escapeString})
tmplCache = template.Must(tmplCache.Parse(SourceTemplate))
tmplCache = template.Must(tmplCache.Parse(FilterTemplate))
tmplCache = template.Must(tmplCache.Parse(MatchTemplate))
tmplCache = template.Must(tmplCache.Parse(Template))
}
func GenerateConfig(tempalteName string, conf interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
if err := tmplCache.ExecuteTemplate(buf, tempalteName, conf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func GenerateClusterConfig(logging mgmtv3.ClusterLoggingSpec, excludeNamespaces, certDir string) ([]byte, error) {
wl, err := newWrapClusterLogging(logging, excludeNamespaces, certDir)
if err != nil {
return nil, errors.Wrap(err, "to wraper cluster logging failed")
}
if wl == nil {
return []byte{}, nil
}
if logging.SyslogConfig != nil && logging.SyslogConfig.Token != "" {
if err = ValidateSyslogToken(wl); err != nil {
return nil, err
}
}
if err = ValidateCustomTags(wl); err != nil {
return nil, err
}
validateData := *wl
if logging.FluentForwarderConfig != nil && wl.EnableShareKey {
validateData.EnableShareKey = false //skip generate precan configure included ruby code
}
if err = ValidateCustomTarget(validateData); err != nil {
return nil, err
}
buf, err := GenerateConfig("cluster-template", wl)
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 []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 := newWrapProjectLogging(v.Spec, formatgrepNamespace, certDir, isSystemProject)
if err != nil {
return nil, err
}
if wpl == nil {
continue
}
if wpl.SyslogConfig.Token != "" {
if err = ValidateSyslogToken(wpl); err != nil {
return nil, err
}
}
if err = ValidateCustomTags(wpl); err != nil {
return nil, err
}
validateData := *wpl
if v.Spec.FluentForwarderConfig != nil && wpl.EnableShareKey {
validateData.EnableShareKey = false //skip generate precan configure included ruby code
}
if err = ValidateCustomTarget(validateData); err != nil {
return nil, err
}
wl = append(wl, *wpl)
}
buf, err := GenerateConfig("project-template", wl)
if err != nil {
return nil, errors.Wrap(err, "generate project config file failed")
}
return buf, nil
}