Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(event): rename event type #60

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions broker/blockingChannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ import (

// BlockingChannel implements the Queue interface using a channel.
type BlockingChannel struct {
channel chan events.Event
channel chan events.Interface
metricsHandler *metrics
}

// NewBlockingChannel returns a BlockingChannel.
func NewBlockingChannel(bufferLen int) *BlockingChannel {
return &BlockingChannel{
channel: make(chan events.Event, bufferLen),
channel: make(chan events.Interface, bufferLen),
metricsHandler: newMetrics("blockingChannel"),
}
}

// Push pushes an event to the queue.
func (bc *BlockingChannel) Push(evt events.Event) {
func (bc *BlockingChannel) Push(evt events.Interface) {
bc.metricsHandler.send(evt)
bc.channel <- evt
}

// Pop an event from the queue.
func (bc *BlockingChannel) Pop(ctx context.Context) events.Event {
func (bc *BlockingChannel) Pop(ctx context.Context) events.Interface {
select {
case evt := <-bc.channel:
bc.metricsHandler.receive(evt)
Expand Down
2 changes: 1 addition & 1 deletion broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (br *Broker) Start(ctx context.Context) error {
}
}

func (br *Broker) eventMetricsHandler(evt events.Event) {
func (br *Broker) eventMetricsHandler(evt events.Interface) {
// Get the correct counter.
c := br.eventMetrics[evt.ResourceKind()]
c.inc(evt)
Expand Down
4 changes: 2 additions & 2 deletions broker/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func newMetrics(name string) *metrics {
}

// send to be called before adding the item to the queue.
func (m *metrics) send(evt events.Event) {
func (m *metrics) send(evt events.Interface) {
if m == nil {
return
}
Expand Down Expand Up @@ -144,7 +144,7 @@ type dispatchedEventsMetrics struct {
deleteCounter prometheus.Counter
}

func (dem *dispatchedEventsMetrics) inc(evt events.Event) {
func (dem *dispatchedEventsMetrics) inc(evt events.Interface) {
switch evt.Type() {
case events.Create:
dem.createCounter.Inc()
Expand Down
4 changes: 2 additions & 2 deletions broker/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (

// Queue used to dispatch events from the collectors to the broker.
type Queue interface {
Push(evt events.Event)
Pop(ctx context.Context) events.Event
Push(evt events.Interface)
Pop(ctx context.Context) events.Interface
}
16 changes: 9 additions & 7 deletions pkg/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,36 @@ const (
Delete = "Delete"
)

// GenericEvent generated for watched kubernetes resources.
type GenericEvent struct {
var _ Interface = &Event{}

// Event generated for watched kubernetes resources.
type Event struct {
*metadata.Event
Subs fields.Subscribers
}

// Subscribers returns the destination nodes.
func (ge *GenericEvent) Subscribers() fields.Subscribers {
func (ge *Event) Subscribers() fields.Subscribers {
return ge.Subs
}

// String returns the event in string format.
func (ge *GenericEvent) String() string {
func (ge *Event) String() string {
return fmt.Sprintf("Resource Kind %q, event type %q, resource name %q, subscribers %q",
ge.Kind, ge.Reason, ge.GetMeta(), ge.Subscribers())
}

// Type returns the event type.
func (ge *GenericEvent) Type() string {
func (ge *Event) Type() string {
return ge.Reason
}

// ResourceKind returns the Kind of the resource for which the event has been crafted.
func (ge *GenericEvent) ResourceKind() string {
func (ge *Event) ResourceKind() string {
return ge.Kind
}

// GRPCMessage returns the grpc message ready to be sent over the grpc connection.
func (ge *GenericEvent) GRPCMessage() *metadata.Event {
func (ge *Event) GRPCMessage() *metadata.Event {
return ge.Event
}
4 changes: 2 additions & 2 deletions pkg/events/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"github.com/alacuku/k8s-metadata/pkg/fields"
)

// Event interface that must be satisfied by events generated for each supported k8s resource.
// Interface must be satisfied by events generated for each supported k8s resource.
// Evens are generated by the collectors and sent to the message broker which is in charge to
// deliver them to all subscribers that have an interest for those events.
type Event interface {
type Interface interface {
Subscribers() fields.Subscribers
String() string
Type() string
Expand Down
12 changes: 6 additions & 6 deletions pkg/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func (g *Resource) AddReferencesForKind(kind string, refs []fields.Reference) {
g.ResourceReferences[kind] = refs
}

// ToEvents returns a slice containing Event based on the internal state of the Resource.
func (g *Resource) ToEvents() []Event {
evts := make([]Event, 3)
// ToEvents returns a slice containing Interface based on the internal state of the Resource.
func (g *Resource) ToEvents() []Interface {
evts := make([]Interface, 3)
var meta, spec, status *string
if g.Meta != "" {
m := g.Meta
Expand All @@ -150,7 +150,7 @@ func (g *Resource) ToEvents() []Event {
}

if len(g.createdFor) != 0 {
evts[0] = &GenericEvent{
evts[0] = &Event{
Event: &metadata.Event{
Reason: Create,
Uid: g.UID,
Expand All @@ -166,7 +166,7 @@ func (g *Resource) ToEvents() []Event {
}

if len(g.updatedFor) != 0 {
evts[1] = &GenericEvent{
evts[1] = &Event{
Event: &metadata.Event{
Reason: Update,
Uid: g.UID,
Expand All @@ -182,7 +182,7 @@ func (g *Resource) ToEvents() []Event {
}

if len(g.deletedFor) != 0 {
evts[2] = &GenericEvent{
evts[2] = &Event{
Event: &metadata.Event{
Reason: Delete,
Uid: g.UID,
Expand Down