github.com/argoproj/argo-cd@v1.8.7/util/argo/audit_logger.go (about) 1 package argo 2 3 import ( 4 "context" 5 6 log "github.com/sirupsen/logrus" 7 v1 "k8s.io/api/core/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/apimachinery/pkg/runtime/schema" 10 "k8s.io/apimachinery/pkg/types" 11 "k8s.io/client-go/kubernetes" 12 13 "fmt" 14 "time" 15 16 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 17 ) 18 19 type AuditLogger struct { 20 kIf kubernetes.Interface 21 component string 22 ns string 23 } 24 25 type EventInfo struct { 26 Type string 27 Reason string 28 } 29 30 type ObjectRef struct { 31 Name string 32 Namespace string 33 ResourceVersion string 34 UID types.UID 35 } 36 37 const ( 38 EventReasonStatusRefreshed = "StatusRefreshed" 39 EventReasonResourceCreated = "ResourceCreated" 40 EventReasonResourceUpdated = "ResourceUpdated" 41 EventReasonResourceDeleted = "ResourceDeleted" 42 EventReasonResourceActionRan = "ResourceActionRan" 43 EventReasonOperationStarted = "OperationStarted" 44 EventReasonOperationCompleted = "OperationCompleted" 45 ) 46 47 func (l *AuditLogger) logEvent(objMeta ObjectRef, gvk schema.GroupVersionKind, info EventInfo, message string, logFields map[string]string) { 48 logCtx := log.WithFields(log.Fields{ 49 "type": info.Type, 50 "reason": info.Reason, 51 }) 52 for field, val := range logFields { 53 logCtx = logCtx.WithField(field, val) 54 } 55 56 switch gvk.Kind { 57 case "Application": 58 logCtx = logCtx.WithField("application", objMeta.Name) 59 case "AppProject": 60 logCtx = logCtx.WithField("project", objMeta.Name) 61 default: 62 logCtx = logCtx.WithField("name", objMeta.Name) 63 } 64 t := metav1.Time{Time: time.Now()} 65 event := v1.Event{ 66 ObjectMeta: metav1.ObjectMeta{ 67 Name: fmt.Sprintf("%v.%x", objMeta.Name, t.UnixNano()), 68 Annotations: logFields, 69 }, 70 Source: v1.EventSource{ 71 Component: l.component, 72 }, 73 InvolvedObject: v1.ObjectReference{ 74 Kind: gvk.Kind, 75 Name: objMeta.Name, 76 Namespace: objMeta.Namespace, 77 ResourceVersion: objMeta.ResourceVersion, 78 APIVersion: gvk.GroupVersion().String(), 79 UID: objMeta.UID, 80 }, 81 FirstTimestamp: t, 82 LastTimestamp: t, 83 Count: 1, 84 Message: message, 85 Type: info.Type, 86 Reason: info.Reason, 87 } 88 logCtx.Info(message) 89 _, err := l.kIf.CoreV1().Events(objMeta.Namespace).Create(context.Background(), &event, metav1.CreateOptions{}) 90 if err != nil { 91 logCtx.Errorf("Unable to create audit event: %v", err) 92 return 93 } 94 } 95 96 func (l *AuditLogger) LogAppEvent(app *v1alpha1.Application, info EventInfo, message string) { 97 objectMeta := ObjectRef{ 98 Name: app.ObjectMeta.Name, 99 Namespace: app.ObjectMeta.Namespace, 100 ResourceVersion: app.ObjectMeta.ResourceVersion, 101 UID: app.ObjectMeta.UID, 102 } 103 l.logEvent(objectMeta, v1alpha1.ApplicationSchemaGroupVersionKind, info, message, map[string]string{ 104 "dest-server": app.Spec.Destination.Server, 105 "dest-namespace": app.Spec.Destination.Namespace, 106 }) 107 } 108 109 func (l *AuditLogger) LogResourceEvent(res *v1alpha1.ResourceNode, info EventInfo, message string) { 110 objectMeta := ObjectRef{ 111 Name: res.ResourceRef.Name, 112 Namespace: res.ResourceRef.Namespace, 113 ResourceVersion: res.ResourceRef.Version, 114 UID: types.UID(res.ResourceRef.UID), 115 } 116 l.logEvent(objectMeta, schema.GroupVersionKind{ 117 Group: res.Group, 118 Version: res.Version, 119 Kind: res.Kind, 120 }, info, message, nil) 121 } 122 123 func (l *AuditLogger) LogAppProjEvent(proj *v1alpha1.AppProject, info EventInfo, message string) { 124 objectMeta := ObjectRef{ 125 Name: proj.ObjectMeta.Name, 126 Namespace: proj.ObjectMeta.Namespace, 127 ResourceVersion: proj.ObjectMeta.ResourceVersion, 128 UID: proj.ObjectMeta.UID, 129 } 130 l.logEvent(objectMeta, v1alpha1.AppProjectSchemaGroupVersionKind, info, message, nil) 131 } 132 133 func NewAuditLogger(ns string, kIf kubernetes.Interface, component string) *AuditLogger { 134 return &AuditLogger{ 135 ns: ns, 136 kIf: kIf, 137 component: component, 138 } 139 }