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 28, 2018
1 parent 43cc2bf commit f3d9841
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 68 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,22 @@ 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.
Checking the type of a resource can be done using:
```
> pod.kind
=> "Pod"
```

#### past version 2.6

The gem raises Kubeclient::HttpError or subclasses now. Catching KubeException still works but is deprecated.

<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
2 changes: 1 addition & 1 deletion test/test_component_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_from_json_v3
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
component_status = client.get_component_status('etcd-0', 'default')

assert_instance_of(Kubeclient::ComponentStatus, component_status)
assert_instance_of(Kubeclient::Resource, component_status)
assert_equal('etcd-0', component_status.metadata.name)
assert_equal('Healthy', component_status.conditions[0].type)
assert_equal('True', component_status.conditions[0].status)
Expand Down
8 changes: 4 additions & 4 deletions test/test_guestbook_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def delete_services(client, namespace, services)
# if the entity is not found, no need to fail the test
services.each do |service|
begin
if service.instance_of?(Kubeclient::Service)
if service.instance_of?(Kubeclient::Resource)
client.delete_service(service.metadata.name, namespace)
else
# it's just a string - service name
Expand All @@ -98,7 +98,7 @@ def delete_replication_controllers(client, namespace, replication_controllers)
# if the entity is not found, no need to fail the test
replication_controllers.each do |rc|
begin
if rc.instance_of?(Kubeclient::ReplicationController)
if rc.instance_of?(Kubeclient::Resource)
client.delete_replication_controller(rc.metadata.name, namespace)
else
# it's just a string - rc name
Expand All @@ -113,7 +113,7 @@ def delete_replication_controllers(client, namespace, replication_controllers)
private

def construct_base_rc(namespace)
rc = Kubeclient::ReplicationController.new
rc = Kubeclient::Resource.new
rc.metadata = {}
rc.metadata.namespace = namespace
rc.metadata.labels = {}
Expand Down Expand Up @@ -192,7 +192,7 @@ def guestbook_rc(namespace)
end

def base_service(namespace)
our_service = Kubeclient::Service.new
our_service = Kubeclient::Resource.new
our_service.metadata = {}
our_service.metadata.namespace = namespace
our_service.metadata.labels = {}
Expand Down
28 changes: 14 additions & 14 deletions test/test_kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def test_entity_list
assert_instance_of(Kubeclient::Common::EntityList, services)
assert_equal('Service', services.kind)
assert_equal(2, services.size)
assert_instance_of(Kubeclient::Service, services[0])
assert_instance_of(Kubeclient::Service, services[1])
assert_instance_of(Kubeclient::Resource, services[0])
assert_instance_of(Kubeclient::Resource, services[1])

assert_requested(:get, 'http://localhost:8080/api/v1/services', times: 1)
end
Expand Down Expand Up @@ -371,18 +371,18 @@ def test_get_all
assert_instance_of(Kubeclient::Common::EntityList, result['event'])
assert_instance_of(Kubeclient::Common::EntityList, result['namespace'])
assert_instance_of(Kubeclient::Common::EntityList, result['secret'])
assert_instance_of(Kubeclient::Service, result['service'][0])
assert_instance_of(Kubeclient::Node, result['node'][0])
assert_instance_of(Kubeclient::Event, result['event'][0])
assert_instance_of(Kubeclient::Endpoint, result['endpoint'][0])
assert_instance_of(Kubeclient::Namespace, result['namespace'][0])
assert_instance_of(Kubeclient::Secret, result['secret'][0])
assert_instance_of(Kubeclient::ResourceQuota, result['resource_quota'][0])
assert_instance_of(Kubeclient::LimitRange, result['limit_range'][0])
assert_instance_of(Kubeclient::PersistentVolume, result['persistent_volume'][0])
assert_instance_of(Kubeclient::PersistentVolumeClaim, result['persistent_volume_claim'][0])
assert_instance_of(Kubeclient::ComponentStatus, result['component_status'][0])
assert_instance_of(Kubeclient::ServiceAccount, result['service_account'][0])
assert_instance_of(Kubeclient::Resource, result['service'][0])
assert_instance_of(Kubeclient::Resource, result['node'][0])
assert_instance_of(Kubeclient::Resource, result['event'][0])
assert_instance_of(Kubeclient::Resource, result['endpoint'][0])
assert_instance_of(Kubeclient::Resource, result['namespace'][0])
assert_instance_of(Kubeclient::Resource, result['secret'][0])
assert_instance_of(Kubeclient::Resource, result['resource_quota'][0])
assert_instance_of(Kubeclient::Resource, result['limit_range'][0])
assert_instance_of(Kubeclient::Resource, result['persistent_volume'][0])
assert_instance_of(Kubeclient::Resource, result['persistent_volume_claim'][0])
assert_instance_of(Kubeclient::Resource, result['component_status'][0])
assert_instance_of(Kubeclient::Resource, result['service_account'][0])
end

def test_get_all_raw
Expand Down
2 changes: 1 addition & 1 deletion test/test_limit_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
limit_range = client.get_limit_range('limits', 'quota-example')

assert_instance_of(Kubeclient::LimitRange, limit_range)
assert_instance_of(Kubeclient::Resource, limit_range)
assert_equal('limits', limit_range.metadata.name)
assert_equal('Container', limit_range.spec.limits[0].type)
assert_equal('100m', limit_range.spec.limits[0].default.cpu)
Expand Down
4 changes: 2 additions & 2 deletions test/test_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_namespace_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
namespace = client.get_namespace('staging')

assert_instance_of(Kubeclient::Namespace, namespace)
assert_instance_of(Kubeclient::Resource, namespace)
assert_equal('e388bc10-c021-11e4-a514-3c970e4a436a', namespace.metadata.uid)
assert_equal('staging', namespace.metadata.name)
assert_equal('1168', namespace.metadata.resourceVersion)
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_create_namespace

client = Kubeclient::Client.new('http://localhost:8080/api/')
created_namespace = client.create_namespace(namespace)
assert_instance_of(Kubeclient::Namespace, created_namespace)
assert_instance_of(Kubeclient::Resource, created_namespace)
assert_equal(namespace.metadata.name, created_namespace.metadata.name)
end
end
2 changes: 1 addition & 1 deletion test/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
node = client.get_node('127.0.0.1')

assert_instance_of(Kubeclient::Node, node)
assert_instance_of(Kubeclient::Resource, node)

assert_equal('041143c5-ce39-11e4-ac24-3c970e4a436a', node.metadata.uid)
assert_equal('127.0.0.1', node.metadata.name)
Expand Down
2 changes: 1 addition & 1 deletion test/test_persistent_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
volume = client.get_persistent_volume('pv0001')

assert_instance_of(Kubeclient::PersistentVolume, volume)
assert_instance_of(Kubeclient::Resource, volume)
assert_equal('pv0001', volume.metadata.name)
assert_equal('10Gi', volume.spec.capacity.storage)
assert_equal('/tmp/data01', volume.spec.hostPath.path)
Expand Down
2 changes: 1 addition & 1 deletion test/test_persistent_volume_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
claim = client.get_persistent_volume_claim('myclaim-1', 'default')

assert_instance_of(Kubeclient::PersistentVolumeClaim, claim)
assert_instance_of(Kubeclient::Resource, claim)
assert_equal('myclaim-1', claim.metadata.name)
assert_equal('3Gi', claim.spec.resources.requests.storage)
assert_equal('pv0001', claim.spec.volumeName)
Expand Down
2 changes: 1 addition & 1 deletion test/test_pod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
pod = client.get_pod('redis-master-pod', 'default')

assert_instance_of(Kubeclient::Pod, pod)
assert_instance_of(Kubeclient::Resource, pod)
assert_equal('redis-master3', pod.metadata.name)
assert_equal('dockerfile/redis', pod.spec.containers[0]['image'])

Expand Down
2 changes: 1 addition & 1 deletion test/test_replication_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
rc = client.get_replication_controller('frontendController', 'default')

assert_instance_of(Kubeclient::ReplicationController, rc)
assert_instance_of(Kubeclient::Resource, rc)
assert_equal('guestbook-controller', rc.metadata.name)
assert_equal('c71aa4c0-a240-11e4-a265-3c970e4a436a', rc.metadata.uid)
assert_equal('default', rc.metadata.namespace)
Expand Down
2 changes: 1 addition & 1 deletion test/test_resource_quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_get_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
quota = client.get_resource_quota('quota', 'quota-example')

assert_instance_of(Kubeclient::ResourceQuota, quota)
assert_instance_of(Kubeclient::Resource, quota)
assert_equal('quota', quota.metadata.name)
assert_equal('20', quota.spec.hard.cpu)
assert_equal('10', quota.spec.hard.secrets)
Expand Down
4 changes: 2 additions & 2 deletions test/test_secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_get_secret_v1
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
secret = client.get_secret('test-secret', 'dev')

assert_instance_of(Kubeclient::Secret, secret)
assert_instance_of(Kubeclient::Resource, secret)
assert_equal('4e38a198-2bcb-11e5-a483-0e840567604d', secret.metadata.uid)
assert_equal('test-secret', secret.metadata.name)
assert_equal('v1', secret.apiVersion)
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_create_secret_v1

client = Kubeclient::Client.new('http://localhost:8080/api/')
created_secret = client.create_secret(secret)
assert_instance_of(Kubeclient::Secret, created_secret)
assert_instance_of(Kubeclient::Resource, created_secret)
assert_equal(secret.metadata.name, created_secret.metadata.name)
assert_equal(secret.metadata.namespace, created_secret.metadata.namespace)
assert_equal(
Expand Down
4 changes: 2 additions & 2 deletions test/test_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_construct_our_own_service
client = Kubeclient::Client.new('http://localhost:8080/api/')
created = client.create_service(our_service)

assert_instance_of(Kubeclient::Service, created)
assert_instance_of(Kubeclient::Resource, created)
assert_equal(created.metadata.name, our_service.metadata.name)
assert_equal(created.spec.ports.size, our_service.spec.ports.size)

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_conversion_from_json_v1
client = Kubeclient::Client.new('http://localhost:8080/api/')
service = client.get_service('redis-slave', 'development')

assert_instance_of(Kubeclient::Service, service)
assert_instance_of(Kubeclient::Resource, service)
assert_equal('2015-04-05T13:00:31Z',
service.metadata.creationTimestamp)
assert_equal('bdb80a8f-db93-11e4-b293-f8b156af4ae1', service.metadata.uid)
Expand Down
Loading

0 comments on commit f3d9841

Please sign in to comment.