Skip to content

Commit

Permalink
Merge pull request #501 from agrare/reject_events_without_a_timestamp
Browse files Browse the repository at this point in the history
Reject any events which do not have a timestamp
  • Loading branch information
Fryguy authored Aug 11, 2023
2 parents 6f5fb86 + f6631b6 commit c2df77b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def monitor_events

def queue_event(event)
event_data = extract_event_data(event)
if !event_valid?(event_data)
_log.info "#{log_prefix} Skipping invalid event [#{event_data[:event_type]}]"
return
end

_log.info "#{log_prefix} Queuing event [#{event_data}]"
event_hash = ManageIQ::Providers::Kubernetes::ContainerManager::EventParser.event_to_hash(event_data, @cfg[:ems_id])
EmsEvent.add_queue('add', @cfg[:ems_id], event_hash)
Expand Down Expand Up @@ -77,7 +82,6 @@ def extract_event_data(event)
event_data[:fieldpath] = event.object.involvedObject.fieldPath
end


event_type_prefix = event_data[:kind].upcase

# Handle event data for specific entities
Expand Down Expand Up @@ -106,4 +110,11 @@ def extract_event_data(event)

event_data
end

def event_valid?(event_data)
# If there is no timestamp we cannot properly handle the event
return false if event_data[:timestamp].nil?

true
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,54 @@ def log_prefix
let(:ems) { FactoryBot.create(:ems_kubernetes) }

describe '#queue_event' do
let(:subject) do
test_class.new(ems).tap { |catcher| catcher.instance_variable_set(:@cfg, :ems_id => ems.id) }
end

let(:kubernetes_event) do
{
'kind' => 'Event',
'apiVersion' => 'v1',
'reason' => 'Scheduled',
'involvedObject' => {
'kind' => 'Pod',
},
'metadata' => {
'uid' => 'SomeRandomUid',
},
'lastTimestamp' => '2016-07-25T11:45:34Z'
}
end

let(:event) { array_recursive_ostruct(:object => kubernetes_event) }

it 'sends the received event to queue after parsing' do
catcher = test_class.new(ems)
expect(catcher).to receive(:extract_event_data).once.with(:raw_event).and_return(:extracted_event)
expect(catcher).to receive(:log_prefix)
expect(catcher).to receive_message_chain("_log.info")
catcher.instance_variable_set(:@cfg, :ems_id => ems.id)
expect(
ManageIQ::Providers::Kubernetes::ContainerManager::EventParser
).to receive(
:event_to_hash
).with(:extracted_event, ems.id).and_return(:hashed_event)
expect(EmsEvent).to receive(:add_queue).with('add', ems.id, :hashed_event)
catcher.queue_event(:raw_event)
expect(EmsEvent).to receive(:add_queue).with('add', ems.id, hash_including(:event_type => "POD_SCHEDULED", :timestamp => '2016-07-25T11:45:34Z'))

subject.queue_event(event)
end

context "with an invalid event" do
let(:kubernetes_event) do
{
'kind' => 'Event',
'apiVersion' => 'v1',
'reason' => 'Scheduled',
'involvedObject' => {
'kind' => 'Pod',
},
'metadata' => {
'uid' => 'SomeRandomUid',
},
'lastTimestamp' => nil
}
end

it "doesn't queue the event" do
expect(EmsEvent).not_to receive(:add_queue)

subject.queue_event(event)
end
end
end

Expand Down Expand Up @@ -287,6 +322,7 @@ def log_prefix
'metadata' => {
'uid' => 'SomeRandomUid',
},
'lastTimestamp' => '2016-07-25T11:45:34Z',
}
end

Expand All @@ -306,6 +342,7 @@ def log_prefix
'metadata' => {
'uid' => 'SomeRandomUid',
},
'lastTimestamp' => '2016-07-25T11:45:34Z',
}
end

Expand Down

0 comments on commit c2df77b

Please sign in to comment.