Skip to content

Commit 3d8e406

Browse files
committed
formatted python files
1 parent 2e385ea commit 3d8e406

14 files changed

+634
-524
lines changed

kube_downscaler/cmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ def get_parser():
4747
parser.add_argument(
4848
"--upscale-target-only",
4949
help="Upscale only resource in target when waking up namespaces",
50-
action="store_true"
50+
action="store_true",
5151
)
5252
parser.add_argument(
53-
"--namespace",
54-
help="Namespace",
55-
default=os.getenv("NAMESPACE", "")
53+
"--namespace", help="Namespace", default=os.getenv("NAMESPACE", "")
5654
)
5755
parser.add_argument(
5856
"--include-resources",
@@ -100,7 +98,9 @@ def get_parser():
10098
parser.add_argument(
10199
"--exclude-deployments",
102100
help="Exclude specific deployments from downscaling. Despite its name, this option will match the name of any included resource type (Deployment, StatefulSet, CronJob, ..). (default: py-kube-downscaler,kube-downscaler,downscaler)",
103-
default=os.getenv("EXCLUDE_DEPLOYMENTS", "py-kube-downscaler,kube-downscaler,downscaler"),
101+
default=os.getenv(
102+
"EXCLUDE_DEPLOYMENTS", "py-kube-downscaler,kube-downscaler,downscaler"
103+
),
104104
)
105105
parser.add_argument(
106106
"--downtime-replicas",

kube_downscaler/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def run_loop(
7979

8080
if len(namespaces) >= 1:
8181
constrained_downscaler = True
82-
logging.info("Namespace argument is not empty, the downscaler will run in constrained mode")
82+
logging.info(
83+
"Namespace argument is not empty, the downscaler will run in constrained mode"
84+
)
8385
else:
8486
constrained_downscaler = False
8587

kube_downscaler/resources/constraint.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class KubeDownscalerJobsConstraint(APIObject):
5-
65
"""Support the Gatakeeper Admission Controller Custom CRDs (https://open-policy-agent.github.io/gatekeeper/website/docs)."""
76

87
version = "constraints.gatekeeper.sh/v1beta1"
@@ -24,15 +23,9 @@ def create_job_constraint(resource_name):
2423
"kind": "KubeDownscalerJobsConstraint",
2524
"metadata": {
2625
"name": resource_name,
27-
"labels": {
28-
"origin": "kube-downscaler"
29-
}
26+
"labels": {"origin": "kube-downscaler"},
3027
},
31-
"spec": {
32-
"match": {
33-
"namespaces": [resource_name]
34-
}
35-
}
28+
"spec": {"match": {"namespaces": [resource_name]}},
3629
}
3730

3831
return obj

kube_downscaler/resources/constrainttemplate.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class ConstraintTemplate(APIObject):
7-
87
"""Support the Gatakeeper Admission Controller Custom CRDs (https://open-policy-agent.github.io/gatekeeper/website/docs)."""
98

109
version = "templates.gatekeeper.sh/v1"
@@ -13,36 +12,47 @@ class ConstraintTemplate(APIObject):
1312

1413
@staticmethod
1514
def create_constraint_template_crd(excluded_jobs, matching_labels):
16-
17-
excluded_jobs_regex = '^(' + '|'.join(excluded_jobs) + ')$'
15+
excluded_jobs_regex = "^(" + "|".join(excluded_jobs) + ")$"
1816

1917
# For backwards compatibility, if the matching_labels FrozenSet has an empty string as the first element,
2018
# we don't ignore anything
2119
first_element = next(iter(matching_labels), None)
2220
first_element_str = first_element.pattern
2321

2422
if first_element_str == "":
25-
logger.debug("Matching_labels arg set to empty string: all resources are considered in the scaling process")
23+
logger.debug(
24+
"Matching_labels arg set to empty string: all resources are considered in the scaling process"
25+
)
2626
matching_labels_arg_is_present = False
2727
else:
2828
matching_labels_arg_is_present = True
2929

3030
if matching_labels_arg_is_present:
3131
matching_labels_rego_string: str = "\n"
3232
for pattern in matching_labels:
33-
matching_labels_rego_string = matching_labels_rego_string + " has_matched_labels(\"" + pattern.pattern + "\", input.review.object.metadata.labels)\n"
33+
matching_labels_rego_string = (
34+
matching_labels_rego_string
35+
+ ' has_matched_labels("'
36+
+ pattern.pattern
37+
+ '", input.review.object.metadata.labels)\n'
38+
)
3439
else:
3540
matching_labels_rego_string: str = ""
3641

37-
rego = """
42+
rego = (
43+
"""
3844
package kubedownscalerjobsconstraint
3945
4046
violation[{"msg": msg}] {
4147
input.review.kind.kind == "Job"
4248
not exist_owner_reference
43-
not exact_match(\"""" + excluded_jobs_regex + """\", input.review.object.metadata.name)
49+
not exact_match(\""""
50+
+ excluded_jobs_regex
51+
+ """\", input.review.object.metadata.name)
4452
not has_exclude_annotation
45-
not is_exclude_until_date_reached""" + matching_labels_rego_string + """
53+
not is_exclude_until_date_reached"""
54+
+ matching_labels_rego_string
55+
+ """
4656
msg := "Job creation is not allowed in this namespace during a kube-downscaler downtime period."
4757
}
4858
@@ -73,6 +83,7 @@ def create_constraint_template_crd(excluded_jobs, matching_labels):
7383
regex.match(pattern, equals_value_contact)
7484
}
7585
"""
86+
)
7687

7788
obj = {
7889
"apiVersion": "templates.gatekeeper.sh/v1",
@@ -82,24 +93,13 @@ def create_constraint_template_crd(excluded_jobs, matching_labels):
8293
"annotations": {
8394
"metadata.gatekeeper.sh/title": "Kube Downscaler Jobs Constraint",
8495
"metadata.gatekeeper.sh/version": "1.0.0",
85-
"description": "Policy to downscale jobs in certain namespaces."
86-
}
96+
"description": "Policy to downscale jobs in certain namespaces.",
97+
},
8798
},
8899
"spec": {
89-
"crd": {
90-
"spec": {
91-
"names": {
92-
"kind": "KubeDownscalerJobsConstraint"
93-
}
94-
}
95-
},
96-
"targets": [
97-
{
98-
"target": "admission.k8s.gatekeeper.sh",
99-
"rego": rego
100-
}
101-
]
102-
}
100+
"crd": {"spec": {"names": {"kind": "KubeDownscalerJobsConstraint"}}},
101+
"targets": [{"target": "admission.k8s.gatekeeper.sh", "rego": rego}],
102+
},
103103
}
104104

105105
return obj

kube_downscaler/resources/keda.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class ScaledObject(NamespacedAPIObject):
5-
65
"""Support the ScaledObject resource (https://keda.sh/docs/2.7/concepts/scaling-deployments/#scaledobject-spec)."""
76

87
version = "keda.sh/v1alpha1"
@@ -21,7 +20,10 @@ def replicas(self):
2120
replicas = -1
2221
elif self.annotations[ScaledObject.keda_pause_annotation] == "0":
2322
replicas = 0
24-
elif self.annotations[ScaledObject.keda_pause_annotation] != "0" and self.annotations[ScaledObject.keda_pause_annotation] is not None:
23+
elif (
24+
self.annotations[ScaledObject.keda_pause_annotation] != "0"
25+
and self.annotations[ScaledObject.keda_pause_annotation] is not None
26+
):
2527
replicas = int(self.annotations[ScaledObject.keda_pause_annotation])
2628
else:
2729
replicas = -1

kube_downscaler/resources/policy.py

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class KubeDownscalerJobsPolicy(NamespacedAPIObject):
5-
65
"""Support the Kyverno Admission Controller Custom CRDs (https://kyverno.io/docs/introduction/#quick-start)."""
76

87
version = "kyverno.io/v1"
@@ -19,29 +18,21 @@ def create_job_policy(namespace):
1918
"namespace": namespace,
2019
"labels": {
2120
"origin": "kube-downscaler",
22-
"kube-downscaler/policy-type": "without-matching-labels"
21+
"kube-downscaler/policy-type": "without-matching-labels",
2322
},
2423
"annotations": {
2524
"policies.kyverno.io/title": "Kube Downscaler Jobs Policy",
2625
"policies.kyverno.io/severity": "medium",
2726
"policies.kyverno.io/subject": "Job",
28-
"policies.kyverno.io/description": "Job creation is not allowed in this namespace during a kube-downscaler downtime period."
29-
}
27+
"policies.kyverno.io/description": "Job creation is not allowed in this namespace during a kube-downscaler downtime period.",
28+
},
3029
},
3130
"spec": {
3231
"validationFailureAction": "Enforce",
3332
"rules": [
3433
{
3534
"name": "kube-downscaler-jobs-policy",
36-
"match": {
37-
"any": [
38-
{
39-
"resources": {
40-
"kinds": ["Job"]
41-
}
42-
}
43-
]
44-
},
35+
"match": {"any": [{"resources": {"kinds": ["Job"]}}]},
4536
"validate": {
4637
"message": "Job creation is not allowed in this namespace during a kube-downscaler downtime period.",
4738
"deny": {
@@ -50,25 +41,25 @@ def create_job_policy(namespace):
5041
{
5142
"key": "{{ request.object.metadata.ownerReferences || 'null'}}",
5243
"operator": "Equals",
53-
"value": "null"
44+
"value": "null",
5445
},
5546
{
5647
"key": "{{request.object.metadata.annotations.\"downscaler/exclude\" || ''}}",
5748
"operator": "NotEquals",
58-
"value": "true"
49+
"value": "true",
5950
},
6051
{
6152
"key": "{{ time_after('{{ time_now() }}','{{ request.object.metadata.annotations.\"downscaler/exclude-until\" || '1970-01-01T00:00:00Z' }}') }}",
6253
"operator": "Equals",
63-
"value": True
64-
}
54+
"value": True,
55+
},
6556
]
6657
}
67-
}
68-
}
58+
},
59+
},
6960
}
70-
]
71-
}
61+
],
62+
},
7263
}
7364

7465
return obj
@@ -83,35 +74,27 @@ def create_job_policy_with_matching_labels(namespace, matching_labels):
8374
"namespace": namespace,
8475
"labels": {
8576
"origin": "kube-downscaler",
86-
"kube-downscaler/policy-type": "with-matching-labels"
77+
"kube-downscaler/policy-type": "with-matching-labels",
8778
},
8879
"annotations": {
8980
"policies.kyverno.io/description": "Job creation is not allowed in this namespace during a kube-downscaler downtime period.",
9081
"policies.kyverno.io/severity": "medium",
9182
"policies.kyverno.io/subject": "Job",
92-
"policies.kyverno.io/title": "Kube Downscaler Jobs Policy"
93-
}
83+
"policies.kyverno.io/title": "Kube Downscaler Jobs Policy",
84+
},
9485
},
9586
"spec": {
9687
"validationFailureAction": "Enforce",
9788
"rules": [
9889
{
99-
"match": {
100-
"any": [
101-
{
102-
"resources": {
103-
"kinds": ["Job"]
104-
}
105-
}
106-
]
107-
},
90+
"match": {"any": [{"resources": {"kinds": ["Job"]}}]},
10891
"name": "kube-downscaler-jobs-policy",
10992
"preconditions": {
11093
"all": [
11194
{
11295
"key": "{{ request.object.metadata.labels || 'NoLabel'}}",
11396
"operator": "NotEquals",
114-
"value": "NoLabel"
97+
"value": "NoLabel",
11598
}
11699
]
117100
},
@@ -120,8 +103,8 @@ def create_job_policy_with_matching_labels(namespace, matching_labels):
120103
"name": "labels",
121104
"variable": {
122105
"jmesPath": "items(request.object.metadata.labels, 'key', 'value')",
123-
"default": []
124-
}
106+
"default": [],
107+
},
125108
}
126109
],
127110
"validate": {
@@ -135,56 +118,63 @@ def create_job_policy_with_matching_labels(namespace, matching_labels):
135118
{
136119
"key": "{{ request.object.metadata.ownerReferences || 'null'}}",
137120
"operator": "Equals",
138-
"value": "null"
121+
"value": "null",
139122
},
140123
{
141124
"key": "{{request.object.metadata.annotations.\"downscaler/exclude\" || ''}}",
142125
"operator": "NotEquals",
143-
"value": "true"
126+
"value": "true",
144127
},
145128
{
146129
"key": "{{ time_after('{{ time_now() }}','{{ request.object.metadata.annotations.\"downscaler/exclude-until\" || '1970-01-01T00:00:00Z' }}') }}",
147130
"operator": "Equals",
148-
"value": True
149-
}
131+
"value": True,
132+
},
150133
]
151134
}
152-
}
135+
},
153136
}
154-
]
155-
}
137+
],
138+
},
156139
}
157-
]
158-
}
140+
],
141+
},
159142
}
160143

161144
for pattern in matching_labels:
162145
matching_labels_condition = {
163-
"key": "{{ regex_match('" + pattern.pattern + "', '{{element.key}}={{element.value}}') }}",
146+
"key": "{{ regex_match('"
147+
+ pattern.pattern
148+
+ "', '{{element.key}}={{element.value}}') }}",
164149
"operator": "Equals",
165-
"value": True
150+
"value": True,
166151
}
167-
obj["spec"]["rules"][0]["validate"]["foreach"][0]["deny"]["conditions"]["all"].append(
168-
matching_labels_condition)
152+
obj["spec"]["rules"][0]["validate"]["foreach"][0]["deny"]["conditions"][
153+
"all"
154+
].append(matching_labels_condition)
169155

170156
return obj
171157

172158
@staticmethod
173159
def append_excluded_jobs_condition(obj, excluded_jobs, has_matching_labels_arg):
174-
175160
excluded_jobs_regex = f"^({'|'.join(excluded_jobs)})$"
176161

177162
excluded_jobs_condition = {
178-
"key": "{{ regex_match('" + excluded_jobs_regex + "', '{{request.object.metadata.name}}') }}",
163+
"key": "{{ regex_match('"
164+
+ excluded_jobs_regex
165+
+ "', '{{request.object.metadata.name}}') }}",
179166
"operator": "NotEquals",
180-
"value": True
167+
"value": True,
181168
}
182169

183170
if has_matching_labels_arg:
184-
obj["spec"]["rules"][0]["validate"]["foreach"][0]["deny"]["conditions"]["all"].append(
185-
excluded_jobs_condition)
171+
obj["spec"]["rules"][0]["validate"]["foreach"][0]["deny"]["conditions"][
172+
"all"
173+
].append(excluded_jobs_condition)
186174
else:
187-
obj["spec"]["rules"][0]["validate"]["deny"]["conditions"]["all"].append(excluded_jobs_condition)
175+
obj["spec"]["rules"][0]["validate"]["deny"]["conditions"]["all"].append(
176+
excluded_jobs_condition
177+
)
188178

189179
return obj
190180

0 commit comments

Comments
 (0)