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 +4 -4
- data/README.md +10 -6
- data/lib/kubeclient.rb +1 -1
- data/lib/kubeclient/common.rb +40 -28
- data/lib/kubeclient/version.rb +1 -1
- data/test/json/created_namespace_b3.json +20 -0
- data/test/json/created_service_b3.json +31 -0
- data/test/test_kubeclient.rb +19 -5
- data/test/test_namespace.rb +15 -0
- data/test/test_service.rb +49 -8
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 606934d03c5c996609d2b20af68672b4d90df04b
|
4
|
+
data.tar.gz: c70040510e8b0745b427c07c64d172f130ae22fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
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
|
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
|
-
|
71
|
+
The GET request should include the namespace name, except for nodes and namespaces entities.
|
72
72
|
<br>
|
73
|
-
|
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
data/lib/kubeclient/common.rb
CHANGED
@@ -8,10 +8,11 @@ module Kubeclient
|
|
8
8
|
yield
|
9
9
|
rescue RestClient::Exception => e
|
10
10
|
begin
|
11
|
-
|
11
|
+
json_error_msg = JSON.parse(e.response || '') || {}
|
12
12
|
rescue JSON::ParserError
|
13
|
-
|
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
|
-
|
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 =
|
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['
|
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[
|
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[
|
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[
|
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[
|
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
|
-
|
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[
|
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
|
191
|
+
def resource_name(entity_type)
|
180
192
|
if @api_version == 'v1beta1'
|
181
193
|
entity_type.pluralize.camelize(:lower)
|
182
194
|
else
|
data/lib/kubeclient/version.rb
CHANGED
@@ -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
|
+
}
|
data/test/test_kubeclient.rb
CHANGED
@@ -48,12 +48,14 @@ class KubeClientTest < MiniTest::Test
|
|
48
48
|
status: 409)
|
49
49
|
|
50
50
|
service = Kubeclient::Service.new
|
51
|
-
service.
|
52
|
-
service.
|
53
|
-
service.
|
54
|
-
service.
|
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/'
|
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'))
|
data/test/test_namespace.rb
CHANGED
@@ -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.
|
8
|
-
|
9
|
-
our_service.
|
10
|
-
our_service.labels
|
11
|
-
our_service.labels.
|
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('
|
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.
|
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.
|
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-
|
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
|