class Kubernetes::Client
Constants
- VERSION
Attributes
apis[RW]
resources[RW]
rest_client[RW]
Public Class Methods
new( options = {} )
click to toggle source
# File lib/kubernetes/client/class.rb, line 9 def initialize( options = {} ) @options = { url: nil, verify_ssl: nil, ssl_client_cert: nil, ssl_client_key: nil, ssl_ca_file: nil, ssl_ca_path: nil, ssl_cert_store: nil, ssl_verify_callback: nil, ssl_verify_callback_warnings: nil, ssl_version: nil, ssl_ciphers: nil, headers: {}, user: nil, password: nil, token: nil, namespace: nil, discovery: true } @options.merge!(options) @options[:headers]['Accept'] = 'application/json' @options[:headers]['Authorization'] = "Bearer #{@options[:token]}" unless options[:token].nil? @rest_client = RestClient::Resource.new(@options[:url], @options) @apis = [] @resources = [] discover if @options[:discovery] == true end
new_in()
click to toggle source
# File lib/kubernetes/client/class.rb, line 38 def self.new_in self.class.new( url: 'https://kubernetes.default.svc', verify_ssl: OpenSSL::SSL::VERIFY_PEER, ssl_ca_file: '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt', token: File.read('/var/run/secrets/kubernetes.io/serviceaccount/token'), namespace: File.read('/var/run/secrets/kubernetes.io/serviceaccount/namespace') ) end
Public Instance Methods
create(kind, options = {name: nil, namespace: nil, definition: {} })
click to toggle source
# File lib/kubernetes/client/class.rb, line 65 def create(kind, options = {name: nil, namespace: nil, definition: {} }) formatter @rest_client[respath(kind, options)].post( options[:definition].to_json ).body end
delete(kind, options = {name: nil, namespace: nil})
click to toggle source
# File lib/kubernetes/client/class.rb, line 71 def delete(kind, options = {name: nil, namespace: nil}) formatter @rest_client[respath(kind, options)].delete.body end
discover(nocache: false)
click to toggle source
# File lib/kubernetes/client/class.rb, line 123 def discover(nocache: false) @apis = [] @resources = {} uri = URI(@options[:url]) cachefile = File.join(File.expand_path('~'),".kubernetes-client.#{uri.host}.#{uri.port}.cache") if File.readable?(cachefile) && ( nocache == false ) @apis, @resources = JSON.load(File.read(cachefile)) @resources.each do |name, value| @resources[name] = { api: value['api'], name: value['name'] } end return end # Load api versions formatter(@rest_client['/api'].get.body)['versions'].each do |version| @apis << "/api/#{version}" end # Load apis versions formatter(@rest_client['/apis'].get.body)['groups'].each do |group| @apis << "/apis/#{group['preferredVersion']['groupVersion']}" end # Load Resources @apis.each do |api| formatter(@rest_client[api].get.body)['resources'].each do |resource| @resources[resource['name']] = { api: api, name: resource['name'] } @resources[resource['kind']] = { api: api, name: resource['name'] } @resources[resource['singularName']] = { api: api, name: resource['name'] } \ unless resource['singularName'].nil? @resources[resource['singularName'].to_s.downcase] = \ { api: api, name: resource['name'] } \ unless resource['singularName'].nil? if resource['shortNames'] resource['shortNames'].each do |shortname| @resources[shortname] = { api: api, name: resource['name'] } end end end end File.write(cachefile, JSON.dump([@apis,@resources])) end
formatter(s)
click to toggle source
# File lib/kubernetes/client/class.rb, line 48 def formatter(s) return JSON.load(s) end
get(kind, options = {name: nil, namespace: nil})
click to toggle source
# File lib/kubernetes/client/class.rb, line 61 def get(kind, options = {name: nil, namespace: nil}) formatter @rest_client[respath(kind, options)].get.body end
method_missing(m, *args, &block)
click to toggle source
# File lib/kubernetes/client/class.rb, line 52 def method_missing(m, *args, &block) return get(m.to_s.sub('get_',''), *args, &block) if m.to_s.start_with?('get_') return create(m.to_s.sub('create_',''), *args, &block) if m.to_s.start_with?('create_') return delete(m.to_s.sub('delete_',''), *args, &block) if m.to_s.start_with?('delete_') return replace(m.to_s.sub('replace_',''), *args, &block) if m.to_s.start_with?('replace_') return patch(m.to_s.sub('patch_',''), *args, &block) if m.to_s.start_with?('patch_') return watch(m.to_s.sub('watch_',''), *args, &block) if m.to_s.start_with?('watch_') end
patch(kind, options = {name: nil, namespace: nil, definition: {} })
click to toggle source
# File lib/kubernetes/client/class.rb, line 81 def patch(kind, options = {name: nil, namespace: nil, definition: {} }) formatter @rest_client[respath(kind, options)].patch( options[:definition].to_json ).body end
replace(kind, options = {name: nil, namespace: nil, definition: {} })
click to toggle source
# File lib/kubernetes/client/class.rb, line 75 def replace(kind, options = {name: nil, namespace: nil, definition: {} }) formatter @rest_client[respath(kind, options)].put( options[:definition].to_json ).body end
respath(kind, options = {name: nil, namespace: nil, watch: false})
click to toggle source
# File lib/kubernetes/client/class.rb, line 110 def respath(kind, options = {name: nil, namespace: nil, watch: false}) raise "No such resource in api catalog, maybe run discovery" unless @resources.member?(kind) resource = @resources[kind] path = resource[:api] path += '/watch' if options[:watch] path += "/namespaces/" + options[:namespace] unless options[:namespace].nil? path += "/" + resource[:name] path += "/" + options[:name] unless options[:name].nil? return path end
watch(kind, options = {name: nil, namespace: nil, watch: true}) { |formatter(chomp)| ... }
click to toggle source
# File lib/kubernetes/client/class.rb, line 87 def watch(kind, options = {name: nil, namespace: nil, watch: true}) block = Proc.new do |response| buffer = '' response.read_body do |chunk| buffer += chunk if buffer.end_with?("\n") buffer.each_line do |line| yield formatter(line.chomp) end buffer = '' end end end req_options = @options.dup req_options.merge!({ method: :get, url: @options[:url] + respath(kind, options), block_response: block, read_timeout: nil }) RestClient::Request.execute(req_options) end