kruby-controller 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +117 -0
  4. data/Rakefile +8 -0
  5. data/exe/kruby-controller +32 -0
  6. data/lib/kruby-controller.rb +8 -0
  7. data/lib/kruby_controller/builder/controller_builder.rb +52 -0
  8. data/lib/kruby_controller/cache/cache.rb +100 -0
  9. data/lib/kruby_controller/cache/deep_copy.rb +32 -0
  10. data/lib/kruby_controller/cache/delta.rb +12 -0
  11. data/lib/kruby_controller/cache/informer.rb +453 -0
  12. data/lib/kruby_controller/cache/request_error.rb +55 -0
  13. data/lib/kruby_controller/cache/store.rb +92 -0
  14. data/lib/kruby_controller/cache/watch_stream.rb +144 -0
  15. data/lib/kruby_controller/cli/create_api_command.rb +335 -0
  16. data/lib/kruby_controller/cli/flag_parser.rb +85 -0
  17. data/lib/kruby_controller/cli/init_command.rb +383 -0
  18. data/lib/kruby_controller/controller.rb +200 -0
  19. data/lib/kruby_controller/crd.rb +141 -0
  20. data/lib/kruby_controller/election/leader_elector.rb +212 -0
  21. data/lib/kruby_controller/finalizer/finalizer_manager.rb +31 -0
  22. data/lib/kruby_controller/generator/crd_generator.rb +109 -0
  23. data/lib/kruby_controller/gvk.rb +59 -0
  24. data/lib/kruby_controller/handler/enqueued_object.rb +18 -0
  25. data/lib/kruby_controller/handler/enqueued_owner.rb +51 -0
  26. data/lib/kruby_controller/handler/predicated.rb +21 -0
  27. data/lib/kruby_controller/kubernetes_object_client.rb +77 -0
  28. data/lib/kruby_controller/logger.rb +11 -0
  29. data/lib/kruby_controller/manager.rb +291 -0
  30. data/lib/kruby_controller/owner/owner_reference.rb +23 -0
  31. data/lib/kruby_controller/queue/rate_limiter.rb +51 -0
  32. data/lib/kruby_controller/queue/work_queue.rb +375 -0
  33. data/lib/kruby_controller/reconciler.rb +18 -0
  34. data/lib/kruby_controller/request.rb +59 -0
  35. data/lib/kruby_controller/result.rb +50 -0
  36. data/lib/kruby_controller/status/status_updater.rb +29 -0
  37. data/lib/kruby_controller/version.rb +5 -0
  38. data/lib/kruby_controller.rb +87 -0
  39. metadata +138 -0
@@ -0,0 +1,453 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+ require "json"
5
+ require "monitor"
6
+
7
+ module KrubyController
8
+ module Cache
9
+ class Informer
10
+ attr_reader :store, :gvk, :synced, :sync_error, :list_page_size
11
+
12
+ def initialize(gvk:, store:, api_client:, handlers: [], namespace: nil, list_page_size: nil)
13
+ @gvk = gvk
14
+ @store = store
15
+ @api_client = api_client
16
+ @handlers = handlers
17
+ @namespace = namespace
18
+ # Optional page size for paged LISTs (issue #45). nil sends no limit
19
+ # and simply follows the server's continue tokens.
20
+ @list_page_size = list_page_size
21
+ @resource_version = nil
22
+ @running = false
23
+ @started = false
24
+ @synced = false
25
+ @sync_error = nil
26
+ @generation = 0
27
+ @lifecycle_lock = Monitor.new
28
+ end
29
+
30
+ def start
31
+ @lifecycle_lock.synchronize do
32
+ return if @started
33
+
34
+ @started = true
35
+ @running = true
36
+ @synced = false
37
+ @sync_error = nil
38
+ @resource_version = nil
39
+ @generation += 1
40
+ generation = @generation
41
+ @populate_thread = Thread.new { populate(generation) }
42
+ @watch_thread = Thread.new { watch_loop(generation) }
43
+ end
44
+ end
45
+
46
+ def wait_for_sync(timeout: 30)
47
+ deadline = Time.now + timeout
48
+ sleep 0.1 until @synced || Time.now >= deadline
49
+ @synced
50
+ end
51
+
52
+ def stop
53
+ workers = @lifecycle_lock.synchronize do
54
+ @running = false
55
+ @started = false
56
+ @synced = false
57
+ @generation += 1
58
+ threads = [@populate_thread, @watch_thread]
59
+ @populate_thread = @watch_thread = nil
60
+ threads
61
+ end
62
+ # A handler may stop the informer reentrantly. Joining while the
63
+ # handler holds the monitor would prevent other workers from exiting.
64
+ return if @lifecycle_lock.mon_owned?
65
+
66
+ workers.each { |thread| join_bounded(thread) }
67
+ end
68
+
69
+ def add_handler(handler)
70
+ @lifecycle_lock.synchronize { @handlers << handler }
71
+ end
72
+
73
+ private
74
+
75
+ def running?(generation)
76
+ @lifecycle_lock.synchronize { @running && @generation == generation }
77
+ end
78
+
79
+ # Invalidate workers before joining them. Network calls stay outside
80
+ # this monitor, but commits and handler delivery share it with stop so
81
+ # a stopped generation cannot mutate state, even after a new start.
82
+ # Monitor permits handlers to call stop without a recursive-lock error.
83
+ def with_active_run(generation)
84
+ @lifecycle_lock.synchronize do
85
+ return unless @running && @generation == generation
86
+
87
+ yield
88
+ end
89
+ end
90
+
91
+ # How long #stop waits for a worker thread to exit before giving up.
92
+ # The watch stream (Typhoeus) exposes no cancellation API, so a stream
93
+ # that stays open cannot be interrupted; bounding the join keeps
94
+ # cleanup from blocking the startup-error path indefinitely.
95
+ STOP_JOIN_TIMEOUT = 10
96
+
97
+ POPULATE_INITIAL_BACKOFF = 1.0
98
+ POPULATE_MAX_BACKOFF = 30.0
99
+ # Initial List plus three retries (design: "3 retries -> Manager
100
+ # startup failure"), so four total attempts.
101
+ POPULATE_MAX_ATTEMPTS = 4
102
+
103
+ # Lists the initial state up to POPULATE_MAX_ATTEMPTS times. On success
104
+ # it replaces the store (dropping objects deleted since the previous
105
+ # run) and emits the snapshot diff, then marks the informer synced;
106
+ # once the retry budget is exhausted it records the cause in
107
+ # +sync_error+ so the cache/manager can surface it and fail startup,
108
+ # instead of leaving the informer inert.
109
+ #
110
+ # Replacing (rather than upserting) matters when the same informer is
111
+ # reused after a stop (e.g. leadership reacquisition): an object deleted
112
+ # while the informer was stopped would otherwise linger in the store
113
+ # forever, since the resumed watch starts at the fresh List's
114
+ # resourceVersion. On the first run the store is empty, so the diff is
115
+ # simply ADDED for every object (standard informer initial-list).
116
+ def populate(generation)
117
+ backoff = POPULATE_INITIAL_BACKOFF
118
+ attempt = 0
119
+ while running?(generation) && attempt < POPULATE_MAX_ATTEMPTS
120
+ attempt += 1
121
+ begin
122
+ resp = fetch_list(generation)
123
+ return unless resp # stopped mid-collection: discard the partial snapshot
124
+
125
+ apply_list(resp, generation, initial: true)
126
+ return
127
+ rescue StandardError => e
128
+ return unless running?(generation)
129
+
130
+ KrubyController.logger.error(
131
+ "Informer populate for #{@gvk} attempt #{attempt}/#{POPULATE_MAX_ATTEMPTS} failed: #{e.message}"
132
+ )
133
+ if attempt == POPULATE_MAX_ATTEMPTS
134
+ with_active_run(generation) { @sync_error = e }
135
+ return
136
+ end
137
+
138
+ interruptible_sleep(respect_retry_after(e, backoff), generation)
139
+ return unless running?(generation)
140
+
141
+ backoff = [backoff * 2, POPULATE_MAX_BACKOFF].min
142
+ end
143
+ end
144
+ end
145
+
146
+ # Sleeps up to +seconds+, waking early when the informer is stopped so
147
+ # #stop (which joins this thread) stays responsive.
148
+ def interruptible_sleep(seconds, generation)
149
+ slept = 0.0
150
+ while slept < seconds && running?(generation)
151
+ step = [0.5, seconds - slept].min
152
+ sleep step
153
+ slept += step
154
+ end
155
+ end
156
+
157
+ # Honors the server's Retry-After when present (issue #45): wait at
158
+ # least that long, or the caller's own backoff, whichever is longer.
159
+ def respect_retry_after(error, fallback)
160
+ retry_after = error.respond_to?(:retry_after) ? error.retry_after : nil
161
+ [retry_after, fallback].compact.max
162
+ end
163
+
164
+ # Joins +thread+ for at most +timeout+ seconds. Returns once the thread
165
+ # has exited; if it is still alive (e.g. blocked in a watch stream that
166
+ # cannot be canceled), logs a warning and returns so the caller is not
167
+ # blocked indefinitely.
168
+ def join_bounded(thread, timeout: STOP_JOIN_TIMEOUT)
169
+ return if thread.nil? || thread == Thread.current
170
+ return unless thread.join(timeout).nil?
171
+
172
+ KrubyController.logger.warn(
173
+ "Informer #{@gvk}: a worker thread did not exit within #{timeout}s; " \
174
+ "proceeding with the stopped worker disabled"
175
+ )
176
+ end
177
+
178
+ # Lists the resource, following metadata.continue tokens until the
179
+ # collection is exhausted (issue #45). The whole snapshot is collected
180
+ # before anything is applied: a failure on page N leaves the store
181
+ # untouched, and the caller's retry starts from page 1 — which is also
182
+ # what an expired continue token (410 Gone) needs.
183
+ #
184
+ # The caller's +generation+ is re-checked before EVERY page fetch: a
185
+ # #stop that lands between pages ends the collection immediately, so a
186
+ # stopped worker neither issues further API calls nor accumulates a
187
+ # partial snapshot that would race the next generation's LIST. Returns
188
+ # nil when the informer was stopped mid-collection.
189
+ def fetch_list(generation)
190
+ items = []
191
+ continue = nil
192
+ resource_version = nil
193
+ loop do
194
+ return nil unless running?(generation)
195
+
196
+ path = paged_list_path(continue)
197
+ response = run_list_request(path)
198
+ page = JSON.parse(response.body)
199
+ raise RequestError.new("Informer list response for #{@gvk} has no items array") unless page["items"].is_a?(Array)
200
+
201
+ items.concat(page["items"])
202
+ resource_version = page.dig("metadata", "resourceVersion")
203
+ continue = page.dig("metadata", "continue")
204
+ break if continue.nil? || continue.empty?
205
+ end
206
+
207
+ # Every page of one logical LIST shares the snapshot resourceVersion
208
+ # pinned by the continue token, so the final page's RV is the
209
+ # snapshot's RV. A missing RV is a protocol violation: continuing
210
+ # with a nil RV would make every subsequent watch start blind.
211
+ raise RequestError.new("Informer list response for #{@gvk} has no resourceVersion") if missing_rv?(resource_version)
212
+
213
+ { "items" => items, "metadata" => { "resourceVersion" => resource_version } }
214
+ end
215
+
216
+ def missing_rv?(value)
217
+ value.nil? || value.to_s.empty?
218
+ end
219
+
220
+ def run_list_request(path)
221
+ request = @api_client.build_request("GET", path, auth_names: ["BearerToken"])
222
+ response = request.run
223
+ return response if response.success?
224
+
225
+ raise RequestError.from_response("Informer list failed for #{@gvk} at #{path}", response)
226
+ end
227
+
228
+ def watch_loop(generation)
229
+ while running?(generation)
230
+ # Wait for the initial List to complete: success sets @synced, an
231
+ # exhausted retry budget sets @sync_error. Do not block forever on
232
+ # a failed initial List (that would leave the informer inert).
233
+ sleep 0.1 while running?(generation) && !@synced && @sync_error.nil?
234
+ break unless running?(generation)
235
+ break if @sync_error
236
+
237
+ watch_once(generation)
238
+ break unless running?(generation)
239
+
240
+ relist(generation)
241
+ end
242
+ end
243
+
244
+ # Runs a single watch from the current resourceVersion. Any break --
245
+ # 410 Gone (stale RV), another ERROR event, HTTP error, network error,
246
+ # or a clean server-side close -- falls through to relist, which
247
+ # re-syncs the cache. A 410 relists immediately; every other failure
248
+ # backs off first (honoring Retry-After when present).
249
+ def watch_once(generation)
250
+ watch_path = with_active_run(generation) { watch_path_for(@gvk, @resource_version) }
251
+ return unless watch_path
252
+
253
+ stream = WatchStream.new(@api_client)
254
+ stream.connect(watch_path) do |event|
255
+ with_active_run(generation) do
256
+ handle_watch_event(event, generation)
257
+ end
258
+ end
259
+ rescue WatchStream::Gone
260
+ return unless running?(generation)
261
+
262
+ KrubyController.logger.info("Watch for #{@gvk}: resourceVersion expired (410 Gone); relisting")
263
+ rescue WatchStream::Error => e
264
+ return unless running?(generation)
265
+
266
+ KrubyController.logger.warn("Watch for #{@gvk} disconnected: #{e.message}; relisting")
267
+ interruptible_sleep(respect_retry_after(e, 1.0), generation)
268
+ rescue StandardError => e
269
+ return unless running?(generation)
270
+
271
+ KrubyController.logger.warn("Watch for #{@gvk} disconnected: #{e.message}; relisting")
272
+ interruptible_sleep(1.0, generation)
273
+ end
274
+
275
+ # Classifies a watch event (issue #45). Only ADDED / MODIFIED /
276
+ # DELETED reach the store and the handlers. BOOKMARK carries a valid
277
+ # resourceVersion for the current state and only advances the RV —
278
+ # nothing is stored or delivered. ERROR events raise: 410 Gone tells
279
+ # watch_loop to relist immediately, anything else disconnects the
280
+ # stream and backs off (honoring a Status retryAfterSeconds) before
281
+ # relisting. Unknown or malformed events are logged and ignored without
282
+ # touching the RV.
283
+ def handle_watch_event(event, generation)
284
+ obj = event["object"]
285
+ case event["type"]
286
+ when "ADDED", "MODIFIED", "DELETED"
287
+ unless valid_data_object?(obj)
288
+ # A data event must name a real object; a null or nameless payload
289
+ # (e.g. an object carrying only metadata.resourceVersion) cannot
290
+ # be keyed or delivered, so ignore it — and do NOT advance the RV
291
+ # to it, which would blind the next watch.
292
+ KrubyController.logger.warn(
293
+ "Informer #{@gvk}: ignoring malformed #{event["type"]} watch event: #{truncate(obj.inspect)}"
294
+ )
295
+ return
296
+ end
297
+
298
+ delta = delta_type(event)
299
+ @resource_version = obj.dig("metadata", "resourceVersion") unless missing_rv?(obj.dig("metadata", "resourceVersion"))
300
+
301
+ @store.add(delta, obj)
302
+ notify(delta, obj, generation)
303
+ when "BOOKMARK"
304
+ rv = obj.is_a?(Hash) ? obj.dig("metadata", "resourceVersion") : nil
305
+ @resource_version = rv unless missing_rv?(rv)
306
+ when "ERROR"
307
+ code = obj.is_a?(Hash) ? obj["code"] : nil
308
+ if code == 410
309
+ raise WatchStream::Gone.new("watch resourceVersion expired (410 Gone)", code: 410)
310
+ end
311
+
312
+ # A streamed 429/5xx Status carries its retry delay in
313
+ # details.retryAfterSeconds — the HTTP Retry-After parser cannot
314
+ # see it (the watch response itself is a 200), so surface it here.
315
+ raise WatchStream::Error.new(
316
+ "watch ERROR event (code: #{code.inspect})", code: code, retry_after: status_retry_after(obj)
317
+ )
318
+ else
319
+ KrubyController.logger.warn(
320
+ "Informer #{@gvk}: unclassified watch event type #{event["type"].inspect}; ignoring"
321
+ )
322
+ end
323
+ end
324
+
325
+ # A data event must be a real named object: a Hash with a Hash metadata
326
+ # block and a nonempty name. Everything else (null, wrong types, a
327
+ # nameless payload) is malformed for storage and delivery.
328
+ def valid_data_object?(obj)
329
+ obj.is_a?(Hash) && obj["metadata"].is_a?(Hash) && !obj.dig("metadata", "name").to_s.empty?
330
+ end
331
+
332
+ # Extracts a valid positive retry delay (seconds) from a watch ERROR
333
+ # Status object's details.retryAfterSeconds, or nil when absent/invalid.
334
+ def status_retry_after(obj)
335
+ return nil unless obj.is_a?(Hash)
336
+
337
+ value = obj.dig("details", "retryAfterSeconds")
338
+ return nil if value.nil?
339
+
340
+ delay = value.to_f
341
+ delay.positive? ? delay : nil
342
+ rescue StandardError
343
+ nil
344
+ end
345
+
346
+ def truncate(text, limit: 200)
347
+ text = text.to_s
348
+ text.length > limit ? "#{text[0...limit]}..." : text
349
+ end
350
+
351
+ # Re-Lists the resource, replaces the store contents, and emits
352
+ # ADDED / MODIFIED / DELETED events for the diff so controllers
353
+ # reconcile the state that changed while the watch was down. A stop
354
+ # that lands mid-collection (fetch_list returns nil) discards the
355
+ # partial snapshot and ends the relist.
356
+ def relist(generation)
357
+ return unless running?(generation)
358
+
359
+ resp = fetch_list(generation)
360
+ return unless resp
361
+
362
+ apply_list(resp, generation)
363
+ rescue StandardError => e
364
+ return unless running?(generation)
365
+
366
+ KrubyController.logger.warn("Informer relist failed for #{@gvk}: #{e.message}; retrying")
367
+ interruptible_sleep(respect_retry_after(e, 1.0), generation)
368
+ retry if running?(generation)
369
+ end
370
+
371
+ def apply_list(resp, generation, initial: false)
372
+ with_active_run(generation) do
373
+ old, new = @store.replace(resp["items"] || [])
374
+ new_rv = resp.dig("metadata", "resourceVersion")
375
+ @resource_version = new_rv unless missing_rv?(new_rv)
376
+ emit_relist_events(old, new, generation)
377
+ @synced = true if initial && running?(generation)
378
+ end
379
+ end
380
+
381
+ def emit_relist_events(old, new, generation)
382
+ (new.keys - old.keys).each { |key| notify(Delta::ADDED, new[key], generation) }
383
+ (old.keys & new.keys).each do |key|
384
+ old_rv = old[key].dig("metadata", "resourceVersion").to_s
385
+ new_rv = new[key].dig("metadata", "resourceVersion").to_s
386
+ notify(Delta::MODIFIED, new[key], generation) if old_rv != new_rv
387
+ end
388
+ (old.keys - new.keys).each { |key| notify(Delta::DELETED, old[key], generation) }
389
+ end
390
+
391
+ # Delivers an event to every registered handler. The delivered object
392
+ # is an independent deep copy of the store-owned object (issue #46):
393
+ # a handler (or a reconciler, predicate, or anything the handler
394
+ # passes it to) may mutate the copy freely without ever corrupting the
395
+ # cache. All handlers of one event share the single copy, so a handler
396
+ # that mutates it is visible to the other handlers of the SAME event —
397
+ # treat the delivered object as read-only, like controller-runtime
398
+ # does. A handler that raises is logged and skipped so one bad handler
399
+ # cannot suppress the remaining handlers for this event, nor the
400
+ # events that follow it (critical during a relist, where the store
401
+ # snapshot is already committed).
402
+ def notify(type, obj, generation)
403
+ delivered = Cache.deep_copy(obj)
404
+ @handlers.each do |h|
405
+ with_active_run(generation) { h.handle(type, delivered) }
406
+ rescue StandardError => e
407
+ KrubyController.logger.error("Handler for #{@gvk} failed on #{type}: #{e.message}")
408
+ end
409
+ end
410
+
411
+ # Namespaces the list path only for namespaced resources; cluster-scoped
412
+ # resources stay cluster-wide even when a namespace is configured.
413
+ def list_path_for(gvk)
414
+ prefix = if gvk.group.empty?
415
+ "/api/#{gvk.version}"
416
+ else
417
+ "/apis/#{gvk.group}/#{gvk.version}"
418
+ end
419
+ scope = if @namespace && gvk.respond_to?(:namespaced?) && gvk.namespaced?
420
+ "/namespaces/#{@namespace}"
421
+ else
422
+ ""
423
+ end
424
+
425
+ "#{prefix}#{scope}/#{gvk.resource}"
426
+ end
427
+
428
+ # Adds the optional page size and the continue token for a paged LIST
429
+ # (issue #45). The bare path is unchanged when neither applies.
430
+ def paged_list_path(continue)
431
+ params = []
432
+ params << "limit=#{@list_page_size}" if @list_page_size
433
+ params << "continue=#{CGI.escape(continue)}" if continue
434
+ return list_path_for(@gvk) if params.empty?
435
+
436
+ "#{list_path_for(@gvk)}?#{params.join('&')}"
437
+ end
438
+
439
+ def watch_path_for(gvk, resource_version)
440
+ "#{list_path_for(gvk)}?watch=true&resourceVersion=#{resource_version}"
441
+ end
442
+
443
+ def delta_type(event)
444
+ case event["type"]
445
+ when "ADDED" then Delta::ADDED
446
+ when "MODIFIED" then Delta::MODIFIED
447
+ when "DELETED" then Delta::DELETED
448
+ else Delta::BOOKKEEPER
449
+ end
450
+ end
451
+ end
452
+ end
453
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+
6
+ module KrubyController
7
+ module Cache
8
+ # An API request failure carrying its HTTP code and optional Retry-After
9
+ # (seconds), so callers can back off correctly (issue #45). Both the
10
+ # informer's List path and WatchStream raise this for HTTP failures.
11
+ class RequestError < KrubyController::Error
12
+ attr_reader :code, :retry_after
13
+
14
+ def initialize(message, code: nil, retry_after: nil)
15
+ super(message)
16
+ @code = code
17
+ @retry_after = retry_after
18
+ end
19
+
20
+ # Builds the error from a raw API response object (Typhoeus-shaped:
21
+ # #code, #headers, #status_message), picking up Retry-After when the
22
+ # server sent one (429 and 5xx responses).
23
+ def self.from_response(message_prefix, response)
24
+ code = response.respond_to?(:code) ? response.code.to_i : nil
25
+ retry_after = parse_retry_after(response)
26
+ message = "#{message_prefix} (HTTP #{code})"
27
+ message += ": #{response.status_message}" if response.respond_to?(:status_message) && response.status_message
28
+
29
+ new(message, code: code, retry_after: retry_after)
30
+ end
31
+
32
+ # Parses a Retry-After value in either form allowed by RFC 7231: a
33
+ # non-negative integer of seconds, or an HTTP-date. HTTP-dates are
34
+ # converted to the remaining delay; invalid or already-past values are
35
+ # discarded (nil) so the caller falls back to its own backoff.
36
+ def self.parse_retry_after(response)
37
+ headers = response.respond_to?(:headers) ? response.headers : nil
38
+ return nil if headers.nil?
39
+
40
+ value = headers["Retry-After"] || headers["retry-after"]
41
+ return nil if value.nil? || value.to_s.strip.empty?
42
+
43
+ value = value.to_s.strip
44
+ if value.match?(/\A\d+(\.\d+)?\z/)
45
+ delay = value.to_f
46
+ else
47
+ delay = Time.httpdate(value) - Time.now
48
+ end
49
+ delay.is_a?(Numeric) && delay.positive? ? delay : nil
50
+ rescue StandardError
51
+ nil
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ module Cache
5
+ # The store owns an independent deep copy of every object it holds
6
+ # (issue #46): both the ingest side (the object passed to #add /
7
+ # #replace belongs to the caller afterwards and may be mutated or
8
+ # reused without affecting the cache) and the outbound side (#get /
9
+ # #list return a deep copy by default, so a caller mutation can never
10
+ # rewrite the cache before an API write). Copying is the default and
11
+ # the only behavior that protects the cache; +copy: false+ hands back
12
+ # the store-owned object for hot paths where the caller only reads it
13
+ # and does not retain it — mutating a +copy: false+ result WILL corrupt
14
+ # the cache (and be visible to every other reader), so that opt-out
15
+ # must never be used by code that mutates.
16
+ class Store
17
+ def initialize
18
+ @store = {}
19
+ @mutex = Mutex.new
20
+ end
21
+
22
+ def add(delta_type, obj)
23
+ key = namespace_name_key(obj)
24
+ stored = Cache.deep_copy(obj)
25
+ @mutex.synchronize do
26
+ case delta_type
27
+ when Delta::ADDED, Delta::MODIFIED
28
+ @store[key] = stored
29
+ when Delta::DELETED
30
+ @store.delete(key)
31
+ end
32
+ end
33
+ end
34
+
35
+ def get(namespace, name, copy: true)
36
+ @mutex.synchronize do
37
+ obj = @store["#{namespace}/#{name}"]
38
+ return nil if obj.nil?
39
+
40
+ copy ? Cache.deep_copy(obj) : obj
41
+ end
42
+ end
43
+
44
+ def list(label_selector: nil, copy: true)
45
+ @mutex.synchronize do
46
+ results = @store.values
47
+ results = results.select { |obj| matches_label_selector?(obj, label_selector) } if label_selector
48
+ copy ? results.map { |obj| Cache.deep_copy(obj) } : results
49
+ end
50
+ end
51
+
52
+ # Atomically replaces the store contents with +items+ (a fresh List
53
+ # result; the store takes an independent deep copy of each item) and
54
+ # returns the [old, new] key -> object maps so the caller can emit
55
+ # ADDED / MODIFIED / DELETED events for the diff. Both maps hold
56
+ # store-owned objects, never the caller's input objects.
57
+ def replace(items)
58
+ @mutex.synchronize do
59
+ old = @store
60
+ new_store = {}
61
+ items.each do |item|
62
+ stored = Cache.deep_copy(item)
63
+ new_store[namespace_name_key(item)] = stored
64
+ end
65
+ @store = new_store
66
+ [old, new_store]
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def namespace_name_key(source)
73
+ if source.is_a?(Hash)
74
+ ns = source.dig("metadata", "namespace") || ""
75
+ name = source.dig("metadata", "name")
76
+ else
77
+ ns = source.metadata.namespace || ""
78
+ name = source.metadata.name
79
+ end
80
+ "#{ns}/#{name}"
81
+ end
82
+
83
+ def matches_label_selector?(obj, selector)
84
+ labels = obj.is_a?(Hash) ? obj.dig("metadata", "labels") || {} : {}
85
+ selector.split(",").all? do |pair|
86
+ key, value = pair.split("=")
87
+ labels[key] == value
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end