Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflector:_list_and_update check k8s api response error before read the items #722

Open
ealyn opened this issue Apr 23, 2023 · 5 comments
Open

Comments

@ealyn
Copy link

ealyn commented Apr 23, 2023

I got an KeyError: 'items' while using KubeIngressProxy, and I add some log to find out reason:

[I 2023-04-23 07:22:57.264 JupyterHub reflector:231] initial_resources keys: dict_keys(['kind', 'apiVersion', 'metadata', 'status', 'message', 'reason', 'details', 'code'])
[I 2023-04-23 07:22:57.265 JupyterHub reflector:232] initial_resources: {'kind': 'Status', 'apiVersion': 'v1', 'metadata': {}, 'status': 'Failure', 'message': 'ingresses.networking.k8s.io is forbidden: User "system:serviceaccount:jupyterhub:hub" cannot list resource "ingresses" in API group "networking.k8s.io" in the namespace "jupyterhub"', 'reason': 'Forbidden', 'details': {'group': 'networking.k8s.io', 'kind': 'ingresses'}, 'code': 403}
[E 2023-04-23 07:22:57.265 JupyterHub reflector:387] Initial list of ingresses failed
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/kubespawner/reflector.py", line 385, in start
await self._list_and_update()
File "/usr/local/lib/python3.9/site-packages/kubespawner/reflector.py", line 235, in _list_and_update
for p in initial_resources["items"]
KeyError: 'items'

Proposed change

  1. function reflector:_list_and_update check k8s api response error before read the items
  2. jupyterhub chart templates/hub/rbac.yaml add ingress api (for https://github.com/jupyterhub/helm-chart)
@consideRatio
Copy link
Member

consideRatio commented Apr 23, 2023

There may be another issue as well, but the key issue is permissions.

The jupyterhub chart isn't pre-configured to grant tge required permissions.

'Failure', 'message': 'ingresses.networking.k8s.io is forbidden: User "system:serviceaccount:jupyterhub:hub" cannot list resource "ingresses" in API group "networking.k8s.io" in the namespace "jupyterhub"'

I recall there are docstrings about required permissions to be found in the class defintion for KubeIngressProxy.

@consideRatio
Copy link
Member

consideRatio commented Apr 23, 2023

A fix for this project is to fail with a better error message if we receive a forbidden response.

@dolfinus
Copy link
Contributor

dolfinus commented May 17, 2023

reflector._list_and_update calls api.list_namespaced_ingress, but it does not raise exceptions if Kubernetes returned 403 error.

This is because _preload_content=False is used, and method parses response object manually:

kwargs = dict(
label_selector=self.label_selector,
field_selector=self.field_selector,
_request_timeout=self.request_timeout,
_preload_content=False,
)

It was added in #424. I'm not sure that it should be used in reflector._list_and_update at all, watcher implementation already has its own options:

method = partial(
getattr(self.api, self.list_method_name), _preload_content=False
)

@ealyn
Copy link
Author

ealyn commented May 23, 2023

@dolfinus as the log I printed begining, the exception is raise at for p in initial_resources["items"]:

initial_resources_raw = await list_method(**kwargs)
# This is an atomic operation on the dictionary!
initial_resources = json.loads(await initial_resources_raw.read())
self.resources = {
f'{p["metadata"]["namespace"]}/{p["metadata"]["name"]}': p
for p in initial_resources["items"]
}

@dolfinus
Copy link
Contributor

If await list_method(**kwargs) is called without _preload_content=False, and Kubernetes API returned 403 error, this function raises exception before for p in initial_resources["items"].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants