fog-kubevirt 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fog/kubevirt/compute/compute.rb +125 -37
  3. data/lib/fog/kubevirt/compute/models/networkattachmentdef.rb +25 -0
  4. data/lib/fog/kubevirt/compute/models/networkattachmentdefs.rb +59 -0
  5. data/lib/fog/kubevirt/compute/models/pvc.rb +59 -0
  6. data/lib/fog/kubevirt/compute/models/pvcs.rb +76 -0
  7. data/lib/fog/kubevirt/compute/models/server.rb +42 -0
  8. data/lib/fog/kubevirt/compute/models/servers.rb +32 -0
  9. data/lib/fog/kubevirt/compute/models/service.rb +36 -0
  10. data/lib/fog/kubevirt/compute/models/services.rb +83 -0
  11. data/lib/fog/kubevirt/compute/models/vm.rb +6 -60
  12. data/lib/fog/kubevirt/compute/models/vm_base.rb +75 -0
  13. data/lib/fog/kubevirt/compute/models/vm_data.rb +164 -0
  14. data/lib/fog/kubevirt/compute/models/vms.rb +59 -19
  15. data/lib/fog/kubevirt/compute/models/volume.rb +73 -0
  16. data/lib/fog/kubevirt/compute/models/volumes.rb +61 -0
  17. data/lib/fog/kubevirt/compute/requests/create_networkattachmentdef.rb +37 -0
  18. data/lib/fog/kubevirt/compute/requests/create_service.rb +18 -0
  19. data/lib/fog/kubevirt/compute/requests/create_vm.rb +2 -0
  20. data/lib/fog/kubevirt/compute/requests/create_volume.rb +19 -0
  21. data/lib/fog/kubevirt/compute/requests/delete_networkattachmentdef.rb +16 -0
  22. data/lib/fog/kubevirt/compute/requests/delete_pvc.rb +16 -0
  23. data/lib/fog/kubevirt/compute/requests/delete_service.rb +16 -0
  24. data/lib/fog/kubevirt/compute/requests/delete_volume.rb +16 -0
  25. data/lib/fog/kubevirt/compute/requests/get_networkattachmentdef.rb +19 -0
  26. data/lib/fog/kubevirt/compute/requests/get_pvc.rb +16 -0
  27. data/lib/fog/kubevirt/compute/requests/get_server.rb +32 -0
  28. data/lib/fog/kubevirt/compute/requests/get_service.rb +16 -0
  29. data/lib/fog/kubevirt/compute/requests/get_vm.rb +1 -1
  30. data/lib/fog/kubevirt/compute/requests/get_vminstance.rb +4 -4
  31. data/lib/fog/kubevirt/compute/requests/get_volume.rb +16 -0
  32. data/lib/fog/kubevirt/compute/requests/list_networkattachmentdefs.rb +21 -0
  33. data/lib/fog/kubevirt/compute/requests/list_pvcs.rb +22 -0
  34. data/lib/fog/kubevirt/compute/requests/list_servers.rb +25 -0
  35. data/lib/fog/kubevirt/compute/requests/list_services.rb +22 -0
  36. data/lib/fog/kubevirt/compute/requests/list_vms.rb +4 -4
  37. data/lib/fog/kubevirt/compute/requests/list_volumes.rb +22 -0
  38. data/lib/fog/kubevirt/version.rb +1 -1
  39. data/spec/compute_v1alpha2_spec.rb +30 -0
  40. data/spec/fixtures/kubevirt/networkattachmentdefinition/networkattachmentdefinitions_crud.yml +320 -0
  41. data/spec/fixtures/kubevirt/pvc/pvcs_crud.yml +316 -0
  42. data/spec/fixtures/kubevirt/service/services_crud.yml +316 -0
  43. data/spec/fixtures/kubevirt/volume/volumes_crud.yml +316 -0
  44. data/spec/network_attachment_definition_v1alpha2_spec.rb +28 -0
  45. data/spec/pvcs_v1alpha2_spec.rb +52 -0
  46. data/spec/shared_context.rb +53 -0
  47. data/spec/spec_helper.rb +10 -0
  48. data/spec/volumes_v1alpha2_spec.rb +45 -0
  49. metadata +87 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef12aa5c8c8f76da4468c2addf70d01f798fca2e
4
- data.tar.gz: 59e0ce857ec78e72f2f47fce4b59b4789aee9bfe
3
+ metadata.gz: 3d98797096ddd6d2f4464998bcdbf56dcac2e9ab
4
+ data.tar.gz: eea11774097b4c70fe95ecbbef47847c454fa548
5
5
  SHA512:
6
- metadata.gz: b715da932cbe7359039d796391e3fa1b149c7abafbd918d1c828bd761e026c3d5049d672fb11f0ff822c9faa86240b47e1536c7aa664c16d87df3be618be5482
7
- data.tar.gz: f1d024c7a07089912a574cc0336475818ea71c284ff814a25bcc9edf4cf7047f0df2a114d54b82aa09af990b575a9aa73423a28defaeda70f5f24327e06b8719
6
+ metadata.gz: c2ea3b5710d1783c0e4710e625d2a8a8271afe322e196a52d59b27164dcf7f76a770fd95f5b77857a5447a6162a60bc96fbad78415d46c3511e005ce65a11aa0
7
+ data.tar.gz: 57e59e0eeb42b6542735181069436c234ca1c236fe179b44c8a45eebab6b44e682c247b7d917eacc079aa43dce2b4c18285ae54094c338d775203a8fab747451
@@ -1,39 +1,64 @@
1
1
  require 'delegate'
2
+ require 'json'
3
+ require 'rest-client'
2
4
 
3
5
  require "fog/core"
4
6
 
5
7
  module Fog
6
8
  module Kubevirt
7
9
  class Compute < Fog::Service
8
- requires :kubevirt_token
9
- recognizes :kubevirt_hostname, :kubevirt_port, :kubevirt_namespace, :kubevirt_log
10
+ recognizes :kubevirt_token, :kubevirt_hostname, :kubevirt_port, :kubevirt_namespace, :kubevirt_log
10
11
 
11
12
  model_path 'fog/kubevirt/compute/models'
12
13
  model :vminstance
13
14
  collection :vminstances
15
+ model :networkattachmentdef
16
+ collection :networkattachmentdefs
14
17
  model :node
15
18
  collection :nodes
16
19
  model :vm
17
20
  collection :vms
21
+ model :pvc
22
+ collection :pvcs
23
+ model :server
24
+ collection :servers
25
+ model :service
26
+ collection :services
18
27
  model :template
19
28
  collection :templates
20
29
  model :volume
21
30
  collection :volumes
22
31
 
23
32
  request_path 'fog/kubevirt/compute/requests'
24
-
33
+ request :create_networkattachmentdef
25
34
  request :create_vm
26
35
  request :create_vminstance
27
36
  request :create_pvc
37
+ request :create_service
38
+ request :create_volume
39
+ request :delete_networkattachmentdef
40
+ request :delete_pvc
41
+ request :delete_service
28
42
  request :delete_vminstance
29
43
  request :delete_vm
44
+ request :delete_volume
30
45
  request :get_vminstance
46
+ request :get_networkattachmentdef
31
47
  request :get_node
48
+ request :get_pvc
32
49
  request :get_vm
50
+ request :get_volume
51
+ request :get_server
52
+ request :get_service
33
53
  request :get_template
34
54
  request :list_vminstances
35
55
  request :list_nodes
56
+ request :list_networkattachmentdefs
36
57
  request :list_vms
58
+ request :list_pvcs
59
+ request :list_volumes
60
+ request :list_servers
61
+ request :list_services
37
62
  request :list_templates
38
63
  request :update_vm
39
64
 
@@ -50,8 +75,9 @@ module Fog
50
75
  end
51
76
 
52
77
  class ExceptionWrapper
53
- def initialize(client)
78
+ def initialize(client, version)
54
79
  @client = client
80
+ @version = version
55
81
  end
56
82
 
57
83
  def method_missing(symbol, *args)
@@ -71,6 +97,10 @@ module Fog
71
97
  def respond_to_missing?(method_name, include_private = false)
72
98
  @client.respond_to?(symbol, include_all) || super
73
99
  end
100
+
101
+ def version
102
+ @version
103
+ end
74
104
  end
75
105
 
76
106
  #
@@ -122,17 +152,19 @@ module Fog
122
152
  include Shared
123
153
 
124
154
  #
125
- # The API version and group of the Kubernetes core:
155
+ # The API group of the Kubernetes core:
126
156
  #
127
157
  CORE_GROUP = ''.freeze
128
- CORE_VERSION = 'v1'.freeze
129
158
 
130
159
  #
131
- # The API version and group of KubeVirt:
160
+ # The API group of KubeVirt:
132
161
  #
133
162
  KUBEVIRT_GROUP = 'kubevirt.io'.freeze
134
- KUBEVIRT_VERSION = 'v1alpha2'.freeze
135
- KUBEVIRT_VERSION_LABEL = KUBEVIRT_GROUP + '/' + KUBEVIRT_VERSION
163
+
164
+ #
165
+ # The API group of the Kubernetes network extention:
166
+ #
167
+ NETWORK_GROUP = 'k8s.cni.cncf.io'.freeze
136
168
 
137
169
  def initialize(options={})
138
170
  require 'kubeclient'
@@ -146,17 +178,6 @@ module Fog
146
178
 
147
179
  @namespace = options[:kubevirt_namespace] || 'default'
148
180
 
149
- # Prepare the TLS and authentication options that will be used for the standard Kubernetes API
150
- # and also for the KubeVirt extension:
151
- @opts = {
152
- :ssl_options => {
153
- :verify_ssl => OpenSSL::SSL::VERIFY_NONE,
154
- },
155
- :auth_options => {
156
- :bearer_token => @kubevirt_token
157
- }
158
- }
159
-
160
181
  # Kubeclient needs different client objects for different API groups. We will keep in this hash the
161
182
  # client objects, indexed by API path/version.
162
183
  @clients = {}
@@ -249,7 +270,7 @@ module Fog
249
270
  populate_notice_attributes(vminstance, notice)
250
271
  vminstance
251
272
  end
252
- watch = kubevirt_client.watch_virtual_machines(opts)
273
+ watch = kubevirt_client.watch_virtual_machine_instances(opts)
253
274
 
254
275
  WatchWrapper.new(watch, mapper)
255
276
  end
@@ -296,7 +317,6 @@ module Fog
296
317
 
297
318
  private
298
319
 
299
-
300
320
  #
301
321
  # Populates required notice attributes
302
322
  #
@@ -310,47 +330,115 @@ module Fog
310
330
  end
311
331
 
312
332
  #
313
- # Lazily creates the a client for the given Kubernetes API path and version.
333
+ # Lazily creates the a client for the given Kubernetes API path.
314
334
  #
315
335
  # @param path [String] The Kubernetes API path.
316
- # @param version [String] The Kubernetes API version.
317
- # @return [Kubeclient::Client] The client for the given path and version.
336
+ # @return [Kubeclient::Client] The client for the given path.
318
337
  #
319
- def create_client(path, version)
320
- # Return the client immediately if it has been created before:
321
- key = path + '/' + version
322
- client = @clients[key]
323
- return client if client
324
-
325
- # Create the client and save it:
338
+ def create_client(path)
326
339
  url = URI::Generic.build(
327
340
  :scheme => 'https',
328
341
  :host => @host,
329
342
  :port => @port,
330
343
  :path => path
331
344
  )
345
+
346
+ if @kubevirt_token.to_s.empty?
347
+ create_client_from_config(path)
348
+ else
349
+ create_client_from_token(url)
350
+ end
351
+ end
352
+
353
+ def create_client_from_token(url)
354
+ # Prepare the TLS and authentication options that will be used for the standard Kubernetes API
355
+ # and also for the KubeVirt extension:
356
+ @opts = {
357
+ :ssl_options => {
358
+ :verify_ssl => OpenSSL::SSL::VERIFY_NONE,
359
+ },
360
+ :auth_options => {
361
+ :bearer_token => @kubevirt_token
362
+ }
363
+ }
364
+ version = detect_version(url.to_s, @opts[:ssl_options])
365
+ key = url.path + '/' + version
366
+
367
+ client = check_client(key)
368
+ return client if client
369
+
332
370
  client = Kubeclient::Client.new(
333
371
  url.to_s,
334
372
  version,
335
373
  @opts
336
374
  )
337
- wrapped_client = ExceptionWrapper.new(client)
375
+
376
+ wrap_client(client, version, key)
377
+ end
378
+
379
+ def create_client_from_config(path)
380
+ config = Kubeclient::Config.read(ENV['KUBECONFIG'] || ENV['HOME']+'/.kube/config')
381
+ context = config.context
382
+ url = context.api_endpoint
383
+ version = detect_version(url + path, context.ssl_options)
384
+ key = path + '/' + version
385
+
386
+ client = check_client(key)
387
+ return client if client
388
+
389
+ client = Kubeclient::Client.new(
390
+ url + path,
391
+ version,
392
+ ssl_options: context.ssl_options,
393
+ auth_options: context.auth_options
394
+ )
395
+
396
+ wrap_client(client, version, key)
397
+ end
398
+
399
+ def check_client(key)
400
+ @clients[key]
401
+ end
402
+
403
+ def wrap_client(client, version, key)
404
+ wrapped_client = ExceptionWrapper.new(client, version)
338
405
  @clients[key] = wrapped_client
339
406
 
340
- # Return the client:
341
407
  wrapped_client
342
408
  end
343
409
 
410
+ def detect_version(url, ssl_options)
411
+ options = {
412
+ ssl_ca_file: ssl_options[:ca_file],
413
+ ssl_cert_store: ssl_options[:cert_store],
414
+ verify_ssl: ssl_options[:verify_ssl],
415
+ ssl_client_cert: ssl_options[:client_cert],
416
+ ssl_client_key: ssl_options[:client_key],
417
+ }
418
+
419
+ response = ::JSON.parse(RestClient::Resource.new(url, options).get)
420
+
421
+ # version detected based on
422
+ # https://github.com/kubernetes-incubator/apiserver-builder/blob/master/docs/concepts/aggregation.md#viewing-discovery-information
423
+ preferredVersion = response["preferredVersion"]
424
+ return preferredVersion["version"] if preferredVersion
425
+ response["versions"][0]
426
+ end
427
+
344
428
  def openshift_client
345
- create_client('/oapi', CORE_VERSION)
429
+ create_client('/oapi')
346
430
  end
347
431
 
348
432
  def kube_client
349
- create_client('/api', CORE_VERSION)
433
+ create_client('/api')
350
434
  end
351
435
 
352
436
  def kubevirt_client
353
- create_client('/apis/' + KUBEVIRT_GROUP, KUBEVIRT_VERSION)
437
+ create_client('/apis/' + KUBEVIRT_GROUP)
438
+ end
439
+
440
+ def kube_net_client
441
+ create_client('/apis/' + NETWORK_GROUP)
354
442
  end
355
443
 
356
444
  def log
@@ -0,0 +1,25 @@
1
+ module Fog
2
+ module Kubevirt
3
+ class Compute
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/kubevirt/compute/models/networkattachmentdef'
3
+
4
+ module Fog
5
+ module Kubevirt
6
+ class Compute
7
+ class Networkattachmentdefs < Fog::Collection
8
+ attr_reader :kind, :resource_version
9
+
10
+ model Fog::Kubevirt::Compute::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 Kubevirt
3
+ class Compute
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/kubevirt/compute/models/pvc'
3
+
4
+ module Fog
5
+ module Kubevirt
6
+ class Compute
7
+ class Pvcs < Fog::Collection
8
+ attr_reader :kind, :resource_version
9
+
10
+ model Fog::Kubevirt::Compute::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