foreman_kubevirt 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a96cfb40c1c6f9f54b17cb33fae6abffec9c0764d4c9bbdd5a17595e18e967f4
4
- data.tar.gz: c5bd84ee5ef168cc16a63dab87e7a2d164880a8cd00a481023d8cc48dd06ad3c
3
+ metadata.gz: fee0a645f764e86b484cfc3655738b44336a596da85c135e381ec0ae8cf856fd
4
+ data.tar.gz: b109ec03942dd9cad8832f63e276fede175d6f7899c40cd6aacac578e956fee8
5
5
  SHA512:
6
- metadata.gz: db867f6014df1f71ad31a315559b266eaccf5d5f96e8267afa3fa88733c167c514d0dfcccc6030ee39d399760186a34d819b6bd18b8c26d705f17c529e85aaf9
7
- data.tar.gz: 034ff8550502a7cdb2d205899b4a5394f7ef231b3938465fb15847338bcb965f5296669a9a9e254bbf116aae3a129721d12e1a78fa66f3d8ca8c2591a0cdf30a
6
+ metadata.gz: 0d4ad6baf08d620d48c3b35e687bdcb61d967ea09bd4eabdd92316f29b57ceba1afa482df5417ec86d5f4218e0ce9e2dcce00800f59bf5ca57ec2aeeb1645bba
7
+ data.tar.gz: 4353bd71b0ad05b9584882ceebdc8f76667e47a025ba09d0a1d5642818bbf1b10d4325ae86c79e41a752fc7ac89c3bc538d7d97225254951df065e3ca7c95092
@@ -2,7 +2,6 @@ module ForemanKubevirt
2
2
  module Concerns
3
3
  module Api
4
4
  module ComputeResourcesControllerExtensions
5
- module ApiPieExtensions
6
5
  extend ::Apipie::DSL::Concern
7
6
  update_api(:create, :update) do
8
7
  param :compute_resource, Hash do
@@ -14,13 +13,6 @@ module ForemanKubevirt
14
13
  end
15
14
  end
16
15
  end
17
- end
18
-
19
- extend ActiveSupport::Concern
20
-
21
- included do
22
- include ApiPieExtensions
23
- end
24
16
  end
25
17
  end
26
18
  end
@@ -0,0 +1,8 @@
1
+ module ForemanKubevirt
2
+ module ComputeAttributeExtensions
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ validates :vm_attrs, :volume => true
6
+ end
7
+ end
8
+ end
@@ -6,7 +6,7 @@ module ForemanKubevirt
6
6
  alias_attribute :token, :password
7
7
  alias_attribute :namespace, :user
8
8
  validates :hostname, :api_port, :namespace, :token, :presence => true
9
- before_save :test_connection
9
+ after_validation :validate_connectivity unless Rails.env.test?
10
10
 
11
11
  def ca_cert
12
12
  attrs[:ca_cert]
@@ -52,13 +52,33 @@ module ForemanKubevirt
52
52
  ComputeResource.model_name
53
53
  end
54
54
 
55
- def test_connection(_options = {})
56
- validate!
55
+ def test_connection(options = {})
56
+ super
57
+ validate_connectivity(options)
58
+ end
59
+
60
+ def caching_enabled
61
+ false
62
+ end
63
+
64
+ def validate_connectivity(options = {})
65
+ return unless connection_properties_valid?
66
+ return false if errors.any?
57
67
  client&.valid? && client&.virt_supported?
58
68
  rescue StandardError => e
59
- errors[:base] << e.message
69
+ if e.message =~ /401/
70
+ errors[:base] << _('The compute resource could not be authenticated')
71
+ else
72
+ errors[:base] << e.message
73
+ end
60
74
  end
61
75
 
76
+ def connection_properties_valid?
77
+ errors[:hostname].empty? && errors[:token].empty? && errors[:namespace].empty? && errors[:api_port].empty?
78
+ end
79
+
80
+
81
+
62
82
  def networks
63
83
  client.networkattachmentdefs.all
64
84
  rescue StandardError => e
@@ -86,11 +106,13 @@ module ForemanKubevirt
86
106
  storage_classes.map { |sc| OpenStruct.new(id: sc.name, description: "#{sc.name} (#{sc.provisioner})") }
87
107
  end
88
108
 
89
- def new_volume(attr = {})
90
- return unless new_volume_errors.empty?
91
109
 
92
- vol = Fog::Kubevirt::Compute::Volume.new(attr)
93
- vol.boot_order = 1 if attr[:bootable] == "on" || attr[:bootable] == "true"
110
+ def new_volume(attrs = {})
111
+ return unless new_volume_errors.empty?
112
+ capacity = attrs.delete(:capacity)
113
+ args = {capacity: capacity}.merge(attrs)
114
+ vol = Fog::Kubevirt::Compute::Volume.new(args)
115
+ vol.boot_order = 1 if args[:bootable] == "on" || args[:bootable] == "true"
94
116
  vol
95
117
  end
96
118
 
@@ -140,12 +162,12 @@ module ForemanKubevirt
140
162
  options = vm_instance_defaults.merge(args.to_hash.deep_symbolize_keys)
141
163
  logger.debug("creating VM with the following options: #{options.inspect}")
142
164
 
143
- volumes = create_volumes_for_vm(options)
144
- interfaces, networks = create_network_devices_for_vm(options, volumes)
145
165
  # Add clound init user data
146
166
  user_data = { "userData" => options[:user_data] } if options[:user_data].present?
147
167
 
148
168
  begin
169
+ volumes = create_volumes_for_vm(options)
170
+ interfaces, networks = create_network_devices_for_vm(options, volumes)
149
171
  client.vms.create(:vm_name => options[:name],
150
172
  :cpus => options[:cpu_cores].to_i,
151
173
  :memory_size => convert_memory(options[:memory] + "b", :mi).to_s,
@@ -155,8 +177,8 @@ module ForemanKubevirt
155
177
  :networks => networks,
156
178
  :interfaces => interfaces)
157
179
  client.servers.get(options[:name])
158
- rescue Fog::Kubevirt::Errors::ClientError => e
159
- delete_pvcs(volumes)
180
+ rescue Exception => e
181
+ delete_pvcs(volumes) if volumes
160
182
  raise e
161
183
  end
162
184
  end
@@ -272,6 +294,29 @@ module ForemanKubevirt
272
294
  convert_memory(memory, :b)
273
295
  end
274
296
 
297
+ def plain_kubevirt_protocol
298
+ "plain.kubevirt.io".freeze
299
+ end
300
+
301
+ def token_protocol(token)
302
+ "base64url.bearer.authorization.k8s.io.#{token}"
303
+ end
304
+
305
+ def console(uuid)
306
+ vm = find_vm_by_uuid(uuid)
307
+ vnc_details = client.vminstances.get_vnc_console_details(vm.name, namespace)
308
+ token = Base64.encode64(vnc_details[:token]).delete!("\n").delete("==")
309
+ {
310
+ :host => vnc_details[:host],
311
+ :port => vnc_details[:port],
312
+ :path => vnc_details[:path],
313
+ :token_protocol => token_protocol(token),
314
+ :plain_protocol => plain_kubevirt_protocol,
315
+ :type => 'vnc',
316
+ :encrypt => true
317
+ }
318
+ end
319
+
275
320
  protected
276
321
 
277
322
  def client
@@ -333,7 +378,11 @@ module ForemanKubevirt
333
378
  end
334
379
 
335
380
  def validate_volume_capacity(volumes_attributes)
336
- volumes_attributes.each { |_, v| raise ::Foreman::Exception.new N_('Capacity was not found') if v[:capacity].empty? }
381
+ volumes_attributes.each do |_, vol|
382
+ if vol[:capacity].to_s.empty? || /\A\d+G?\Z/.match(vol[:capacity].to_s).nil?
383
+ raise Foreman::Exception.new(N_("Volume size #{vol[:capacity]} is not valid"))
384
+ end
385
+ end
337
386
  end
338
387
 
339
388
  def validate_only_single_bootable_volume(volumes_attributes)
@@ -422,8 +471,8 @@ module ForemanKubevirt
422
471
  def create_network_devices_for_vm(options, volumes)
423
472
  interfaces = []
424
473
  networks = []
425
-
426
474
  options[:interfaces_attributes].values.each do |iface|
475
+ raise ::Foreman::Exception.new N_('cni_provider or network are missing') unless (iface.key?(:cni_provider) && iface.key?(:network))
427
476
  if iface[:cni_provider] == 'pod'
428
477
  nic, net = create_pod_network_element
429
478
  else
@@ -0,0 +1,9 @@
1
+ class VolumeValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ value[:volumes_attributes].each do |_, attrs|
4
+ if attrs["capacity"].to_s.empty? || /\A\d+G?\Z/.match(attrs["capacity"].to_s).nil?
5
+ record.errors.add(attribute, _("Volume size #{attrs["capacity"]} is not valid"))
6
+ end
7
+ end
8
+ end
9
+ end
@@ -5,7 +5,9 @@
5
5
  :keep_value => true, :unset => unset_password?,
6
6
  :help_block => _("e.g. eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9..."),
7
7
  :help_inline => documentation_button('/foreman_kubevirt/0.x/index.html#4.1SettingComputeResourceTokenandX509CAvalues',
8
- root_url: 'https://theforeman.org/plugins') %>
8
+ :root_url => 'https://theforeman.org/plugins'),
9
+ :id => "compute_resource_password"
10
+ %>
9
11
 
10
12
  <%= textarea_f f, :ca_cert, :label => _("X509 Certification Authorities"),
11
13
  :placeholder => _("Optionally provide a CA, or a correctly ordered CA chain or a path to a file. If left blank - insecure.") %>
@@ -1,6 +1,10 @@
1
1
  <% if f.object.type.nil? || f.object.type == 'persistentVolumeClaim' %>
2
+ <% if @set %>
3
+ <% @set.errors[:vm_attrs].each do | error| %>
4
+ <%= alert :class => 'alert-danger', :header => '', :text => error %>
5
+ <% end %>
6
+ <% end %>
2
7
  <% storage_classes = compute_resource.storage_classes %>
3
-
4
8
  <%= select_f f, :storage_class,
5
9
  compute_resource.storage_classes_for_select,
6
10
  :id,
@@ -10,7 +14,6 @@
10
14
  :label_size => "col-md-2",
11
15
  :disabled => !new_vm,
12
16
  :class => "col-md-2" %>
13
-
14
17
  <%= text_f f,:capacity, :label => _('Size (GB)'), :label_size => "col-md-2", :disabled => !new_vm, :class => "col-md-2 pvc-size" %>
15
18
 
16
19
  <% bootable = f.object.boot_order.nil? ? false : f.object.bootable %>
@@ -62,6 +62,8 @@ module ForemanKubevirt
62
62
  require "fog/kubevirt/compute/models/networkattachmentdef"
63
63
  require File.expand_path("../../app/models/concerns/fog_extensions/kubevirt/network", __dir__)
64
64
  Fog::Kubevirt::Compute::Networkattachmentdef.send(:include, ::FogExtensions::Kubevirt::Network)
65
+ ComputeAttribute.send :include, ForemanKubevirt::ComputeAttributeExtensions
66
+
65
67
  rescue StandardError => e
66
68
  Rails.logger.warn "Foreman-Kubevirt: skipping engine hook (#{e})"
67
69
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanKubevirt
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
@@ -126,9 +126,22 @@ class ForemanKubevirtTest < ActiveSupport::TestCase
126
126
  exception = assert_raise(Foreman::Exception) do
127
127
  compute_resource.create_vm(vm_args)
128
128
  end
129
- assert_match(/Capacity was not found/, exception.message)
129
+ assert_match(/Volume size is not valid/, exception.message)
130
130
  end
131
131
 
132
+
133
+ test "should fail when creating a VM with not valid capacity" do
134
+ vm_args = NETWORK_BASED_VM_ARGS.deep_dup
135
+ vm_args["volumes_attributes"]["0"]["capacity"] = "TG"
136
+ Fog.mock!
137
+ compute_resource = new_kubevirt_vcr
138
+ exception = assert_raise(Foreman::Exception) do
139
+ compute_resource.create_vm(vm_args)
140
+ end
141
+ assert_match(/Volume size TG is not valid/, exception.message)
142
+ end
143
+
144
+
132
145
  test "should fail when creating a VM with two bootable PVCs" do
133
146
  vm_args = NETWORK_BASED_VM_ARGS.deep_dup
134
147
  vm_args["volumes_attributes"]["0"]["bootable"] = "true"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_kubevirt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moti Asayag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-01 00:00:00.000000000 Z
11
+ date: 2019-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.3'
47
+ version: 1.3.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: 1.3.2
55
55
  description: Provision and manage Kubevirt Virtual Machines from Foreman.
56
56
  email:
57
57
  - masayag@redhat.com
@@ -70,7 +70,9 @@ files:
70
70
  - app/models/concerns/fog_extensions/kubevirt/server.rb
71
71
  - app/models/concerns/fog_extensions/kubevirt/vmnic.rb
72
72
  - app/models/concerns/fog_extensions/kubevirt/volume.rb
73
+ - app/models/foreman_kubevirt/compute_attribute_extensions.rb
73
74
  - app/models/foreman_kubevirt/kubevirt.rb
75
+ - app/validators/volume_validator.rb
74
76
  - app/views/api/v2/compute_resources/kubevirt.json.rabl
75
77
  - app/views/compute_resources/form/_kubevirt.html.erb
76
78
  - app/views/compute_resources/show/_kubevirt.html.erb