def delete(body, meta, spec, status, **kwargs): api = K8SApi.from_env() deployment_name = f"{meta['name']}-{LAYER}" logger.info(f"Deleting deployment {deployment_name} ...") try: deployment = Deployment.objects(api).filter( namespace=meta["namespace"]).get(name=deployment_name) deployment.delete() except ObjectDoesNotExist: logger.warning(f"Deployment {deployment_name} doesn't exist") logger.info(f"Deleting deployment {deployment_name} ... done!") service_name = f"{meta['name']}" logger.info(f"Deleting service {service_name} ...") try: service = Service.objects(api).filter(namespace=meta["namespace"]).get( name=service_name) service.delete() except ObjectDoesNotExist: logger.warning(f"Service {service_name} doesn't exist") logger.info(f"Deleting service {service_name} ... done!") api.session.close() return {'job1-status': 100}
def find_backend_application(client, ingress, rule): ''' The Ingress object might not have a "application" label, so let's try to find the application by looking at the backend service and its pods ''' paths = rule.get('http', {}).get('paths', []) selectors = [] for path in paths: service_name = path.get('backend', {}).get('serviceName') if service_name: try: service = Service.objects( client, namespace=ingress.namespace).get(name=service_name) except ObjectDoesNotExist: logger.debug( f'Referenced service does not exist: {ingress.namespace}/{service_name}' ) else: selector = service.obj['spec'].get('selector', {}) selectors.append(selector) application = get_application_from_labels(selector) if application: return application # we still haven't found the application, let's look up pods by label selectors for selector in selectors: application_candidates = set() for pod in Pod.objects(client).filter(namespace=ingress.namespace, selector=selector): application = get_application_from_labels(pod.labels) if application: application_candidates.add(application) if len(application_candidates) == 1: return application_candidates.pop() return ''
def test_services(kube_cluster: Cluster): services_response = Service.objects( kube_cluster.kube_client).filter(namespace="default") for service_name in [ "apiserver", "giantswarm-todo-app-mysql", "todomanager" ]: service = services_response.get_by_name(service_name) assert service is not None
def delete(namespace, names, logger): api = HTTPClient(KubeConfig.from_file()) for name in names: deploy = Deployment.objects(api, namespace=namespace).get(name=name) deploy.delete() logger.info(f'delete Deployment: {str(deploy)}') service = Service.objects(api, namespace=namespace).get(name=name) service.delete() logger.info(f'delete Service: {str(service)}')
def get_application_label_from_service(client: pykube.HTTPClient, namespace, service_name): try: service = Service.objects(client, namespace=namespace).get(name=service_name) except ObjectDoesNotExist: logger.debug( f"Referenced service does not exist: {namespace}/{service_name}") return None, None else: selector = service.obj["spec"].get("selector", {}) application = get_application_from_labels(selector) if application: return application, [] return "", selector
def test_get_todos(kube_cluster: Cluster, cluster_type: str, chart_extra_info: Dict[str, str]): # unfortunately, when services and deployments are ready, traffic forwarding doesn't yet # work fo 100% :( That's why we need a retry. logger.info( f"Running on cluster of type {cluster_type}, extra info is: {chart_extra_info}" ) apiserver_service = (Service.objects(kube_cluster.kube_client).filter( namespace="default").get(name="apiserver")) res = proxy_http_get(kube_cluster.kube_client, apiserver_service, "v1/todo") assert res is not None assert res.content == b"null\n" assert res.status_code == 200 assert "Content-Type" in res.headers assert res.headers["Content-Type"] == "application/json; charset=utf-8"
def test_create_delete_todo_entry(kube_cluster: Cluster): apiserver_service = (Service.objects(kube_cluster.kube_client).filter( namespace="default").get(name="apiserver")) body = '{"Text":"testing"}' headers = {"Content-Type": "application/json"} res = proxy_http_post( kube_cluster.kube_client, apiserver_service, "v1/todo", data=body, headers=headers, ) assert res is not None assert res.status_code == 200 todo_id = json.loads(res.text)["id"] res = proxy_http_delete(kube_cluster.kube_client, apiserver_service, f"v1/todo/{todo_id}") assert res is not None assert res.status_code == 200
def test_stormforger_load_app_creation( kube_cluster: Cluster, stormforger_load_app_factory: StormforgerLoadAppFactoryFunc) -> None: """This test deploys stormforger_load_app and checks if it response to basic requests. To make this example run, you need a ready cluster with Giant Swarm App Platform. The easiest way to create it is to run: `kube-app-testing.sh -j`. """ loadtest_app = stormforger_load_app_factory(1, "test.local", None) assert loadtest_app.app is not None wait_for_deployments_to_run(kube_cluster.kube_client, [loadtest_app.app.name], loadtest_app.app.namespace, 60) srv: Service = Service.objects(kube_cluster.kube_client, loadtest_app.app.namespace).get_by_name( loadtest_app.app.name) assert srv is not None res = srv.proxy_http_get("/", headers={"Host": "test.local"}) assert res is not None assert res.ok assert res.status_code == 200 assert res.text.startswith("GET / HTTP/1.1\r\nHost: test.local\r\n")
def find_backend_application(client: pykube.HTTPClient, ingress: Ingress, rule): """ Find the application ID for a given Ingress object. The Ingress object might not have a "application" label, so let's try to find the application by looking at the backend service and its pods """ paths = rule.get("http", {}).get("paths", []) selectors = [] for path in paths: service_name = path.get("backend", {}).get("serviceName") if service_name: try: service = Service.objects(client, namespace=ingress.namespace).get( name=service_name ) except ObjectDoesNotExist: logger.debug( f"Referenced service does not exist: {ingress.namespace}/{service_name}" ) else: selector = service.obj["spec"].get("selector", {}) selectors.append(selector) application = get_application_from_labels(selector) if application: return application # we still haven't found the application, let's look up pods by label selectors for selector in selectors: application_candidates = set() for pod in Pod.objects(client).filter( namespace=ingress.namespace, selector=selector ): application = get_application_from_labels(pod.labels) if application: application_candidates.add(application) if len(application_candidates) == 1: return application_candidates.pop() return ""