From ed0678097f67aa55194b2d987c383f51c5fcb413 Mon Sep 17 00:00:00 2001 From: Etienne Audet-Cobello Date: Thu, 7 Nov 2024 15:21:59 -0500 Subject: [PATCH] better validation --- charms/worker/k8s/src/charm.py | 44 ++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/charms/worker/k8s/src/charm.py b/charms/worker/k8s/src/charm.py index fa58c23f..fe86f8eb 100755 --- a/charms/worker/k8s/src/charm.py +++ b/charms/worker/k8s/src/charm.py @@ -63,6 +63,7 @@ from snap import version as snap_version from token_distributor import ClusterTokenType, TokenCollector, TokenDistributor, TokenStrategy from typing_extensions import Literal +from pydantic import ValidationError # Log messages can be retrieved using juju debug-log log = logging.getLogger(__name__) @@ -168,30 +169,31 @@ def _resolve_feature_relations(self, event: ops.EventBase): log.warning("No relation data found for feature relation on %s", relation.name) continue - parsed_relation_data = FeatureRelationData.parse_obj(raw_relation_data) + try: + parsed_relation_data = FeatureRelationData.parse_obj(raw_relation_data) + except ValidationError as e: + log.warning("The relation data on %s is not valid: %s", relation.name, e) + continue + feature_name = parsed_relation_data.name - feature_version = parsed_relation_data.version feature_attributes = parsed_relation_data.attributes - if feature_name and feature_version and feature_attributes: - feature_config_classes: Dict[str, type] = { - "load-balancer": LoadBalancerConfig, - "local-storage": LocalStorageConfig, - } - if not (config_class := feature_config_classes.get(feature_name)): - status.add(ops.BlockedStatus(f"Unsupported feature {feature_name}")) - continue - - feature_config = config_class.parse_raw(feature_attributes) - log.info("Updating feature [%s] with config [%s]", feature_name, feature_config) - - cluster_config = UserFacingClusterConfig(**{feature_name: feature_config}) - update_request = UpdateClusterConfigRequest(config=cluster_config) - self.api_manager.update_cluster_config(update_request) - - log.info("Feature [%s] updated", feature_name) - else: - log.warning("Couldn't resolve feature relation because the relation data is not valid") + feature_config_classes: Dict[str, type] = { + "load-balancer": LoadBalancerConfig, + "local-storage": LocalStorageConfig, + } + if not (config_class := feature_config_classes.get(feature_name)): + status.add(ops.BlockedStatus(f"Unsupported feature {feature_name}")) + continue + + feature_config = config_class.parse_raw(feature_attributes) + log.info("Updating feature [%s] with config [%s]", feature_name, feature_config) + + cluster_config = UserFacingClusterConfig(**{feature_name: feature_config}) + update_request = UpdateClusterConfigRequest(config=cluster_config) + self.api_manager.update_cluster_config(update_request) + + log.info("Feature [%s] updated", feature_name) def _k8s_info(self, event: ops.EventBase): """Send cluster information on the kubernetes-info relation.