Class: Kubeclient::Common::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kubeclient/common.rb

Overview

Common methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_entity_methods(entity_types) ⇒ Object



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
61
62
63
64
65
# File 'lib/kubeclient/common.rb', line 32

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 |options = {}|
      get_entities(entity_type, klass, options)
    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, namespace = nil|
      get_entity(entity_type, klass, name, namespace)
    end

    define_method("delete_#{entity_name}") do |name, namespace = nil|
      delete_entity(entity_type, name, namespace)
    end

    define_method("create_#{entity_name}") do |entity_config|
      create_entity(entity_type, entity_config, klass)
    end

    define_method("update_#{entity_name}") do |entity_config|
      update_entity(entity_type, entity_config)
    end
  end
end

Instance Method Details

#build_namespace_prefix(namespace) ⇒ Object



26
27
28
# File 'lib/kubeclient/common.rb', line 26

def build_namespace_prefix(namespace)
  namespace.to_s.empty? ? '' : "namespaces/#{namespace}/"
end

#create_entity(entity_type, entity_config, klass) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/kubeclient/common.rb', line 148

def create_entity(entity_type, entity_config, klass)
  # to_hash should be called because of issue #9 in recursive open
  # struct
  hash = entity_config.to_hash

  ns_prefix = build_namespace_prefix(entity_config..namespace)

  # TODO: temporary solution to add "kind" and apiVersion to request
  # until this issue is solved
  # https://github.com/GoogleCloudPlatform/kubernetes/issues/6439
  hash['kind'] = entity_type
  hash['apiVersion'] = @api_version
  response = handle_exception do
    rest_client[ns_prefix + resource_name(entity_type)].post(hash.to_json)
  end
  result = JSON.parse(response)
  new_entity(result, klass)
end

#create_rest_client(path = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/kubeclient/common.rb', line 67

def create_rest_client(path = nil)
  path ||= @api_endpoint.path
  options = {
    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]
  }
  RestClient::Resource.new(@api_endpoint.merge(path).to_s, options)
end

#delete_entity(entity_type, name, namespace = nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/kubeclient/common.rb', line 140

def delete_entity(entity_type, name, namespace = nil)
  ns_prefix = build_namespace_prefix(namespace)
  handle_exception do
    rest_client[ns_prefix + resource_name(entity_type) + "/#{name}"]
      .delete
  end
end

#get_entities(entity_type, klass, options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kubeclient/common.rb', line 107

def get_entities(entity_type, klass, options)
  params = {}
  if options[:label_selector]
    params['labelSelector'] = options[:label_selector]
  end

  # TODO: namespace support?
  response = handle_exception do
    rest_client[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, namespace = nil) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/kubeclient/common.rb', line 131

def get_entity(entity_type, klass, name, namespace = nil)
  ns_prefix = build_namespace_prefix(namespace)
  response = handle_exception do
    rest_client[ns_prefix + resource_name(entity_type) + "/#{name}"].get
  end
  result = JSON.parse(response)
  new_entity(result, klass)
end

#handle_exceptionObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kubeclient/common.rb', line 7

def handle_exception
  yield
rescue RestClient::Exception => e
  begin
    json_error_msg = JSON.parse(e.response || '') || {}
  rescue JSON::ParserError
    json_error_msg = {}
  end
  err_message = json_error_msg['message'] || e.message
  raise KubeException.new(e.http_code, err_message)
end

#handle_uri(uri, path) ⇒ Object



19
20
21
22
23
24
# File 'lib/kubeclient/common.rb', line 19

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



179
180
181
# File 'lib/kubeclient/common.rb', line 179

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

#resource_name(entity_type) ⇒ Object



193
194
195
# File 'lib/kubeclient/common.rb', line 193

def resource_name(entity_type)
  entity_type.pluralize.downcase
end

#rest_clientObject



78
79
80
81
82
# File 'lib/kubeclient/common.rb', line 78

def rest_client
  @rest_client ||= begin
    create_rest_client("#{@api_endpoint.path}/#{@api_version}")
  end
end

#retrieve_all_entities(entity_types) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/kubeclient/common.rb', line 183

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



197
198
199
200
201
202
203
204
205
# File 'lib/kubeclient/common.rb', line 197

def ssl_options(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



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kubeclient/common.rb', line 167

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
  ns_prefix = build_namespace_prefix(entity_config..namespace)
  handle_exception do
    rest_client[ns_prefix + resource_name(entity_type) + "/#{name}"]
      .put(hash.to_json)
  end
end

#watch_entities(entity_type, resource_version = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kubeclient/common.rb', line 84

def watch_entities(entity_type, resource_version = nil)
  resource = 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

  options = {
    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, options)
end