def namespaces_idx(name: str, logger: kopf.Logger, labels: kopf.Labels, **_: Any) -> Dict[str, Any]: """Index of namespace by name""" return { name: { "name": name, "team": labels.get("orbit/team"), "env": labels.get("orbit/env") } }
def delete_poddefaults( namespace: str, name: str, labels: kopf.Labels, spec: kopf.Spec, logger: kopf.Logger, namespaces_idx: kopf.Index[str, Dict[str, Any]], **_: Any, ) -> str: team = labels.get("orbit/team", None) if team is None: logger.error("Missing required orbit/team label") return "MissingTeam" # Contruct a pseudo poddefault for the team to be deleted from users poddefault = poddefault_utils.construct( name=name, desc=spec.get("desc", ""), labels={ "orbit/space": "team", "orbit/team": team }, ) user_namespaces = [ namespace["name"] for namespace in namespaces_idx.get(team, []) ] poddefault_utils.delete_poddefaults_from_user_namespaces( poddefaults=[poddefault], user_namespaces=user_namespaces, client=dynamic_client(), logger=logger, ) return "PodDefaultsDeleted"
def labels_match(labels: kopf.Labels, selector_labels: kopf.Labels) -> bool: for key, value in selector_labels.items(): label_value = labels.get(key, None) if label_value != value: logger.debug( "NoHit: Label value check, label %s with value %s does not equal %s", key, label_value, value, ) return False return True
def expressions_match(labels: kopf.Labels, selector_expressions: List[Dict[str, Any]]) -> bool: for match_expression in selector_expressions: pod_label_value = labels.get(match_expression["key"], None) operator = match_expression["operator"] values = match_expression.get("values", []) if operator == "Exists" and pod_label_value is None: logger.debug( "NoHit: Exists check, label %s does not exist", match_expression["key"], ) return False if operator == "NotExists" and pod_label_value is not None: logger.debug( "NoHit: NotExists check, label %s does exist with value %s", match_expression["key"], pod_label_value, ) return False if operator == "In" and pod_label_value not in values: logger.debug( "NoHit: In check, label %s has value %s which is not in %s", match_expression["key"], pod_label_value, values, ) return False if operator == "NotIn" and pod_label_value in values: logger.debug( "NoHit: NotIn check, label %s has value %s which is in %s", match_expression["key"], pod_label_value, values, ) return False return True
def create_poddefaults( namespace: str, name: str, labels: kopf.Labels, spec: kopf.Spec, status: kopf.Status, patch: kopf.Patch, logger: kopf.Logger, namespaces_idx: kopf.Index[str, Dict[str, Any]], **_: Any, ) -> str: team = labels.get("orbit/team", None) if team is None: logger.error("Missing required orbit/team label") patch["status"] = {"podDefaultsCreation": "MissingTeam"} return "MissingTeam" # Contruct a pseudo poddefault for the team to be copied to users poddefault = poddefault_utils.construct( name=name, desc=spec.get("desc", ""), labels={ "orbit/space": "team", "orbit/team": team }, ) user_namespaces = [ns.get("name") for ns in namespaces_idx.get(team, [])] poddefault_utils.copy_poddefaults_to_user_namespaces( poddefaults=[poddefault], user_namespaces=user_namespaces, client=dynamic_client(), logger=logger, ) patch["status"] = {"podDefaultsCreation": "Complete"} return "PodDefaultsCreated"
def _should_process_podsetting(labels: kopf.Labels, status: kopf.Status, **_: Any) -> bool: return (labels.get("orbit/space") == "team" and "orbit/disable-watcher" not in labels and "podDefaultsCreation" not in status)
def _should_index_namespaces(labels: kopf.Labels, **_: Any) -> bool: return labels.get("orbit/space", None) == "user" and "orbit/team" in labels
def _should_index_podsetting(labels: kopf.Labels, **_: Any) -> bool: return labels.get("orbit/space") == "team" and "orbit/team" in labels