diff --git a/app/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin.rb b/app/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin.rb index 15816812b7..f1e8b41591 100644 --- a/app/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin.rb +++ b/app/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin_spec.rb b/spec/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin_spec.rb index af62de3e1f..b86e338e76 100644 --- a/spec/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin_spec.rb +++ b/spec/models/manageiq/providers/kubernetes/container_manager/event_catcher_mixin_spec.rb @@ -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 @@ -287,6 +322,7 @@ def log_prefix 'metadata' => { 'uid' => 'SomeRandomUid', }, + 'lastTimestamp' => '2016-07-25T11:45:34Z', } end @@ -306,6 +342,7 @@ def log_prefix 'metadata' => { 'uid' => 'SomeRandomUid', }, + 'lastTimestamp' => '2016-07-25T11:45:34Z', } end