forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configsyncer.go
248 lines (204 loc) · 8.41 KB
/
configsyncer.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package configsyncer
import (
"fmt"
"sort"
"strings"
"github.com/rancher/norman/controller"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/passwordgetter"
"github.com/rancher/rancher/pkg/project"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
projectv3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/pkg/errors"
k8scorev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
const (
passwordSecretPrefix = "cattle-global-data:"
)
// This controller is responsible for generate fluentd config
// and updating the config secret
// so the config reload could detect the file change and reload
func NewConfigSyncer(cluster *config.UserContext, SecretManager *SecretManager) *ConfigSyncer {
clusterName := cluster.ClusterName
clusterLoggingLister := cluster.Management.Management.ClusterLoggings(clusterName).Controller().Lister()
projectLoggingLister := cluster.Management.Management.ProjectLoggings(metav1.NamespaceAll).Controller().Lister()
namespaceLister := cluster.Core.Namespaces(metav1.NamespaceAll).Controller().Lister()
secrets := cluster.Management.Core.Secrets(metav1.NamespaceAll)
configGenerator := NewConfigGenerator(clusterName, projectLoggingLister, namespaceLister)
passwordGetter := passwordgetter.NewPasswordGetter(secrets)
return &ConfigSyncer{
apps: cluster.Management.Project.Apps(metav1.NamespaceAll),
clusterName: clusterName,
clusterLoggingLister: clusterLoggingLister,
projectLoggingLister: projectLoggingLister,
projectLister: cluster.Management.Management.Projects(clusterName).Controller().Lister(),
secretManager: SecretManager,
configGenerator: configGenerator,
passwordGetter: passwordGetter,
}
}
type ConfigSyncer struct {
apps projectv3.AppInterface
clusterName string
clusterLoggingLister mgmtv3.ClusterLoggingLister
projectLoggingLister mgmtv3.ProjectLoggingLister
projectLister mgmtv3.ProjectLister
secretManager *SecretManager
configGenerator *ConfigGenerator
passwordGetter *passwordgetter.PasswordGetter
}
func (s *ConfigSyncer) NamespaceSync(key string, obj *k8scorev1.Namespace) (runtime.Object, error) {
return obj, s.sync()
}
func (s *ConfigSyncer) ClusterLoggingSync(key string, obj *mgmtv3.ClusterLogging) (runtime.Object, error) {
return obj, s.sync()
}
func (s *ConfigSyncer) ProjectLoggingSync(key string, obj *mgmtv3.ProjectLogging) (runtime.Object, error) {
return obj, s.sync()
}
func (s *ConfigSyncer) sync() error {
project, err := project.GetSystemProject(s.clusterName, s.projectLister)
if err != nil {
return err
}
systemProjectName := project.Name
systemProjectID := fmt.Sprintf("%s:%s", project.Namespace, project.Name)
isDeployed, err := s.isAppDeploy(systemProjectName)
if err != nil {
return err
}
if !isDeployed {
return nil
}
allClusterLoggings, err := s.clusterLoggingLister.List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "List cluster loggings failed")
}
var clusterLoggings []*mgmtv3.ClusterLogging
for _, logging := range allClusterLoggings {
cp := logging.DeepCopy()
if err := s.passwordGetter.GetPasswordFromSecret(&cp.Spec.LoggingTargets); err != nil {
return errors.Wrap(err, "get password from secret failed")
}
clusterLoggings = append(clusterLoggings, cp)
}
allProjectLoggings, err := s.projectLoggingLister.List("", labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "List project logging failed")
}
var projectLoggings []*mgmtv3.ProjectLogging
for _, logging := range allProjectLoggings {
if controller.ObjectInCluster(s.clusterName, logging) {
cp := logging.DeepCopy()
if err := s.passwordGetter.GetPasswordFromSecret(&cp.Spec.LoggingTargets); err != nil {
return errors.Wrap(err, "get password from secret failed")
}
projectLoggings = append(projectLoggings, cp)
}
}
sort.Slice(projectLoggings, func(i, j int) bool {
return projectLoggings[i].Name < projectLoggings[j].Name
})
if err = s.syncSSLCert(clusterLoggings, projectLoggings); err != nil {
return err
}
if err = s.syncClusterConfig(clusterLoggings, systemProjectID); err != nil {
return err
}
return s.syncProjectConfig(projectLoggings, systemProjectID)
}
func (s *ConfigSyncer) isAppDeploy(appNamespace string) (bool, error) {
appName := loggingconfig.AppName
app, err := s.apps.GetNamespaced(appNamespace, appName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, errors.Wrapf(err, "get app %s failed", appName)
}
if app.DeletionTimestamp != nil {
return false, nil
}
return true, nil
}
func (s *ConfigSyncer) syncClusterConfig(clusterLoggings []*mgmtv3.ClusterLogging, systemProjectID string) error {
secretName := loggingconfig.RancherLoggingConfigSecretName()
namespace := loggingconfig.LoggingNamespace
var clusterLogging *mgmtv3.ClusterLogging
if len(clusterLoggings) != 0 {
clusterLogging = clusterLoggings[0]
}
buf, err := s.configGenerator.GenerateClusterLoggingConfig(clusterLogging, systemProjectID, loggingconfig.DefaultCertDir)
if err != nil {
return err
}
data := map[string][]byte{
loggingconfig.LoggingSecretClusterConfigKey: buf,
}
return s.secretManager.updateSecret(secretName, namespace, data)
}
func (s *ConfigSyncer) syncProjectConfig(projectLoggings []*mgmtv3.ProjectLogging, systemProjectID string) error {
secretName := loggingconfig.RancherLoggingConfigSecretName()
namespace := loggingconfig.LoggingNamespace
buf, err := s.configGenerator.GenerateProjectLoggingConfig(projectLoggings, systemProjectID, loggingconfig.DefaultCertDir)
if err != nil {
return err
}
data := map[string][]byte{
loggingconfig.LoggingSecretProjectConfigKey: buf,
}
return s.secretManager.updateSecret(secretName, namespace, data)
}
func (s *ConfigSyncer) syncSSLCert(clusterLoggings []*mgmtv3.ClusterLogging, projectLoggings []*mgmtv3.ProjectLogging) error {
secretname := loggingconfig.RancherLoggingSSLSecretName()
namespace := loggingconfig.LoggingNamespace
sslConfig := make(map[string][]byte)
for _, v := range clusterLoggings {
ca, cert, key := GetSSLConfig(v.Spec.LoggingTargets)
sslConfig[loggingconfig.SecretDataKeyCa(loggingconfig.ClusterLevel, v.Namespace)] = []byte(ca)
sslConfig[loggingconfig.SecretDataKeyCert(loggingconfig.ClusterLevel, v.Namespace)] = []byte(cert)
sslConfig[loggingconfig.SecretDataKeyCertKey(loggingconfig.ClusterLevel, v.Namespace)] = []byte(key)
}
for _, v := range projectLoggings {
target := v.Spec.LoggingTargets
ca, cert, key := GetSSLConfig(target)
projectKey := strings.Replace(v.Spec.ProjectName, ":", "_", -1)
caByte := []byte(ca)
sslConfig[loggingconfig.SecretDataKeyCa(loggingconfig.ProjectLevel, projectKey)] = caByte
sslConfig[loggingconfig.SecretDataKeyCert(loggingconfig.ProjectLevel, projectKey)] = []byte(cert)
sslConfig[loggingconfig.SecretDataKeyCertKey(loggingconfig.ProjectLevel, projectKey)] = []byte(key)
}
return s.secretManager.updateSecret(secretname, namespace, sslConfig)
}
func GetSSLConfig(target mgmtv3.LoggingTargets) (string, string, string) {
var certificate, clientCert, clientKey string
if target.ElasticsearchConfig != nil {
certificate = target.ElasticsearchConfig.Certificate
clientCert = target.ElasticsearchConfig.ClientCert
clientKey = target.ElasticsearchConfig.ClientKey
} else if target.SplunkConfig != nil {
certificate = target.SplunkConfig.Certificate
clientCert = target.SplunkConfig.ClientCert
clientKey = target.SplunkConfig.ClientKey
} else if target.KafkaConfig != nil {
certificate = target.KafkaConfig.Certificate
clientCert = target.KafkaConfig.ClientCert
clientKey = target.KafkaConfig.ClientKey
} else if target.SyslogConfig != nil {
certificate = target.SyslogConfig.Certificate
clientCert = target.SyslogConfig.ClientCert
clientKey = target.SyslogConfig.ClientKey
} else if target.FluentForwarderConfig != nil {
certificate = target.FluentForwarderConfig.Certificate
} else if target.CustomTargetConfig != nil {
certificate = target.CustomTargetConfig.Certificate
clientCert = target.CustomTargetConfig.ClientCert
clientKey = target.CustomTargetConfig.ClientKey
}
return certificate, clientCert, clientKey
}