Skip to content

Commit 783c58b

Browse files
committed
Simplify derivation of singular & plural method names, fix ManageIQ#307 ManageIQ#367
Also fixes ManageIQ#307 - get_security_context_constraints.kind, get_endpoints.kind are now plural as in kubernetes. Also fixes ManageIQ#367 - create_security_context_constraint now works.
1 parent 7d45d82 commit 783c58b

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

lib/kubeclient/common.rb

+40-15
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,38 @@ def discover
132132
end
133133

134134
def self.parse_definition(kind, name)
135-
# "name": "componentstatuses", networkpolicies, endpoints
136-
# "kind": "ComponentStatus" NetworkPolicy, Endpoints
137-
# maintain pre group api compatibility for endpoints and securitycontextconstraints.
138-
# See: https://github.com/kubernetes/kubernetes/issues/8115
139-
kind = kind[0..-2] if %w[Endpoints SecurityContextConstraints].include?(kind)
140-
141-
prefix = kind =~ /[A-Z]/ ? kind[0..kind.rindex(/[A-Z]/)] : kind # NetworkP
142-
m = name.match(/^#{prefix.downcase}(.*)$/)
143-
m && OpenStruct.new(
144-
entity_type: kind, # ComponentStatus
145-
resource_name: name, # componentstatuses
146-
method_names: [
147-
ClientMixin.underscore_entity(kind), # component_status
148-
ClientMixin.underscore_entity(prefix) + m[1] # component_statuses
149-
]
135+
# Kubernetes gives us have 3 inputs:
136+
# kind: "ComponentStatus"
137+
# name: "componentstatuses"
138+
# singularName: "componentstatus" (usually kind.downcase)
139+
# and want to derive singular and plural method names, with underscores:
140+
# "component_status"
141+
# "component_statuses"
142+
# kind's CamelCase word boundaries determine our placement of underscores.
143+
144+
if IRREGULAR_NAMES[kind]
145+
# In a few cases, the given kind / singularName itself is still plural.
146+
# We require a distinct singular method name, so force it.
147+
method_names = IRREGULAR_NAMES[kind]
148+
else
149+
# TODO: respect singularName from discovery?
150+
# But how? If it differs from kind.downcase, kind's word boundaries don't apply.
151+
singular_name = kind.downcase
152+
153+
if name.start_with?(kind.downcase)
154+
plural_suffix = name[kind.downcase.length..-1] # "es"
155+
singular_underscores = ClientMixin.underscore_entity(kind) # "component_status"
156+
method_names = [singular_underscores, singular_underscores + plural_suffix]
157+
else
158+
# Something weird, can't infer underscores for plural so just give them up
159+
method_names = [singular_name, name]
160+
end
161+
end
162+
163+
OpenStruct.new(
164+
entity_type: kind,
165+
resource_name: name,
166+
method_names: method_names
150167
)
151168
end
152169

@@ -435,6 +452,14 @@ def api
435452

436453
private
437454

455+
IRREGULAR_NAMES = {
456+
# In a few cases, the given kind itself is still plural.
457+
# https://github.com/kubernetes/kubernetes/issues/8115
458+
'Endpoints' => %w[endpoint endpoints],
459+
'SecurityContextConstraints' => %w[security_context_constraint
460+
security_context_constraints]
461+
}.freeze
462+
438463
# Format datetime according to RFC3339
439464
def format_datetime(value)
440465
case value

test/test_endpoint.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require_relative 'test_helper'
22

3-
# Endpoint entity tests
3+
# kind: 'Endpoints' entity tests.
4+
# This is one of the unusual `kind`s that are already plural (https://github.com/kubernetes/kubernetes/issues/8115).
5+
# We force singular in method names like 'create_endpoint',
6+
# but `kind` should remain plural as in kubernetes.
47
class TestEndpoint < MiniTest::Test
58
def test_create_endpoint
69
stub_core_api_list
@@ -44,11 +47,8 @@ def test_get_endpoints
4447
assert_equal('EndpointsList', collection[:kind])
4548
assert_equal('v1', collection[:apiVersion])
4649

50+
# Stripping of 'List' in collection.kind RecursiveOpenStruct mode only is historic.
4751
collection = client.get_endpoints
48-
# TODO: this is wrong. https://github.com/abonas/kubeclient/issues/307
49-
# Kubernetes for single object uses kind: "Endpoints" (!) and
50-
# kind: "EndpointsList" for plural. While we force singular in method names
51-
# to distinguish `get_endpoint` vs `get_endpoints`, we should not touch .kind.
52-
assert_equal('Endpoint', collection.kind)
52+
assert_equal('Endpoints', collection.kind)
5353
end
5454
end

test/test_security_context_constraint.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ def test_create_security_context_constraint
2222
type: 'MustRunAs'
2323
}
2424
)
25-
# TODO: kind here is wrong, this doesn't work at all!
26-
# https://github.com/abonas/kubeclient/issues/367
27-
# SecurityContextConstraint in version "v1" cannot be handled as a SecurityContextConstraints:
28-
# no kind "SecurityContextConstraint" is registered for version "security.openshift.io/v1"
2925
req_body = '{"metadata":{"name":"teleportation"},"runAsUser":{"type":"MustRunAs"},' \
3026
'"seLinuxContext":{"type":"MustRunAs"},' \
31-
'"kind":"SecurityContextConstraint","apiVersion":"security.openshift.io/v1"}'
27+
'"kind":"SecurityContextConstraints","apiVersion":"security.openshift.io/v1"}'
3228

3329
stub_request(:post, 'http://localhost:8080/apis/security.openshift.io/v1/securitycontextconstraints')
3430
.with(body: req_body)
@@ -59,8 +55,8 @@ def test_get_security_context_constraints
5955
assert_equal('SecurityContextConstraintsList', collection[:kind])
6056
assert_equal('security.openshift.io/v1', collection[:apiVersion])
6157

62-
# TODO: this is wrong. https://github.com/abonas/kubeclient/issues/307
58+
# Stripping of 'List' in collection.kind RecursiveOpenStruct mode only is historic.
6359
collection = client.get_security_context_constraints
64-
assert_equal('SecurityContextConstraint', collection.kind)
60+
assert_equal('SecurityContextConstraints', collection.kind)
6561
end
6662
end

0 commit comments

Comments
 (0)