Skip to content

Commit

Permalink
Refactor derivation of singular & plural method names
Browse files Browse the repository at this point in the history
Also fixes #307 - get_endpoints.kind now plural as in kubernetes.
  • Loading branch information
cben committed Nov 23, 2018
1 parent f4a8b63 commit e728976
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
55 changes: 40 additions & 15 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,38 @@ def discover
end

def self.parse_definition(kind, name)
# "name": "componentstatuses", networkpolicies, endpoints
# "kind": "ComponentStatus" NetworkPolicy, Endpoints
# maintain pre group api compatibility for endpoints and securitycontextconstraints.
# See: https://github.com/kubernetes/kubernetes/issues/8115
kind = kind[0..-2] if %w[Endpoints SecurityContextConstraints].include?(kind)

prefix = kind =~ /[A-Z]/ ? kind[0..kind.rindex(/[A-Z]/)] : kind # NetworkP
m = name.match(/^#{prefix.downcase}(.*)$/)
m && OpenStruct.new(
entity_type: kind, # ComponentStatus
resource_name: name, # componentstatuses
method_names: [
ClientMixin.underscore_entity(kind), # component_status
ClientMixin.underscore_entity(prefix) + m[1] # component_statuses
]
# Kubernetes gives us have 3 inputs:
# kind: "ComponentStatus"
# name: "componentstatuses"
# singularName: "componentstatus" (usually kind.downcase)
# and want to derive singular and plural method names, with underscores:
# "component_status"
# "component_statuses"
# kind's CamelCase word boundaries determine our placement of underscores.

if IRREGULAR_NAMES[kind]
# In a few cases, the given kind / singularName itself is still plural.
# We require a distinct singular method name, so force it.
method_names = IRREGULAR_NAMES[kind]
else
# TODO: respect singularName from discovery?
# But how? If it differs from kind.downcase, kind's word boundaries don't apply.
singular_name = kind.downcase

if name.start_with?(kind.downcase)
plural_suffix = name[kind.downcase.length..-1] # "es"
singular_underscores = ClientMixin.underscore_entity(kind) # "component_status"
method_names = [singular_underscores, singular_underscores + plural_suffix]
else
# Something weird, can't infer underscores for plural so just give them up
method_names = [singular_name, name]
end
end

OpenStruct.new(
entity_type: kind,
resource_name: name,
method_names: method_names
)
end

Expand Down Expand Up @@ -435,6 +452,14 @@ def api

private

IRREGULAR_NAMES = {
# In a few cases, the given kind itself is still plural.
# https://github.com/kubernetes/kubernetes/issues/8115
'Endpoints' => %w[endpoint endpoints],
'SecurityContextConstraints' => %w[security_context_constraint
security_context_constraints]
}.freeze

# Format datetime according to RFC3339
def format_datetime(value)
case value
Expand Down
12 changes: 6 additions & 6 deletions test/test_endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require_relative 'test_helper'

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

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

0 comments on commit e728976

Please sign in to comment.