def service_create(self, context, service): LOG.debug("service_create") bay = conductor_utils.retrieve_bay(context, service.bay_uuid) self.k8s_api = k8s.create_k8s_api(context, bay) manifest = k8s_manifest.parse(service.manifest) try: resp = self.k8s_api.create_namespaced_service(body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) if resp is None: raise exception.ServiceCreationFailed(bay_uuid=service.bay_uuid) service['uuid'] = resp.metadata.uid service['name'] = resp.metadata.name service['labels'] = ast.literal_eval(resp.metadata.labels) service['selector'] = ast.literal_eval(resp.spec.selector) service['ip'] = resp.spec.cluster_ip service_value = [] for p in resp.spec.ports: ports = p.to_dict() if not ports['name']: ports['name'] = 'k8s-service' service_value.append(ports) service['ports'] = service_value return service
def service_create(self, context, service): LOG.debug("service_create") self.k8s_api = k8s.create_k8s_api(context, service.bay_uuid) manifest = k8s_manifest.parse(service.manifest) try: resp = self.k8s_api.create_namespaced_service(body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) if resp is None: raise exception.ServiceCreationFailed(bay_uuid=service.bay_uuid) service['uuid'] = resp.metadata.uid service['name'] = resp.metadata.name service['labels'] = ast.literal_eval(resp.metadata.labels) service['selector'] = ast.literal_eval(resp.spec.selector) service['ip'] = resp.spec.cluster_ip service_value = [] for p in resp.spec.ports: ports = p.to_dict() if not ports['name']: ports['name'] = 'k8s-service' service_value.append(ports) service['ports'] = service_value return service
def parse_manifest(self): try: manifest = k8s_manifest.parse(self._get_manifest()) except ValueError as e: raise exception.InvalidParameterValue(message=str(e)) try: self.name = manifest["metadata"]["name"] except (KeyError, TypeError): raise exception.InvalidParameterValue( _("Field metadata['name'] can't be empty in manifest.")) try: self.replicas = manifest["spec"]["replicas"] except (KeyError, TypeError): pass try: self.selector = manifest["spec"]["selector"] except (KeyError, TypeError): raise exception.InvalidParameterValue( _("Field spec['selector'] can't be empty in manifest.")) try: self.labels = manifest["spec"]["template"]["metadata"]["labels"] except (KeyError, TypeError): raise exception.InvalidParameterValue(_( "Field spec['template']['metadata']['labels'] " "can't be empty in manifest.")) try: images = [] for cont in manifest["spec"]["template"]["spec"]["containers"]: images.append(cont["image"]) self.images = images except (KeyError, TypeError): raise exception.InvalidParameterValue(_( "Field spec['template']['spec']['containers'] " "can't be empty in manifest."))
def parse_manifest(self): try: manifest = k8s_manifest.parse(self._get_manifest()) except ValueError as e: raise exception.InvalidParameterValue(message=str(e)) try: self.name = manifest["id"] except KeyError: raise exception.InvalidParameterValue( "'id' can't be empty in manifest.") if "labels" in manifest: self.labels = manifest["labels"]
def service_create(self, context, service): LOG.debug("service_create") self.k8s_api = k8s.create_k8s_api(context, service) manifest = k8s_manifest.parse(service.manifest) try: self.k8s_api.createService(body=manifest, namespaces='default') except error.HTTPError as err: message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) # call the service object to persist in db service.create(context) return service
def service_create(self, context, service): LOG.debug("service_create") self.k8s_api = k8s.create_k8s_api(context, service) manifest = k8s_manifest.parse(service.manifest) try: self.k8s_api.create_namespaced_service(body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) # call the service object to persist in db service.create(context) return service
def rc_create(self, context, rc): LOG.debug("rc_create") self.k8s_api = k8s.create_k8s_api(context, rc) manifest = k8s_manifest.parse(rc.manifest) try: self.k8s_api.create_namespaced_replication_controller( body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) # call the rc object to persist in db rc.create(context) return rc
def rc_create(self, context, rc): LOG.debug("rc_create") self.k8s_api = k8s.create_k8s_api(context, rc) manifest = k8s_manifest.parse(rc.manifest) try: self.k8s_api.createReplicationController(body=manifest, namespaces='default') except error.HTTPError as err: message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) # call the rc object to persist in db rc.create(context) return rc
def test_parse_with_yaml(self): yaml_str = ''' id: redis-master kind: Service port: 6379 containerPort: 6379 selector: name: redis-master labels: name: redis-master ''' manifest = k8s_manifest.parse(yaml_str) self.assertIsInstance(manifest, dict)
def pod_update(self, context, pod): LOG.debug("pod_update %s", pod.uuid) self.k8s_api = k8s.create_k8s_api(context, pod) manifest = k8s_manifest.parse(pod.manifest) try: self.k8s_api.replacePod(name=pod.name, body=manifest, namespaces='default') except error.HTTPError as err: message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) # call the pod object to persist in db pod.refresh(context) pod.save() return pod
def service_update(self, context, service): LOG.debug("service_update %s", service.uuid) self.k8s_api = k8s.create_k8s_api(context, service) manifest = k8s_manifest.parse(service.manifest) try: self.k8s_api.replace_namespaced_service(name=str(service.name), body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) # call the service object to persist in db service.refresh(context) service.save() return service
def pod_update(self, context, pod): LOG.debug("pod_update %s", pod.uuid) self.k8s_api = k8s.create_k8s_api(context, pod) manifest = k8s_manifest.parse(pod.manifest) try: self.k8s_api.replace_namespaced_pod(name=str(pod.name), body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) # call the pod object to persist in db pod.refresh(context) pod.save() return pod
def rc_update(self, context, rc): LOG.debug("rc_update %s", rc.uuid) k8s_master_url = _retrieve_k8s_master_url(context, rc) self.k8s_api = k8s_master_url manifest = k8s_manifest.parse(rc.manifest) try: self.k8s_api.replaceReplicationController(name=rc.name, body=manifest, namespaces='default') except error.HTTPError as err: message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) # call the rc object to persist in db rc.refresh(context) rc.save() return rc
def service_update(self, context, service): LOG.debug("service_update %s", service.uuid) k8s_master_url = _retrieve_k8s_master_url(context, service) self.k8s_api = k8s_master_url manifest = k8s_manifest.parse(service.manifest) try: self.k8s_api.replaceService(name=service.name, body=manifest, namespaces='default') except error.HTTPError as err: message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) # call the service object to persist in db service.refresh(context) service.save() return service
def test_parse_with_yaml(self): port = 6389 containerPort = 6380 yaml_str = ''' id: redis-master kind: Service port: %d containerPort: %d selector: name: redis-master labels: name: redis-master ''' % (port, containerPort) manifest = k8s_manifest.parse(yaml_str) self.assertIsInstance(manifest, dict) self.assertEqual(port, manifest['port']) self.assertEqual(containerPort, manifest['containerPort'])
def parse_manifest(self): try: manifest = k8s_manifest.parse(self._get_manifest()) except ValueError as e: raise exception.InvalidParameterValue(message=str(e)) try: self.name = manifest["metadata"]["name"] except (KeyError, TypeError): raise exception.InvalidParameterValue("Field metadata['name'] can't be empty in manifest.") try: self.ports = manifest["spec"]["ports"][:] except (KeyError, TypeError): raise exception.InvalidParameterValue("Field spec['ports'] can't be empty in manifest.") if "selector" in manifest["spec"]: self.selector = manifest["spec"]["selector"] if "labels" in manifest["metadata"]: self.labels = manifest["metadata"]["labels"]
def test_parse_with_json(self): json_str = ''' { "id": "redis-master", "kind": "Service", "apiVersion": "v1beta1", "port": 6379, "containerPort": 6379, "selector": { "name": "redis-master" }, "labels": { "name": "redis-master" } } ''' manifest = k8s_manifest.parse(json_str) self.assertIsInstance(manifest, dict)
def parse_manifest(self): try: manifest = k8s_manifest.parse(self._get_manifest()) except ValueError as e: raise exception.InvalidParameterValue(message=str(e)) try: self.name = manifest["metadata"]["name"] except (KeyError, TypeError): raise exception.InvalidParameterValue( "Field metadata['name'] can't be empty in manifest.") try: self.ports = manifest["spec"]["ports"][:] except (KeyError, TypeError): raise exception.InvalidParameterValue( "Field spec['ports'] can't be empty in manifest.") if "selector" in manifest["spec"]: self.selector = manifest["spec"]["selector"] if "labels" in manifest["metadata"]: self.labels = manifest["metadata"]["labels"]
def parse_manifest(self): try: manifest = k8s_manifest.parse(self._get_manifest()) except ValueError as e: raise exception.InvalidParameterValue(message=str(e)) try: self.name = manifest["metadata"]["name"] except (KeyError, TypeError): raise exception.InvalidParameterValue( "Field metadata['name'] can't be empty in manifest.") images = [] try: for container in manifest["spec"]["containers"]: images.append(container["image"]) self.images = images except (KeyError, TypeError): raise exception.InvalidParameterValue( "Field spec['containers'] can't be empty in manifest.") if "labels" in manifest["metadata"]: self.labels = manifest["metadata"]["labels"]
def rc_create(self, context, rc): LOG.debug("rc_create") self.k8s_api = k8s.create_k8s_api(context, rc.bay_uuid) manifest = k8s_manifest.parse(rc.manifest) try: resp = self.k8s_api.create_namespaced_replication_controller( body=manifest, namespace='default') except rest.ApiException as err: raise exception.KubernetesAPIFailed(err=err) if resp is None: raise exception.ReplicationControllerCreationFailed( bay_uuid=rc.bay_uuid) rc['uuid'] = resp.metadata.uid rc['name'] = resp.metadata.name rc['images'] = [c.image for c in resp.spec.template.spec.containers] rc['labels'] = ast.literal_eval(resp.metadata.labels) rc['replicas'] = resp.status.replicas return rc
def pod_create(self, context, pod): LOG.debug("pod_create") self.k8s_api = k8s.create_k8s_api(context, pod) manifest = k8s_manifest.parse(pod.manifest) try: resp = self.k8s_api.create_namespaced_pod(body=manifest, namespace='default') except rest.ApiException as err: pod.status = 'failed' if err.status != 409: pod.create(context) raise exception.KubernetesAPIFailed(err=err) pod.status = resp.status.phase pod.host = resp.spec.node_name # call the pod object to persist in db # TODO(yuanying): parse pod file and, # - extract pod name and set it # - extract pod labels and set it # When do we get pod labels and name? pod.create(context) return pod
def pod_create(self, context, pod): LOG.debug("pod_create") self.k8s_api = k8s.create_k8s_api(context, pod) manifest = k8s_manifest.parse(pod.manifest) try: resp = self.k8s_api.createPod(body=manifest, namespaces='default') except error.HTTPError as err: pod.status = 'failed' if err.code != 409: pod.create(context) message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) pod.status = resp.status.phase pod.host = resp.spec.host # call the pod object to persist in db # TODO(yuanying): parse pod file and, # - extract pod name and set it # - extract pod labels and set it # When do we get pod labels and name? pod.create(context) return pod
def pod_create(self, context, pod): LOG.debug("pod_create") k8s_master_url = _retrieve_k8s_master_url(context, pod) self.k8s_api = k8s_master_url manifest = k8s_manifest.parse(pod.manifest) try: resp = self.k8s_api.createPod(body=manifest, namespaces='default') except error.HTTPError as err: pod.status = 'failed' if err.code != 409: pod.create(context) message = ast.literal_eval(err.read())['message'] raise exception.KubernetesAPIFailed(code=err.code, message=message) pod.status = resp.status.phase # call the pod object to persist in db # TODO(yuanying): parse pod file and, # - extract pod name and set it # - extract pod labels and set it # When do we get pod labels and name? pod.create(context) return pod
def pod_create(self, context, pod): LOG.debug("pod_create") self.k8s_api = k8s.create_k8s_api(context, pod.bay_uuid) manifest = k8s_manifest.parse(pod.manifest) try: resp = self.k8s_api.create_namespaced_pod(body=manifest, namespace='default') except rest.ApiException as err: pod.status = 'failed' raise exception.KubernetesAPIFailed(err=err) if resp is None: raise exception.PodCreationFailed(bay_uuid=pod.bay_uuid) pod['uuid'] = resp.metadata.uid pod['name'] = resp.metadata.name pod['images'] = [c.image for c in resp.spec.containers] pod['labels'] = ast.literal_eval(resp.metadata.labels) pod['status'] = resp.status.phase pod['host'] = resp.spec.node_name return pod
def test_parse_with_json(self): port = 6379 containerPort = 6380 json_str = ''' { "id": "redis-master", "kind": "Service", "apiVersion": "v1", "port": %d, "containerPort": %d, "selector": { "name": "redis-master" }, "labels": { "name": "redis-master" } } ''' % (port, containerPort) manifest = k8s_manifest.parse(json_str) self.assertIsInstance(manifest, dict) self.assertEqual(port, manifest['port']) self.assertEqual(containerPort, manifest['containerPort'])