Skip to content

Commit 5c4b298

Browse files
authored
Merge pull request ManageIQ#366 from cben/refactor-singular-plural
Simpler derivation of singular & plural method names
2 parents c864f8e + 8c481f2 commit 5c4b298

25 files changed

+1420
-142
lines changed

CHANGELOG.md

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

77
## Unreleased
88

9+
### Fixed
10+
- Support custom resources with lowercase `kind` (#361).
11+
- `create_security_context_constraint` now works (#366).
12+
- `get_security_context_constraints.kind`, `get_endpoints.kind` are now plural as in kubernetes (#366).
13+
914
### Added
10-
- Add support for retrieving large lists of objects in chunks (#356)
15+
- Add support for retrieving large lists of objects in chunks (#356).
1116

1217
## 4.0.0 — 2018-07-23
1318

@@ -86,6 +91,6 @@ No changes since 2.5.0, fixed packaging mistake.
8691

8792
### Added
8893

89-
- `as: raw` option for `get_*` methods returning a string (#262 via #271)
94+
- `as: raw` option for `get_*` methods returning a string (#262 via #271).
9095

9196
## 2.4.0 - 2017-05-10

lib/kubeclient/common.rb

+42-19
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

@@ -321,9 +338,7 @@ def create_entity(entity_type, resource_name, entity_config)
321338
# TODO: temporary solution to add "kind" and apiVersion to request
322339
# until this issue is solved
323340
# https://github.com/GoogleCloudPlatform/kubernetes/issues/6439
324-
# TODO: #2 solution for
325-
# https://github.com/kubernetes/kubernetes/issues/8115
326-
hash[:kind] = (entity_type.eql?('Endpoint') ? 'Endpoints' : entity_type)
341+
hash[:kind] = entity_type
327342
hash[:apiVersion] = @api_group + @api_version
328343
response = handle_exception do
329344
rest_client[ns_prefix + resource_name]
@@ -435,7 +450,15 @@ def api
435450

436451
private
437452

438-
# Format ditetime according to RFC3339
453+
IRREGULAR_NAMES = {
454+
# In a few cases, the given kind itself is still plural.
455+
# https://github.com/kubernetes/kubernetes/issues/8115
456+
'Endpoints' => %w[endpoint endpoints],
457+
'SecurityContextConstraints' => %w[security_context_constraint
458+
security_context_constraints]
459+
}.freeze
460+
461+
# Format datetime according to RFC3339
439462
def format_datetime(value)
440463
case value
441464
when DateTime, Time

0 commit comments

Comments
 (0)