Skip to content

Commit 1529114

Browse files
committed
update linting
1 parent 89a0104 commit 1529114

22 files changed

+133
-97
lines changed

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
---
12
github: acrlabs

.github/workflows/test.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
---
12
name: Run tests
23

3-
on:
4+
on: # yamllint disable-line rule:truthy
45
push:
56

67
jobs:

.mise.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tools]
2-
python = "3.10"
2+
python = "3.11"
33

44
[env]
55
MYPYPATH = "./stubs"

.pre-commit-config.yaml

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ repos:
55
rev: v4.4.0
66
hooks:
77
- id: end-of-file-fixer
8-
- id: check-yaml
9-
args: ["--allow-multiple-documents"]
108
- id: trailing-whitespace
11-
- repo: https://github.com/pycqa/isort
12-
rev: 5.13.2
9+
- repo: https://github.com/adrienverge/yamllint.git
10+
rev: v1.35.1
1311
hooks:
14-
- id: isort
15-
args:
16-
- --sl
17-
- repo: https://github.com/PyCQA/flake8
18-
rev: 6.0.0
19-
hooks:
20-
- id: flake8
12+
- id: yamllint
13+
args: ['--strict', '-d', '{extends: default, rules: {line-length: {max: 120}}}']
2114
- repo: https://github.com/pre-commit/mirrors-mypy
2215
rev: v1.4.1
2316
hooks:
2417
- id: mypy
2518
additional_dependencies: [cdk8s, types-simplejson, types-pyyaml]
19+
- repo: https://github.com/astral-sh/ruff-pre-commit
20+
rev: v0.4.2
21+
hooks:
22+
- id: ruff
23+
args: ["--fix"]
24+
- id: ruff-format

examples/workflows/k8s_plan.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
---
12
name: Compute k8s plan
23

3-
on:
4+
on: # yamllint disable-line rule:truthy
45
pull_request:
56
paths:
67
- 'k8s/**'

examples/workflows/pr_comment_finished.yml

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
---
12
name: Comment on the PR
23

3-
on:
4+
on: # yamllint disable-line rule:truthy
45
workflow_run:
56
workflows: ["Compute k8s plan"]
67
types:
@@ -12,7 +13,8 @@ jobs:
1213
if: >
1314
github.event.workflow_run.event == 'pull_request' &&
1415
github.event.workflow_run.conclusion == 'success'
15-
16+
env:
17+
ASSETS_URL: "https://raw.githubusercontent.com/acrlabs/fireconfig/master/assets"
1618
steps:
1719
- name: Download artifact
1820
uses: actions/download-artifact@v4
@@ -41,10 +43,11 @@ jobs:
4143
echo "<!-- 🔥config summary -->" > fireconfig-comment.md
4244
echo "## Kubernetes Object DAG" >> fireconfig-comment.md
4345
cat k8s-plan-artifacts/dag.mermaid >> fireconfig-comment.md
44-
echo '<img src="https://raw.githubusercontent.com/acrlabs/fireconfig/master/assets/new.png" width=10/> New object' >> fireconfig-comment.md
45-
echo '<img src="https://raw.githubusercontent.com/acrlabs/fireconfig/master/assets/removed.png" width=10/> Deleted object' >> fireconfig-comment.md
46-
echo '<img src="https://raw.githubusercontent.com/acrlabs/fireconfig/master/assets/changed.png" width=10/> Updated object' >> fireconfig-comment.md
47-
echo '<img src="https://raw.githubusercontent.com/acrlabs/fireconfig/master/assets/pod_recreate.png" width=10/> Updated object (causes pod recreation)' >> fireconfig-comment.md
46+
echo '<img src="${ASSETS_URL}/new.png" width=10/> New object' >> fireconfig-comment.md
47+
echo '<img src="${ASSETS_URL}/removed.png" width=10/> Deleted object' >> fireconfig-comment.md
48+
echo '<img src="${ASSETS_URL}/changed.png" width=10/> Updated object' >> fireconfig-comment.md
49+
echo '<img src="${ASSETS_URL}/pod_recreate.png" width=10/> Updated object (causes pod recreation)' \
50+
>> fireconfig-comment.md
4851
echo "## Detailed Diff" >> fireconfig-comment.md
4952
cat k8s-plan-artifacts/k8s.df >> fireconfig-comment.md
5053

examples/workflows/pr_comment_starting.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
---
12
name: Update the PR Comment
23

3-
on:
4+
on: # yamllint disable-line rule:truthy
45
#######################################################################################
56
# WARNING: DO NOT CHANGE THIS ACTION TO CHECK OUT OR EXECUTE ANY CODE!!!!! #
67
# #

fireconfig/container.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212

1313
class ContainerBuilder:
14-
def __init__(self, name: str, image: str, command: T.Optional[str] = None, args: T.Optional[T.Sequence[str]] = None):
14+
def __init__(
15+
self, name: str, image: str, command: T.Optional[str] = None, args: T.Optional[T.Sequence[str]] = None
16+
):
1517
self._name = name
1618
self._image = image
1719

@@ -35,7 +37,8 @@ def with_env(self, env: EnvBuilder, names: T.Optional[T.Sequence[str]] = None) -
3537
return self
3638

3739
def with_resources(
38-
self, *,
40+
self,
41+
*,
3942
requests: T.Optional[T.Mapping[str, T.Any]] = None,
4043
limits: T.Optional[T.Mapping[str, T.Any]] = None,
4144
) -> T.Self:
@@ -67,9 +70,7 @@ def build(self) -> k8s.Container:
6770
else:
6871
optional["env"] = []
6972
if self._ports:
70-
optional["ports"] = [
71-
k8s.ContainerPort(container_port=p) for p in self._ports
72-
]
73+
optional["ports"] = [k8s.ContainerPort(container_port=p) for p in self._ports]
7374
if self._resources is not None:
7475
optional["resources"] = {}
7576
if self._resources.limits is not None:

fireconfig/deployment.py

+26-23
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def service_name(self) -> str:
4141
def with_replicas(self, min_replicas: int, max_replicas: T.Optional[int] = None) -> T.Self:
4242
if max_replicas is not None:
4343
if min_replicas > max_replicas:
44-
raise ValueError(f'min_replicas cannot be larger than max_replicas: {min_replicas} > {max_replicas}')
44+
raise ValueError(f"min_replicas cannot be larger than max_replicas: {min_replicas} > {max_replicas}")
4545
self._replicas = (min_replicas, max_replicas)
4646
else:
4747
self._replicas = min_replicas
@@ -77,7 +77,7 @@ def with_toleration(self, key: str, value: str = "", effect: TaintEffect = Taint
7777
self._tolerations.append((key, value, effect))
7878
return self
7979

80-
def _build(self, meta: k8s.ObjectMeta, chart: Chart) -> k8s.KubeDeployment:
80+
def _build(self, meta: k8s.ObjectMeta, chart: Chart) -> k8s.KubeDeployment: # noqa: PLR0912
8181
pod_meta: T.MutableMapping[str, T.Any] = {}
8282
if self._pod_annotations:
8383
pod_meta["annotations"] = self._pod_annotations
@@ -96,7 +96,8 @@ def _build(self, meta: k8s.ObjectMeta, chart: Chart) -> k8s.KubeDeployment:
9696
if self._service_account_role is not None:
9797
sa = self._build_service_account(chart)
9898
rb = self._build_role_binding_for_service_account(
99-
chart, sa,
99+
chart,
100+
sa,
100101
self._service_account_role,
101102
self._service_account_role_is_cluster_role,
102103
)
@@ -112,23 +113,22 @@ def _build(self, meta: k8s.ObjectMeta, chart: Chart) -> k8s.KubeDeployment:
112113
self._build_service(chart)
113114

114115
if len(self._tolerations) > 0:
115-
optional["tolerations"] = [
116-
{"key": t[0], "value": t[1], "effect": t[2]} for t in self._tolerations
117-
]
116+
optional["tolerations"] = [{"key": t[0], "value": t[1], "effect": t[2]} for t in self._tolerations]
118117

119118
vols: VolumeDefsWithObject = dict()
120119
for c in self._containers:
121120
vols = {**vols, **c.build_volumes(chart)}
122121

123122
if vols:
124123
optional["volumes"] = []
125-
for (defn, obj) in vols.values():
124+
for defn, obj in vols.values():
126125
optional["volumes"].append(defn)
127126
if obj is not None:
128127
self._deps.append(obj)
129128

130129
depl = k8s.KubeDeployment(
131-
chart, f"{self._tag}depl",
130+
chart,
131+
f"{self._tag}depl",
132132
metadata=meta,
133133
spec=k8s.DeploymentSpec(
134134
selector=k8s.LabelSelector(match_labels=self._selector),
@@ -139,15 +139,17 @@ def _build(self, meta: k8s.ObjectMeta, chart: Chart) -> k8s.KubeDeployment:
139139
containers=[c.build() for c in self._containers],
140140
**optional,
141141
),
142-
)
143-
)
142+
),
143+
),
144144
)
145145

146146
for i in range(len(self._containers)):
147-
depl.add_json_patch(JsonPatch.add(
148-
f"/spec/template/spec/containers/{i}/env/-",
149-
{"name": "POD_OWNER", "value": depl.name},
150-
))
147+
depl.add_json_patch(
148+
JsonPatch.add(
149+
f"/spec/template/spec/containers/{i}/env/-",
150+
{"name": "POD_OWNER", "value": depl.name},
151+
)
152+
)
151153

152154
return depl
153155

@@ -158,12 +160,12 @@ def _build_service_account(self, chart: Chart) -> k8s.KubeServiceAccount:
158160
def _build_service(self, chart: Chart) -> k8s.KubeService:
159161
assert self._service_ports
160162
return k8s.KubeService(
161-
chart, "service",
163+
chart,
164+
"service",
162165
metadata={"name": self._service_name},
163166
spec=k8s.ServiceSpec(
164167
ports=[
165-
k8s.ServicePort(port=p, target_port=k8s.IntOrString.from_number(p))
166-
for p in self._service_ports
168+
k8s.ServicePort(port=p, target_port=k8s.IntOrString.from_number(p)) for p in self._service_ports
167169
],
168170
selector=self._selector,
169171
),
@@ -176,12 +178,13 @@ def _build_role_binding_for_service_account(
176178
role_name: str,
177179
is_cluster_role: bool,
178180
) -> T.Union[k8s.KubeClusterRoleBinding, k8s.KubeRoleBinding]:
179-
180-
subjects = [k8s.Subject(
181-
kind="ServiceAccount",
182-
name=service_account.name,
183-
namespace=chart.namespace,
184-
)]
181+
subjects = [
182+
k8s.Subject(
183+
kind="ServiceAccount",
184+
name=service_account.name,
185+
namespace=chart.namespace,
186+
)
187+
]
185188
role_ref = k8s.RoleRef(
186189
api_group="rbac.authorization.k8s.io",
187190
kind="ClusterRole" if is_cluster_role else "Role",

fireconfig/env.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, env: T.Mapping[str, str] = dict()):
1111

1212
def with_field_ref(self, name: str, field: DownwardAPIField, key: T.Optional[str] = None) -> T.Self:
1313
field_str = str(field)
14-
if field in (DownwardAPIField.ANNOTATION, DownwardAPIField.LABEL):
14+
if field in {DownwardAPIField.ANNOTATION, DownwardAPIField.LABEL}:
1515
field_str = field_str.format(key)
1616

1717
self._env[name] = ("valueFrom", {"fieldRef": {"fieldPath": field_str}})

fireconfig/namespace.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from fireconfig import k8s
44

55
_STANDARD_NAMESPACES = [
6-
'default',
7-
'kube-node-lease',
8-
'kube-public',
9-
'kube-system',
6+
"default",
7+
"kube-node-lease",
8+
"kube-public",
9+
"kube-system",
1010
]
1111

1212

fireconfig/object.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ def build(self, chart: Chart):
4545
return obj
4646

4747
@abstractmethod
48-
def _build(self, meta: k8s.ObjectMeta, chart: Chart):
49-
...
48+
def _build(self, meta: k8s.ObjectMeta, chart: Chart): ...

fireconfig/output.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def format_mermaid_graph(
2323
old_dag_filename: T.Optional[str],
2424
resource_changes: T.Mapping[str, ResourceChanges],
2525
) -> str:
26-
2726
mermaid = "```mermaid\n"
2827
mermaid += "%%{init: {'themeVariables': {'mainBkg': '#ddd'}}}%%\n"
2928
mermaid += "graph LR\n\n"
@@ -66,8 +65,8 @@ def format_diff(resource_changes: T.Mapping[str, ResourceChanges]) -> str:
6665
for res, c in sorted(resource_changes.items()):
6766
diff_details += f"<details><summary>\n\n#### {res}: {c.state.name}\n\n</summary>\n\n"
6867
for path, r1, r2 in c.changes:
69-
r1_str = json.dumps(r1, indent=' ') if r1 != notpresent else r1
70-
r2_str = json.dumps(r2, indent=' ') if r2 != notpresent else r2
68+
r1_str = json.dumps(r1, indent=" ") if r1 != notpresent else r1
69+
r2_str = json.dumps(r2, indent=" ") if r2 != notpresent else r2
7170
diff_details += f"```\n{path}:\n{r1_str} --> {r2_str}\n```\n\n"
7271
diff_details += "</details>\n"
7372

fireconfig/plan.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ def update_state(self, change_type: str, path: str, kind: T.Optional[str]):
8686
elif kind == "Deployment":
8787
# TODO - this is obviously incomplete, it will not detect all cases
8888
# when pod recreation happens
89-
if (
90-
path.startswith("root['spec']['template']['spec']")
91-
or path.startswith("root['spec']['selector']")
92-
):
89+
if path.startswith("root['spec']['template']['spec']") or path.startswith("root['spec']['selector']"):
9390
self._state = ResourceState.ChangedWithPodRecreate
9491
else:
9592
self._state = ResourceState.Changed
@@ -109,7 +106,7 @@ def compute_diff(app: App) -> T.Tuple[T.Mapping[str, T.Any], T.Mapping[str, str]
109106
kinds = {}
110107
old_defs = {}
111108
for filename in glob(f"{app.outdir}/*{app.output_file_extension}"):
112-
with open(filename) as f:
109+
with open(filename, encoding="utf-8") as f:
113110
parsed_filename = re.match(app.outdir + r"\/(\d{4}-)?(.*)" + app.output_file_extension, filename)
114111
old_chart = "UNKNOWN"
115112
if parsed_filename:
@@ -148,7 +145,7 @@ def walk_dep_graph(v: DependencyVertex, subgraphs: T.Mapping[str, ChartSubgraph]
148145

149146

150147
def get_resource_changes(diff: T.Mapping[str, T.Any], kinds: T.Mapping[str, str]) -> T.Mapping[str, ResourceChanges]:
151-
resource_changes: T.MutableMapping[str, ResourceChanges] = defaultdict(lambda: ResourceChanges())
148+
resource_changes: T.MutableMapping[str, ResourceChanges] = defaultdict(ResourceChanges)
152149
for change_type, items in diff.items():
153150
for i in items:
154151
root_item = i.path(output_format="list")[0]
@@ -176,25 +173,25 @@ def find_deleted_nodes(
176173
return
177174

178175
old_dag_lines = []
179-
with open(old_dag_filename) as f:
176+
with open(old_dag_filename, encoding="utf-8") as f:
180177
current_chart = None
181178
del_lines = False
182179

183-
for l in f.readlines():
184-
chart_match = re.match(r"^\s*subgraph (.*)", l)
180+
for ln in f.readlines():
181+
chart_match = re.match(r"^\s*subgraph (.*)", ln)
185182
if chart_match:
186183
current_chart = chart_match.group(1)
187-
elif re.match(r"^\s*end$", l):
184+
elif re.match(r"^\s*end$", ln):
188185
current_chart = None
189-
if l.startswith(DELETED_OBJS_START):
186+
if ln.startswith(DELETED_OBJS_START):
190187
del_lines = True
191-
elif l.startswith(DELETED_OBJS_END):
188+
elif ln.startswith(DELETED_OBJS_END):
192189
del_lines = False
193190
elif current_chart is not None and not del_lines:
194-
old_dag_lines.append((current_chart, l))
191+
old_dag_lines.append((current_chart, ln))
195192

196193
for res, changes in resource_changes.items():
197194
if changes.state == ResourceState.Removed:
198-
for chart, l in old_dag_lines:
199-
if res in l:
200-
subgraphs[chart].add_deleted_line(l)
195+
for chart, ln in old_dag_lines:
196+
if res in ln:
197+
subgraphs[chart].add_deleted_line(ln)

fireconfig/subgraph.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ def add_edge(self, s: DependencyVertex, t: DependencyVertex):
2626
t_name = self.add_node(t)
2727
self._dag[s_name].append(t_name)
2828

29-
def add_deleted_line(self, l: str):
30-
self._deleted_lines.add(l)
29+
def add_deleted_line(self, ln: str):
30+
self._deleted_lines.add(ln)
3131

3232
def nodes(self) -> T.List[T.Tuple[str, str]]:
3333
return [(n, self._kinds[n]) for n in self._dag.keys()]
3434

3535
def edges(self) -> T.List[T.Tuple[str, str]]:
36-
return [(s, e) for s, l in self._dag.items() for e in l]
36+
return [(s, e) for s, ln in self._dag.items() for e in ln]
3737

3838
def deleted_lines(self) -> T.Iterable[str]:
3939
return self._deleted_lines

0 commit comments

Comments
 (0)