37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
watcher.go 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.2-rc14

搜索帮助

0d507c66 1850385 C8b1a773 1850385