Class: Fog::Kubevirt::Compute::Vms

Inherits:
Collection
  • Object
show all
Includes:
Shared
Defined in:
lib/fog/kubevirt/compute/models/vms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared

#deep_merge!, #object_to_hash

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



10
11
12
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 10

def kind
  @kind
end

#resource_versionObject (readonly)

Returns the value of attribute resource_version.



10
11
12
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 10

def resource_version
  @resource_version
end

Instance Method Details

#add_vm_storage(vm_name, vm_volumes) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 183

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

    if v.type == 'containerDisk'
      # set image
      if v.config.nil?
        volumes.push(:name => volume_name, :containerDisk => {:image => v.info})
      else
        volumes.push(:name => volume_name, v.type.to_sym => v.config)
      end
      disk[:disk][:bus] = v.bus || "virtio"
    elsif v.type == 'persistentVolumeClaim'
      # set claim
      if v.config.nil?
        volumes.push(:name => volume_name, :persistentVolumeClaim => {:claimName => v.info})
      else
        volumes.push(:name => volume_name, v.type.to_sym => v.config)
      end
      disk[:disk][:bus] = v.bus || "virtio"
    else
      # convert type into symbol and pass :config as volume content
      volumes.push(:name => volume_name, v.type.to_sym => v.config)
      disk[:disk][:bus] = v.bus if v.bus
    end
    disks.push(disk)
  end

  return volumes, disks
end

#all(filters = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

#create(args = {}) ⇒ Object

Creates a virtual machine using provided paramters: :vm_name [String] - name of a vm :cpus [String] - number of cpus :memory_size [String] - amount of memory :memory_unit [String] - memory unit to use, default to ‘M’ :image [String] - name of a container disk :pvc [String] - name of a persistent volume claim :cloudinit [Hash] - number of items needed to configure cloud-init :networks - networks to which the vm should be connected, i.e:

[ { :name => 'default', :pod => {} } ,
  { :name => 'ovs-red', :multus => { :networkName => 'red'} }
]

:interfaces - 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' }
]

Parameters:

  • :image (String)

    name of container disk.

  • :volumes (Array)

    the volumes (Fog::Kubevirt::Compute::Volume) to be used by the VM

  • attributes (Hash)

    containing details about vm about to be created.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 75

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)
  vm_volumes =  args.fetch(:volumes, 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?
  service.create_vm(vm)
end

#delete(name) ⇒ Object



43
44
45
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 43

def delete(name)
  service.delete_vm(name, service.namespace)
end

#get(name) ⇒ Object



39
40
41
# File 'lib/fog/kubevirt/compute/models/vms.rb', line 39

def get(name)
  new service.get_vm(name)
end

#new(attributes = {}) ⇒ Object



30
31
32
33
34
35
36
37
# 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