kubeclient 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kubeclient might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 969c44ae3642047678c13559de1790323feffa7e
4
- data.tar.gz: 5a19f3d5e8e0c8d764bc2746999f2a78d8fde8bb
3
+ metadata.gz: 606934d03c5c996609d2b20af68672b4d90df04b
4
+ data.tar.gz: c70040510e8b0745b427c07c64d172f130ae22fb
5
5
  SHA512:
6
- metadata.gz: da458497e1dd0590faea0ce5175c10670ba424b074444845f15bca7f122dd3fd460448ac1d385cb762906dc493297967281a9c3d1f432d5d98ffa27f7f0a2100
7
- data.tar.gz: 7fb264604c4ba1d5a6405ec51a6ea1eb2d9686bc08bf71cc2b7aa24eec5574d51eeb016bf9cace170e7a08d6a97ff1e6fbe9f47c32df9fefceeab20ec67db13c
6
+ metadata.gz: 5f45329a666f56582aab3c390a6f16fe45cc3a9cc973792b080fddfc2b6a08303ef65c324c19b571803f9b7357103ec2b27404be2a9a96b41ddea7ed75ac8eb3
7
+ data.tar.gz: c92d77763653279b5302432d74af067f58029138c7592492a2603ed6dc25920e0bf316533d7649a6f110ee250b0cc68f8f8e965a8a653e6c1ebc35a90f1f152b
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Dependency Status](https://gemnasium.com/abonas/kubeclient.svg)](https://gemnasium.com/abonas/kubeclient)
7
7
 
8
8
  A Ruby client for Kubernetes REST api.
9
- The client supports GET, POST, PUT, DELETE on nodes, pods, services, replication controllers and namespaces.
9
+ The client supports GET, POST, PUT, DELETE on nodes, pods, services, replication controllers, namespaces and endpoints.
10
10
  The client currently supports Kubernetes REST api version v1beta3.
11
11
 
12
12
  ## Installation
@@ -61,16 +61,20 @@ Examples:
61
61
  <br>
62
62
  `pods = client.get_pods`
63
63
  <br>
64
- You can get entities which have specific labels by specifying input parameter named `label-selector`: <br>
64
+ You can get entities which have specific labels by specifying a parameter named `label_selector` (named `labelSelector` in Kubernetes server): <br>
65
65
  `pods = client.get_pods(label_selector: 'name=redis-master')` <br>
66
- You can specify multiple labels and that returns entities which have both labels: <br>
66
+ You can specify multiple labels (that option will return entities which have both labels: <br>
67
67
  `pods = client.get_pods(label_selector: 'name=redis-master,app=redis')`
68
68
 
69
- 2. Get a specific node (and respectively: get_service "service id" , get_pod "pod id" , get_replication_controller "rc id" )
69
+ 2. Get a specific node (and respectively: get_service "service name" , get_pod "pod name" , get_replication_controller "rc name" )
70
70
  <br>
71
- `node1 = client.get_node "127.0.0.1"`
71
+ The GET request should include the namespace name, except for nodes and namespaces entities.
72
72
  <br>
73
- Note - Kubernetes doesn't work with the uid, but rather with the 'id' property.
73
+ `node = client.get_node "127.0.0.1"`
74
+ <br>
75
+ `service = client.get_service "guestbook", 'development'`
76
+ <br>
77
+ Note - Kubernetes doesn't work with the uid, but rather with the 'name' property.
74
78
  Querying with uid causes 404.
75
79
 
76
80
  3. Delete a service (and respectively delete_pod "pod id" , delete_replication_controller "rc id", delete node "node id") <br>
data/lib/kubeclient.rb CHANGED
@@ -41,7 +41,7 @@ module Kubeclient
41
41
 
42
42
  def api
43
43
  response = handle_exception do
44
- RestClient::Resource.new(@api_endpoint.to_s).get
44
+ create_rest_client.get
45
45
  end
46
46
  JSON.parse(response)
47
47
  end
@@ -8,10 +8,11 @@ module Kubeclient
8
8
  yield
9
9
  rescue RestClient::Exception => e
10
10
  begin
11
- err_message = JSON.parse(e.response)['message']
11
+ json_error_msg = JSON.parse(e.response || '') || {}
12
12
  rescue JSON::ParserError
13
- err_message = e.message
13
+ json_error_msg = {}
14
14
  end
15
+ err_message = json_error_msg['message'] || e.message
15
16
  raise KubeException.new(e.http_code, err_message)
16
17
  end
17
18
 
@@ -22,6 +23,10 @@ module Kubeclient
22
23
  if @api_endpoint.path.end_with? '/'
23
24
  end
24
25
 
26
+ def build_namespace_prefix(namespace)
27
+ namespace.to_s.empty? ? '' : "namespaces/#{namespace}/"
28
+ end
29
+
25
30
  public
26
31
 
27
32
  def self.define_entity_methods(entity_types)
@@ -41,8 +46,8 @@ module Kubeclient
41
46
  end
42
47
 
43
48
  # get a single entity of a specific type by name
44
- define_method("get_#{entity_name}") do |name|
45
- get_entity(entity_type, klass, name)
49
+ define_method("get_#{entity_name}") do |name, namespace = nil|
50
+ get_entity(entity_type, klass, name, namespace)
46
51
  end
47
52
 
48
53
  define_method("delete_#{entity_name}") do |name|
@@ -50,7 +55,7 @@ module Kubeclient
50
55
  end
51
56
 
52
57
  define_method("create_#{entity_name}") do |entity_config|
53
- create_entity(entity_type, entity_config)
58
+ create_entity(entity_type, entity_config, klass)
54
59
  end
55
60
 
56
61
  define_method("update_#{entity_name}") do |entity_config|
@@ -59,22 +64,25 @@ module Kubeclient
59
64
  end
60
65
  end
61
66
 
67
+ def create_rest_client(path = nil)
68
+ path ||= @api_endpoint.path
69
+ options = {
70
+ ssl_ca_file: @ssl_options[:ca_file],
71
+ verify_ssl: @ssl_options[:verify_ssl],
72
+ ssl_client_cert: @ssl_options[:client_cert],
73
+ ssl_client_key: @ssl_options[:client_key]
74
+ }
75
+ RestClient::Resource.new(@api_endpoint.merge(path).to_s, options)
76
+ end
77
+
62
78
  def rest_client
63
79
  @rest_client ||= begin
64
- options = {
65
- ssl_ca_file: @ssl_options[:ca_file],
66
- verify_ssl: @ssl_options[:verify_ssl],
67
- ssl_client_cert: @ssl_options[:client_cert],
68
- ssl_client_key: @ssl_options[:client_key]
69
- }
70
- endpoint_with_ver = @api_endpoint
71
- .merge("#{@api_endpoint.path}/#{@api_version}")
72
- RestClient::Resource.new(endpoint_with_ver, options)
80
+ create_rest_client("#{@api_endpoint.path}/#{@api_version}")
73
81
  end
74
82
  end
75
83
 
76
84
  def watch_entities(entity_type, resource_version = nil)
77
- resource = get_resource_name(entity_type.to_s)
85
+ resource = resource_name(entity_type.to_s)
78
86
 
79
87
  uri = @api_endpoint
80
88
  .merge("#{@api_endpoint.path}/#{@api_version}/watch/#{resource}")
@@ -99,12 +107,12 @@ module Kubeclient
99
107
  def get_entities(entity_type, klass, options)
100
108
  params = {}
101
109
  if options[:label_selector]
102
- params['label-selector'] = options[:label_selector]
110
+ params['labelSelector'] = options[:label_selector]
103
111
  end
104
112
 
105
113
  # TODO: namespace support?
106
114
  response = handle_exception do
107
- rest_client[get_resource_name(entity_type)].get(params: params)
115
+ rest_client[resource_name(entity_type)].get(params: params)
108
116
  end
109
117
 
110
118
  result = JSON.parse(response)
@@ -120,9 +128,10 @@ module Kubeclient
120
128
  EntityList.new(entity_type, resource_version, collection)
121
129
  end
122
130
 
123
- def get_entity(entity_type, klass, name)
131
+ def get_entity(entity_type, klass, name, namespace = nil)
132
+ ns_prefix = build_namespace_prefix(namespace)
124
133
  response = handle_exception do
125
- rest_client[get_resource_name(entity_type) + "/#{name}"].get
134
+ rest_client[ns_prefix + resource_name(entity_type) + "/#{name}"].get
126
135
  end
127
136
  result = JSON.parse(response)
128
137
  new_entity(result, klass)
@@ -130,22 +139,27 @@ module Kubeclient
130
139
 
131
140
  def delete_entity(entity_type, name)
132
141
  handle_exception do
133
- rest_client[get_resource_name(entity_type) + "/#{name}"].delete
142
+ rest_client[resource_name(entity_type) + "/#{name}"].delete
134
143
  end
135
144
  end
136
145
 
137
- def create_entity(entity_type, entity_config)
146
+ def create_entity(entity_type, entity_config, klass)
138
147
  # to_hash should be called because of issue #9 in recursive open
139
148
  # struct
140
149
  hash = entity_config.to_hash
150
+
151
+ ns_prefix = build_namespace_prefix(entity_config.metadata.namespace)
152
+
141
153
  # TODO: temporary solution to add "kind" and apiVersion to request
142
154
  # until this issue is solved
143
155
  # https://github.com/GoogleCloudPlatform/kubernetes/issues/6439
144
156
  hash['kind'] = entity_type
145
157
  hash['apiVersion'] = @api_version
146
- handle_exception do
147
- rest_client[get_resource_name(entity_type)].post(hash.to_json)
158
+ response = handle_exception do
159
+ rest_client[ns_prefix + resource_name(entity_type)].post(hash.to_json)
148
160
  end
161
+ result = JSON.parse(response)
162
+ new_entity(result, klass)
149
163
  end
150
164
 
151
165
  def update_entity(entity_type, entity_config)
@@ -153,11 +167,9 @@ module Kubeclient
153
167
  # to_hash should be called because of issue #9 in recursive open
154
168
  # struct
155
169
  hash = entity_config.to_hash
156
- # TODO: temporary solution to delete id till this issue is solved
157
- # https://github.com/GoogleCloudPlatform/kubernetes/issues/3085
158
- hash.delete(:id)
170
+ ns_prefix = build_namespace_prefix(entity_config.metadata.namespace)
159
171
  handle_exception do
160
- rest_client[get_resource_name(entity_type) + "/#{name}"]
172
+ rest_client[ns_prefix + resource_name(entity_type) + "/#{name}"]
161
173
  .put(hash.to_json)
162
174
  end
163
175
  end
@@ -176,7 +188,7 @@ module Kubeclient
176
188
  end
177
189
  end
178
190
 
179
- def get_resource_name(entity_type)
191
+ def resource_name(entity_type)
180
192
  if @api_version == 'v1beta1'
181
193
  entity_type.pluralize.camelize(:lower)
182
194
  else
@@ -1,4 +1,4 @@
1
1
  # Kubernetes REST-API Client
2
2
  module Kubeclient
3
- VERSION = '0.1.11'
3
+ VERSION = '0.1.12'
4
4
  end
@@ -0,0 +1,20 @@
1
+ {
2
+ "kind": "Namespace",
3
+ "apiVersion": "v1beta3",
4
+ "metadata": {
5
+ "name": "development",
6
+ "selfLink": "/api/v1beta3/namespaces/development",
7
+ "uid": "13d820d6-df5b-11e4-bd42-f8b156af4ae1",
8
+ "resourceVersion": "2533",
9
+ "creationTimestamp": "2015-04-10T08:24:59Z"
10
+ },
11
+ "spec": {
12
+ "finalizers": [
13
+ "kubernetes"
14
+ ]
15
+ },
16
+ "status": {
17
+ "phase": "Active"
18
+ }
19
+ }
20
+
@@ -0,0 +1,31 @@
1
+ {
2
+ "kind": "Service",
3
+ "apiVersion": "v1beta3",
4
+ "metadata": {
5
+ "name": "guestbook",
6
+ "namespace": "staging",
7
+ "selfLink": "/api/v1beta3/namespaces/staging/services/guestbook",
8
+ "uid": "29885239-df58-11e4-bd42-f8b156af4ae1",
9
+ "resourceVersion": "1908",
10
+ "creationTimestamp": "2015-04-10T08:04:07Z",
11
+ "labels": {
12
+ "name": "guestbook"
13
+ }
14
+ },
15
+ "spec": {
16
+ "ports": [
17
+ {
18
+ "name": "",
19
+ "protocol": "TCP",
20
+ "port": 3000,
21
+ "targetPort": "http-server"
22
+ }
23
+ ],
24
+ "selector": {
25
+ "name": "guestbook"
26
+ },
27
+ "portalIP": "10.0.0.99",
28
+ "sessionAffinity": "None"
29
+ },
30
+ "status": {}
31
+ }
@@ -48,12 +48,14 @@ class KubeClientTest < MiniTest::Test
48
48
  status: 409)
49
49
 
50
50
  service = Kubeclient::Service.new
51
- service.name = 'redisslave'
52
- service.port = 80
53
- service.container_port = 6379
54
- service.protocol = 'TCP'
51
+ service.metadata = {}
52
+ service.metadata.name = 'redisslave'
53
+ service.metadata.namespace = 'default'
54
+ # service.port = 80
55
+ # service.container_port = 6379
56
+ # service.protocol = 'TCP'
55
57
 
56
- client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1beta3'
58
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
57
59
 
58
60
  exception = assert_raises(KubeException) do
59
61
  service = client.create_service service
@@ -74,6 +76,18 @@ class KubeClientTest < MiniTest::Test
74
76
  assert_includes(response, 'versions')
75
77
  end
76
78
 
79
+ def test_api_ssl_failure
80
+ error_message = 'certificate verify failed'
81
+
82
+ stub_request(:get, 'http://localhost:8080/api')
83
+ .to_raise(OpenSSL::SSL::SSLError.new(error_message))
84
+
85
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
86
+
87
+ exception = assert_raises(KubeException) { client.api }
88
+ assert_equal(error_message, exception.message)
89
+ end
90
+
77
91
  def test_api_valid
78
92
  stub_request(:get, 'http://localhost:8080/api')
79
93
  .to_return(status: 200, body: open_test_json_file('versions_list.json'))
@@ -27,4 +27,19 @@ class TestNamespace < MiniTest::Test
27
27
  client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1beta3'
28
28
  client.delete_namespace our_namespace.name
29
29
  end
30
+
31
+ def test_create_namespace
32
+ stub_request(:post, %r{/namespaces})
33
+ .to_return(body: open_test_json_file('created_namespace_b3.json'),
34
+ status: 201)
35
+
36
+ namespace = Kubeclient::Namespace.new
37
+ namespace.metadata = {}
38
+ namespace.metadata.name = 'development'
39
+
40
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
41
+ created_namespace = client.create_namespace namespace
42
+ assert_instance_of(Kubeclient::Namespace, created_namespace)
43
+ assert_equal(namespace.metadata.name, created_namespace.metadata.name)
44
+ end
30
45
  end
data/test/test_service.rb CHANGED
@@ -4,18 +4,35 @@ require 'test_helper'
4
4
  class TestService < MiniTest::Test
5
5
  def test_construct_our_own_service
6
6
  our_service = Kubeclient::Service.new
7
- our_service.name = 'redis-service'
8
- # TODO, new ports assignment to be added
9
- our_service.labels = {}
10
- our_service.labels.component = 'apiserver'
11
- our_service.labels.provider = 'kubernetes'
7
+ our_service.metadata = {}
8
+ our_service.metadata.name = 'guestbook'
9
+ our_service.metadata.namespace = 'staging'
10
+ our_service.metadata.labels = {}
11
+ our_service.metadata.labels.name = 'guestbook'
12
+
13
+ our_service.spec = {}
14
+ our_service.spec.ports = [{ 'port' => 3000,
15
+ 'targetPort' => 'http-server',
16
+ 'protocol' => 'TCP'
17
+ }]
12
18
 
13
- assert_equal('kubernetes', our_service.labels.provider)
14
- assert_equal('apiserver', our_service.labels.component)
19
+ assert_equal('guestbook', our_service.metadata.labels.name)
15
20
 
16
21
  hash = our_service.to_h
17
22
 
18
- assert_equal our_service.labels.provider, hash[:labels][:provider]
23
+ assert_equal our_service.metadata.labels.name,
24
+ hash[:metadata][:labels][:name]
25
+
26
+ stub_request(:post, %r{namespaces/staging/services})
27
+ .to_return(body: open_test_json_file('created_service_b3.json'),
28
+ status: 201)
29
+
30
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
31
+ created = client.create_service our_service
32
+
33
+ assert_instance_of(Kubeclient::Service, created)
34
+ assert_equal(created.metadata.name, our_service.metadata.name)
35
+ assert_equal(created.spec.ports.size, our_service.spec.ports.size)
19
36
  end
20
37
 
21
38
  def test_conversion_from_json_v3
@@ -56,4 +73,28 @@ class TestService < MiniTest::Test
56
73
  client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1beta3'
57
74
  client.delete_service our_service.id
58
75
  end
76
+
77
+ def test_get_service_no_ns
78
+ # when not specifying namespace for entities which
79
+ # are not node or namespace, the request will fail
80
+ stub_request(:get, %r{/services/redis-slave})
81
+ .to_return(status: 404)
82
+
83
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
84
+
85
+ exception = assert_raises(KubeException) do
86
+ client.get_service 'redis-slave'
87
+ end
88
+ assert_equal(404, exception.error_code)
89
+ end
90
+
91
+ def test_get_service
92
+ stub_request(:get, %r{/namespaces/development/services/redis-slave})
93
+ .to_return(body: open_test_json_file('service_b3.json'),
94
+ status: 200)
95
+
96
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
97
+ service = client.get_service 'redis-slave', 'development'
98
+ assert_equal('redis-slave', service.metadata.name)
99
+ end
59
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubeclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alissa Bonas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -158,6 +158,8 @@ files:
158
158
  - lib/kubeclient/version.rb
159
159
  - lib/kubeclient/watch_notice.rb
160
160
  - lib/kubeclient/watch_stream.rb
161
+ - test/json/created_namespace_b3.json
162
+ - test/json/created_service_b3.json
161
163
  - test/json/empty_pod_list_b3.json
162
164
  - test/json/endpoint_list_b3.json
163
165
  - test/json/entity_list_b3.json
@@ -209,6 +211,8 @@ signing_key:
209
211
  specification_version: 4
210
212
  summary: A client for Kubernetes REST api
211
213
  test_files:
214
+ - test/json/created_namespace_b3.json
215
+ - test/json/created_service_b3.json
212
216
  - test/json/empty_pod_list_b3.json
213
217
  - test/json/endpoint_list_b3.json
214
218
  - test/json/entity_list_b3.json