forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
144 lines (121 loc) · 3.94 KB
/
watcher.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
package watcher
import (
"context"
"reflect"
"time"
"github.com/rancher/rancher/pkg/controllers/user/logging/utils"
"github.com/rancher/rancher/pkg/ticker"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/config/dialer"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type endpointWatcher struct {
dialerFactory dialer.Factory
clusterName string
clusterLoggings mgmtv3.ClusterLoggingInterface
projectLoggings mgmtv3.ProjectLoggingInterface
}
func StartEndpointWatcher(ctx context.Context, cluster *config.UserContext) {
s := &endpointWatcher{
dialerFactory: cluster.Management.Dialer,
clusterName: cluster.ClusterName,
clusterLoggings: cluster.Management.Management.ClusterLoggings(cluster.ClusterName),
projectLoggings: cluster.Management.Management.ProjectLoggings(metav1.NamespaceAll),
}
go s.watch(ctx, 120*time.Second)
}
func (e *endpointWatcher) watch(ctx context.Context, interval time.Duration) {
for range ticker.Context(ctx, interval) {
if err := e.checkClusterTarget(); err != nil {
logrus.Error(err)
}
if err := e.checkProjectTarget(); err != nil {
logrus.Error(err)
}
}
}
func (e *endpointWatcher) checkClusterTarget() error {
cls, err := e.clusterLoggings.Controller().Lister().List(e.clusterName, labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "list clusterlogging fail in endpoint watcher")
}
if len(cls) == 0 {
return nil
}
obj := cls[0]
clusterDialer, err := e.dialerFactory.ClusterDialer(obj.Spec.ClusterName)
if err != nil {
return errors.Wrapf(err, "get cluster dailer %s failed", obj.Spec.ClusterName)
}
wl := utils.NewLoggingTargetTestWrap(obj.Spec.LoggingTargets)
if wl == nil {
err = nil
} else {
err = wl.TestReachable(clusterDialer, false)
}
updatedObj := setClusterLoggingErrMsg(obj, err)
if reflect.DeepEqual(updatedObj, obj) {
return nil
}
_, updateErr := e.clusterLoggings.Update(updatedObj)
if updateErr != errors.Wrapf(updateErr, "set clusterlogging fail in watch endpoint") {
return updateErr
}
return nil
}
func (e *endpointWatcher) checkProjectTarget() error {
clusterDialer, err := e.dialerFactory.ClusterDialer(e.clusterName)
if err != nil {
return errors.Wrapf(err, "get cluster dailer %s failed", e.clusterName)
}
pls, err := e.projectLoggings.Controller().Lister().List(metav1.NamespaceAll, labels.NewSelector())
if err != nil {
return errors.Wrapf(err, "list clusterlogging fail in endpoint watcher")
}
for _, v := range pls {
wp := utils.NewLoggingTargetTestWrap(v.Spec.LoggingTargets)
if wp == nil {
err = nil
} else {
err = wp.TestReachable(clusterDialer, false)
}
updatedObj := setProjectLoggingErrMsg(v, err)
if reflect.DeepEqual(updatedObj, v) {
continue
}
_, updateErr := e.projectLoggings.Update(updatedObj)
if updateErr != errors.Wrapf(updateErr, "set project fail in watch endpoint") {
return updateErr
}
}
return nil
}
func setProjectLoggingErrMsg(obj *mgmtv3.ProjectLogging, err error) *mgmtv3.ProjectLogging {
updatedObj := obj.DeepCopy()
if err != nil {
mgmtv3.LoggingConditionUpdated.False(updatedObj)
mgmtv3.LoggingConditionUpdated.Message(updatedObj, err.Error())
return updatedObj
}
mgmtv3.LoggingConditionUpdated.True(updatedObj)
mgmtv3.LoggingConditionUpdated.Message(updatedObj, "")
return updatedObj
}
func setClusterLoggingErrMsg(obj *mgmtv3.ClusterLogging, err error) *mgmtv3.ClusterLogging {
updatedObj := obj.DeepCopy()
if err != nil {
updatedObj.Status.FailedSpec = &obj.Spec
mgmtv3.LoggingConditionUpdated.False(updatedObj)
mgmtv3.LoggingConditionUpdated.Message(updatedObj, err.Error())
return updatedObj
}
updatedObj.Status.FailedSpec = nil
updatedObj.Status.AppliedSpec = obj.Spec
mgmtv3.LoggingConditionUpdated.True(updatedObj)
mgmtv3.LoggingConditionUpdated.Message(updatedObj, "")
return updatedObj
}