Class: Kubeclient::Common::Client
- Inherits:
-
Object
- Object
- Kubeclient::Common::Client
- Defined in:
- lib/kubeclient/common.rb
Overview
Common methods
Class Method Summary collapse
Instance Method Summary collapse
- #create_entity(entity_type, entity_config) ⇒ Object
- #delete_entity(entity_type, name) ⇒ Object
- #get_entities(entity_type, klass, options) ⇒ Object
- #get_entity(entity_type, klass, name) ⇒ Object
- #get_resource_name(entity_type) ⇒ Object
- #handle_exception ⇒ Object
- #handle_uri(uri, path) ⇒ Object
- #new_entity(hash, klass) ⇒ Object
- #rest_client ⇒ Object
- #retrieve_all_entities(entity_types) ⇒ Object
- #ssl_options(client_cert: nil, client_key: nil, ca_file: nil, verify_ssl: OpenSSL::SSL::VERIFY_PEER) ⇒ Object
- #update_entity(entity_type, entity_config) ⇒ Object
- #watch_entities(entity_type, resource_version = nil) ⇒ Object
Class Method Details
.define_entity_methods(entity_types) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/kubeclient/common.rb', line 27 def self.define_entity_methods(entity_types) entity_types.each do |klass, entity_type| entity_name = entity_type.underscore entity_name_plural = entity_name.pluralize # get all entities of a type e.g. get_nodes, get_pods, etc. define_method("get_#{entity_name_plural}") do | = {}| get_entities(entity_type, klass, ) end # watch all entities of a type e.g. watch_nodes, watch_pods, etc. define_method("watch_#{entity_name_plural}") \ do |resource_version = nil| watch_entities(entity_type, resource_version) end # get a single entity of a specific type by name define_method("get_#{entity_name}") do |name| get_entity(entity_type, klass, name) end define_method("delete_#{entity_name}") do |name| delete_entity(entity_type, name) end define_method("create_#{entity_name}") do |entity_config| create_entity(entity_type, entity_config) end define_method("update_#{entity_name}") do |entity_config| update_entity(entity_type, entity_config) end end end |
Instance Method Details
#create_entity(entity_type, entity_config) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/kubeclient/common.rb', line 137 def create_entity(entity_type, entity_config) # to_hash should be called because of issue #9 in recursive open # struct hash = entity_config.to_hash handle_exception do rest_client[get_resource_name(entity_type)].post(hash.to_json) end end |
#delete_entity(entity_type, name) ⇒ Object
131 132 133 134 135 |
# File 'lib/kubeclient/common.rb', line 131 def delete_entity(entity_type, name) handle_exception do rest_client[get_resource_name(entity_type) + "/#{name}"].delete end end |
#get_entities(entity_type, klass, options) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kubeclient/common.rb', line 99 def get_entities(entity_type, klass, ) params = {} if [:label_selector] params['label-selector'] = [:label_selector] end # TODO: namespace support? response = handle_exception do rest_client[get_resource_name(entity_type)].get(params: params) end result = JSON.parse(response) resource_version = result.fetch('resourceVersion', nil) if resource_version.nil? resource_version = result.fetch('metadata', {}).fetch('resourceVersion', nil) end collection = result['items'].map { |item| new_entity(item, klass) } EntityList.new(entity_type, resource_version, collection) end |
#get_entity(entity_type, klass, name) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/kubeclient/common.rb', line 123 def get_entity(entity_type, klass, name) response = handle_exception do rest_client[get_resource_name(entity_type) + "/#{name}"].get end result = JSON.parse(response) new_entity(result, klass) end |
#get_resource_name(entity_type) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/kubeclient/common.rb', line 174 def get_resource_name(entity_type) if @api_version == 'v1beta1' entity_type.pluralize.camelize(:lower) else entity_type.pluralize.downcase end end |
#handle_exception ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kubeclient/common.rb', line 7 def handle_exception yield rescue RestClient::Exception => e begin = JSON.parse(e.response)['message'] rescue JSON::ParserError = e. end raise KubeException.new(e.http_code, ) end |
#handle_uri(uri, path) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/kubeclient/common.rb', line 18 def handle_uri(uri, path) @api_endpoint = (uri.is_a? URI) ? uri : URI.parse(uri) @api_endpoint.path = path if @api_endpoint.path.empty? @api_endpoint.path = @api_endpoint.path.chop \ if @api_endpoint.path.end_with? '/' end |
#new_entity(hash, klass) ⇒ Object
160 161 162 |
# File 'lib/kubeclient/common.rb', line 160 def new_entity(hash, klass) klass.new(hash) end |
#rest_client ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/kubeclient/common.rb', line 62 def rest_client @rest_client ||= begin = { ssl_ca_file: @ssl_options[:ca_file], verify_ssl: @ssl_options[:verify_ssl], ssl_client_cert: @ssl_options[:client_cert], ssl_client_key: @ssl_options[:client_key] } endpoint_with_ver = @api_endpoint .merge("#{@api_endpoint.path}/#{@api_version}") RestClient::Resource.new(endpoint_with_ver, ) end end |
#retrieve_all_entities(entity_types) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/kubeclient/common.rb', line 164 def retrieve_all_entities(entity_types) entity_types.each_with_object({}) do |(_, entity_type), result_hash| # method call for get each entities # build hash of entity name to array of the entities method_name = "get_#{entity_type.underscore.pluralize}" key_name = entity_type.underscore result_hash[key_name] = send(method_name) end end |
#ssl_options(client_cert: nil, client_key: nil, ca_file: nil, verify_ssl: OpenSSL::SSL::VERIFY_PEER) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/kubeclient/common.rb', line 182 def (client_cert: nil, client_key: nil, ca_file: nil, verify_ssl: OpenSSL::SSL::VERIFY_PEER) @ssl_options = { ca_file: ca_file, verify_ssl: verify_ssl, client_cert: client_cert, client_key: client_key } end |
#update_entity(entity_type, entity_config) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/kubeclient/common.rb', line 146 def update_entity(entity_type, entity_config) name = entity_config.name # to_hash should be called because of issue #9 in recursive open # struct hash = entity_config.to_hash # TODO: temporary solution to delete id till this issue is solved # https://github.com/GoogleCloudPlatform/kubernetes/issues/3085 hash.delete(:id) handle_exception do rest_client[get_resource_name(entity_type) + "/#{name}"] .put(hash.to_json) end end |
#watch_entities(entity_type, resource_version = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kubeclient/common.rb', line 76 def watch_entities(entity_type, resource_version = nil) resource = get_resource_name(entity_type.to_s) uri = @api_endpoint .merge("#{@api_endpoint.path}/#{@api_version}/watch/#{resource}") unless resource_version.nil? uri.query = URI.encode_www_form('resourceVersion' => resource_version) end = { use_ssl: uri.scheme == 'https', ca_file: @ssl_options[:ca_file], # ruby Net::HTTP uses verify_mode instead of verify_ssl # http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html verify_mode: @ssl_options[:verify_ssl], client_cert: @ssl_options[:client_cert], client_key: @ssl_options[:client_key] } WatchStream.new(uri, ) end |