Skip to content

stop pull/watch thread when resource not found #78

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

Merged
merged 1 commit into from
Jan 12, 2022
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
38 changes: 24 additions & 14 deletions lib/fluent/plugin/in_kubernetes_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ def create_pull_thread(conf)
tag = generate_tag resource_name
while thread_current_running?
log.debug "Going to pull #{resource_name}"
response = @client.public_send "get_#{resource_name}", options
begin
response = @client.public_send "get_#{resource_name}", options
rescue Kubeclient::ResourceNotFoundError, NoMethodError
log.error "resource '#{resource_name}' not found. Stopped pulling it"
break
end
now = Fluent::Engine.now
es = Fluent::MultiEventStream.new

Expand Down Expand Up @@ -243,22 +248,27 @@ def create_watcher_thread(conf)

thread_create :"watch_#{resource_name}" do
while thread_current_running?
@client.public_send("watch_#{resource_name}", options).tap do |watcher|
tag = generate_tag "#{resource_name}"
begin
watcher.each do |entity|
begin
entity = JSON.parse(entity)
router.emit tag, Fluent::Engine.now, entity
options[:resource_version] = entity['object']['metadata']['resourceVersion']
@storage.put resource_name, entity['object']['metadata']['resourceVersion']
rescue => e
log.info "Got exception #{e} parsing entity #{entity}. Resetting watcher."
begin
@client.public_send("watch_#{resource_name}", options).tap do |watcher|
tag = generate_tag "#{resource_name}"
begin
watcher.each do |entity|
begin
entity = JSON.parse(entity)
router.emit tag, Fluent::Engine.now, entity
options[:resource_version] = entity['object']['metadata']['resourceVersion']
@storage.put resource_name, entity['object']['metadata']['resourceVersion']
rescue => e
log.info "Got exception #{e} parsing entity #{entity}. Resetting watcher."
end
end
rescue => e
log.info "Got exception #{e}. Resetting watcher."
end
rescue => e
log.info "Got exception #{e}. Resetting watcher."
end
rescue Kubeclient::ResourceNotFoundError, NoMethodError
log.error "resource '#{resource_name}' not found. Stopped watching it"
break
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/fluent/plugin/in_kubernetes_objects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,28 @@
f.unlink
end
end
it "checks for invalid pull request" do
d = create_input_driver(<<~CONF)
kubernetes_url #{k8s_url}
<pull>
resource_name fakeResource
</pull>
CONF

d.run expect_emits: 0, timeout: 3

expect(d.logs.any? { |log| log.include? "resource 'fakeResource' not found." }).must_equal(true)
end
it "checks for invalid watch request" do
d = create_input_driver(<<~CONF)
kubernetes_url #{k8s_url}
<watch>
resource_name fakeResource
</watch>
CONF

d.run expect_emits: 0, timeout: 3
expect(d.logs.any? { |log| log.include? "resource 'fakeResource' not found." }).must_equal(true)
end
end
end