Kami commented on code in PR #1983: URL: https://github.com/apache/libcloud/pull/1983#discussion_r1581834132
########## libcloud/compute/drivers/kubevirt.py: ########## @@ -391,63 +772,162 @@ def create_node( ) raise KeyError(msg) + claim_name = disk["volume_spec"]["claim_name"] + + if claim_name not in self.ex_list_persistent_volume_claims(namespace=namespace): + if ( + "size" not in disk["volume_spec"] + or "storage_class_name" not in disk["volume_spec"] + ): + msg = ( + "disk['volume_spec']['size'] and " + "disk['volume_spec']['storage_class_name'] " + "are both required to create " + "a new claim." + ) + raise KeyError(msg) + size = disk["volume_spec"]["size"] + storage_class = disk["volume_spec"]["storage_class_name"] + volume_mode = disk["volume_spec"].get("volume_mode", "Filesystem") + access_mode = disk["volume_spec"].get("access_mode", "ReadWriteOnce") + self.create_volume( + size=size, + name=claim_name, + location=location, + ex_storage_class_name=storage_class, + ex_volume_mode=volume_mode, + ex_access_mode=access_mode, + ) + volumes_dict = { - "persistentVolumeClaim": {"claimName": claimName}, + "persistentVolumeClaim": {"claimName": claim_name}, "name": disk_name, } + else: + warnings.warn( + "The disk type {} is not tested. Use at your own risk.".format(disk_type) + ) + volumes_dict = {disk_type: disk.get("volume_spec", {}), "name": disk_name} + disk_dict = {device: {"bus": bus}, "name": disk_name} vm["spec"]["template"]["spec"]["domain"]["devices"]["disks"].append(disk_dict) vm["spec"]["template"]["spec"]["volumes"].append(volumes_dict) + # end of for disk in ex_disks + # image -> containerDisk # adding image in a container Disk if isinstance(image, NodeImage): image = image.name - volumes_dict = {"containerDisk": {"image": image}, "name": "boot-disk"} - disk_dict = {"disk": {"bus": "virtio"}, "name": "boot-disk"} - vm["spec"]["template"]["spec"]["domain"]["devices"]["disks"].append(disk_dict) - vm["spec"]["template"]["spec"]["volumes"].append(volumes_dict) + boot_disk_name = "boot-disk-" + str(uuid.uuid4()) + volumes_dict = {"containerDisk": {"image": image}, "name": boot_disk_name} + disk_dict = {"disk": {"bus": "virtio"}, "name": boot_disk_name} + # boot disk should be the first one, otherwise it will not boot + vm["spec"]["template"]["spec"]["domain"]["devices"]["disks"].insert(0, disk_dict) + vm["spec"]["template"]["spec"]["volumes"].insert(0, volumes_dict) + + # auth -> cloud-init + if auth is not None: + # auth requires cloud-init, + # and only one cloud-init volume is supported by kubevirt. + # So if both auth and cloud-init are provided, raise an error. + + for volume in vm["spec"]["template"]["spec"]["volumes"]: + if "cloudInitNoCloud" in volume or "cloudInitConfigDrive" in volume: + raise ValueError( + "Setting auth and cloudInit at the same time is not supported." + "Use deploy_node() instead." + ) + + # cloud-init volume + cloud_init_volume = "auth-cloudinit-" + str(uuid.uuid4()) + disk_dict = {"disk": {"bus": "virtio"}, "name": cloud_init_volume} + volume_dict = { + "name": cloud_init_volume, + "cloudInitNoCloud": {"userData": ""}, + } + + # auth + # cloud_init_config reference: https://kubevirt.io/user-guide/virtual_machines/startup_scripts/#injecting-ssh-keys-with-cloud-inits-cloud-config + if isinstance(auth, NodeAuthSSHKey): + public_key = auth.pubkey + cloud_init_config = ( + """#cloud-config\n""" """ssh_authorized_keys:\n""" """ - {}\n""" + ).format(public_key) + elif isinstance(auth, NodeAuthPassword): + password = auth.password + cloud_init_config = ( + """#cloud-config\n""" Review Comment: It would be a bit more readable if we stored cloud init configs in template files and then load those + render them here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@libcloud.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org