Skip to content

Commit

Permalink
fix(events): Correct event API Version. Fixes argoproj#2994
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed May 11, 2020
1 parent 46b11e1 commit 292a03c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
47 changes: 13 additions & 34 deletions util/argo/audit_logger.go
Expand Up @@ -7,8 +7,6 @@ import (
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"

wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
Expand All @@ -26,13 +24,6 @@ type EventInfo struct {
Reason string
}

type ObjectRef struct {
Name string
Namespace string
ResourceVersion string
UID types.UID
}

const (
EventReasonWorkflowRunning = "WorkflowRunning"
EventReasonWorkflowSucceeded = "WorkflowSucceeded"
Expand All @@ -43,35 +34,29 @@ const (
EventReasonWorkflowNodeError = "WorkflowNodeError"
)

func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, info EventInfo, message string, annotations map[string]string) {
func (l *AuditLogger) logEvent(workflow *wfv1.Workflow, info EventInfo, message string, annotations map[string]string) {
logCtx := log.WithFields(log.Fields{
"type": info.Type,
"reason": info.Reason,
"type": info.Type,
"reason": info.Reason,
"workflow": workflow.Name,
})
switch gvk.Kind {
case "Workflow":
logCtx = logCtx.WithField("workflow", objMeta.Name)
default:
logCtx = logCtx.WithField("name", objMeta.Name)
}
t := metav1.Time{Time: time.Now()}
event := corev1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v.%x", objMeta.Name, t.UnixNano()),
Name: fmt.Sprintf("%v.%x", workflow.Name, t.UnixNano()),
Annotations: annotations,
},
Source: corev1.EventSource{
Component: l.component,
},
InvolvedObject: corev1.ObjectReference{
Kind: gvk.Kind,
Name: objMeta.Name,
Namespace: objMeta.Namespace,
ResourceVersion: objMeta.ResourceVersion,
APIVersion: gvk.Version,
UID: objMeta.UID,
Kind: wfv1.WorkflowSchemaGroupVersionKind.Kind,
APIVersion: wfv1.SchemeGroupVersion.String(),
Name: workflow.Name,
Namespace: workflow.Namespace,
ResourceVersion: workflow.ResourceVersion,
UID: workflow.UID,
},

FirstTimestamp: t,
LastTimestamp: t,
Count: 1,
Expand All @@ -80,21 +65,15 @@ func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, i
Reason: info.Reason,
}
logCtx.WithField("event", event).Debug()
_, err := l.kIf.CoreV1().Events(objMeta.Namespace).Create(&event)
_, err := l.kIf.CoreV1().Events(workflow.Namespace).Create(&event)
if err != nil {
logCtx.Errorf("Unable to create audit event: %v", err)
return
}
}

func (l *AuditLogger) logWorkflowEvent(workflow *wfv1.Workflow, info EventInfo, message string, annotations map[string]string) {
objectMeta := ObjectRef{
Name: workflow.ObjectMeta.Name,
Namespace: workflow.ObjectMeta.Namespace,
ResourceVersion: workflow.ObjectMeta.ResourceVersion,
UID: workflow.ObjectMeta.UID,
}
l.logEvent(objectMeta, wfv1.WorkflowSchemaGroupVersionKind, info, message, annotations)
l.logEvent(workflow, info, message, annotations)
}

func (l *AuditLogger) LogWorkflowEvent(workflow *wfv1.Workflow, info EventInfo, message string) {
Expand Down
2 changes: 1 addition & 1 deletion util/argo/audit_logger_test.go
Expand Up @@ -33,7 +33,7 @@ func TestAuditLogger_logEvent(t *testing.T) {
Namespace: "my-ns",
Name: "my-wf",
UID: "my-uid",
APIVersion: "v1alpha1",
APIVersion: "argoproj.io/v1alpha1",
ResourceVersion: "1234",
}, e.InvolvedObject)
assert.NotEmpty(t, e.FirstTimestamp)
Expand Down

0 comments on commit 292a03c

Please sign in to comment.