fog-kubevirt 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fog/compute/kubevirt.rb +60 -14
- data/lib/fog/compute/kubevirt/models/server.rb +42 -0
- data/lib/fog/compute/kubevirt/models/servers.rb +32 -0
- data/lib/fog/compute/kubevirt/models/service.rb +36 -0
- data/lib/fog/compute/kubevirt/models/services.rb +83 -0
- data/lib/fog/compute/kubevirt/models/vm.rb +6 -53
- data/lib/fog/compute/kubevirt/models/vm_base.rb +83 -0
- data/lib/fog/compute/kubevirt/models/vm_data.rb +163 -0
- data/lib/fog/compute/kubevirt/models/vminstance.rb +10 -2
- data/lib/fog/compute/kubevirt/models/vms.rb +110 -0
- data/lib/fog/compute/kubevirt/requests/create_service.rb +18 -0
- data/lib/fog/compute/kubevirt/requests/delete_service.rb +16 -0
- data/lib/fog/compute/kubevirt/requests/get_server.rb +32 -0
- data/lib/fog/compute/kubevirt/requests/get_service.rb +16 -0
- data/lib/fog/compute/kubevirt/requests/get_vm.rb +1 -1
- data/lib/fog/compute/kubevirt/requests/list_servers.rb +25 -0
- data/lib/fog/compute/kubevirt/requests/list_services.rb +22 -0
- data/lib/fog/kubevirt.rb +1 -0
- data/lib/fog/kubevirt/version.rb +1 -1
- data/spec/compute_v1alpha2_spec.rb +30 -0
- data/spec/fixtures/kubevirt/service/v1alpha2/services_crud.yml +172 -0
- data/spec/shared_context.rb +54 -0
- data/spec/spec_helper.rb +10 -0
- metadata +67 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0eae64c2d198ea65729e7f1c66986096a49381fa
|
4
|
+
data.tar.gz: e92414ce86e90a85c3db8ae77a900217e710bad3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54daf2d0f13edd1f6493d97df1a00ddcd18d4327ddce709c112c7dba80cab7daf3e5cab4f83225038792969dc08e7b9de083c9d5723d5f8d36376736dd7aca5d
|
7
|
+
data.tar.gz: e25fa5d0391f3ca87fa2104ba2f4970db66b2d0c2d82043fbfb0c453a8d3c5440569e464f96a22bd06e7e1753195cbb186363cfa356dd86c3a3da2dc76ddd65c
|
data/lib/fog/compute/kubevirt.rb
CHANGED
@@ -6,7 +6,8 @@ module Fog
|
|
6
6
|
module Compute
|
7
7
|
class Kubevirt < Fog::Service
|
8
8
|
requires :kubevirt_token
|
9
|
-
recognizes :kubevirt_hostname, :kubevirt_port, :kubevirt_namespace,
|
9
|
+
recognizes :kubevirt_hostname, :kubevirt_port, :kubevirt_namespace, \
|
10
|
+
:kubevirt_log, :kubevirt_verify_ssl, :kubevirt_ca_cert
|
10
11
|
|
11
12
|
model_path 'fog/compute/kubevirt/models'
|
12
13
|
model :vminstance
|
@@ -15,6 +16,10 @@ module Fog
|
|
15
16
|
collection :nodes
|
16
17
|
model :vm
|
17
18
|
collection :vms
|
19
|
+
model :server
|
20
|
+
collection :servers
|
21
|
+
model :service
|
22
|
+
collection :services
|
18
23
|
model :template
|
19
24
|
collection :templates
|
20
25
|
model :volume
|
@@ -25,15 +30,21 @@ module Fog
|
|
25
30
|
request :create_vm
|
26
31
|
request :create_vminstance
|
27
32
|
request :create_pvc
|
33
|
+
request :create_service
|
34
|
+
request :delete_service
|
28
35
|
request :delete_vminstance
|
29
36
|
request :delete_vm
|
30
37
|
request :get_vminstance
|
31
38
|
request :get_node
|
32
39
|
request :get_vm
|
40
|
+
request :get_server
|
41
|
+
request :get_service
|
33
42
|
request :get_template
|
34
43
|
request :list_vminstances
|
35
44
|
request :list_nodes
|
36
45
|
request :list_vms
|
46
|
+
request :list_servers
|
47
|
+
request :list_services
|
37
48
|
request :list_templates
|
38
49
|
request :update_vm
|
39
50
|
|
@@ -121,17 +132,18 @@ module Fog
|
|
121
132
|
require "ostruct"
|
122
133
|
include Shared
|
123
134
|
|
124
|
-
#
|
125
|
-
# The API version and group of the Kubernetes core:
|
126
|
-
#
|
127
|
-
CORE_GROUP = ''.freeze
|
128
|
-
CORE_VERSION = 'v1'.freeze
|
129
|
-
|
130
135
|
#
|
131
136
|
# The API version and group of KubeVirt:
|
132
137
|
#
|
133
138
|
KUBEVIRT_GROUP = 'kubevirt.io'.freeze
|
134
139
|
KUBEVIRT_VERSION = 'v1alpha2'.freeze
|
140
|
+
KUBEVIRT_VERSION_LABEL = KUBEVIRT_GROUP + '/' + KUBEVIRT_VERSION
|
141
|
+
|
142
|
+
#
|
143
|
+
# The API version and group of the Kubernetes core:
|
144
|
+
#
|
145
|
+
CORE_GROUP = ''.freeze
|
146
|
+
CORE_VERSION = 'v1'.freeze
|
135
147
|
|
136
148
|
def initialize(options={})
|
137
149
|
require 'kubeclient'
|
@@ -144,13 +156,8 @@ module Fog
|
|
144
156
|
@log ||= ::Logger.new(STDOUT)
|
145
157
|
|
146
158
|
@namespace = options[:kubevirt_namespace] || 'default'
|
147
|
-
|
148
|
-
# Prepare the TLS and authentication options that will be used for the standard Kubernetes API
|
149
|
-
# and also for the KubeVirt extension:
|
150
159
|
@opts = {
|
151
|
-
:ssl_options =>
|
152
|
-
:verify_ssl => OpenSSL::SSL::VERIFY_NONE,
|
153
|
-
},
|
160
|
+
:ssl_options => obtain_ssl_options(options),
|
154
161
|
:auth_options => {
|
155
162
|
:bearer_token => @kubevirt_token
|
156
163
|
}
|
@@ -289,6 +296,10 @@ module Fog
|
|
289
296
|
url.to_s
|
290
297
|
end
|
291
298
|
|
299
|
+
def namespace
|
300
|
+
@namespace
|
301
|
+
end
|
302
|
+
|
292
303
|
private
|
293
304
|
|
294
305
|
|
@@ -351,6 +362,41 @@ module Fog
|
|
351
362
|
def log
|
352
363
|
@log
|
353
364
|
end
|
365
|
+
|
366
|
+
#
|
367
|
+
# Prepare the TLS and authentication options that will be used for the
|
368
|
+
# standard Kubernetes API and also for the KubeVirt extension
|
369
|
+
#
|
370
|
+
# @param options [Hash] a hash with connection options
|
371
|
+
#
|
372
|
+
def obtain_ssl_options(options)
|
373
|
+
verify_ssl = options[:kubevirt_verify_ssl]
|
374
|
+
if verify_ssl == true
|
375
|
+
ca = options[:kubevirt_ca_cert] || ""
|
376
|
+
ca = IO.read(ca) if File.file?(ca)
|
377
|
+
certs = ca.split(/(?=-----BEGIN)/).reject(&:empty?).collect do |pem|
|
378
|
+
OpenSSL::X509::Certificate.new(pem)
|
379
|
+
end
|
380
|
+
|
381
|
+
cert_store = OpenSSL::X509::Store.new
|
382
|
+
certs.each do |cert|
|
383
|
+
cert_store.add_cert(cert)
|
384
|
+
end
|
385
|
+
|
386
|
+
ssl_options = {
|
387
|
+
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
|
388
|
+
:cert_store => cert_store
|
389
|
+
}
|
390
|
+
elsif verify_ssl == false || verify_ssl.to_s.empty?
|
391
|
+
ssl_options = {
|
392
|
+
:verify_ssl => OpenSSL::SSL::VERIFY_NONE
|
393
|
+
}
|
394
|
+
else
|
395
|
+
ssl_options = {
|
396
|
+
:verify_ssl => verify_ssl
|
397
|
+
}
|
398
|
+
end
|
399
|
+
end
|
354
400
|
end
|
355
401
|
|
356
402
|
class Mock
|
@@ -368,5 +414,5 @@ module Fog
|
|
368
414
|
# TODO provide mocking
|
369
415
|
end
|
370
416
|
end
|
371
|
-
end
|
417
|
+
end
|
372
418
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'fog/compute/models/server'
|
2
|
+
require 'fog/compute/kubevirt/models/vm_base'
|
3
|
+
|
4
|
+
module Fog
|
5
|
+
module Compute
|
6
|
+
class Kubevirt
|
7
|
+
class Server < Fog::Compute::Server
|
8
|
+
include Shared
|
9
|
+
include VmAction
|
10
|
+
extend VmBase
|
11
|
+
define_properties
|
12
|
+
|
13
|
+
attribute :state, :aliases => 'phase'
|
14
|
+
attribute :ip_address
|
15
|
+
attribute :node_name
|
16
|
+
|
17
|
+
def destroy(options = {})
|
18
|
+
stop(options)
|
19
|
+
service.delete_vm(name, namespace)
|
20
|
+
end
|
21
|
+
|
22
|
+
def ready?
|
23
|
+
running?(status) && running?(state) && !ip_address.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.parse(object)
|
27
|
+
server = parse_object(object)
|
28
|
+
server[:status] = object[:phase]
|
29
|
+
server[:node_name] = object[:node_name]
|
30
|
+
server[:ip_address] = object[:ip_address]
|
31
|
+
server
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def running?(status)
|
37
|
+
!status.nil? && 'running'.casecmp(status).zero?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'fog/core/collection'
|
2
|
+
require 'fog/compute/kubevirt/models/server'
|
3
|
+
|
4
|
+
module Fog
|
5
|
+
module Compute
|
6
|
+
class Kubevirt
|
7
|
+
class Servers < Fog::Collection
|
8
|
+
attr_reader :kind, :resource_version
|
9
|
+
|
10
|
+
model Fog::Compute::Kubevirt::Server
|
11
|
+
|
12
|
+
def all(filters = {})
|
13
|
+
servers = service.list_servers(filters)
|
14
|
+
@kind = servers.kind
|
15
|
+
@resource_version = servers.resource_version
|
16
|
+
load servers
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(id)
|
20
|
+
new service.get_server(id)
|
21
|
+
end
|
22
|
+
|
23
|
+
def bootstrap(new_attributes = {})
|
24
|
+
server = create(new_attributes)
|
25
|
+
server.wait_for { stopped? }
|
26
|
+
server.start
|
27
|
+
server
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Fog
|
2
|
+
module Compute
|
3
|
+
class Kubevirt
|
4
|
+
class Service < Fog::Model
|
5
|
+
identity :name
|
6
|
+
|
7
|
+
attribute :namespace, :aliases => 'metadata_namespace'
|
8
|
+
attribute :resource_version, :aliases => 'metadata_resource_version'
|
9
|
+
attribute :cluster_ip, :aliases => 'spec_cluster_ip'
|
10
|
+
attribute :node_port, :aliases => 'spec_ports_node_port'
|
11
|
+
attribute :port, :aliases => 'spec_ports_port'
|
12
|
+
attribute :target_port, :aliases => 'spec_ports_target_port'
|
13
|
+
attribute :selector, :aliases => 'spec_selector'
|
14
|
+
|
15
|
+
def self.parse(object)
|
16
|
+
metadata = object[:metadata]
|
17
|
+
spec = object[:spec]
|
18
|
+
ports = spec[:ports][0]
|
19
|
+
selector = spec[:selector]
|
20
|
+
srv = {
|
21
|
+
:namespace => metadata[:namespace],
|
22
|
+
:name => metadata[:name],
|
23
|
+
:resource_version => metadata[:resourceVersion],
|
24
|
+
:cluster_ip => spec[:clusterIP],
|
25
|
+
:node_port => ports[:nodePort],
|
26
|
+
:port => ports[:port],
|
27
|
+
:target_port => ports[:target_port],
|
28
|
+
}
|
29
|
+
srv[:selector] = selector[:special] unless selector.nil?
|
30
|
+
|
31
|
+
srv
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'fog/core/collection'
|
2
|
+
require 'fog/compute/kubevirt/models/service'
|
3
|
+
|
4
|
+
module Fog
|
5
|
+
module Compute
|
6
|
+
class Kubevirt
|
7
|
+
class Services < Fog::Collection
|
8
|
+
attr_reader :kind, :resource_version
|
9
|
+
|
10
|
+
model Fog::Compute::Kubevirt::Service
|
11
|
+
|
12
|
+
def all(filters = {})
|
13
|
+
begin
|
14
|
+
srvs = service.list_services(filters)
|
15
|
+
rescue ::Fog::Kubevirt::Errors::ClientError
|
16
|
+
# we assume that we get 404
|
17
|
+
srvs = []
|
18
|
+
end
|
19
|
+
@kind = srvs.kind
|
20
|
+
@resource_version = srvs.resource_version
|
21
|
+
load srvs
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(name)
|
25
|
+
new service.get_service(name)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Creates a service using provided paramters:
|
29
|
+
# :name [String] - name of a service
|
30
|
+
# :port [int] - a port which will be exposed on node and cluster
|
31
|
+
# :target_port [int] - a vmi port which will be forwarded
|
32
|
+
# :vmi_name [String] - name of a vmi to be selected
|
33
|
+
# :service_type [String] - service type used to create service
|
34
|
+
#
|
35
|
+
# @param [Hash] attributes containing details about service to be
|
36
|
+
# created.
|
37
|
+
def create(args = {})
|
38
|
+
port = args[:port]
|
39
|
+
name = args[:name]
|
40
|
+
target_port = args[:target_port]
|
41
|
+
vmi_name = args[:vmi_name]
|
42
|
+
service_type = args[:service_type]
|
43
|
+
|
44
|
+
srv = {
|
45
|
+
:apiVersion => "v1",
|
46
|
+
:kind => "Service",
|
47
|
+
:metadata => {
|
48
|
+
:name => name,
|
49
|
+
:namespace => service.namespace
|
50
|
+
},
|
51
|
+
:spec => {
|
52
|
+
:externalTrafficPolicy => "Cluster",
|
53
|
+
:ports => [
|
54
|
+
{:nodePort => port,
|
55
|
+
:port => port,
|
56
|
+
:protocol => "TCP",
|
57
|
+
:targetPort => target_port
|
58
|
+
}
|
59
|
+
],
|
60
|
+
:selector => {
|
61
|
+
:"kubevirt.io/vm" => vmi_name
|
62
|
+
},
|
63
|
+
:type => service_type
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
service.create_service(srv)
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete(name)
|
71
|
+
begin
|
72
|
+
srv = get(name)
|
73
|
+
rescue ::Fog::Kubevirt::Errors::ClientError
|
74
|
+
# the service doesn't exist
|
75
|
+
srv = nil
|
76
|
+
end
|
77
|
+
|
78
|
+
service.delete_service(name, service.namespace) unless srv.nil?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -1,62 +1,15 @@
|
|
1
|
+
require 'fog/compute/kubevirt/models/vm_base'
|
2
|
+
|
1
3
|
module Fog
|
2
4
|
module Compute
|
3
5
|
class Kubevirt
|
4
6
|
class Vm < Fog::Model
|
5
|
-
include
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attribute :namespace, :aliases => 'metadata_namespace'
|
10
|
-
attribute :resource_version, :aliases => 'metadata_resource_version'
|
11
|
-
attribute :uid, :aliases => 'metadata_uid'
|
12
|
-
attribute :labels, :aliases => 'metadata_labels'
|
13
|
-
attribute :owner_reference, :aliases => 'metadata_owner_reference'
|
14
|
-
attribute :annotations, :aliases => 'metadata_annotations'
|
15
|
-
attribute :cpu_cores, :aliases => 'spec_cpu_cores'
|
16
|
-
attribute :memory, :aliases => 'spec_memory'
|
17
|
-
attribute :disks, :aliases => 'spec_disks'
|
18
|
-
attribute :volumes, :aliases => 'spec_volumes'
|
19
|
-
|
20
|
-
|
21
|
-
def start(options = {})
|
22
|
-
# Change the `running` attribute to `true` so that the virtual machine controller will take it and
|
23
|
-
# create the virtual machine instance.
|
24
|
-
vm = service.get_raw_vm(name)
|
25
|
-
vm = deep_merge!(vm,
|
26
|
-
:spec => {
|
27
|
-
:running => true
|
28
|
-
}
|
29
|
-
)
|
30
|
-
service.update_vm(vm)
|
31
|
-
end
|
32
|
-
|
33
|
-
def stop(options = {})
|
34
|
-
vm = service.get_raw_vm(name)
|
35
|
-
vm = deep_merge!(vm,
|
36
|
-
:spec => {
|
37
|
-
:running => false
|
38
|
-
}
|
39
|
-
)
|
40
|
-
service.update_vm(vm)
|
41
|
-
end
|
7
|
+
include VmAction
|
8
|
+
extend VmBase
|
9
|
+
define_properties
|
42
10
|
|
43
11
|
def self.parse(object)
|
44
|
-
|
45
|
-
spec = object[:spec][:template][:spec]
|
46
|
-
domain = spec[:domain]
|
47
|
-
{
|
48
|
-
:namespace => metadata[:namespace],
|
49
|
-
:name => metadata[:name],
|
50
|
-
:resource_version => metadata[:resourceVersion],
|
51
|
-
:uid => metadata[:uid],
|
52
|
-
:labels => metadata[:labels],
|
53
|
-
:annotations => metadata[:annotations],
|
54
|
-
:owner_reference => metadata[:ownerReferences],
|
55
|
-
:cpu_cores => domain[:cpu][:cores],
|
56
|
-
:memory => domain[:resources][:requests][:memory],
|
57
|
-
:disks => domain[:devices][:disks],
|
58
|
-
:volumes => spec[:volumes]
|
59
|
-
}
|
12
|
+
parse_object(object)
|
60
13
|
end
|
61
14
|
end
|
62
15
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'fog/compute/kubevirt/models/vm_data'
|
2
|
+
|
3
|
+
module Fog
|
4
|
+
module Compute
|
5
|
+
class Kubevirt
|
6
|
+
module VmBase
|
7
|
+
include VmData
|
8
|
+
|
9
|
+
def define_properties
|
10
|
+
identity :name
|
11
|
+
|
12
|
+
attribute :namespace, :aliases => 'metadata_namespace'
|
13
|
+
attribute :resource_version, :aliases => 'metadata_resource_version'
|
14
|
+
attribute :uid, :aliases => 'metadata_uid'
|
15
|
+
attribute :labels, :aliases => 'metadata_labels'
|
16
|
+
attribute :owner_reference, :aliases => 'metadata_owner_reference'
|
17
|
+
attribute :annotations, :aliases => 'metadata_annotations'
|
18
|
+
attribute :cpu_cores, :aliases => 'spec_cpu_cores'
|
19
|
+
attribute :memory, :aliases => 'spec_memory'
|
20
|
+
attribute :disks, :aliases => 'spec_disks'
|
21
|
+
attribute :volumes, :aliases => 'spec_volumes'
|
22
|
+
attribute :status, :aliases => 'spec_running'
|
23
|
+
attribute :interfaces, :aliases => 'spec_interfaces'
|
24
|
+
attribute :networks, :aliases => 'spec_networks'
|
25
|
+
attribute :machine_type, :aliases => 'spec_machine_type'
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_object(object)
|
29
|
+
metadata = object[:metadata]
|
30
|
+
spec = object[:spec][:template][:spec]
|
31
|
+
domain = spec[:domain]
|
32
|
+
owner = metadata[:ownerReferences]
|
33
|
+
annotations = metadata[:annotations]
|
34
|
+
cpu = domain[:cpu]
|
35
|
+
mem = domain.dig(:resources, :requests, :memory)
|
36
|
+
vm = {
|
37
|
+
:namespace => metadata[:namespace],
|
38
|
+
:name => metadata[:name],
|
39
|
+
:resource_version => metadata[:resourceVersion],
|
40
|
+
:uid => metadata[:uid],
|
41
|
+
:labels => metadata[:labels],
|
42
|
+
:disks => parse_disks(domain[:devices][:disks]),
|
43
|
+
:volumes => parse_volumes(spec[:volumes]),
|
44
|
+
:interfaces => parse_interfaces(domain[:devices][:interfaces]),
|
45
|
+
:networks => parse_networks(spec[:networks]),
|
46
|
+
:state => object[:spec][:running].to_s == "true" ? "running" : "stopped",
|
47
|
+
:machine_type => domain.dig(:machine, :type)
|
48
|
+
}
|
49
|
+
vm[:owner_reference] = owner unless owner.nil?
|
50
|
+
vm[:annotations] = annotations unless annotations.nil?
|
51
|
+
vm[:cpu_cores] = cpu[:cores] unless cpu.nil?
|
52
|
+
vm[:memory] = mem unless mem.nil?
|
53
|
+
|
54
|
+
vm
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module VmAction
|
59
|
+
def start(options = {})
|
60
|
+
# Change the `running` attribute to `true` so that the virtual machine controller will take it and
|
61
|
+
# create the virtual machine instance.
|
62
|
+
vm = service.get_raw_vm(name)
|
63
|
+
vm = deep_merge!(vm,
|
64
|
+
:spec => {
|
65
|
+
:running => true
|
66
|
+
}
|
67
|
+
)
|
68
|
+
service.update_vm(vm)
|
69
|
+
end
|
70
|
+
|
71
|
+
def stop(options = {})
|
72
|
+
vm = service.get_raw_vm(name)
|
73
|
+
vm = deep_merge!(vm,
|
74
|
+
:spec => {
|
75
|
+
:running => false
|
76
|
+
}
|
77
|
+
)
|
78
|
+
service.update_vm(vm)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|