Skip to content

Commit bde1671

Browse files
Add Makefile
The logic of some of the scripts under "./scripts/" has been moved to the new Makefile. This improves the developer experience since this Makefile provides all the commands a developer needs to write code in this repo. Apart from that, this commit also fixes the last lint issues, so now the linter returns a 10/10 score.
1 parent 9f47365 commit bde1671

14 files changed

+75
-68
lines changed

.github/workflows/test-pr.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fetch-depth: 0
1717

1818
- name: Check pyproject.toml
19-
run: ./scripts/check-pyproject.sh
19+
run: make check-pyproject
2020

2121
- name: Set up Python 3.11
2222
uses: actions/setup-python@v4
@@ -30,7 +30,7 @@ jobs:
3030
run: poetry install
3131

3232
- name: Linter
33-
run: scripts/format-code.sh --check
33+
run: make lint
3434

3535
- name: Run unittests
36-
run: poetry run pytest tests --timeout 15
36+
run: make unittests

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,6 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
#.idea/
161+
162+
# Make
163+
out/

Makefile

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
SRC_DIR := ./generic_k8s_webhook
2+
TEST_DIR := ./tests
3+
SRC_FILES := $(shell find $(SRC_DIR) -type f -name '*.py')
4+
TEST_FILES := $(shell find $(TEST_DIR) -type f -name '*.py')
5+
6+
out/install-deps.stamp: pyproject.toml poetry.lock
7+
poetry install
8+
mkdir -p out
9+
touch out/install-deps.stamp
10+
11+
install-deps: out/install-deps.stamp
12+
13+
out/build.stamp: install-deps $(SRC_FILES) $(TEST_FILES)
14+
poetry build
15+
touch out/build.stamp
16+
17+
build: out/build.stamp
18+
19+
.PHONY: lint
20+
lint: build
21+
poetry run isort $(SRC_DIR) $(TEST_DIR) -c
22+
poetry run black $(SRC_DIR) $(TEST_DIR) --check
23+
poetry run pylint $(SRC_DIR) -v
24+
25+
.PHONY: format
26+
format: build
27+
poetry run isort $(SRC_DIR) $(TEST_DIR)
28+
poetry run black $(SRC_DIR) $(TEST_DIR)
29+
30+
.PHONY: unittests
31+
unittests: build
32+
poetry run pytest tests --timeout 15
33+
34+
.PHONY: check-pyproject
35+
check-pyproject:
36+
echo "Check the pyproject.toml has 'version = \"0.0.0\"'"
37+
grep 'version = "0.0.0"' pyproject.toml
38+
39+
.PHONY: docker
40+
docker:
41+
docker build -t generic-k8s-webhook:latest .
42+
43+
all-tests: check-pyproject lint unittests docker
44+
45+
all-tests-seq: | check-pyproject lint unittests docker

docs/contributor-guide.md

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Required tools
44

55
- [Git](https://git-scm.com/downloads)
6-
- [Python 3.10+](https://www.python.org/downloads/)
6+
- [Python 3.11+](https://www.python.org/downloads/)
77
- [Poetry](https://python-poetry.org/docs/)
88
- [Docker](https://docs.docker.com/install/)
99

@@ -29,44 +29,48 @@ poetry run python3 generic_k8s_webhook/main.py --config <GenericWebhookConfigFil
2929

3030
## Format code
3131

32-
The project uses [black](https://black.readthedocs.io/en/stable/) and [isort](https://pycqa.github.io/isor) to format the code. To do so automatically, you can run this script:
32+
The project uses [black](https://black.readthedocs.io/en/stable/) and [isort](https://pycqa.github.io/isor) to format the code. To do so automatically, you can execute this make rule:
3333

3434
```bash
35-
./scripts/format-code.sh
35+
make format
3636
```
3737

3838
## Test
3939

40-
You can run all the tests executed in the CI by calling:
40+
You can run (sequentially) all the tests executed in the CI by calling:
4141

4242
```bash
43-
./scripts/run-all-tests.sh
43+
make all-tests-seq
4444
```
4545

4646
These tests can be splitted in 3 categories.
4747

4848
### Linter
4949

50-
The linting phase checks that the code is well [formatted](#format-code). Apart from that, it also runs [pylint](https://www.pylint.org/). In order to pass the linting phase, `pylint` must not detect any error and must give an overall score of >9.5.
50+
The linting phase checks that the code is well [formatted](#format-code). Apart from that, it also runs [pylint](https://www.pylint.org/). In order to pass the linting phase, `pylint` must not detect any error,
51+
even if it's a minor one.
5152

5253
```bash
53-
./scripts/format-code.sh --check
54+
make lint
5455
```
5556

57+
In some cases, it's acceptable to add `# pylint: disable=<rule>` to disable
58+
a specific linting issue in a specific line, only if fixing this issue makes the code worse.
59+
5660
### Unittests and e2e tests
5761

5862
We use [pytest](https://docs.pytest.org/en/7.3.x/) to test the functionality of the app. These tests are defined under the [tests](../tests/) directory and they are a mix of both pure unittests and end-to-end tests.
5963

6064
```bash
61-
poetry run pytest tests
65+
make unittests
6266
```
6367

6468
### Docker build
6569

6670
The last phase of our testing suite is building the docker container that has our app installed in it.
6771

6872
```bash
69-
docker build -t generic-k8s-webhook:latest .
73+
make docker
7074
```
7175

7276
## Structure of the code

generic_k8s_webhook/config_parser/entrypoint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import copy
22

3-
import generic_k8s_webhook.config_parser.expr_parser as expr_parser
43
import generic_k8s_webhook.config_parser.operator_parser as op_parser
54
from generic_k8s_webhook import utils
5+
from generic_k8s_webhook.config_parser import expr_parser
66
from generic_k8s_webhook.config_parser.action_parser import ActionParserV1
77
from generic_k8s_webhook.config_parser.jsonpatch_parser import JsonPatchParserV1
88
from generic_k8s_webhook.config_parser.webhook_parser import WebhookParserV1

generic_k8s_webhook/config_parser/expr_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def ref(self, items):
110110

111111
def boolean(self, items):
112112
(elem,) = items
113-
elem_bool = True if elem == "true" else False
113+
elem_bool = elem == "true"
114114
return op.Const(elem_bool)
115115

116116

generic_k8s_webhook/config_parser/operator_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import abc
22
import inspect
33

4-
import generic_k8s_webhook.config_parser.expr_parser as expr_parser
54
from generic_k8s_webhook import operators, utils
5+
from generic_k8s_webhook.config_parser import expr_parser
66
from generic_k8s_webhook.config_parser.common import ParsingException
77

88

generic_k8s_webhook/http_server.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def run(self) -> None:
4343
while not self.stop_event.wait(self.refresh_period):
4444
try:
4545
self._reload_manifest()
46-
except Exception as e:
46+
except Exception as e: # pylint: disable=broad-exception-caught
4747
logging.error(e, exc_info=True)
4848

4949
def get_webhooks(self) -> list[Webhook]:
@@ -61,7 +61,7 @@ class BaseHandler(http.server.BaseHTTPRequestHandler):
6161
def do_GET(self):
6262
try:
6363
self._do_get()
64-
except Exception as e:
64+
except Exception as e: # pylint: disable=broad-exception-caught
6565
logging.error(e, exc_info=True)
6666

6767
def _do_get(self):
@@ -74,7 +74,7 @@ def _do_get(self):
7474
def do_POST(self):
7575
try:
7676
self._do_post()
77-
except Exception as e:
77+
except Exception as e: # pylint: disable=broad-exception-caught
7878
logging.error(e, exc_info=True)
7979

8080
def _do_post(self):
@@ -124,7 +124,7 @@ def _get_path(self) -> str:
124124

125125

126126
class Server:
127-
def __init__(
127+
def __init__( # pylint: disable=too-many-arguments
128128
self, port: int, certfile: str, keyfile: str, generic_webhook_config_file: str, config_refresh_period: float = 5
129129
) -> None:
130130
"""Validating/Mutating webhook server. It listens to requests made at port <port>

generic_k8s_webhook/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def cli(args):
4141
def start_server(args):
4242
server = Server(args.port, args.cert_file, args.key_file, args.config)
4343

44-
def stop_server(*args):
44+
def stop_server(*args): # pylint: disable=unused-argument
4545
threading.Thread(target=server.stop).start()
4646

4747
signal.signal(signal.SIGINT, stop_server)

generic_k8s_webhook/operators.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,9 @@ def get_value(self, contexts: list) -> Any:
157157
list_arg_values = self.args.get_value(contexts)
158158
if len(list_arg_values) < 2:
159159
return True
160-
elif len(list_arg_values) == 2:
160+
if len(list_arg_values) == 2:
161161
return self._op(list_arg_values[0], list_arg_values[1])
162-
else:
163-
raise ValueError("A comparison cannot have more than 2 operands")
162+
raise ValueError("A comparison cannot have more than 2 operands")
164163

165164
def input_type(self) -> type | None:
166165
return list[None]

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ missing-module-docstring, \
3737
missing-class-docstring, \
3838
missing-function-docstring, \
3939
too-few-public-methods, \
40+
fixme, \
4041
"""
4142

4243
[build-system]

scripts/check-pyproject.sh

-9
This file was deleted.

scripts/format-code.sh

-21
This file was deleted.

scripts/run-all-tests.sh

-15
This file was deleted.

0 commit comments

Comments
 (0)