<s> import base64 <EOL> import json <EOL> import logging <EOL> import random <EOL> from perfkitbenchmarker import disk <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import virtual_machine , linux_virtual_machine <EOL> from perfkitbenchmarker import vm_util <EOL> from perfkitbenchmarker import providers <EOL> from perfkitbenchmarker . providers . kubernetes import kubernetes_disk <EOL> from perfkitbenchmarker . vm_util import OUTPUT_STDOUT as STDOUT , OUTPUT_STDERR as STDERR , OUTPUT_EXIT_CODE as EXIT_CODE <EOL> FLAGS = flags . FLAGS <EOL> DEFAULT_ZONE = '<STR_LIT>' <EOL> SECRET_PREFIX = '<STR_LIT>' <EOL> UBUNTU_IMAGE = '<STR_LIT>' <EOL> SELECTOR_PREFIX = '<STR_LIT>' <EOL> class KubernetesVirtualMachine ( virtual_machine . BaseVirtualMachine ) : <EOL> """<STR_LIT>""" <EOL> CLOUD = providers . KUBERNETES <EOL> def __init__ ( self , vm_spec ) : <EOL> """<STR_LIT>""" <EOL> super ( KubernetesVirtualMachine , self ) . __init__ ( vm_spec ) <EOL> self . num_scratch_disks = <NUM_LIT:0> <EOL> self . name = self . name . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> self . user_name = FLAGS . username <EOL> self . image = self . image or UBUNTU_IMAGE <EOL> def _CreateDependencies ( self ) : <EOL> self . _CheckPrerequisites ( ) <EOL> self . _CreateSecret ( ) <EOL> self . _CreateVolumes ( ) <EOL> def _DeleteDependencies ( self ) : <EOL> self . _DeleteVolumes ( ) <EOL> self . _DeleteSecret ( ) <EOL> def _Create ( self ) : <EOL> self . _CreatePod ( ) <EOL> self . _CreateService ( ) <EOL> self . _WaitForPodBootCompletion ( ) <EOL> self . _WaitForEndpoint ( ) <EOL> @ vm_util . Retry ( ) <EOL> def _PostCreate ( self ) : <EOL> self . _SetSshDetails ( ) <EOL> self . _ConfigureProxy ( ) <EOL> self . _SetupDevicesPaths ( ) <EOL> def _Delete ( self ) : <EOL> self . _DeletePod ( ) <EOL> self . _DeleteService ( ) <EOL> def _CheckPrerequisites ( self ) : <EOL> """<STR_LIT>""" <EOL> if not FLAGS . kubernetes_nodes : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not FLAGS . kubectl : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not FLAGS . kubeconfig : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . disk_specs and self . disk_specs [ <NUM_LIT:0> ] . disk_type == disk . STANDARD : <EOL> if not FLAGS . ceph_monitors : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _CreatePod ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] <EOL> create_rc_body = self . _BuildPodBody ( ) <EOL> output = vm_util . IssueCommand ( create_cmd , input = create_rc_body ) <EOL> if output [ EXIT_CODE ] : <EOL> raise Exception ( "<STR_LIT>" % output [ STDERR ] ) <EOL> logging . info ( output [ STDOUT ] . rstrip ( ) ) <EOL> def _CreateService ( self ) : <EOL> """<STR_LIT>""" <EOL> create_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] <EOL> create_svc_body = self . _BuildServiceBody ( ) <EOL> output = vm_util . IssueCommand ( create_cmd , input = create_svc_body ) <EOL> if output [ EXIT_CODE ] : <EOL> raise Exception ( "<STR_LIT>" % output [ STDERR ] ) <EOL> logging . info ( output [ STDOUT ] . rstrip ( ) ) <EOL> @ vm_util . Retry ( poll_interval = <NUM_LIT:10> , max_retries = <NUM_LIT:100> , log_errors = False ) <EOL> def _WaitForPodBootCompletion ( self ) : <EOL> """<STR_LIT>""" <EOL> exists_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name ] <EOL> logging . info ( "<STR_LIT>" % self . name ) <EOL> pod_info , _ , _ = vm_util . IssueCommand ( exists_cmd , suppress_warning = True ) <EOL> if pod_info : <EOL> pod_info = json . loads ( pod_info ) <EOL> containers = pod_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if len ( containers ) == <NUM_LIT:1> : <EOL> pod_status = pod_info [ '<STR_LIT:status>' ] [ '<STR_LIT>' ] <EOL> if ( containers [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] . startswith ( self . name ) <EOL> and pod_status == "<STR_LIT>" ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> return <EOL> raise Exception ( "<STR_LIT>" % <EOL> self . name ) <EOL> @ vm_util . Retry ( poll_interval = <NUM_LIT:10> , max_retries = <NUM_LIT:20> , log_errors = False ) <EOL> def _WaitForEndpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( "<STR_LIT>" % self . name ) <EOL> exists_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name ] <EOL> endpoint , _ , _ = vm_util . IssueCommand ( exists_cmd , suppress_warning = True ) <EOL> if endpoint : <EOL> endpoint = json . loads ( endpoint ) <EOL> if len ( endpoint [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> logging . info ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> raise Exception ( "<STR_LIT>" % self . name ) <EOL> def _DeletePod ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_pod = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name ] <EOL> output = vm_util . IssueCommand ( delete_pod ) <EOL> logging . info ( output [ STDOUT ] . rstrip ( ) ) <EOL> def _DeleteService ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_service = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name ] <EOL> output = vm_util . IssueCommand ( delete_service ) <EOL> logging . info ( output [ STDOUT ] . rstrip ( ) ) <EOL> @ vm_util . Retry ( poll_interval = <NUM_LIT:10> , max_retries = <NUM_LIT:20> ) <EOL> def _Exists ( self ) : <EOL> """<STR_LIT>""" <EOL> exists_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name ] <EOL> pod_info , _ , _ = vm_util . IssueCommand ( exists_cmd , suppress_warning = True ) <EOL> if pod_info : <EOL> return True <EOL> return False <EOL> def _CreateVolumes ( self ) : <EOL> """<STR_LIT>""" <EOL> self . scratch_disks = kubernetes_disk . CreateDisks ( self . disk_specs , self . name ) <EOL> @ vm_util . Retry ( poll_interval = <NUM_LIT:10> , max_retries = <NUM_LIT:20> , log_errors = False ) <EOL> def _DeleteVolumes ( self ) : <EOL> """<STR_LIT>""" <EOL> for scratch_disk in self . scratch_disks [ : ] : <EOL> scratch_disk . _Delete ( ) <EOL> self . scratch_disks . remove ( scratch_disk ) <EOL> def _CreateSecret ( self ) : <EOL> """<STR_LIT>""" <EOL> cat_cmd = [ '<STR_LIT>' , vm_util . GetPublicKeyPath ( ) ] <EOL> key_file , _ = vm_util . IssueRetryableCommand ( cat_cmd ) <EOL> encoded_public_key = base64 . b64encode ( key_file ) <EOL> template = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : SECRET_PREFIX + self . name <EOL> } , <EOL> "<STR_LIT:data>" : { <EOL> "<STR_LIT>" : encoded_public_key <EOL> } <EOL> } <EOL> create_secret_body = json . dumps ( template ) <EOL> create_secret = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] <EOL> output = vm_util . IssueCommand ( create_secret , input = create_secret_body ) <EOL> if output [ EXIT_CODE ] : <EOL> raise Exception ( "<STR_LIT>" % output [ STDERR ] ) <EOL> logging . info ( output [ STDOUT ] . rstrip ( ) ) <EOL> def _DeleteSecret ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_secret = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , SECRET_PREFIX + self . name ] <EOL> result = vm_util . IssueCommand ( delete_secret ) <EOL> logging . info ( result [ STDOUT ] . rstrip ( ) ) <EOL> def DeleteScratchDisks ( self ) : <EOL> pass <EOL> def _SetSshDetails ( self ) : <EOL> """<STR_LIT>""" <EOL> get_service_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name , '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , _ , _ = vm_util . IssueCommand ( get_service_cmd , suppress_warning = True ) <EOL> service = json . loads ( stdout ) <EOL> ports = service . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> if not ports : <EOL> raise Exception ( "<STR_LIT>" % self . name ) <EOL> self . ssh_port = ports [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> get_pod_cmd = [ FLAGS . kubectl , '<STR_LIT>' % FLAGS . kubeconfig , <EOL> '<STR_LIT>' , '<STR_LIT>' , self . name , '<STR_LIT>' , '<STR_LIT>' ] <EOL> stdout , _ , _ = vm_util . IssueCommand ( get_pod_cmd , suppress_warning = True ) <EOL> pod = json . loads ( stdout ) <EOL> pod_ip = pod . get ( '<STR_LIT:status>' , { } ) . get ( '<STR_LIT>' , None ) <EOL> if not pod_ip : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . internal_ip = pod_ip <EOL> self . ip_address = random . choice ( FLAGS . kubernetes_nodes ) <EOL> def _ConfigureProxy ( self ) : <EOL> """<STR_LIT>""" <EOL> if FLAGS . http_proxy : <EOL> http_proxy = "<STR_LIT>" <EOL> self . RemoteCommand ( http_proxy % FLAGS . http_proxy ) <EOL> if FLAGS . https_proxy : <EOL> https_proxy = "<STR_LIT>" <EOL> self . RemoteCommand ( https_proxy % FLAGS . http_proxy ) <EOL> if FLAGS . ftp_proxy : <EOL> ftp_proxy = "<STR_LIT>" <EOL> self . RemoteCommand ( ftp_proxy % FLAGS . ftp_proxy ) <EOL> def _SetupDevicesPaths ( self ) : <EOL> """<STR_LIT>""" <EOL> for scratch_disk in self . scratch_disks : <EOL> scratch_disk . SetDevicePath ( self ) <EOL> def _BuildPodBody ( self ) : <EOL> """<STR_LIT>""" <EOL> container = self . _BuildContainerBody ( ) <EOL> volumes = self . _BuildVolumesBody ( ) <EOL> template = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : self . name , <EOL> "<STR_LIT>" : { <EOL> SELECTOR_PREFIX : self . name <EOL> } <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : volumes , <EOL> "<STR_LIT>" : [ container ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> return json . dumps ( template ) <EOL> def _BuildVolumesBody ( self ) : <EOL> """<STR_LIT>""" <EOL> volumes = [ ] <EOL> secret_volume = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : SECRET_PREFIX + self . name <EOL> } <EOL> } <EOL> volumes . append ( secret_volume ) <EOL> for scratch_disk in self . scratch_disks : <EOL> scratch_disk . AttachVolumeInfo ( volumes ) <EOL> return volumes <EOL> def _BuildContainerBody ( self ) : <EOL> """<STR_LIT>""" <EOL> public_key = "<STR_LIT>" + "<STR_LIT>" <EOL> container_boot_commands = public_key + "<STR_LIT>" <EOL> container = { <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:-c>" , <EOL> container_boot_commands <EOL> ] , <EOL> "<STR_LIT:image>" : self . image , <EOL> "<STR_LIT:name>" : self . name , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : FLAGS . docker_in_privileged_mode <EOL> } , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } <EOL> for scratch_disk in self . scratch_disks : <EOL> scratch_disk . AttachVolumeMountInfo ( container [ '<STR_LIT>' ] ) <EOL> return container <EOL> def _BuildServiceBody ( self ) : <EOL> """<STR_LIT>""" <EOL> service = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : self . name <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:port>" : self . ssh_port <EOL> } <EOL> ] , <EOL> "<STR_LIT>" : { <EOL> SELECTOR_PREFIX : self . name <EOL> } , <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> return json . dumps ( service ) <EOL> class DebianBasedKubernetesVirtualMachine ( KubernetesVirtualMachine , <EOL> linux_virtual_machine . DebianMixin ) : <EOL> DEFAULT_IMAGE = UBUNTU_IMAGE </s>
