class Fog::Kubevirt::Compute::Vms
Attributes
Public Instance Methods
# File lib/fog/kubevirt/compute/models/vms.rb, line 209 def add_vm_storage(vm_name, vm_volumes) volumes, disks = [], [] vm_volumes.each_with_index do |v, idx| volume_name = v.name.nil? ? normalized_name(vm_name) + "-disk-0" + idx.to_s : normalized_name(v.name) disk = { :name => volume_name, :disk => {} } disk[:bootOrder] = v.boot_order if v.boot_order volume_config = v.config if v.type == 'containerDisk' volume_config ||= {:image => v.info} disk[:disk][:bus] = v.bus || "virtio" elsif v.type == 'persistentVolumeClaim' volume_config ||= {:claimName => v.info} disk[:disk][:bus] = v.bus || "virtio" elsif v.type == 'dataVolume' volume_config ||= {:name => v.info} disk[:disk][:bus] = v.bus || "virtio" else disk[:disk][:bus] = v.bus if v.bus end # convert type into symbol and pass :config as volume content volumes.push(:name => volume_name, v.type.to_sym => volume_config) disks.push(disk) end return volumes, disks end
# File lib/fog/kubevirt/compute/models/vms.rb, line 14 def all(filters = {}) begin vms = service.list_vms(filters) @kind = vms.kind @resource_version = vms.resource_version rescue ::Fog::Kubevirt::Errors::ClientError # we assume that we get 404 vms = [] @kind = 'VirtualMachine' end load vms end
Creates a virtual machine using provided paramters
@param args [Hash] attributes containing details about vm about to be
created.
@option args [String] :vm_name name of the vm @option args [String,Number,nil] :cpus (nil) number of cpus @option args [String,Number] :memory_size amount of memory @option args [“K”,“M”,“G”,“T”] :memory_unit (“M”) memory unit to use @option args [String] :pvc name of a persistent volume claim @option args [Hash] :cloudinit ({}) items for configuring cloud-init @option args [Array<Hash>,nil] :networks (nil) networks to which the vm should be connected, i.e:
[ { :name => 'default', :pod => {} } ,
{ :name => 'ovs-red', :multus => { :networkName => 'red'} }
]
@option args [Array<Hash>,nil] :interfaces (nil) network interfaces for the vm, correlated to
:networks section by network's name, i.e:
[ { :name => 'default', :bridge => {} },
{ :name => 'red', # correlated to networks[networkName]
:bridge => {},
:bootOrder => 1, # 1 to boot from network interface
:macAddress => '12:34:56:AB:CD:EF' }
]
@option args [Hash,nil] :extra_domain (nil) extra parameters to merge into the domain-part of the VM object, i.e:
{
:features => {
:smm => { :enabled => true }
},
:firmware => {
:bootloader => {
:efi => { :secureBoot => false }
}
}
}
@option args [Array<Fog::Kubevirt::Compute::Volume>] :volumes the volumes (Fog::Kubevirt::Compute::Volume) to be used by the VM @option args [Array<Hash>,nil] :volume_templates (nil) the dataVolumeTemplates to be used by the VM
# File lib/fog/kubevirt/compute/models/vms.rb, line 82 def create(args = {}) vm_name = args.fetch(:vm_name) cpus = args.fetch(:cpus, nil) memory_size = args.fetch(:memory_size) memory_unit = args.fetch(:memory_unit, "M") init = args.fetch(:cloudinit, {}) networks = args.fetch(:networks, nil) interfaces = args.fetch(:interfaces, nil) extra_domain = args.fetch(:extra_domain, nil) vm_volumes = args.fetch(:volumes, nil) volume_templates = args.fetch(:volume_templates, nil) if vm_volumes.nil? || vm_volumes.empty? raise ::Fog::Kubevirt::Errors::ValidationError end memory = "#{memory_size}#{memory_unit}" ::Fog::Kubevirt::Utils::UnitConverter.validate(memory) volumes, disks = add_vm_storage(vm_name, vm_volumes) unless init.empty? volumes.push(:cloudInitNoCloud => init, :name => "cloudinitvolume") end vm = { :kind => "VirtualMachine", :metadata => { :labels => { :"kubevirt.io/vm" => vm_name, }, :name => vm_name, :namespace => service.namespace, }, :spec => { :running => false, :template => { :metadata => { :creationTimestamp => nil, :labels => { :"kubevirt.io/vm" => vm_name } }, :spec => { :domain => { :devices => { :disks => disks }, :machine => { :type => "" }, :resources => { :requests => { :memory => memory } } }, :terminationGracePeriodSeconds => 0, :volumes => volumes } } } } vm = deep_merge!(vm, :spec => { :template => { :spec => { :domain => { :cpu => { :cores => cpus } } } } } ) unless cpus.nil? vm[:spec][:template][:spec][:domain][:devices][:disks].push( :disk => { :bus => "virtio" }, :name => "cloudinitvolume", ) unless init.empty? vm = deep_merge!(vm, :spec => { :template => { :spec => { :networks => networks } } } ) unless networks.nil? vm = deep_merge!(vm, :spec => { :template => { :spec => { :domain => { :devices => { :interfaces => interfaces } } } } } ) unless interfaces.nil? vm = deep_merge!(vm, :spec => { :dataVolumeTemplates => volume_templates } ) unless volume_templates.nil? || volume_templates.empty? vm = deep_merge!(vm, :spec => { :template => { :spec => { :domain => extra_domain } } } ) unless extra_domain.nil? || extra_domain.empty? service.create_vm(vm) end
# File lib/fog/kubevirt/compute/models/vms.rb, line 43 def delete(name) service.delete_vm(name, service.namespace) end
# File lib/fog/kubevirt/compute/models/vms.rb, line 39 def get(name) new service.get_vm(name) end
# File lib/fog/kubevirt/compute/models/vms.rb, line 30 def new(attributes = {}) vm = super vm.disks = [] unless vm.disks vm.volumes = [] unless vm.volumes vm.interfaces = [] unless vm.interfaces vm.networks = [] unless vm.networks vm end
Private Instance Methods
# File lib/fog/kubevirt/compute/models/vms.rb, line 242 def normalized_name(name) name.gsub(/[._]+/,'-') end