forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterlogging.go
187 lines (161 loc) · 7.74 KB
/
clusterlogging.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
package logging
import (
"context"
"reflect"
"github.com/pkg/errors"
"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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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"
)
// ClusterLoggingLifecycle listens for clusterLogging CRD in management API
// and update the changes to configmap, deploy fluentd, embedded elasticsearch, embedded kibana
type ClusterLoggingLifecycle struct {
clusterName string
clusterLoggings v3.ClusterLoggingInterface
clusterLoggingLister v3.ClusterLoggingLister
clusterRoleBindings rbacv1.ClusterRoleBindingInterface
clusterLister v3.ClusterLister
configmaps v1.ConfigMapInterface
daemonsets v1beta2.DaemonSetInterface
deployments v1beta2.DeploymentInterface
deploymentLister v1beta2.DeploymentLister
endpointLister v1.EndpointsLister
k8sNodeLister v1.NodeLister
namespaces v1.NamespaceInterface
nodeLister v3.NodeLister
projectLoggingLister v3.ProjectLoggingLister
roles rbacv1.RoleInterface
rolebindings rbacv1.RoleBindingInterface
services v1.ServiceInterface
serviceLister v1.ServiceLister
serviceAccounts v1.ServiceAccountInterface
}
func registerClusterLogging(cluster *config.UserContext) {
clusterloggingClient := cluster.Management.Management.ClusterLoggings(cluster.ClusterName)
lifecycle := &ClusterLoggingLifecycle{
clusterName: cluster.ClusterName,
clusterLoggings: clusterloggingClient,
clusterLoggingLister: clusterloggingClient.Controller().Lister(),
clusterRoleBindings: cluster.RBAC.ClusterRoleBindings(loggingconfig.LoggingNamespace),
clusterLister: cluster.Management.Management.Clusters("").Controller().Lister(),
configmaps: cluster.Core.ConfigMaps(loggingconfig.LoggingNamespace),
daemonsets: cluster.Apps.DaemonSets(loggingconfig.LoggingNamespace),
deployments: cluster.Apps.Deployments(loggingconfig.LoggingNamespace),
deploymentLister: cluster.Apps.Deployments("").Controller().Lister(),
k8sNodeLister: cluster.Management.Core.Nodes("").Controller().Lister(),
namespaces: cluster.Core.Namespaces(""),
nodeLister: cluster.Management.Management.Nodes("").Controller().Lister(),
endpointLister: cluster.Core.Endpoints("").Controller().Lister(),
projectLoggingLister: cluster.Management.Management.ProjectLoggings("").Controller().Lister(),
roles: cluster.RBAC.Roles(loggingconfig.LoggingNamespace),
rolebindings: cluster.RBAC.RoleBindings(loggingconfig.LoggingNamespace),
services: cluster.Core.Services(loggingconfig.LoggingNamespace),
serviceLister: cluster.Core.Services("").Controller().Lister(),
serviceAccounts: cluster.Core.ServiceAccounts(loggingconfig.LoggingNamespace),
}
clusterloggingClient.AddClusterScopedLifecycle("cluster-logging-controller", cluster.ClusterName, lifecycle)
}
func (c *ClusterLoggingLifecycle) Create(obj *v3.ClusterLogging) (*v3.ClusterLogging, error) {
newObj, err := v3.LoggingConditionProvisioned.DoUntilTrue(obj, func() (runtime.Object, error) {
return obj, provision(c.namespaces, c.configmaps, c.serviceAccounts, c.clusterRoleBindings, c.daemonsets, c.clusterLister, c.clusterName)
})
return newObj.(*v3.ClusterLogging), err
}
func (c *ClusterLoggingLifecycle) Updated(obj *v3.ClusterLogging) (*v3.ClusterLogging, error) {
newObj, err := v3.LoggingConditionProvisioned.DoUntilTrue(obj, func() (runtime.Object, error) {
return obj, provision(c.namespaces, c.configmaps, c.serviceAccounts, c.clusterRoleBindings, c.daemonsets, c.clusterLister, c.clusterName)
})
if err != nil {
return newObj.(*v3.ClusterLogging), err
}
cl := newObj.(*v3.ClusterLogging)
if reflect.DeepEqual(cl.Spec, obj.Status.AppliedSpec) {
return newObj.(*v3.ClusterLogging), nil
}
newObj, err = v3.LoggingConditionUpdated.Do(newObj, func() (runtime.Object, error) {
return c.update(obj)
})
if err != nil {
return newObj.(*v3.ClusterLogging), err
}
cl = newObj.(*v3.ClusterLogging)
cl.Status.AppliedSpec = cl.Spec
return cl, nil
}
func (c *ClusterLoggingLifecycle) Remove(obj *v3.ClusterLogging) (*v3.ClusterLogging, error) {
isAllDisable, err := utils.CleanResource(c.namespaces, c.clusterLoggingLister, c.projectLoggingLister)
if err != nil {
return obj, err
}
if !isAllDisable {
return obj, utils.UnsetConfigMap(c.configmaps, loggingconfig.ClusterLoggingName, "cluster")
}
return obj, nil
}
func (c *ClusterLoggingLifecycle) createOrUpdateClusterConfigMap() error {
clusterLoggingList, err := c.clusterLoggings.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return errors.Wrap(err, "list cluster logging failed")
}
if len(clusterLoggingList) == 0 {
return errors.New("no cluster logging configured")
}
conf := make(map[string]interface{})
wpClusterlogging, err := utils.ToWrapClusterLogging(clusterLoggingList[0].Spec)
if err != nil {
return errors.Wrap(err, "to wraper cluster logging failed")
}
conf["clusterTarget"] = wpClusterlogging
err = generator.GenerateConfigFile(loggingconfig.ClusterConfigPath, generator.ClusterTemplate, "cluster", conf)
if err != nil {
return errors.Wrap(err, "generate cluster config file failed")
}
return utils.UpdateConfigMap(loggingconfig.ClusterConfigPath, loggingconfig.ClusterLoggingName, "cluster", c.configmaps)
}
func (c *ClusterLoggingLifecycle) update(obj *v3.ClusterLogging) (newobj *v3.ClusterLogging, err error) {
ctx, cancel := context.WithCancel(context.Background())
logger, io := utils.NewClusterLoggingLogger(obj, c.clusterLoggings, v3.LoggingConditionUpdated)
defer func() {
defer io.Close()
defer cancel()
}()
//embedded
if utils.GetClusterTarget(obj.Spec) == "embedded" {
logger.Infof("Start creating embedded %s, %s", loggingconfig.EmbeddedESName, loggingconfig.EmbeddedKibanaName)
if err = utils.CreateOrUpdateEmbeddedTarget(c.deployments, c.serviceAccounts, c.services, c.roles, c.rolebindings, loggingconfig.LoggingNamespace, obj); err != nil {
return obj, err
}
logger.Infof("Checking embedded components deployment progress")
if err = utils.UpdateEmbeddedEndpointWithRetry(ctx, c.deploymentLister, c.endpointLister, c.serviceLister, c.clusterLoggings, c.nodeLister, c.k8sNodeLister, c.clusterName, logger); err != nil {
return obj, err
}
} else {
if err = utils.RemoveEmbeddedTarget(c.deployments, c.serviceAccounts, c.services, c.roles, c.rolebindings); err != nil {
return obj, err
}
}
if err = c.createOrUpdateClusterConfigMap(); err != nil {
return obj, err
}
return c.clusterLoggings.Get(obj.Name, metav1.GetOptions{})
}
func provision(namespaces v1.NamespaceInterface, configmaps v1.ConfigMapInterface, serviceAccounts v1.ServiceAccountInterface, clusterRoleBindings rbacv1.ClusterRoleBindingInterface, daemonsets v1beta2.DaemonSetInterface, clusterLister v3.ClusterLister, clusterName string) error {
if err := utils.IniteNamespace(namespaces); err != nil {
return err
}
if err := utils.InitConfigMap(configmaps); err != nil {
return err
}
if err := utils.CreateLogAggregator(daemonsets, serviceAccounts, clusterRoleBindings, clusterLister, clusterName, loggingconfig.LoggingNamespace); err != nil {
return err
}
return utils.CreateFluentd(daemonsets, serviceAccounts, clusterRoleBindings, loggingconfig.LoggingNamespace)
}