Skip to content

Commit 59e1d72

Browse files
committed
Fallback to lower-case method names
When names of methods aren't conventional, there will be an attempt to produce method names based on the following logic: 1. If the plural name without separators starts with lower-cased 'kind', use underscores as separators (assuming plural has separators) 2. In case of a mismatch, remove any separators and plural name will be the lower-case of it. For instance, the CNI defines entity named NetworkAttachmentDefinition. Its resource is 'network-attachment-definition', so to access the resource a call should be made for: /apis/k8s.cni.cncf.io/v1/namespaces/default/network-attachment-definitions However, with the current code, the generated methods for that resource rely on the reported resource definition: { "name"=>"network-attachment-definitions", "singularName"=>"network-attachment-definition", "namespaced"=>true, "kind"=>"NetworkAttachmentDefinition", "verbs"=>["delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"], "shortNames"=>["net-attach-def"] } And by that, the stored information for the entity looks like: <OpenStruct entity_type="NetworkAttachmentDefinition", resource_name="network-attachment-definitions", method_names=["networkattachmentdefinition", "network-attachment-definitions"]> This produces unsupported generated method names such as 'get_network-attachment-definitions' and 'watch_network-attachment-definitions' which prevents client to access that resource. Using the PR, the generated method names will be: 'get_network_attachment_definition' and 'get_network_definitions'.
1 parent 4ad560b commit 59e1d72

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Kubeclient release versioning follows [SemVer](https://semver.org/).
66

77
## Unreleased
88

9+
- For resources that contain dashes in name, the generated method names will replace the dash in names with underscores.
10+
911
### Security
1012
- Really made `Kubeclient::Config.new(data, nil)` prevent external file lookups.
1113
README documented this since 3.1.1 (#334) but alas that was a lie — absolute paths always worked.

lib/kubeclient/common.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,19 @@ def self.parse_definition(kind, name)
150150
# But how? If it differs from kind.downcase, kind's word boundaries don't apply.
151151
singular_name = kind.downcase
152152

153-
if name.start_with?(kind.downcase)
153+
if name.start_with?(singular_name)
154154
plural_suffix = name[kind.downcase.length..-1] # "es"
155155
singular_underscores = ClientMixin.underscore_entity(kind) # "component_status"
156156
method_names = [singular_underscores, singular_underscores + plural_suffix]
157157
else
158-
# Something weird, can't infer underscores for plural so just give them up
159-
method_names = [singular_name, name]
158+
underscored_name = name.tr('-', '_')
159+
singular_underscores = ClientMixin.underscore_entity(kind)
160+
if underscored_name.start_with?(singular_underscores)
161+
method_names = [singular_underscores, underscored_name]
162+
else
163+
# fallback to lowercase, no separators for both names
164+
method_names = [singular_name, underscored_name.tr('_', '')]
165+
end
160166
end
161167
end
162168

test/test_common.rb

+15
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,19 @@ def test_format_datetime_with_time
7474
formatted = client.send(:format_datetime, value)
7575
assert_equal(formatted, '2018-04-30T19:20:33.000000000+00:00')
7676
end
77+
78+
def test_parse_definition_with_unconventional_names
79+
%w[
80+
TestWithDash test-with-dashes test_with_dash test_with_dashes
81+
TestUnderscore test_underscores test_underscore test_underscores
82+
TestMismatch other-odd-name testmismatch otheroddname
83+
MixedDashMinus mixed-dash_minuses mixed_dash_minus mixed_dash_minuses
84+
Noseparator noseparators noseparator noseparators
85+
SameUptoWordboundary sameup-toword-boundarys sameuptowordboundary sameuptowordboundarys
86+
].each_slice(4) do |kind, plural, expected_single, expected_plural|
87+
method_names = Kubeclient::ClientMixin.parse_definition(kind, plural).method_names
88+
assert_equal(method_names[0], expected_single)
89+
assert_equal(method_names[1], expected_plural)
90+
end
91+
end
7792
end

0 commit comments

Comments
 (0)