Skip to content

Commit

Permalink
watch for crd changes during sleep instead of during all of the apply…
Browse files Browse the repository at this point in the history
… so that CRDs that get written by kubetruth don't trigger a watch event
  • Loading branch information
wr0ngway committed Jul 2, 2021
1 parent 6adb365 commit 3fac73e
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions lib/kubetruth/etl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,52 @@ def interruptible_sleep(interval)
Kernel.sleep interval
end

def watch_crds_to_interrupt(&block)
begin
begin
watcher = kubeapi.watch_project_mappings

thr = Thread.new do
logger.debug "Created watcher for CRD"
watcher.each do |notice|
logger.debug {"Interrupting polling sleep, CRD watcher woke up for: #{notice}"}
interrupt_sleep
break
end
logger.debug "CRD watcher exiting"
end

block.call
ensure
watcher.finish
thr.join(5)
end
rescue => e
logger.log_exception(e, "Failure in watch/polling logic")
end
end

def interrupt_sleep
Thread.new { @sleeper&.run }
end

def with_polling(interval, &block)
while true

begin
watcher = kubeapi.watch_project_mappings

run_time = Benchmark.measure do
begin
thr = Thread.new do
logger.debug "Created watcher for CRD"
watcher.each do |notice|
logger.debug {"Interrupting polling sleep, CRD watcher woke up for: #{notice}"}
interrupt_sleep
break
end
logger.debug "CRD watcher exiting"
end

run_time = Benchmark.measure do
begin
block.call
rescue Kubetruth::Template::Error => e
logger.error e.message
rescue => e
logger.log_exception(e, "Failure while applying config transforms")
end
end
logger.info "Benchmark: #{run_time}"

logger.info("Poller sleeping for #{interval}")
interruptible_sleep(interval)
ensure
watcher.finish
thr.join(5)
block.call
rescue Kubetruth::Template::Error => e
logger.error e.message
rescue => e
logger.log_exception(e, "Failure while applying config transforms")
end
end
logger.info "Benchmark: #{run_time}"

rescue => e
logger.log_exception(e, "Failure in watch/polling logic")
watch_crds_to_interrupt do
logger.info("Poller sleeping for #{interval}")
interruptible_sleep(interval)
end

end
Expand Down

0 comments on commit 3fac73e

Please sign in to comment.