def test_get_by_name_nonexistent(self): name = "yodep-{0}".format(str(uuid.uuid4())) dep = utils.create_deployment(name=name) if utils.is_reachable(dep.config.api_host): result = K8sDeployment.get_by_name(config=dep.config, name=name) self.assertIsInstance(result, list) self.assertEqual(0, len(result))
def has_deployment(self, name): o = K8sDeployment(config=self.k8s_config, name=name) res = False for ds in o.list(): if ds.name == name: res = True break else: continue return res
def test_init_no_args(self): try: K8sDeployment() self.fail("Should not fail.") except SyntaxError: pass except IOError: pass except Exception as err: self.fail("Unhandled exception: [ {0} ]".format( err.__class__.__name__))
def test_inmarsat(self): cfg_default = K8sConfig(kubeconfig=None, api_host="127.0.0.1:8001") container = K8sContainer(name='nginx', image='nginx:latest') container.add_port(container_port=80, host_port=80, name='nginx') deployment = K8sDeployment(config=cfg_default, name='nginx', replicas=3) deployment.add_container(container) deployment.create()
def test_get_by_name(self): cont_name = "yocontainer" container = utils.create_container(name=cont_name) name = "yodep-{0}".format(str(uuid.uuid4())) dep = utils.create_deployment(name=name) dep.add_container(container) if utils.is_reachable(dep.config.api_host): dep.create() result = K8sDeployment.get_by_name(config=dep.config, name=name) self.assertIsInstance(result, list) self.assertEqual(1, len(result)) self.assertIsInstance(result[0], K8sDeployment) self.assertEqual(dep, result[0])
def create_deployment(self, name, image, env, rs=1, type='tomcat', port=8080, env_dict=False, mount_path=False): is_run = False if self.has_deployment(name): # image=self.get_deployment(name)['spec']['template']['spec']['containers'][0]['image'] is_run = True container = K8sContainer(name=name, image=image) container.add_port(container_port=port) try: if env_dict: for k, v in env_dict.items(): container.add_env(name=str(k), value=str(v)) container.add_env(name='ENVNAME', value=str(env)) except Exception as ex: return "Error: 添加容器环境变量失败" + ex try: mount_path = mount_path if mount_path else '/usr/local/tomcat/logs' if type == "tomcat": container.add_env(name='aliyun_logs_' + name, value='stdout') else: container.add_env(name='aliyun_logs_' + name, value=mount_path + '/*.log') mount = K8sVolumeMount(name=name + '-logs', mount_path=mount_path) container.add_volume_mount(mount) except Exception as ex: return "Error: 容器数据卷添加失败" + ex container.resources = {'requests': {"cpu": "1000m", "memory": "2000M"}} deployment = K8sDeployment(config=self.k8s_config, name=name, replicas=rs) volume = K8sVolume(name=name + '-logs', type='hostPath') volume.path = "/acs/logs/" + self.namespace + '/' + name deployment.add_volume(volume) deployment.add_container(container) return deployment.update() if is_run else deployment.create()
def deployment_action(self): cfg_cert = K8sConfig(kubeconfig=None, auth=(self.k8s_username, self.k8s_password), api_host=self.k8s_host) deployment = K8sDeployment(config=cfg_cert, name=self.k8s_deployment_name) deployment.add_label(k='project', v=self.project_name) if not deployment._exists(): container = K8sContainer(name=self.k8s_deployment_name, image=self.k8s_image) container.add_env('KS_PROJECT_VERSION', self.project_version) container.add_container_resources_limits(cpu='2', memory='1024Mi') # container.add_image_pull_policy(policy='Always') if re.search("(JAVA|RPC)",self.go_environment_name,re.I): container.add_volume_mount( K8sVolumeMount(name="{project}-resources-volume".format(project=self.k8s_deployment_name), mount_path="/data/{project}/resources".format( project=self.project_name))) container.add_volume_mount(K8sVolumeMount(name='{project}-log-volume'.format(project=self.k8s_deployment_name), mount_path='/data/logs/')) vol = K8sVolume(name="{project}-resources-volume".format(project=self.k8s_deployment_name), type='hostPath') vol.path = "/data/resources/{group}/{project}".format(group=self.project_group, project=self.project_name) deployment.add_volume(vol) vol_log = K8sVolume(name="{project}-log-volume".format(project=self.k8s_deployment_name), type='hostPath') vol_log.path = "/data/logs/{group}/{project}".format(group=self.project_group, project=self.project_name) deployment.add_volume(vol_log) deployment.add_container(container) deployment.add_change_cause('Image Version:{version}'.format(version=self.k8s_image)) deployment.add_image_pull_secrets([{'name': 'prodregistrykey'}]) deployment.create() else: deployment.change_container_resources_limits(cpu='2', memory='1024Mi') deployment.set_container_image(name=self.k8s_deployment_name, image=self.k8s_image) deployment.add_change_cause('Image Version:{version}'.format(version=self.k8s_image)) deployment.update() deployment.scale(self.project_replicas)
def deployment_action(self): cfg_cert = K8sConfig(kubeconfig=None,auth=(self.k8s_username,self.k8s_password), api_host=self.k8s_host) deployment = K8sDeployment(config=cfg_cert, name=self.k8s_deployment_name) deployment.add_label(k='project',v=self.project_name) if not deployment._exists(): container = K8sContainer(name=self.k8s_deployment_name, image=self.k8s_image) container.add_env('KS_PROJECT_VERSION',self.project_version) container.add_image_pull_policy(policy='Always') container.add_volume_mount(K8sVolumeMount(name="{project}-resources-volume".format(project=self.k8s_deployment_name), mount_path="/data/{project}/resources".format( project=self.project_name))) #container.add_volume_mount(K8sVolumeMount(name='localtime-volumn',mount_path='/etc/localtime')) container.add_volume_mount(K8sVolumeMount(name='{project}-log-volume'.format(project=self.k8s_deployment_name), mount_path='/data/logs/')) container.add_container_resources_limits(cpu='2', memory='1024Mi') #container.add_container_resources_requests(cpu='200m',memory='512M') deployment.add_container(container) deployment.add_change_cause('Image Version:{version}'.format(version=self.k8s_image)) deployment.add_image_pull_secrets([{'name': 'ksszregistrykey'}]) vol = K8sVolume(name="{project}-resources-volume".format(project=self.k8s_deployment_name), type='hostPath') vol.path = "/data/docker/resources/{env}/{project}".format(env=self.project_status,project=self.project_name) deployment.add_volume(vol) #vol_localtime = K8sVolume(name='localtime-volumn', type='hostPath') #vol_localtime.path = "/etc/localtime" #deployment.add_volume(vol_localtime) vol_log = K8sVolume(name="{project}-log-volume".format(project=self.k8s_deployment_name), type='hostPath') vol_log.path = "/data/docker/logs/{env}/{project}".format(env=self.project_status,project=self.project_name) deployment.add_volume(vol_log) deployment.create() else: deployment.change_container_resources_limits(cpu='2', memory='1024Mi') deployment.set_container_image(name=self.k8s_deployment_name, image=self.k8s_image) deployment.add_change_cause('Image Version:{version}'.format(version=self.k8s_image)) deployment.update() deployment.scale(self.project_replicas)
def test_get_by_name_invalid_config(self): name = "yoname" config = object() with self.assertRaises(SyntaxError): K8sDeployment.get_by_name(config=config, name=name)
def del_deployment(self, name): o = K8sDeployment(config=self.k8s_config, name=name) return o.delete()
def get_deployment(self, name): o = K8sDeployment(config=self.k8s_config, name=name) return o.get_model()
def test_get_by_name_invalid_name(self): name = object() with self.assertRaises(SyntaxError): K8sDeployment.get_by_name(name=name)
def test_init_with_invalid_config(self): config = object() with self.assertRaises(SyntaxError): K8sDeployment(config=config)
def test_get_by_name_none_args(self): with self.assertRaises(SyntaxError): K8sDeployment.get_by_name()