Skip to content

Commit 28ae5c8

Browse files
committed
refactor: simplify a bit the maxLocalTopicBytes validation code
Rather than doing nested diff we simply try to get the max_limit directly. If we get it means the topic is whitelisted and we check that value, if not it means the topics is not whitelisted and we raise the error.
1 parent 77d19a4 commit 28ae5c8

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

kp_pre_commit_hooks/gitops_values_validation.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
import sys
33
import textwrap
4+
from collections import defaultdict
45
from dataclasses import dataclass, field
56
from functools import cache, cached_property
67
from pathlib import Path
@@ -49,7 +50,7 @@
4950
}
5051

5152
# Topics with special max local bytes limits
52-
WHITELISTED_TOPICS_FOR_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV = {
53+
ALLOWED_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV = {
5354
"ais-listener.nmea": {
5455
"prod": {
5556
"max_limit": 5_368_709_120, # 5GB
@@ -449,21 +450,20 @@ def validate_max_local_topic_bytes_compliance(self, value, schema):
449450
if topic_max_local_bytes is None:
450451
continue
451452

452-
if topic_name in WHITELISTED_TOPICS_FOR_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV:
453-
if topic_env in WHITELISTED_TOPICS_FOR_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV[topic_name]:
454-
if topic_max_local_bytes > WHITELISTED_TOPICS_FOR_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV[topic_name][topic_env]["max_limit"]:
455-
yield ValidationError(
456-
f"maxLocalTopicBytes exceeds the allowed maximum of {WHITELISTED_TOPICS_FOR_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV[topic_name][topic_env]['max_limit']} "
457-
f"for topic '{topic_name}' in environment '{topic_env}'.\n"
458-
" See https://kpler.atlassian.net/wiki/x/BgGKS for more information."
459-
)
460-
continue
461-
462-
yield ValidationError(
463-
"maxLocalTopicBytes can only be used with allowed topics"
464-
f" and topic '{topic_name}' is not allowed in environment '{topic_env}'."
465-
" See https://kpler.atlassian.net/wiki/x/BgGKS for more information."
466-
)
453+
max_allowed_values = ALLOWED_MAX_LOCAL_TOPIC_BYTES_BY_TOPIC_AND_ENV.get(topic_name, {}).get(topic_env, {}).get("max_limit")
454+
if not max_allowed_values:
455+
yield ValidationError(
456+
"maxLocalTopicBytes can only be used with allowed topics"
457+
f" and topic '{topic_name}' is not allowed in environment '{topic_env}'."
458+
" See https://kpler.atlassian.net/wiki/x/BgGKS for more information."
459+
)
460+
elif topic_max_local_bytes > max_allowed_values:
461+
yield ValidationError(
462+
f"maxLocalTopicBytes exceeds the allowed maximum of {max_allowed_values} "
463+
f"for topic '{topic_name}' in environment '{topic_env}'.\n"
464+
" See https://kpler.atlassian.net/wiki/x/BgGKS for more information."
465+
)
466+
467467

468468
def validate_forbidden_environment_variables(self, value, schema):
469469
if not isinstance(value, dict):

0 commit comments

Comments
 (0)