Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit bb75640

Browse files
authored
Collect resource types also for non-preferred API version (#67)
* #61 add a test for the bug * #61 collect non-preferred API versions, too * #61 simplify code * #61 also test the API version * #61 use same debug log message as before * update dependencies incl. pykube-ng * fix build: ekalinin/nodeenv#253
1 parent 050b179 commit bb75640

File tree

6 files changed

+224
-76
lines changed

6 files changed

+224
-76
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
# formatters
1212

1313
- repo: https://github.com/asottile/reorder_python_imports
14-
rev: v1.9.0
14+
rev: v2.1.0
1515
hooks:
1616
- id: reorder-python-imports
1717

@@ -27,7 +27,7 @@ repos:
2727
- id: black
2828

2929
- repo: https://github.com/asottile/pyupgrade
30-
rev: v1.26.2
30+
rev: v2.1.0
3131
hooks:
3232
- id: pyupgrade
3333
stages: [push]
@@ -69,7 +69,7 @@ repos:
6969
stages: [push]
7070

7171
- repo: https://github.com/adrienverge/yamllint
72-
rev: v1.20.0
72+
rev: v1.21.0
7373
hooks:
7474
- id: yamllint
7575
args: ["--strict", "-d", "{rules: {line-length: {max: 180}}}"]
@@ -82,16 +82,10 @@ repos:
8282
#
8383

8484
- repo: https://github.com/pre-commit/mirrors-mypy
85-
rev: v0.761
85+
rev: v0.770
8686
hooks:
8787
- id: mypy
8888

89-
- repo: https://github.com/pryorda/dockerfilelint-precommit-hooks
90-
rev: v0.1.0
91-
hooks:
92-
- id: dockerfilelint
93-
stages: [commit] # required
94-
9589
# miscellaneous
9690

9791
- repo: https://github.com/pre-commit/pre-commit-hooks
@@ -125,6 +119,6 @@ repos:
125119
# http://jorisroovers.com/gitlint/#using-gitlint-through-pre-commit
126120

127121
- repo: https://github.com/jorisroovers/gitlint
128-
rev: v0.12.0
122+
rev: v0.13.1
129123
hooks:
130124
- id: gitlint

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ lint:
1515
poetry run pre-commit run --all-files
1616

1717
test: install lint
18-
poetry run mypy --ignore-missing-imports kube_janitor
1918
poetry run coverage run --source=kube_janitor -m py.test -v
2019
poetry run coverage report
2120

kube_janitor/resources.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def namespaced_object_factory(kind: str, name: str, api_version: str):
1414
)
1515

1616

17+
def discover_api_group(api, group_version: str):
18+
logger.debug(f"Collecting resources in API group {group_version}..")
19+
response = api.get(version=group_version)
20+
response.raise_for_status()
21+
return response.json()["resources"]
22+
23+
1724
def discover_namespaced_api_resources(api):
1825
core_version = "v1"
1926
r = api.get(version=core_version)
@@ -22,30 +29,49 @@ def discover_namespaced_api_resources(api):
2229
# ignore subresources like pods/proxy
2330
if (
2431
resource["namespaced"]
25-
and "delete" in resource["verbs"]
2632
and "/" not in resource["name"]
33+
and "delete" in resource["verbs"]
2734
):
2835
yield core_version, resource
2936

3037
r = api.get(version="/apis")
3138
r.raise_for_status()
39+
group_versions = set()
3240
for group in r.json()["groups"]:
41+
pref_version = group["preferredVersion"]["groupVersion"]
42+
group_versions.add((pref_version, pref_version))
43+
for version in group.get("versions", []):
44+
group_version = version["groupVersion"]
45+
group_versions.add((group_version, pref_version))
46+
47+
yielded = set()
48+
non_preferred = []
49+
for group_version, pref_version in sorted(group_versions):
3350
try:
34-
pref_version = group["preferredVersion"]["groupVersion"]
35-
logger.debug(f"Collecting resources in API group {pref_version}..")
36-
r2 = api.get(version=pref_version)
37-
r2.raise_for_status()
38-
for resource in r2.json()["resources"]:
39-
if (
40-
resource["namespaced"]
41-
and "delete" in resource["verbs"]
42-
and "/" not in resource["name"]
43-
):
44-
yield pref_version, resource
51+
resources = discover_api_group(api, group_version)
4552
except Exception as e:
46-
logger.error(
47-
f"Could not collect resources in API group {pref_version}: {e}"
53+
# do not crash if one API group is not available
54+
# see https://codeberg.org/hjacobs/kube-web-view/issues/64
55+
logger.warning(
56+
f"Could not collect resources in API group {group_version}: {e}"
4857
)
58+
continue
59+
60+
for resource in resources:
61+
if (
62+
resource["namespaced"]
63+
and "/" not in resource["name"]
64+
and "delete" in resource["verbs"]
65+
):
66+
if group_version == pref_version:
67+
yield group_version, resource
68+
yielded.add((group_version, resource["name"]))
69+
else:
70+
non_preferred.append((group_version, resource))
71+
72+
for group_version, resource in non_preferred:
73+
if (group_version, resource["name"]) not in yielded:
74+
yield group_version, resource
4975

5076

5177
def get_namespaced_resource_types(api):

poetry.lock

Lines changed: 47 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_clean_up.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def get(**kwargs):
557557
api_mock.delete.assert_called_once_with(
558558
data='{"propagationPolicy": "Background"}',
559559
namespace="ns-1",
560-
url="customfoos/foo-1",
560+
url="/customfoos/foo-1",
561561
version="srcco.de/v1",
562562
)
563563

@@ -640,7 +640,7 @@ def get(**kwargs):
640640
api_mock.delete.assert_called_once_with(
641641
data='{"propagationPolicy": "Background"}',
642642
namespace="ns-1",
643-
url="customfoos/foo-1",
643+
url="/customfoos/foo-1",
644644
version="srcco.de/v1",
645645
)
646646

@@ -733,6 +733,6 @@ def get(**kwargs):
733733
api_mock.delete.assert_called_once_with(
734734
data='{"propagationPolicy": "Background"}',
735735
namespace="ns-1",
736-
url="customfoos/foo-1",
736+
url="/customfoos/foo-1",
737737
version="srcco.de/v1",
738738
)

0 commit comments

Comments
 (0)