From 075599619cded42ba98dfba10eabe20cdc933ce6 Mon Sep 17 00:00:00 2001 From: Mooli Tayer Date: Tue, 23 Jan 2018 10:43:58 +0200 Subject: [PATCH] Drop entity classes --- README.md | 6 ++++++ lib/kubeclient.rb | 4 +--- lib/kubeclient/common.rb | 41 +++++++++----------------------------- lib/kubeclient/resource.rb | 11 ++++++++++ 4 files changed, 27 insertions(+), 35 deletions(-) create mode 100644 lib/kubeclient/resource.rb diff --git a/README.md b/README.md index 8a2edf61..d575522c 100644 --- a/README.md +++ b/README.md @@ -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` + #### past version 1.2.0 Replace Specific Entity class references: diff --git a/lib/kubeclient.rb b/lib/kubeclient.rb index ddce1ca3..264b9fa5 100644 --- a/lib/kubeclient.rb +++ b/lib/kubeclient.rb @@ -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' @@ -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, diff --git a/lib/kubeclient/common.rb b/lib/kubeclient/common.rb index 6c465689..41a6c8fa 100644 --- a/lib/kubeclient/common.rb +++ b/lib/kubeclient/common.rb @@ -49,7 +49,6 @@ module ClientMixin attr_reader :discovered def initialize_client( - class_owner, uri, path, version, @@ -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 @@ -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. @@ -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]}") \ @@ -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| @@ -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] } @@ -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 @@ -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}"] @@ -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: {}) @@ -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 @@ -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) @@ -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| diff --git a/lib/kubeclient/resource.rb b/lib/kubeclient/resource.rb new file mode 100644 index 00000000..3a59ae3f --- /dev/null +++ b/lib/kubeclient/resource.rb @@ -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 \ No newline at end of file