forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectlogging.go
219 lines (190 loc) · 7.35 KB
/
projectlogging.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package logging
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rancher/norman/controller"
"github.com/rancher/types/apis/apps/v1beta2"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
rbacv1 "github.com/rancher/types/apis/rbac.authorization.k8s.io/v1"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/generator"
"github.com/rancher/rancher/pkg/controllers/user/logging/utils"
"github.com/rancher/rancher/pkg/ticker"
)
const (
ProjectIDAnnotation = "field.cattle.io/projectId"
)
// ProjectLoggingSyncer listens for projectLogging CRD in management API
// and update the changes to configmap, deploy fluentd
type ProjectLoggingSyncer struct {
clusterName string
clusterLister v3.ClusterLister
clusterRoleBindings rbacv1.ClusterRoleBindingInterface
clusterLoggingLister v3.ClusterLoggingLister
configmaps v1.ConfigMapInterface
daemonsets v1beta2.DaemonSetInterface
namespaces v1.NamespaceInterface
projectLoggings v3.ProjectLoggingInterface
secrets v1.SecretInterface
serviceAccounts v1.ServiceAccountInterface
}
type projectLoggingEndpointWatcher struct {
projectLoggings v3.ProjectLoggingInterface
}
func registerProjectLogging(ctx context.Context, cluster *config.UserContext) {
projectLoggings := cluster.Management.Management.ProjectLoggings("")
syncer := &ProjectLoggingSyncer{
clusterName: cluster.ClusterName,
clusterLister: cluster.Management.Management.Clusters("").Controller().Lister(),
clusterRoleBindings: cluster.RBAC.ClusterRoleBindings(loggingconfig.LoggingNamespace),
clusterLoggingLister: cluster.Management.Management.ClusterLoggings("").Controller().Lister(),
configmaps: cluster.Core.ConfigMaps(loggingconfig.LoggingNamespace),
daemonsets: cluster.Apps.DaemonSets(loggingconfig.LoggingNamespace),
namespaces: cluster.Core.Namespaces(""),
projectLoggings: projectLoggings,
secrets: cluster.Core.Secrets(loggingconfig.LoggingNamespace),
serviceAccounts: cluster.Core.ServiceAccounts(loggingconfig.LoggingNamespace),
}
watcher := projectLoggingEndpointWatcher{
projectLoggings: projectLoggings,
}
projectLoggings.AddClusterScopedHandler("project-logging-controller", cluster.ClusterName, syncer.Sync)
go watcher.watch(ctx, watcherSyncInterval)
}
func (p *projectLoggingEndpointWatcher) watch(ctx context.Context, interval time.Duration) {
for range ticker.Context(ctx, interval) {
if err := p.checkTarget(); err != nil {
logrus.Error(err)
}
}
}
func (c *ProjectLoggingSyncer) Sync(key string, obj *v3.ProjectLogging) error {
if obj == nil || obj.DeletionTimestamp != nil || utils.GetProjectTarget(obj.Spec) == "none" {
isAllDisable, err := utils.CleanResource(c.namespaces, c.clusterLoggingLister, c.projectLoggings.Controller().Lister(), nil, obj)
if err != nil {
return err
}
if obj != nil && !isAllDisable {
if err = c.createOrUpdateProjectConfigMap(obj.Name); err != nil {
return err
}
if err := utils.UnsetSecret(c.secrets, loggingconfig.SSLSecretName, getProjectSecretPrefix(obj.Spec.ProjectName)); err != nil {
return err
}
}
var updateErr error
if obj != nil && !reflect.DeepEqual(obj.Spec, obj.Status.AppliedSpec) {
updatedObj := obj.DeepCopy()
updatedObj.Status.AppliedSpec = obj.Spec
v3.LoggingConditionProvisioned.False(updatedObj)
v3.LoggingConditionUpdated.False(updatedObj)
_, updateErr = c.projectLoggings.Update(updatedObj)
}
return updateErr
}
original := obj
obj = original.DeepCopy()
newObj, err := c.doSync(obj)
if err != nil {
return err
}
if newObj != nil && !reflect.DeepEqual(newObj, original) {
if _, err = c.projectLoggings.Update(newObj); err != nil {
return err
}
}
return nil
}
func (c *ProjectLoggingSyncer) doSync(obj *v3.ProjectLogging) (*v3.ProjectLogging, error) {
newObj := obj.DeepCopy()
_, err := v3.LoggingConditionProvisioned.Do(obj, func() (runtime.Object, error) {
return obj, provision(c.namespaces, c.configmaps, c.serviceAccounts, c.clusterRoleBindings, c.daemonsets, c.clusterLister, c.secrets, c.clusterName)
})
if err != nil {
return obj, err
}
_, err = v3.LoggingConditionUpdated.Do(obj, func() (runtime.Object, error) {
if err := utils.UpdateSSLAuthentication(getProjectSecretPrefix(obj.Spec.ProjectName), obj.Spec.ElasticsearchConfig, obj.Spec.SplunkConfig, obj.Spec.KafkaConfig, obj.Spec.SyslogConfig, c.secrets); err != nil {
return obj, err
}
return obj, c.createOrUpdateProjectConfigMap("")
})
if err != nil {
return obj, err
}
newObj.Status.AppliedSpec = obj.Spec
return newObj, nil
}
func (c *ProjectLoggingSyncer) createOrUpdateProjectConfigMap(excludeName string) error {
projectLoggings, err := c.projectLoggings.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return errors.Wrap(err, "list project logging failed")
}
if len(projectLoggings) == 0 {
return fmt.Errorf("no project logging configured")
}
ns, err := c.namespaces.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return errors.Wrap(err, "list namespace failed")
}
var wl []utils.WrapProjectLogging
for _, v := range projectLoggings {
if v.Name == excludeName {
continue
}
if controller.ObjectInCluster(c.clusterName, v) {
var grepNamespace []string
for _, v2 := range ns {
if nsProjectName, ok := v2.Annotations[ProjectIDAnnotation]; ok && nsProjectName == v.Spec.ProjectName {
grepNamespace = append(grepNamespace, v2.Name)
}
}
formatgrepNamespace := fmt.Sprintf("(%s)", strings.Join(grepNamespace, "|"))
projectLogging, err := utils.ToWrapProjectLogging(formatgrepNamespace, v.Spec)
if err != nil {
return err
}
wl = append(wl, *projectLogging)
}
}
conf := make(map[string]interface{})
conf["projectTargets"] = wl
err = generator.GenerateConfigFile(loggingconfig.ProjectConfigPath, generator.ProjectTemplate, "project", conf)
if err != nil {
return errors.Wrap(err, "generate project config file failed")
}
return utils.UpdateConfigMap(loggingconfig.ProjectConfigPath, loggingconfig.ProjectLoggingName, "project", c.configmaps)
}
func (p *projectLoggingEndpointWatcher) checkTarget() error {
pls, err := p.projectLoggings.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "list projectlogging fail in endpoint watcher")
}
if len(pls) == 0 {
return nil
}
var mergedErrs error
for _, v := range pls {
_, _, err = utils.GetWrapConfig(v.Spec.ElasticsearchConfig, v.Spec.SplunkConfig, v.Spec.SyslogConfig, v.Spec.KafkaConfig, nil)
if err != nil {
updatedObj := v.DeepCopy()
v3.LoggingConditionUpdated.False(updatedObj)
v3.LoggingConditionUpdated.Message(updatedObj, err.Error())
_, updateErr := p.projectLoggings.Update(updatedObj)
mergedErrs = mergedErrors(mergedErrs, errors.Wrapf(err, "%s:%s", v.Namespace, v.Name), updateErr)
}
}
return errors.Wrapf(mergedErrs, "check project logging reachable fail in watch endpoint")
}
func getProjectSecretPrefix(projectName string) string {
return "project_" + strings.Replace(projectName, ":", "_", -1)
}