github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/eventrecorder/event_recorder.go (about) 1 // Copyright 2021 iLogtail Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package eventrecorder 16 17 import ( 18 "context" 19 20 corev1 "k8s.io/api/core/v1" 21 v1 "k8s.io/api/core/v1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/client-go/kubernetes" 25 "k8s.io/client-go/kubernetes/scheme" 26 typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1" 27 restclient "k8s.io/client-go/rest" 28 "k8s.io/client-go/tools/record" 29 ref "k8s.io/client-go/tools/reference" 30 31 "github.com/alibaba/ilogtail/pkg/logger" 32 ) 33 34 type EventRecorder struct { 35 recorder record.EventRecorder 36 define EventDefine 37 } 38 39 var eventRecorder = &EventRecorder{} 40 var nodeIP string 41 var nodeName string 42 var podName string 43 var podNamespace string 44 45 func SetEventRecorder(kubeclientset *kubernetes.Clientset, module string) { 46 logger.Info(context.Background(), "Creating event broadcaster") 47 eventBroadcaster := record.NewBroadcaster() 48 eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")}) 49 recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: Logtail}) 50 51 eventRecorder.recorder = recorder 52 eventRecorder.define = *NewEventDefine(module) 53 } 54 55 func Init(nodeIPStr, nodeNameStr, podNameStr, podNamespaceStr string) { 56 var cfg *restclient.Config 57 58 cfg, err := restclient.InClusterConfig() 59 logger.Info(context.Background(), "init event_revorder", "") 60 61 nodeIP = nodeIPStr 62 nodeName = nodeNameStr 63 podName = podNameStr 64 podNamespace = podNamespaceStr 65 if err != nil { 66 logger.Error(context.Background(), "INIT_ALARM", "Error create EventRecorder: %s", err.Error()) 67 return 68 } 69 kubeClient, err := kubernetes.NewForConfig(cfg) 70 if err != nil { 71 logger.Error(context.Background(), "INIT_ALARM", "Error create EventRecorder: %s", err.Error()) 72 return 73 } 74 SetEventRecorder(kubeClient, "logtail") 75 } 76 77 func GetEventRecorder() *EventRecorder { 78 if eventRecorder.recorder != nil { 79 return eventRecorder 80 } 81 return nil 82 } 83 84 // func (e *EventRecorder) SendNormalEvent(object runtime.Object, action Action, message string) { 85 86 // if e == nil || e.recorder == nil { 87 // return 88 // } 89 // if message == "" { 90 // message = "success" 91 // } 92 // e.recorder.Event(object, corev1.EventTypeNormal, e.define.getInfoAction(action), message) 93 // } 94 95 // func (e *EventRecorder) SendErrorEvent(object runtime.Object, action Action, alarm Alarm, message string) { 96 // if e == nil || e.recorder == nil { 97 // return 98 // } 99 // if message == "" { 100 // message = "failed" 101 // } 102 // if alarm == "" { 103 // alarm = "Fail" 104 // } 105 // e.recorder.Event(object, corev1.EventTypeWarning, e.define.getErrorAction(action, alarm), message) 106 // } 107 108 func (e *EventRecorder) SendNormalEventWithAnnotation(object runtime.Object, annotations map[string]string, action Action, message string) { 109 if e == nil || e.recorder == nil { 110 logger.Info(context.Background(), "send normal event", "annotations", annotations, "message", message) 111 return 112 } 113 if message == "" { 114 message = "success" 115 } 116 if len(nodeIP) > 0 { 117 annotations["nodeIP"] = nodeIP 118 } 119 if len(nodeName) > 0 { 120 annotations["nodeName"] = nodeName 121 } 122 e.recorder.AnnotatedEventf(object, annotations, corev1.EventTypeNormal, e.define.getInfoAction(action), message) 123 } 124 125 func (e *EventRecorder) SendErrorEventWithAnnotation(object runtime.Object, annotations map[string]string, action Action, alarm Alarm, message string) { 126 if e == nil || e.recorder == nil { 127 logger.Info(context.Background(), "send error event", "annotations", annotations, "alarm", alarm, "message", message) 128 return 129 } 130 if message == "" { 131 message = "failed" 132 } 133 if alarm == "" { 134 alarm = "Fail" 135 } 136 if len(nodeIP) > 0 { 137 annotations["nodeIP"] = nodeIP 138 } 139 if len(nodeName) > 0 { 140 annotations["nodeName"] = nodeName 141 } 142 if alarm == "" { 143 alarm = CommonAlarm 144 } 145 e.recorder.AnnotatedEventf(object, annotations, corev1.EventTypeWarning, e.define.getErrorAction(action, alarm), message) 146 } 147 148 func (e *EventRecorder) GetObject() runtime.Object { 149 currPodName := "logtail-ds" 150 if len(podName) > 0 { 151 currPodName = podName 152 } else if len(nodeIP) > 0 { 153 currPodName = currPodName + "-" + nodeIP 154 } 155 156 currPodNamespace := "kube-system" 157 if len(podNamespace) > 0 { 158 currPodNamespace = podNamespace 159 } 160 161 podInfo := &v1.Pod{ 162 ObjectMeta: metav1.ObjectMeta{ 163 Name: currPodName, 164 Namespace: currPodNamespace, 165 }, 166 } 167 168 ref, err := ref.GetReference(scheme.Scheme, podInfo) 169 if err == nil { 170 return ref 171 } 172 return nil 173 }