@@ -132,21 +132,38 @@ def discover
132
132
end
133
133
134
134
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
150
167
)
151
168
end
152
169
@@ -321,9 +338,7 @@ def create_entity(entity_type, resource_name, entity_config)
321
338
# TODO: temporary solution to add "kind" and apiVersion to request
322
339
# until this issue is solved
323
340
# 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
327
342
hash [ :apiVersion ] = @api_group + @api_version
328
343
response = handle_exception do
329
344
rest_client [ ns_prefix + resource_name ]
@@ -435,7 +450,15 @@ def api
435
450
436
451
private
437
452
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
439
462
def format_datetime ( value )
440
463
case value
441
464
when DateTime , Time
0 commit comments