def _create_iip(self, pod_name, pod_namespace, proj_uuid, vn_obj, vmi): # Instance-ip for pods are ALWAYS allocated from pod ipam on this # VN. Get the subnet uuid of the pod ipam on this VN, so we can request # an IP from it. vn = VirtualNetworkKM.find_by_name_or_uuid(vn_obj.get_uuid()) if not vn: # It is possible our cache may not have the VN yet. Locate it. vn = VirtualNetworkKM.locate(vn_obj.get_uuid()) if self._is_pod_network_isolated(pod_namespace): vn_namespace = pod_namespace else: vn_namespace = 'default' if self._is_ip_fabric_forwarding_enabled(vn_namespace): ipam_fq_name = vnc_kube_config.ip_fabric_ipam_fq_name() else: ipam_fq_name = vnc_kube_config.pod_ipam_fq_name() pod_ipam_subnet_uuid = vn.get_ipam_subnet_uuid(ipam_fq_name) # Create instance-ip. iip_uuid = str(uuid.uuid1()) iip_name = VncCommon.make_name(pod_name, iip_uuid) perms2 = PermType2() perms2.owner = proj_uuid perms2.owner_access = PERMS_RWX iip_obj = InstanceIp(name=iip_name, subnet_uuid=pod_ipam_subnet_uuid, display_name=iip_name, perms2=perms2) iip_obj.uuid = iip_uuid iip_obj.add_virtual_network(vn_obj) # Creation of iip requires the vmi vnc object. vmi_obj = self._vnc_lib.virtual_machine_interface_read( fq_name=vmi.fq_name) iip_obj.add_virtual_machine_interface(vmi_obj) InstanceIpKM.add_annotations(self, iip_obj, pod_namespace, pod_name) self._logger.debug("%s: Create IIP from ipam_fq_name [%s]" " pod_ipam_subnet_uuid [%s]" " vn [%s] vmi_fq_name [%s]" %\ (self._name, ipam_fq_name, pod_ipam_subnet_uuid, vn.name, vmi.fq_name)) try: self._vnc_lib.instance_ip_create(iip_obj) except RefsExistError: self._vnc_lib.instance_ip_update(iip_obj) InstanceIpKM.locate(iip_obj.uuid) return iip_obj
def test_associate_scoped_tag_to_virtual_machine(self): project = Project('project-%s' % self.id()) self.api.project_create(project) vm = VirtualMachine('vm-%s' % self.id()) vm_uuid = self.api.virtual_machine_create(vm) vm = self.api.virtual_machine_read(id=vm_uuid) tag_type = 'fake_type-%s' % self.id() tag_value = 'fake_value-%s' % self.id() tag = Tag(tag_type_name=tag_type, tag_value=tag_value, parent_obj=project) self.api.tag_create(tag) self.assertRaises(NoIdError, self.api.set_tag, vm, tag_type, tag_value) perms2 = PermType2() perms2.owner = project.uuid.replace('-', '') vm.set_perms2(perms2) self.api.virtual_machine_update(vm) self.api.set_tag(vm, tag_type, tag_value)
def _create_vm(self, pod_namespace, pod_id, pod_name, labels, proj_uuid): vm_name = VncCommon.make_name(pod_name, pod_id) display_name = VncCommon.make_display_name(pod_namespace, pod_name) perms2 = PermType2() perms2.owner = proj_uuid perms2.owner_access = PERMS_RWX vm_obj = VirtualMachine(name=vm_name, perms2=perms2, display_name=display_name) vm_obj.uuid = pod_id vm_obj.set_server_type("container") VirtualMachineKM.add_annotations(self, vm_obj, pod_namespace, pod_name, k8s_uuid=str(pod_id), labels=json.dumps(labels)) try: self._vnc_lib.virtual_machine_create(vm_obj) except RefsExistError: vm_obj = self._vnc_lib.virtual_machine_read(id=pod_id) VirtualMachineKM.locate(vm_obj.uuid) return vm_obj
print >> sys.stderr, "Fetched project: ", proj_obj ## GET VN vn_obj = None vn_obj = api.virtual_network_read(fq_name=vn_fq_name) ipam = vn_obj.get_network_ipam_refs() vn_uuid = vn_obj.uuid subnet_uuid = ipam[0]["attr"].ipam_subnets[0].subnet_uuid print >> sys.stderr, "Fetched VN: ", vn_obj ## CREATE VM proj_uuid = proj_obj.uuid vm_obj = None vm_uuid = None pod_uuid = str(uuid.uuid1()) perms2 = PermType2() perms2.owner = proj_uuid perms2.owner_access = PERMS_RWX vm_obj = VirtualMachine(name=vm_name, perms2=perms2, display_name=vm_display_name, parent_obj=proj_obj) vm_obj.uuid = pod_uuid vm_obj.set_server_type("container") try: vm_response = api.virtual_machine_create(vm_obj) print >> sys.stderr, "Created VM: ", vm_response vm_uuid = vm_response except RefsExistError as ref: print >> sys.stderr, "Not creating VM, already exists: ", str(ref) vm_obj = api.virtual_machine_read(fq_name=[vm_name]) print >> sys.stderr, vm_obj vm_uuid = vm_obj.uuid