Skip to content

Commit

Permalink
fix panic when k8s_event.GetEventRecorder() is nil (#1264)
Browse files Browse the repository at this point in the history
* Add eventRecorder to the operationWrapper
  • Loading branch information
quzard authored Dec 19, 2023
1 parent a1e3ce5 commit 8a19fb1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
60 changes: 25 additions & 35 deletions helper/envconfig/aliyunlog_operation_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
47 changes: 31 additions & 16 deletions pkg/helper/eventrecorder/event_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand Down

0 comments on commit 8a19fb1

Please # to comment.