github.com/cilium/cilium@v1.16.2/pkg/hubble/observer/observeroption/option.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Hubble 3 4 package observeroption 5 6 import ( 7 "context" 8 9 "github.com/sirupsen/logrus" 10 11 pb "github.com/cilium/cilium/api/v1/flow" 12 "github.com/cilium/cilium/api/v1/observer" 13 v1 "github.com/cilium/cilium/pkg/hubble/api/v1" 14 "github.com/cilium/cilium/pkg/hubble/container" 15 "github.com/cilium/cilium/pkg/hubble/filters" 16 observerTypes "github.com/cilium/cilium/pkg/hubble/observer/types" 17 ) 18 19 // Server gives access to the Hubble server 20 type Server interface { 21 GetOptions() Options 22 GetLogger() logrus.FieldLogger 23 } 24 25 // Options stores all the configurations values for the hubble server. 26 type Options struct { 27 MaxFlows container.Capacity // max number of flows that can be stored in the ring buffer 28 MonitorBuffer int // buffer size for monitor payload 29 30 OnServerInit []OnServerInit // invoked when the hubble server is initialized 31 OnMonitorEvent []OnMonitorEvent // invoked before an event is decoded 32 OnDecodedFlow []OnDecodedFlow // invoked after a flow has been decoded 33 OnDecodedEvent []OnDecodedEvent // invoked after an event has been decoded 34 OnBuildFilter []filters.OnBuildFilter // invoked while building a flow filter 35 OnFlowDelivery []OnFlowDelivery // invoked before a flow is delivered via API 36 OnGetFlows []OnGetFlows // invoked on new GetFlows API call 37 } 38 39 // returning `stop: true` from a callback stops the execution chain, regardless 40 // of the error encountered (for example, explicitly filtering out certain 41 // events, or similar). 42 type stop = bool 43 44 // Option customizes the configuration of the hubble server. 45 type Option func(o *Options) error 46 47 // OnServerInit is invoked after all server options have been applied 48 type OnServerInit interface { 49 OnServerInit(Server) error 50 } 51 52 // OnServerInitFunc implements OnServerInit for a single function 53 type OnServerInitFunc func(Server) error 54 55 // OnServerInit is invoked after all server options have been applied 56 func (f OnServerInitFunc) OnServerInit(srv Server) error { 57 return f(srv) 58 } 59 60 // OnMonitorEvent is invoked before each monitor event is decoded 61 type OnMonitorEvent interface { 62 OnMonitorEvent(context.Context, *observerTypes.MonitorEvent) (stop, error) 63 } 64 65 // OnMonitorEventFunc implements OnMonitorEvent for a single function 66 type OnMonitorEventFunc func(context.Context, *observerTypes.MonitorEvent) (stop, error) 67 68 // OnMonitorEvent is invoked before each monitor event is decoded 69 func (f OnMonitorEventFunc) OnMonitorEvent(ctx context.Context, event *observerTypes.MonitorEvent) (stop, error) { 70 return f(ctx, event) 71 } 72 73 // OnDecodedFlow is invoked after a flow has been decoded 74 type OnDecodedFlow interface { 75 OnDecodedFlow(context.Context, *pb.Flow) (stop, error) 76 } 77 78 // OnDecodedFlowFunc implements OnDecodedFlow for a single function 79 type OnDecodedFlowFunc func(context.Context, *pb.Flow) (stop, error) 80 81 // OnDecodedFlow is invoked after a flow has been decoded 82 func (f OnDecodedFlowFunc) OnDecodedFlow(ctx context.Context, flow *pb.Flow) (stop, error) { 83 return f(ctx, flow) 84 } 85 86 // OnDecodedEvent is invoked after an event has been decoded 87 type OnDecodedEvent interface { 88 OnDecodedEvent(context.Context, *v1.Event) (stop, error) 89 } 90 91 // OnDecodedEventFunc implements OnDecodedEvent for a single function 92 type OnDecodedEventFunc func(context.Context, *v1.Event) (stop, error) 93 94 // OnDecodedEvent is invoked after a flow has been decoded 95 func (f OnDecodedEventFunc) OnDecodedEvent(ctx context.Context, event *v1.Event) (stop, error) { 96 return f(ctx, event) 97 } 98 99 // OnFlowDelivery is invoked before a flow is delivered via the API 100 type OnFlowDelivery interface { 101 OnFlowDelivery(context.Context, *pb.Flow) (stop, error) 102 } 103 104 // OnFlowDeliveryFunc implements OnFlowDelivery for a single function 105 type OnFlowDeliveryFunc func(context.Context, *pb.Flow) (stop, error) 106 107 // OnFlowDelivery is invoked before a flow is delivered via the API 108 func (f OnFlowDeliveryFunc) OnFlowDelivery(ctx context.Context, flow *pb.Flow) (stop, error) { 109 return f(ctx, flow) 110 } 111 112 // OnGetFlows is invoked for each GetFlows call 113 type OnGetFlows interface { 114 OnGetFlows(context.Context, *observer.GetFlowsRequest) (context.Context, error) 115 } 116 117 // OnGetFlowsFunc implements OnGetFlows for a single function 118 type OnGetFlowsFunc func(context.Context, *observer.GetFlowsRequest) (context.Context, error) 119 120 // OnGetFlows is invoked for each GetFlows call 121 func (f OnGetFlowsFunc) OnGetFlows(ctx context.Context, req *observer.GetFlowsRequest) (context.Context, error) { 122 return f(ctx, req) 123 } 124 125 // WithMonitorBuffer controls the size of the buffered channel between the 126 // monitor socket and the hubble ring buffer. 127 func WithMonitorBuffer(size int) Option { 128 return func(o *Options) error { 129 o.MonitorBuffer = size 130 return nil 131 } 132 } 133 134 // WithMaxFlows that the ring buffer is initialized to hold. 135 func WithMaxFlows(capacity container.Capacity) Option { 136 return func(o *Options) error { 137 o.MaxFlows = capacity 138 return nil 139 } 140 } 141 142 // WithOnServerInit adds a new callback to be invoked after server initialization 143 func WithOnServerInit(f OnServerInit) Option { 144 return func(o *Options) error { 145 o.OnServerInit = append(o.OnServerInit, f) 146 return nil 147 } 148 } 149 150 // WithOnServerInitFunc adds a new callback to be invoked after server initialization 151 func WithOnServerInitFunc(f func(Server) error) Option { 152 return WithOnServerInit(OnServerInitFunc(f)) 153 } 154 155 // WithOnMonitorEvent adds a new callback to be invoked before decoding 156 func WithOnMonitorEvent(f OnMonitorEvent) Option { 157 return func(o *Options) error { 158 o.OnMonitorEvent = append(o.OnMonitorEvent, f) 159 return nil 160 } 161 } 162 163 // WithOnMonitorEventFunc adds a new callback to be invoked before decoding 164 func WithOnMonitorEventFunc(f func(context.Context, *observerTypes.MonitorEvent) (stop, error)) Option { 165 return WithOnMonitorEvent(OnMonitorEventFunc(f)) 166 } 167 168 // WithOnDecodedFlow adds a new callback to be invoked after decoding 169 func WithOnDecodedFlow(f OnDecodedFlow) Option { 170 return func(o *Options) error { 171 o.OnDecodedFlow = append(o.OnDecodedFlow, f) 172 return nil 173 } 174 } 175 176 // WithOnDecodedFlowFunc adds a new callback to be invoked after decoding 177 func WithOnDecodedFlowFunc(f func(context.Context, *pb.Flow) (stop, error)) Option { 178 return WithOnDecodedFlow(OnDecodedFlowFunc(f)) 179 } 180 181 // WithOnDecodedEvent adds a new callback to be invoked after decoding an event. 182 func WithOnDecodedEvent(f OnDecodedEvent) Option { 183 return func(o *Options) error { 184 o.OnDecodedEvent = append(o.OnDecodedEvent, f) 185 return nil 186 } 187 } 188 189 // WithOnDecodedEventFunc adds a new callback to be invoked after decoding an event 190 func WithOnDecodedEventFunc(f func(context.Context, *v1.Event) (stop, error)) Option { 191 return WithOnDecodedEvent(OnDecodedEventFunc(f)) 192 } 193 194 // WithOnBuildFilter adds a new callback to be invoked while building a flow filter 195 func WithOnBuildFilter(f filters.OnBuildFilter) Option { 196 return func(o *Options) error { 197 o.OnBuildFilter = append(o.OnBuildFilter, f) 198 return nil 199 } 200 } 201 202 // WithOnBuildFilterFunc adds a new callback to be invoked while building flow filters 203 func WithOnBuildFilterFunc(f filters.OnBuildFilterFunc) Option { 204 return WithOnBuildFilter(filters.OnBuildFilterFunc(f)) 205 } 206 207 // WithOnFlowDelivery adds a new callback to be invoked before a flow is delivered via the API 208 func WithOnFlowDelivery(f OnFlowDelivery) Option { 209 return func(o *Options) error { 210 o.OnFlowDelivery = append(o.OnFlowDelivery, f) 211 return nil 212 } 213 } 214 215 // WithOnFlowDeliveryFunc adds a new callback to be invoked before a flow is delivered via the API 216 func WithOnFlowDeliveryFunc(f OnFlowDeliveryFunc) Option { 217 return WithOnFlowDelivery(f) 218 } 219 220 // WithOnGetFlows adds a new callback to be invoked for each GetFlows call 221 func WithOnGetFlows(f OnGetFlows) Option { 222 return func(o *Options) error { 223 o.OnGetFlows = append(o.OnGetFlows, f) 224 return nil 225 } 226 } 227 228 // WithOnGetFlowsFunc adds a new callback to be invoked for each GetFlows call 229 func WithOnGetFlowsFunc(f OnGetFlowsFunc) Option { 230 return WithOnGetFlows(f) 231 }