diff --git a/helper/envconfig/aliyunlog_operation_wrapper.go b/helper/envconfig/aliyunlog_operation_wrapper.go index db93e4c035..a264083cde 100644 --- a/helper/envconfig/aliyunlog_operation_wrapper.go +++ b/helper/envconfig/aliyunlog_operation_wrapper.go @@ -42,6 +42,7 @@ type operationWrapper struct { project string logstoreCacheMap map[string]time.Time configCacheMap map[string]time.Time + eventRecorder *k8s_event.EventRecorder } func createDefaultK8SIndex(logstoremode string) *aliyunlog.Index { @@ -102,8 +103,9 @@ func createClientInterface(endpoint, accessKeyID, accessKeySecret, stsToken stri func createAliyunLogOperationWrapper(project string, clientInterface aliyunlog.ClientInterface) (*operationWrapper, error) { var err error wrapper := &operationWrapper{ - logClient: clientInterface, - project: project, + logClient: clientInterface, + project: project, + eventRecorder: k8s_event.GetEventRecorder(), } logger.Info(context.Background(), "init aliyun log operation wrapper", "begin") // retry when make project fail @@ -202,16 +204,14 @@ func (o *operationWrapper) createProductLogstore(config *AliyunLogConfigSpec, pr annotations := GetAnnotationByObject(config, project, logstore, product, config.LogtailConfig.ConfigName, false) if err != nil { - if k8s_event.GetEventRecorder() != nil { - customErr := CustomErrorFromPopError(err) - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateProductLogStore, "", fmt.Sprintf("create product log failed, error: %s", err.Error())) - } + customErr := CustomErrorFromPopError(err) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateProductLogStore, "", fmt.Sprintf("create product log failed, error: %s", err.Error())) logger.Warning(context.Background(), "CREATE_PRODUCT_ALARM", "create product error, error", err) return err - } else if k8s_event.GetEventRecorder() != nil { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.CreateProductLogStore, "create product log success") } + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.CreateProductLogStore, "create product log success") + o.addLogstoreCache(project, logstore) return nil } @@ -335,15 +335,13 @@ func (o *operationWrapper) makesureLogstoreExist(config *AliyunLogConfigSpec) er } annotations := GetAnnotationByObject(config, project, logstore, "", config.LogtailConfig.ConfigName, false) if err != nil { - if k8s_event.GetEventRecorder() != nil { - customErr := CustomErrorFromSlsSDKError(err) - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateLogstore, "", fmt.Sprintf("create logstore failed, error: %s", err.Error())) - } + customErr := CustomErrorFromSlsSDKError(err) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateLogstore, "", fmt.Sprintf("create logstore failed, error: %s", err.Error())) return err - } else if k8s_event.GetEventRecorder() != nil { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.CreateLogstore, "create logstore success") } + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.CreateLogstore, "create logstore success") + // after create logstore success, wait 1 sec time.Sleep(time.Second) // use default k8s index @@ -396,12 +394,10 @@ func (o *operationWrapper) makesureProjectExist(config *AliyunLogConfigSpec, pro } annotations := GetAnnotationByObject(config, project, logstore, "", configName, false) if err != nil { - if k8s_event.GetEventRecorder() != nil { - customErr := CustomErrorFromSlsSDKError(err) - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateProject, "", fmt.Sprintf("create project failed, error: %s", err.Error())) - } - } else if k8s_event.GetEventRecorder() != nil { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.CreateProject, "create project success") + customErr := CustomErrorFromSlsSDKError(err) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.CreateProject, "", fmt.Sprintf("create project failed, error: %s", err.Error())) + } else { + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.CreateProject, "create project success") } return err } @@ -668,14 +664,10 @@ func (o *operationWrapper) updateConfigInner(config *AliyunLogConfigSpec) error annotations := GetAnnotationByObject(config, project, logstore, "", config.LogtailConfig.ConfigName, true) if err != nil { - if k8s_event.GetEventRecorder() != nil { - customErr := CustomErrorFromSlsSDKError(err) - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.UpdateConfig, "", fmt.Sprintf("update config failed, error: %s", err.Error())) - } + customErr := CustomErrorFromSlsSDKError(err) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.UpdateConfig, "", fmt.Sprintf("update config failed, error: %s", err.Error())) } else { - if k8s_event.GetEventRecorder() != nil { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.UpdateConfig, "update config success") - } + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.UpdateConfig, "update config success") } } else { @@ -707,12 +699,10 @@ func (o *operationWrapper) updateConfigInner(config *AliyunLogConfigSpec) error } annotations := GetAnnotationByObject(config, project, logstore, "", config.LogtailConfig.ConfigName, true) if err != nil { - if k8s_event.GetEventRecorder() != nil { - customErr := CustomErrorFromSlsSDKError(err) - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.UpdateConfig, "", fmt.Sprintf("update config failed, error: %s", err.Error())) - } - } else if k8s_event.GetEventRecorder() != nil { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.UpdateConfig, "update config success") + customErr := CustomErrorFromSlsSDKError(err) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, customErr), k8s_event.UpdateConfig, "", fmt.Sprintf("update config failed, error: %s", err.Error())) + } else { + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.UpdateConfig, "update config success") } } if err != nil { @@ -729,9 +719,9 @@ func (o *operationWrapper) updateConfigInner(config *AliyunLogConfigSpec) error err = o.TagLogtailConfig(project, config.LogtailConfig.ConfigName, logtailConfigTags) annotations := GetAnnotationByObject(config, project, logstore, "", config.LogtailConfig.ConfigName, true) if err != nil { - k8s_event.GetEventRecorder().SendErrorEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), GetAnnotationByError(annotations, CustomErrorFromSlsSDKError(err)), k8s_event.CreateTag, "", fmt.Sprintf("tag config %s error :%s", config.LogtailConfig.ConfigName, err.Error())) + o.eventRecorder.SendErrorEventWithAnnotation(o.eventRecorder.GetObject(), GetAnnotationByError(annotations, CustomErrorFromSlsSDKError(err)), k8s_event.CreateTag, "", fmt.Sprintf("tag config %s error :%s", config.LogtailConfig.ConfigName, err.Error())) } else { - k8s_event.GetEventRecorder().SendNormalEventWithAnnotation(k8s_event.GetEventRecorder().GetObject(), annotations, k8s_event.CreateTag, fmt.Sprintf("tag config %s success", config.LogtailConfig.ConfigName)) + o.eventRecorder.SendNormalEventWithAnnotation(o.eventRecorder.GetObject(), annotations, k8s_event.CreateTag, fmt.Sprintf("tag config %s success", config.LogtailConfig.ConfigName)) } // check if config is in the machine group diff --git a/pkg/helper/eventrecorder/event_recorder.go b/pkg/helper/eventrecorder/event_recorder.go index f143efb44a..4fb47f35cb 100644 --- a/pkg/helper/eventrecorder/event_recorder.go +++ b/pkg/helper/eventrecorder/event_recorder.go @@ -81,24 +81,35 @@ func GetEventRecorder() *EventRecorder { return nil } -func (e *EventRecorder) SendNormalEvent(object runtime.Object, action Action, message string) { - if message == "" { - message = "success" - } - e.recorder.Event(object, corev1.EventTypeNormal, e.define.getInfoAction(action), message) -} - -func (e *EventRecorder) SendErrorEvent(object runtime.Object, action Action, alarm Alarm, message string) { - if message == "" { - message = "failed" - } - if alarm == "" { - alarm = "Fail" - } - e.recorder.Event(object, corev1.EventTypeWarning, e.define.getErrorAction(action, alarm), message) -} +// func (e *EventRecorder) SendNormalEvent(object runtime.Object, action Action, message string) { + +// if e == nil || e.recorder == nil { +// return +// } +// if message == "" { +// message = "success" +// } +// e.recorder.Event(object, corev1.EventTypeNormal, e.define.getInfoAction(action), message) +// } + +// func (e *EventRecorder) SendErrorEvent(object runtime.Object, action Action, alarm Alarm, message string) { +// if e == nil || e.recorder == nil { +// return +// } +// if message == "" { +// message = "failed" +// } +// if alarm == "" { +// alarm = "Fail" +// } +// e.recorder.Event(object, corev1.EventTypeWarning, e.define.getErrorAction(action, alarm), message) +// } func (e *EventRecorder) SendNormalEventWithAnnotation(object runtime.Object, annotations map[string]string, action Action, message string) { + logger.Info(context.Background(), "send normal event", "annotations", annotations, "message", message) + if e == nil || e.recorder == nil { + return + } if message == "" { message = "success" } @@ -112,6 +123,10 @@ func (e *EventRecorder) SendNormalEventWithAnnotation(object runtime.Object, ann } func (e *EventRecorder) SendErrorEventWithAnnotation(object runtime.Object, annotations map[string]string, action Action, alarm Alarm, message string) { + logger.Info(context.Background(), "send error event", "annotations", annotations, "alarm", alarm, "message", message) + if e == nil || e.recorder == nil { + return + } if message == "" { message = "failed" }