Skip to content

Commit

Permalink
feat(server): log app Spec along with event (argoproj#16416)
Browse files Browse the repository at this point in the history
* adding in spec to logappevent params ARGO-1017:

Signed-off-by: Nandini <[email protected]>

* updating logappevent to include spec

Signed-off-by: Nandini <[email protected]>

* updated logevent to take marshal logfields into json

Signed-off-by: Nandini <[email protected]>

* removing spec from parameters just grabbing from app.spec

Signed-off-by: Nandini <[email protected]>

* removing app.spec from test

Signed-off-by: Nandini <[email protected]>

---------

Signed-off-by: Nandini <[email protected]>
Co-authored-by: Nandini Singh <[email protected]>
  • Loading branch information
2 people authored and tesla59 committed Dec 16, 2023
1 parent 5c63982 commit 289d22d
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions util/argo/audit_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package argo

import (
"context"
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -45,14 +46,27 @@ const (
EventReasonOperationCompleted = "OperationCompleted"
)

func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, info EventInfo, message string, logFields map[string]string) {
func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, info EventInfo, message string, logFields map[string]interface{}) {
logCtx := log.WithFields(log.Fields{
"type": info.Type,
"reason": info.Reason,
})
for field, val := range logFields {
logCtx = logCtx.WithField(field, val)
}
logFieldStrings := make(map[string]string)
for field, val := range logFields {
if valStr, ok := val.(string); ok {
logFieldStrings[field] = valStr
continue
}
vJsonStr, err := json.Marshal(val)
if err != nil {
logCtx.Errorf("Unable to marshal audit event field %v: %v", field, err)
continue
}
logFieldStrings[field] = string(vJsonStr)
}

switch gvk.Kind {
case application.ApplicationKind:
Expand All @@ -66,7 +80,7 @@ func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, i
event := v1.Event{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v.%x", objMeta.Name, t.UnixNano()),
Annotations: logFields,
Annotations: logFieldStrings,
},
Source: v1.EventSource{
Component: l.component,
Expand Down Expand Up @@ -101,13 +115,14 @@ func (l *AuditLogger) LogAppEvent(app *v1alpha1.Application, info EventInfo, mes
ResourceVersion: app.ObjectMeta.ResourceVersion,
UID: app.ObjectMeta.UID,
}
fields := map[string]string{
fields := map[string]interface{}{
"dest-server": app.Spec.Destination.Server,
"dest-namespace": app.Spec.Destination.Namespace,
}
if user != "" {
fields["user"] = user
}
fields["spec"] = app.Spec
l.logEvent(objectMeta, v1alpha1.ApplicationSchemaGroupVersionKind, info, message, fields)
}

Expand All @@ -118,7 +133,7 @@ func (l *AuditLogger) LogAppSetEvent(app *v1alpha1.ApplicationSet, info EventInf
ResourceVersion: app.ObjectMeta.ResourceVersion,
UID: app.ObjectMeta.UID,
}
fields := map[string]string{}
fields := make(map[string]interface{})
if user != "" {
fields["user"] = user
}
Expand All @@ -132,7 +147,7 @@ func (l *AuditLogger) LogResourceEvent(res *v1alpha1.ResourceNode, info EventInf
ResourceVersion: res.ResourceRef.Version,
UID: types.UID(res.ResourceRef.UID),
}
fields := map[string]string{}
fields := make(map[string]interface{})
if user != "" {
fields["user"] = user
}
Expand Down

0 comments on commit 289d22d

Please sign in to comment.