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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 319db701970bf4226124b43a83dbb82b309ba038581663abe7fe4640fa754cdc
4
+ data.tar.gz: fe067fb89f1208248944e635aea83f87f2ddccc3f4be47075ad3b6d3cff5d7fe
5
+ SHA512:
6
+ metadata.gz: 24ae042ad462e747da05d7e182174a2055926013f5189a4d4aaa21430b5fdb8f671fd6deb31581d5c4ce173855f461e47a9cad508872c30f840e2812c91897a1
7
+ data.tar.gz: 808a0584b98107f5de0a6d377651bdc2f4f9465eca2abe05a380455ca92d739f1761023449c6f8df6e5f47a189371c157953d39a4973d6df1252d31c46888571
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Kruby Controller Framework
2
+
3
+ [![Ruby](https://img.shields.io/badge/ruby-3.3+-brightgreen.svg)](https://www.ruby-lang.org/)
4
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
5
+ [![CI](https://github.com/doridoridoriand/kubebuilder-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/doridoridoriand/kubebuilder-ruby/actions/workflows/ci.yml)
6
+
7
+ **The Kubernetes controller pattern, in Ruby.** A framework for building custom controllers on top of kruby (the Kubernetes Ruby client).
8
+
9
+ A Ruby port of the kubebuilder / controller-runtime concepts.
10
+
11
+ ## Overview
12
+
13
+ A framework for writing controllers for Kubernetes custom resources (CRDs) in Ruby.
14
+
15
+ The following is a minimal example of registering a reconciler and reading from the cache. Before running it, install the target CRD and grant RBAC for List / Watch and Lease. Handling resource creation and status updates is omitted for brevity.
16
+
17
+ ```ruby
18
+ require "kruby-controller"
19
+
20
+ class CronTask
21
+ include KrubyController::CRD
22
+ self.group = "stable.example.com"
23
+ self.version = "v1"
24
+ self.kind = "CronTask"
25
+ self.plural = "crontasks"
26
+ end
27
+
28
+ CRON_TASK_GVK = KrubyController::GVK.new(
29
+ group: CronTask.group, version: CronTask.version,
30
+ kind: CronTask.kind, resource: CronTask.plural
31
+ )
32
+
33
+ class CronTaskReconciler < KrubyController::Reconciler
34
+ def reconcile(request)
35
+ obj = cache.get(CRON_TASK_GVK, request.namespace, request.name)
36
+ return KrubyController::Result.ok unless obj
37
+
38
+ logger.info("Reconciling #{request.namespace}/#{request.name}")
39
+ KrubyController::Result.ok
40
+ end
41
+ end
42
+
43
+ manager = KrubyController::Manager.new(kube_config: ENV["KUBECONFIG"])
44
+ manager.add(CronTaskReconciler, for: CronTask)
45
+ manager.start
46
+ ```
47
+
48
+ ## Features
49
+
50
+ | Feature | Status |
51
+ |---|---|
52
+ | Manager / reconcile loop / shared cache / leader election | Basic implementation. Reliability aspects (sync ordering, shutdown paths) still need hardening |
53
+ | Event handlers / owner references / finalizers / status | Basic implementation |
54
+ | CRD generator / CLI `init` and `create api` | Basic implementation. Some schemas and deploy assets are not yet covered |
55
+ | Integrated client / event recording / probes / metrics | Not implemented |
56
+ | Webhooks / multi-version conversion | Not implemented |
57
+
58
+ The presence of an implementation does not imply production readiness or feature parity with Kubebuilder. See the [parity review](docs/design/parity-review.md) for details and remaining gaps.
59
+
60
+ ## Helpers (Finalizer / Status)
61
+
62
+ `FinalizerManager` / `StatusUpdater` require a client that provides `update` (a PUT of the whole object) and `patch_status` (a PATCH on the `/status` subresource). kruby's `CustomObjectsApi` has neither, so pass the framework's `KrubyController::KubernetesObjectClient` as the client:
63
+
64
+ ```ruby
65
+ config = Kubernetes::Configuration.default_config
66
+ api_client = Kubernetes::ApiClient.new(config)
67
+ gvk = KrubyController::GVK.new(group: "stable.example.com", version: "v1", kind: "CronTask", resource: "crontasks")
68
+ client = KrubyController::KubernetesObjectClient.new(api_client: api_client, gvk: gvk)
69
+
70
+ finalizers = KrubyController::Finalizer::FinalizerManager.new(client: client, gvk: gvk)
71
+ status = KrubyController::Status::StatusUpdater.new(client: client, gvk: gvk)
72
+
73
+ # inside reconcile
74
+ finalizers.add_finalizer(obj, "crontask.example.com/finalizer")
75
+ status.update_status(obj, { "phase" => "Running" })
76
+ ```
77
+
78
+ ## Requirements
79
+
80
+ - Ruby 3.3+
81
+ - kruby ~> 1.36
82
+ - Kubernetes 1.31+
83
+
84
+ ## Installation
85
+
86
+ Install the gem:
87
+
88
+ ```sh
89
+ gem install kruby-controller
90
+ ```
91
+
92
+ or add it to your Gemfile:
93
+
94
+ ```ruby
95
+ gem "kruby-controller", "~> 0.1.0"
96
+ ```
97
+
98
+ ## Example
99
+
100
+ A complete, runnable example operator lives in [examples/hello-world/](examples/hello-world/) — a CRD class plus an idempotent reconciler, verified end-to-end against a real kind cluster.
101
+
102
+ ## Development
103
+
104
+ ```bash
105
+ bundle install
106
+ bundle exec rake
107
+ ```
108
+
109
+ ## Documentation
110
+
111
+ - [Specification](docs/design/specification.md)
112
+ - [Design document](docs/design/design.md)
113
+ - [Parity review vs. upstream Kubebuilder](docs/design/parity-review.md)
114
+
115
+ ## License
116
+
117
+ Apache License 2.0 — see [LICENSE](LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:spec]
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "kruby-controller"
5
+
6
+ command = ARGV.shift
7
+
8
+ begin
9
+ case command
10
+ when "init"
11
+ KrubyController::CLI::InitCommand.new(ARGV).run
12
+ when "create"
13
+ subcommand = ARGV.shift
14
+ case subcommand
15
+ when "api"
16
+ KrubyController::CLI::CreateApiCommand.new(ARGV).run
17
+ else
18
+ warn "Unknown create subcommand: #{subcommand.inspect}"
19
+ warn "Usage: kruby-controller create api --group <G> --kind <K>"
20
+ exit 1
21
+ end
22
+ when nil, "-h", "--help"
23
+ puts "Usage: kruby-controller {init|create api}"
24
+ else
25
+ warn "Unknown command: #{command.inspect}"
26
+ warn "Usage: kruby-controller {init|create api}"
27
+ exit 1
28
+ end
29
+ rescue KrubyController::CLI::UsageError => e
30
+ warn e.message
31
+ exit 1
32
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ # rubocop:disable Style/MixinUsage
5
+ end
6
+ # rubocop:enable Style/MixinUsage
7
+
8
+ require "kruby_controller"
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ module Builder
5
+ class ControllerBuilder
6
+ def initialize(manager, for_gvk)
7
+ @manager = manager
8
+ @for_gvk = for_gvk
9
+ @owns = []
10
+ @watches = []
11
+ @predicate = nil
12
+ @max_concurrent = 1
13
+ end
14
+
15
+ def for(gvk)
16
+ @for_gvk = gvk
17
+ self
18
+ end
19
+
20
+ def owns(gvk)
21
+ @owns << gvk
22
+ self
23
+ end
24
+
25
+ def watches(gvk, predicate: nil, handler: nil)
26
+ @watches << { gvk:, predicate:, handler: }
27
+ self
28
+ end
29
+
30
+ def with_max_concurrent_reconciles(n)
31
+ @max_concurrent = n
32
+ self
33
+ end
34
+
35
+ def with_predicate(predicate)
36
+ @predicate = predicate
37
+ self
38
+ end
39
+
40
+ def complete_with(reconciler)
41
+ @manager.add(
42
+ reconciler,
43
+ for_gvk: @for_gvk,
44
+ max_concurrent: @max_concurrent,
45
+ owns: @owns,
46
+ watches: @watches,
47
+ predicate: @predicate
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ module Cache
5
+ class SharedCache
6
+ def initialize(kube_config: nil, namespace: nil, list_page_size: nil)
7
+ @kube_config = kube_config
8
+ @namespace = namespace
9
+ # Optional page size for every informer's paged LIST (issue #45).
10
+ @list_page_size = list_page_size
11
+ @informers = {}
12
+ @mutex = Mutex.new
13
+ end
14
+
15
+ def register(gvk:)
16
+ @mutex.synchronize do
17
+ return @informers[gvk.to_s] if @informers.key?(gvk.to_s)
18
+
19
+ store = Store.new
20
+ informer = Informer.new(gvk:, store:, api_client: api_client, handlers: [], namespace: @namespace,
21
+ list_page_size: @list_page_size)
22
+ @informers[gvk.to_s] = informer
23
+ informer
24
+ end
25
+ end
26
+
27
+ # Returns an independent deep copy of the cached object by default
28
+ # (issue #46): the reconciler may mutate the result — e.g. before an
29
+ # API update — without ever rewriting the cache. +copy: false+ hands
30
+ # back the store-owned object for hot paths that only read it and do
31
+ # not retain it; mutating that result corrupts the cache.
32
+ def get(gvk, namespace, name, copy: true)
33
+ informer = @informers[gvk.to_s]
34
+ return nil unless informer
35
+
36
+ informer.store.get(namespace, name, copy:)
37
+ end
38
+
39
+ # See #get for the copy contract. The +namespace+ filter runs BEFORE
40
+ # the copy, so a filtered +list+ only pays for the objects it returns:
41
+ # the store hands out an un-copied snapshot (store-owned objects are
42
+ # never mutated in place, so the snapshot stays a consistent view), the
43
+ # namespace selection runs on it outside the store lock, and only the
44
+ # selected objects are deep-copied — also outside the lock.
45
+ def list(gvk, namespace: nil, label_selector: nil, copy: true)
46
+ informer = @informers[gvk.to_s]
47
+ return [] unless informer
48
+
49
+ results = informer.store.list(label_selector:, copy: false)
50
+ results = results.select { |o| ns_matches?(o, namespace) } if namespace
51
+ copy ? results.map { |o| Cache.deep_copy(o) } : results
52
+ end
53
+
54
+ # Waits until every informer is synced. If an informer exhausts its
55
+ # initial-List retry budget, the recorded cause is raised so Manager
56
+ # startup fails with the real error instead of hanging.
57
+ def wait_for_sync(timeout: 30)
58
+ deadline = Time.now + timeout
59
+ loop do
60
+ informers = @mutex.synchronize { @informers.values }
61
+ failed = informers.find { |i| !i.synced && i.sync_error }
62
+ raise failed.sync_error if failed
63
+
64
+ return true if informers.all?(&:synced)
65
+ return false if Time.now >= deadline
66
+
67
+ sleep 0.1
68
+ end
69
+ end
70
+
71
+ def start
72
+ @informers.values.each(&:start)
73
+ end
74
+
75
+ def stop
76
+ @informers.values.each(&:stop)
77
+ end
78
+
79
+ private
80
+
81
+ def api_client
82
+ @api_client ||= begin
83
+ config = if @kube_config
84
+ cfg = Kubernetes::Configuration.new
85
+ Kubernetes::KubeConfig.new(@kube_config).configure(cfg)
86
+ cfg
87
+ else
88
+ Kubernetes::Configuration.default_config
89
+ end
90
+ Kubernetes::ApiClient.new(config)
91
+ end
92
+ end
93
+
94
+ def ns_matches?(obj, namespace)
95
+ obj_ns = obj.is_a?(Hash) ? obj.dig("metadata", "namespace") : ""
96
+ obj_ns == namespace
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ module Cache
5
+ # Deep-copies JSON-shaped object trees so the cache and its consumers
6
+ # never share a mutable object (issue #46).
7
+ #
8
+ # Policy: Hash, Array and String are copied recursively (string keys are
9
+ # copied too). nil / true / false / Numeric / Symbol are immutable and
10
+ # are passed through. Any other object (e.g. a kruby resource class
11
+ # instance) is passed through UNCOPIED — the framework's informers
12
+ # deliver JSON-shaped Hashes (parsed per event / per List), so sharing a
13
+ # non-JSON object is a by-convention read-only case, not a copy target.
14
+ #
15
+ # Measured on Ruby 3.4.4 (M2 MacBook): ~12µs for a typical CRD object
16
+ # (~1.3KB of JSON-shaped data), ~23µs for a ~1MB configmap payload.
17
+ module_function
18
+
19
+ def deep_copy(obj)
20
+ case obj
21
+ when Hash
22
+ obj.each_with_object({}) { |(k, v), copy| copy[deep_copy(k)] = deep_copy(v) }
23
+ when Array
24
+ obj.map { |v| deep_copy(v) }
25
+ when String
26
+ obj.dup
27
+ else
28
+ obj
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrubyController
4
+ module Cache
5
+ module Delta
6
+ ADDED = :added
7
+ MODIFIED = :modified
8
+ DELETED = :deleted
9
+ BOOKKEEPER = :bookkeeper
10
+ end
11
+ end
12
+ end