fog-kubevirt 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fog/compute/kubevirt.rb +64 -20
  3. data/lib/fog/compute/kubevirt/models/networkattachmentdef.rb +25 -0
  4. data/lib/fog/compute/kubevirt/models/networkattachmentdefs.rb +59 -0
  5. data/lib/fog/compute/kubevirt/models/pvc.rb +59 -0
  6. data/lib/fog/compute/kubevirt/models/pvcs.rb +76 -0
  7. data/lib/fog/compute/kubevirt/models/server.rb +3 -1
  8. data/lib/fog/compute/kubevirt/models/vm_data.rb +5 -4
  9. data/lib/fog/compute/kubevirt/models/vms.rb +44 -7
  10. data/lib/fog/compute/kubevirt/models/volume.rb +73 -0
  11. data/lib/fog/compute/kubevirt/models/volumes.rb +61 -0
  12. data/lib/fog/compute/kubevirt/requests/create_networkattachmentdef.rb +37 -0
  13. data/lib/fog/compute/kubevirt/requests/create_vm.rb +1 -0
  14. data/lib/fog/compute/kubevirt/requests/create_volume.rb +19 -0
  15. data/lib/fog/compute/kubevirt/requests/delete_networkattachmentdef.rb +16 -0
  16. data/lib/fog/compute/kubevirt/requests/delete_pvc.rb +16 -0
  17. data/lib/fog/compute/kubevirt/requests/delete_volume.rb +16 -0
  18. data/lib/fog/compute/kubevirt/requests/get_networkattachmentdef.rb +19 -0
  19. data/lib/fog/compute/kubevirt/requests/get_pvc.rb +16 -0
  20. data/lib/fog/compute/kubevirt/requests/get_vminstance.rb +4 -4
  21. data/lib/fog/compute/kubevirt/requests/get_volume.rb +16 -0
  22. data/lib/fog/compute/kubevirt/requests/list_networkattachmentdefs.rb +21 -0
  23. data/lib/fog/compute/kubevirt/requests/list_pvcs.rb +22 -0
  24. data/lib/fog/compute/kubevirt/requests/list_vms.rb +4 -4
  25. data/lib/fog/compute/kubevirt/requests/list_volumes.rb +22 -0
  26. data/lib/fog/kubevirt/version.rb +1 -1
  27. data/spec/compute_v1alpha2_spec.rb +1 -1
  28. data/spec/fixtures/kubevirt/networkattachmentdefinition/networkattachmentdefinitions_crud.yml +320 -0
  29. data/spec/fixtures/kubevirt/pvc/pvcs_crud.yml +316 -0
  30. data/spec/fixtures/kubevirt/service/services_crud.yml +316 -0
  31. data/spec/fixtures/kubevirt/volume/volumes_crud.yml +316 -0
  32. data/spec/network_attachment_definition_v1alpha2_spec.rb +28 -0
  33. data/spec/pvcs_v1alpha2_spec.rb +52 -0
  34. data/spec/shared_context.rb +2 -2
  35. data/spec/volumes_v1alpha2_spec.rb +45 -0
  36. metadata +33 -20
  37. data/spec/fixtures/kubevirt/service/v1alpha2/services_crud.yml +0 -172
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ce96c8011542dbc39b76e8754b57bd708a1c1e6
4
- data.tar.gz: 0cda9190f6c0bbb60d5acf1b47b487b7ae273741
3
+ metadata.gz: 1de135b0b0723b41c540c9bc1247b67d1f8176ef
4
+ data.tar.gz: 8845f174d0923e0224e0683c710d0a5219608c7b
5
5
  SHA512:
6
- metadata.gz: c8ab23a99601911709e4d24495da5b00da048d7a61dff8287a79dff42850ba4f6b2fb853590f77125bfedba4860bb6e3aef8de9dd53bd799b5f126020bca697f
7
- data.tar.gz: f0fd54f70d37c1fb7669a2101e5c8ecc08e2d35be275977acba208efdd442b9dc6cabae66ebd67a4acf519941e3fec9c328e27fd5fbe82d431633e61e8d4555c
6
+ metadata.gz: ea3d49cf4931797629028d01cea8332d78f57c90b130e8d362e0bd1002d19e539ceee45280f2a6d16c2da45fa11e40da14da12cdb31a9bc2da2af6038bd3560f
7
+ data.tar.gz: 06ad5911dc8b3c7f110f33992ee61e19403f4643b0d97607000dff270e20036ad6b2414f54b72a545f606c927d15c93237d25915aebcdf176fb5327c13549218
@@ -1,4 +1,6 @@
1
1
  require 'delegate'
2
+ require 'json'
3
+ require 'rest-client'
2
4
 
3
5
  require "fog/core"
4
6
 
@@ -12,10 +14,14 @@ module Fog
12
14
  model_path 'fog/compute/kubevirt/models'
13
15
  model :vminstance
14
16
  collection :vminstances
17
+ model :networkattachmentdef
18
+ collection :networkattachmentdefs
15
19
  model :node
16
20
  collection :nodes
17
21
  model :vm
18
22
  collection :vms
23
+ model :pvc
24
+ collection :pvcs
19
25
  model :server
20
26
  collection :servers
21
27
  model :service
@@ -26,23 +32,33 @@ module Fog
26
32
  collection :volumes
27
33
 
28
34
  request_path 'fog/compute/kubevirt/requests'
29
-
35
+ request :create_networkattachmentdef
30
36
  request :create_vm
31
37
  request :create_vminstance
32
38
  request :create_pvc
33
39
  request :create_service
40
+ request :create_volume
41
+ request :delete_networkattachmentdef
42
+ request :delete_pvc
34
43
  request :delete_service
35
44
  request :delete_vminstance
36
45
  request :delete_vm
46
+ request :delete_volume
37
47
  request :get_vminstance
48
+ request :get_networkattachmentdef
38
49
  request :get_node
50
+ request :get_pvc
39
51
  request :get_vm
52
+ request :get_volume
40
53
  request :get_server
41
54
  request :get_service
42
55
  request :get_template
43
56
  request :list_vminstances
44
57
  request :list_nodes
58
+ request :list_networkattachmentdefs
45
59
  request :list_vms
60
+ request :list_pvcs
61
+ request :list_volumes
46
62
  request :list_servers
47
63
  request :list_services
48
64
  request :list_templates
@@ -61,8 +77,9 @@ module Fog
61
77
  end
62
78
 
63
79
  class ExceptionWrapper
64
- def initialize(client)
80
+ def initialize(client, version)
65
81
  @client = client
82
+ @version = version
66
83
  end
67
84
 
68
85
  def method_missing(symbol, *args)
@@ -82,6 +99,10 @@ module Fog
82
99
  def respond_to_missing?(method_name, include_private = false)
83
100
  @client.respond_to?(symbol, include_all) || super
84
101
  end
102
+
103
+ def version
104
+ @version
105
+ end
85
106
  end
86
107
 
87
108
  #
@@ -133,17 +154,19 @@ module Fog
133
154
  include Shared
134
155
 
135
156
  #
136
- # The API version and group of KubeVirt:
157
+ # The API group of KubeVirt:
137
158
  #
138
159
  KUBEVIRT_GROUP = 'kubevirt.io'.freeze
139
- KUBEVIRT_VERSION = 'v1alpha2'.freeze
140
- KUBEVIRT_VERSION_LABEL = KUBEVIRT_GROUP + '/' + KUBEVIRT_VERSION
141
160
 
142
161
  #
143
- # The API version and group of the Kubernetes core:
162
+ # The API group of the Kubernetes core:
144
163
  #
145
164
  CORE_GROUP = ''.freeze
146
- CORE_VERSION = 'v1'.freeze
165
+
166
+ #
167
+ # The API group of the Kubernetes network extention:
168
+ #
169
+ NETWORK_GROUP = 'k8s.cni.cncf.io'.freeze
147
170
 
148
171
  def initialize(options={})
149
172
  require 'kubeclient'
@@ -302,7 +325,6 @@ module Fog
302
325
 
303
326
  private
304
327
 
305
-
306
328
  #
307
329
  # Populates required notice attributes
308
330
  #
@@ -316,18 +338,12 @@ module Fog
316
338
  end
317
339
 
318
340
  #
319
- # Lazily creates the a client for the given Kubernetes API path and version.
341
+ # Lazily creates the a client for the given Kubernetes API path.
320
342
  #
321
343
  # @param path [String] The Kubernetes API path.
322
- # @param version [String] The Kubernetes API version.
323
344
  # @return [Kubeclient::Client] The client for the given path and version.
324
345
  #
325
- def create_client(path, version)
326
- # Return the client immediately if it has been created before:
327
- key = path + '/' + version
328
- client = @clients[key]
329
- return client if client
330
-
346
+ def create_client(path)
331
347
  # Create the client and save it:
332
348
  url = URI::Generic.build(
333
349
  :scheme => 'https',
@@ -335,28 +351,56 @@ module Fog
335
351
  :port => @port,
336
352
  :path => path
337
353
  )
354
+
355
+ version = detect_version(url.to_s, @opts[:ssl_options])
356
+ key = path + '/' + version
357
+ client = @clients[key]
358
+ return client if client
359
+
338
360
  client = Kubeclient::Client.new(
339
361
  url.to_s,
340
362
  version,
341
363
  @opts
342
364
  )
343
- wrapped_client = ExceptionWrapper.new(client)
365
+ wrapped_client = ExceptionWrapper.new(client, version)
344
366
  @clients[key] = wrapped_client
345
367
 
346
368
  # Return the client:
347
369
  wrapped_client
348
370
  end
349
371
 
372
+ def detect_version(url, ssl_options)
373
+ options = {
374
+ ssl_ca_file: ssl_options[:ca_file],
375
+ ssl_cert_store: ssl_options[:cert_store],
376
+ verify_ssl: ssl_options[:verify_ssl],
377
+ ssl_client_cert: ssl_options[:client_cert],
378
+ ssl_client_key: ssl_options[:client_key],
379
+ }
380
+
381
+ response = ::JSON.parse(RestClient::Resource.new(url, options).get)
382
+
383
+ # version detected based on
384
+ # https://github.com/kubernetes-incubator/apiserver-builder/blob/master/docs/concepts/aggregation.md#viewing-discovery-information
385
+ preferredVersion = response["preferredVersion"]
386
+ return preferredVersion["version"] if preferredVersion
387
+ response["versions"][0]
388
+ end
389
+
350
390
  def openshift_client
351
- create_client('/oapi', CORE_VERSION)
391
+ create_client('/oapi')
352
392
  end
353
393
 
354
394
  def kube_client
355
- create_client('/api', CORE_VERSION)
395
+ create_client('/api')
356
396
  end
357
397
 
358
398
  def kubevirt_client
359
- create_client('/apis/' + KUBEVIRT_GROUP, KUBEVIRT_VERSION)
399
+ create_client('/apis/' + KUBEVIRT_GROUP)
400
+ end
401
+
402
+ def kube_net_client
403
+ create_client('/apis/' + NETWORK_GROUP)
360
404
  end
361
405
 
362
406
  def log
@@ -0,0 +1,25 @@
1
+ module Fog
2
+ module Compute
3
+ class Kubevirt
4
+ class Networkattachmentdef < Fog::Model
5
+ identity :name
6
+
7
+ attribute :namespace, :aliases => 'metadata_namespace'
8
+ attribute :resource_version, :aliases => 'metadata_resource_version'
9
+ attribute :uid, :aliases => 'metadata_uid'
10
+ attribute :config, :aliases => 'spec_config'
11
+
12
+ def self.parse(object)
13
+ metadata = object[:metadata]
14
+ {
15
+ :namespace => metadata[:namespace],
16
+ :name => metadata[:name],
17
+ :resource_version => metadata[:resourceVersion],
18
+ :uid => metadata[:uid],
19
+ :config => object[:spec][:config]
20
+ }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,59 @@
1
+ require 'fog/core/collection'
2
+ require 'fog/compute/kubevirt/models/networkattachmentdef'
3
+
4
+ module Fog
5
+ module Compute
6
+ class Kubevirt
7
+ class Networkattachmentdefs < Fog::Collection
8
+ attr_reader :kind, :resource_version
9
+
10
+ model Fog::Compute::Kubevirt::Networkattachmentdef
11
+
12
+ def all(filters = {})
13
+ networkattachmentdefs = service.list_networkattachmentdefs(filters)
14
+ @kind = networkattachmentdefs.kind
15
+ @resource_version = networkattachmentdefs.resource_version
16
+ load networkattachmentdefs
17
+ end
18
+
19
+ def get(name)
20
+ new service.get_networkattachmentdef(name)
21
+ end
22
+
23
+ # Creates network attachment definition using provided parameters:
24
+ # name[String] - name of the network attachment
25
+ # config[String] - the configuration of the attachment, i.e.:
26
+ # '{ :cniVersion => "0.3.1", :type => "ovs", :bridge => "red" }'
27
+ #
28
+ # @param args[Hash] contains attachment definition
29
+ def create(args = {})
30
+ name = args[:name]
31
+ config = args[:config]
32
+
33
+ net_att_def = {
34
+ :metadata => {
35
+ :name => name,
36
+ :namespace => service.namespace
37
+ },
38
+ :spec => {
39
+ :config => config
40
+ }
41
+ }
42
+
43
+ service.create_networkattachmentdef(net_att_def)
44
+ end
45
+
46
+ def delete(name)
47
+ begin
48
+ net_att_def = get(name)
49
+ rescue ::Fog::Kubevirt::Errors::ClientError
50
+ # the network attachment definition doesn't exist
51
+ net_att_def = nil
52
+ end
53
+
54
+ service.delete_networkattachmentdef(name, service.namespace) unless net_att_def.nil?
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,59 @@
1
+ module Fog
2
+ module Compute
3
+ class Kubevirt
4
+ class Pvc < Fog::Model
5
+ # Metadata
6
+ identity :name
7
+ attribute :resource_version, :aliases => 'metadata_resource_version'
8
+ attribute :uid, :aliases => 'metadata_uid'
9
+ attribute :namespace, :aliases => 'metadata_namespace'
10
+
11
+ # Spec
12
+ attribute :access_modes, :aliases => 'spec_access_modes'
13
+ attribute :storage_class, :aliases => 'spec_storage_class'
14
+ attribute :volume_mode, :aliases => 'spec_volume_mode'
15
+ attribute :volume_name, :aliases => 'spec_volume_name'
16
+
17
+ # Spec - selector
18
+ attribute :match_expressions, :aliases => 'spec_match_expressions'
19
+ attribute :match_labels, :aliases => 'spec_match_labels'
20
+
21
+ # Spec - resources
22
+ attribute :limits, :aliases => 'spec_limits'
23
+ attribute :requests, :aliases => 'spec_requests'
24
+
25
+ # Status
26
+ attribute :phase, :aliases => 'status_phase'
27
+ attribute :reason, :aliases => 'status_reason'
28
+ attribute :message, :aliases => 'status_message'
29
+
30
+ def self.parse(object)
31
+ metadata = object[:metadata]
32
+ spec = object[:spec]
33
+ status = object[:status]
34
+
35
+ {
36
+ :name => metadata[:name],
37
+ :resource_version => metadata[:resourceVersion],
38
+ :uid => metadata[:uid],
39
+ :namespace => metadata[:namespace],
40
+
41
+ :access_modes => spec[:accessModes],
42
+ :storage_class => spec[:storageClassName],
43
+ :volume_mode => spec[:volumeMode],
44
+ :volume_name => spec[:volumeName],
45
+
46
+ :match_expressions => spec.dig(:selector, :matchExpressions),
47
+ :match_labels => spec.dig(:selector, :matchLabels),
48
+ :limits => spec.dig(:resources, :limits),
49
+ :requests => spec.dig(:resources, :requests),
50
+
51
+ :phase => status[:phase],
52
+ :reason => status[:reason],
53
+ :message => status[:message]
54
+ }
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,76 @@
1
+ require 'fog/core/collection'
2
+ require 'fog/compute/kubevirt/models/pvc'
3
+
4
+ module Fog
5
+ module Compute
6
+ class Kubevirt
7
+ class Pvcs < Fog::Collection
8
+ attr_reader :kind, :resource_version
9
+
10
+ model Fog::Compute::Kubevirt::Pvc
11
+
12
+ def all(filters = {})
13
+ pvcs = service.list_pvcs(filters)
14
+ @kind = pvcs.kind
15
+ @resource_version = pvcs.resource_version
16
+ load pvcs
17
+ end
18
+
19
+ def get(name)
20
+ new service.get_pvc(name)
21
+ end
22
+
23
+ # Creates a pvc using provided paramters:
24
+ # https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims
25
+ # :name [String] - name of a pvc
26
+ # :namespace [String] - the namespace of the PVC, should be the same as POD's pvc
27
+ # :storage_class [String] - the storage class name of the pvc
28
+ # :volume_name [String] - The volume name to
29
+ # :volume_mode [String] - Filesystem or block device
30
+ # :accessModes [Arr] - the access modes for the volume, values are specified here:
31
+ # :requests [Hash] - the request for storage resource, i.e. { :storage => "20Gi" }
32
+ # :limits [Hash] - the maximum amount of storage to consume, i.e. { :storage => "20Gi" }
33
+ # :match_labels [Hash] - label query over volumes for binding
34
+ # :match_expressions [Hash] - list of label selector requirements
35
+ # @param [Hash] attributes containing details about pvc to be created.
36
+ def create(args = {})
37
+ name = args[:name]
38
+
39
+ pvc = {
40
+ :apiVersion => "v1",
41
+ :kind => "PersistentVolumeClaim",
42
+ :metadata => {
43
+ :name => name,
44
+ :namespace => args[:namespace]
45
+ },
46
+ :spec => {
47
+ :storageClassName => args[:storage_class],
48
+ :resources => {},
49
+ :selector => {},
50
+ }
51
+ }
52
+
53
+ pvc[:spec][:resources].merge!(:requests => args[:requests]) if args[:requests]
54
+ pvc[:spec][:resources].merge!(:limits => args[:limits]) if args[:limits]
55
+ pvc[:spec][:selector].merge!(:matchLabels => args[:match_labels]) if args[:match_labels]
56
+ pvc[:spec][:selector].merge!(:matchExpressions => args[:match_expressions]) if args[:match_expressions]
57
+ pvc[:spec][:accessModes] = args[:access_modes] if args[:access_modes]
58
+ pvc[:spec][:volumeMode] = args[:volume_mode] if args[:volume_mode]
59
+ pvc[:spec][:volumeName] = args[:volume_name] if args[:volume_name]
60
+ service.create_pvc(pvc)
61
+ end
62
+
63
+ def delete(name)
64
+ begin
65
+ pvc = get(name)
66
+ rescue ::Fog::Kubevirt::Errors::ClientError
67
+ # the pvc doesn't exist
68
+ pvc = nil
69
+ end
70
+
71
+ service.delete_pvc(name) unless pvc.nil?
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -19,8 +19,10 @@ module Fog
19
19
  service.delete_vm(name, namespace)
20
20
  end
21
21
 
22
+ # TODO: Once IP Addresses are reported to any networks, we should consider also
23
+ # the availabity of it (by extending the condition with !ip_address.empty?)
22
24
  def ready?
23
- running?(status) && running?(state) && !ip_address.empty?
25
+ running?(status) && running?(state)
24
26
  end
25
27
 
26
28
  def self.parse(object)
@@ -89,9 +89,9 @@ module Fog
89
89
  object.each do |v|
90
90
  volume = VmVolume.new
91
91
  volume.name = v[:name]
92
- if v.keys.include?(:registryDisk)
93
- volume.type = 'registryDisk'
94
- volume.info = v.dig(:registryDisk, :image)
92
+ if v.keys.include?(:containerDisk)
93
+ volume.type = 'containerDisk'
94
+ volume.info = v.dig(:containerDisk, :image)
95
95
  elsif v.keys.include?(:persistentVolumeClaim)
96
96
  volume.type = 'persistentVolumeClaim'
97
97
  volume.info = v.dig(:persistentVolumeClaim, :claimName)
@@ -133,6 +133,7 @@ module Fog
133
133
  :model,
134
134
  :ports,
135
135
  :boot_order
136
+ alias :mac :mac_address
136
137
  end
137
138
 
138
139
  class VmNetwork
@@ -151,7 +152,7 @@ module Fog
151
152
 
152
153
  class VmVolume
153
154
  attr_accessor :name,
154
- # values: registryDisk, persistentVolumeClaim, emptyDisk,
155
+ # values: containerDisk, persistentVolumeClaim, emptyDisk,
155
156
  # ephemeral, cloudInitNoCloud, hostDisk, secret,
156
157
  # dataVolume, serviceAccount, configMap
157
158
  :type,