Skip to content

Commit

Permalink
better validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Nov 7, 2024
1 parent 4a09647 commit ed06780
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ed06780

Please sign in to comment.