Skip to content

Commit

Permalink
Merge pull request #366 from cben/refactor-singular-plural
Browse files Browse the repository at this point in the history
Simpler derivation of singular & plural method names
  • Loading branch information
cben authored Nov 26, 2018
2 parents c864f8e + 8c481f2 commit 5c4b298
Show file tree
Hide file tree
Showing 25 changed files with 1,420 additions and 142 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ Kubeclient release versioning follows [SemVer](https://semver.org/).

## Unreleased

### Fixed
- Support custom resources with lowercase `kind` (#361).
- `create_security_context_constraint` now works (#366).
- `get_security_context_constraints.kind`, `get_endpoints.kind` are now plural as in kubernetes (#366).

### Added
- Add support for retrieving large lists of objects in chunks (#356)
- Add support for retrieving large lists of objects in chunks (#356).

## 4.0.0 — 2018-07-23

Expand Down Expand Up @@ -86,6 +91,6 @@ No changes since 2.5.0, fixed packaging mistake.

### Added

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

## 2.4.0 - 2017-05-10
61 changes: 42 additions & 19 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 @@ -321,9 +338,7 @@ def create_entity(entity_type, resource_name, entity_config)
# TODO: temporary solution to add "kind" and apiVersion to request
# until this issue is solved
# https://github.com/GoogleCloudPlatform/kubernetes/issues/6439
# TODO: #2 solution for
# https://github.com/kubernetes/kubernetes/issues/8115
hash[:kind] = (entity_type.eql?('Endpoint') ? 'Endpoints' : entity_type)
hash[:kind] = entity_type
hash[:apiVersion] = @api_group + @api_version
response = handle_exception do
rest_client[ns_prefix + resource_name]
Expand Down Expand Up @@ -435,7 +450,15 @@ def api

private

# Format ditetime according to RFC3339
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
when DateTime, Time
Expand Down
Loading

0 comments on commit 5c4b298

Please sign in to comment.