Skip to content

Commit

Permalink
Drop entity classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooli Tayer committed Jan 23, 2018
1 parent 9104e05 commit 0755996
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,16 @@ client.process_template template

## Upgrading

#### past version 3.0

Specific entity classes mentioned in (past version 1.2.0)[past_version_1.2.0] have been dropped.
Return values and expected classes are always Kubeclient::Resource.

#### past version 2.0

Replace `KubeException` with `Kubeclient::HttpException`

<a name="past_version_1.2.0">
#### past version 1.2.0
Replace Specific Entity class references:

Expand Down
4 changes: 1 addition & 3 deletions lib/kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'kubeclient/entity_list'
require 'kubeclient/http_error'
require 'kubeclient/missing_kind_compatibility'
require 'kubeclient/resource'
require 'kubeclient/resource_not_found_error'
require 'kubeclient/version'
require 'kubeclient/watch_notice'
Expand All @@ -15,15 +16,12 @@ module Kubeclient
# Kubernetes Client
class Client
include ClientMixin
# define a multipurpose resource class, available before discovery
ClientMixin.resource_class(Kubeclient, 'Resource')
def initialize(
uri,
version = 'v1',
**options
)
initialize_client(
Kubeclient,
uri,
'/api',
version,
Expand Down
41 changes: 9 additions & 32 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module ClientMixin
attr_reader :discovered

def initialize_client(
class_owner,
uri,
path,
version,
Expand All @@ -62,7 +61,6 @@ def initialize_client(
validate_auth_options(auth_options)
handle_uri(uri, path)

@class_owner = class_owner
@entities = {}
@discovered = false
@api_version = version
Expand Down Expand Up @@ -156,28 +154,11 @@ def build_namespace_prefix(namespace)
namespace.to_s.empty? ? '' : "namespaces/#{namespace}/"
end

def self.resource_class(class_owner, entity_type)
if class_owner.const_defined?(entity_type, false)
class_owner.const_get(entity_type, false)
else
class_owner.const_set(
entity_type,
Class.new(RecursiveOpenStruct) do
def initialize(hash = nil, args = {})
args[:recurse_over_arrays] = true
super(hash, args)
end
end
)
end
end

def define_entity_methods
@entities.values.each do |entity|
klass = ClientMixin.resource_class(@class_owner, entity.entity_type)
# get all entities of a type e.g. get_nodes, get_pods, etc.
define_singleton_method("get_#{entity.method_names[1]}") do |options = {}|
get_entities(entity.entity_type, klass, entity.resource_name, options)
get_entities(entity.entity_type, entity.resource_name, options)
end

# watch all entities of a type e.g. watch_nodes, watch_pods, etc.
Expand All @@ -192,7 +173,7 @@ def define_entity_methods
# get a single entity of a specific type by name
define_singleton_method("get_#{entity.method_names[0]}") \
do |name, namespace = nil, opts = {}|
get_entity(klass, entity.resource_name, name, namespace, opts)
get_entity(entity.resource_name, name, namespace, opts)
end

define_singleton_method("delete_#{entity.method_names[0]}") \
Expand All @@ -201,7 +182,7 @@ def define_entity_methods
end

define_singleton_method("create_#{entity.method_names[0]}") do |entity_config|
create_entity(entity.entity_type, entity.resource_name, entity_config, klass)
create_entity(entity.entity_type, entity.resource_name, entity_config)
end

define_singleton_method("update_#{entity.method_names[0]}") do |entity_config|
Expand Down Expand Up @@ -271,7 +252,7 @@ def watch_entities(resource_name, options = {})
#
# Default response type will return a collection RecursiveOpenStruct
# (:ros) objects, unless `:as` is passed with `:raw`.
def get_entities(entity_type, klass, resource_name, options = {})
def get_entities(entity_type, resource_name, options = {})
params = {}
SEARCH_ARGUMENTS.each { |k, v| params[k] = options[v] if options[v] }

Expand All @@ -290,7 +271,7 @@ def get_entities(entity_type, klass, resource_name, options = {})
end

# result['items'] might be nil due to https://github.com/kubernetes/kubernetes/issues/13096
collection = result['items'].to_a.map { |item| new_entity(item, klass) }
collection = result['items'].to_a.map { |item| Kubeclient::Resource.new(item) }

Kubeclient::Common::EntityList.new(entity_type, resource_version, collection)
end
Expand All @@ -300,7 +281,7 @@ def get_entities(entity_type, klass, resource_name, options = {})
#
# Default response type will return an entity as a RecursiveOpenStruct
# (:ros) object, unless `:as` is passed with `:raw`.
def get_entity(klass, resource_name, name, namespace = nil, options = {})
def get_entity(resource_name, name, namespace = nil, options = {})
ns_prefix = build_namespace_prefix(namespace)
response = handle_exception do
rest_client[ns_prefix + resource_name + "/#{name}"]
Expand All @@ -309,7 +290,7 @@ def get_entity(klass, resource_name, name, namespace = nil, options = {})
return response.body if options[:as] == :raw

result = JSON.parse(response)
new_entity(result, klass)
Kubeclient::Resource.new(result)
end

def delete_entity(resource_name, name, namespace = nil, delete_options: {})
Expand All @@ -329,7 +310,7 @@ def delete_entity(resource_name, name, namespace = nil, delete_options: {})
end
end

def create_entity(entity_type, resource_name, entity_config, klass)
def create_entity(entity_type, resource_name, entity_config)
# Duplicate the entity_config to a hash so that when we assign
# kind and apiVersion, this does not mutate original entity_config obj.
hash = entity_config.to_hash
Expand All @@ -348,7 +329,7 @@ def create_entity(entity_type, resource_name, entity_config, klass)
.post(hash.to_json, { 'Content-Type' => 'application/json' }.merge(@headers))
end
result = JSON.parse(response)
new_entity(result, klass)
Kubeclient::Resource.new(result)
end

def update_entity(resource_name, entity_config)
Expand All @@ -371,10 +352,6 @@ def patch_entity(resource_name, name, patch, namespace = nil)
end
end

def new_entity(hash, klass)
klass.new(hash)
end

def all_entities(options = {})
discover unless @discovered
@entities.values.each_with_object({}) do |entity, result_hash|
Expand Down
11 changes: 11 additions & 0 deletions lib/kubeclient/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'recursive_open_struct'

module Kubeclient
# Represents all the objects returned by Kubeclient
class Resource < RecursiveOpenStruct
def initialize(hash = nil, args = {})
args[:recurse_over_arrays] = true
super(hash, args)
end
end
end

0 comments on commit 0755996

Please sign in to comment.