def _sync_from_parent(group, version, plural, name, namespace, meta, attrs_to_trim=None, *args, **kwargs): _, _ = args, kwargs parent, prv, pgn = util.get_spec(g, v, r, n, ns) # check if child exists mounts = parent.get("mount", {}) gvr_str = util.gvr(group, version, plural) nsn_str = util.spaced_name(name, namespace) if (gvr_str not in mounts or (nsn_str not in mounts[gvr_str] and name not in mounts[gvr_str])): self._logger.warning( f"Unable to find the {nsn_str} or {name} in the {parent}") return models = mounts[gvr_str] n_ = name if name in models else nsn_str patch = models[n_] if attrs_to_trim is not None: patch = util.trim_attr(patch, attrs_to_trim) _, resp, e = util.check_gen_and_patch_spec(group, version, plural, name, namespace, patch, gen=meta["generation"]) if e is not None: self._logger.warning(f"Unable to sync from parent due to {e}") else: model_id = util.model_id(group, version, plural, name, namespace) new_gen = resp["metadata"]["generation"] self._children_gen[model_id] = new_gen if meta["generation"] + 1 == new_gen: self._children_skip_gen[model_id] = new_gen
def _prune_mounts(mounts, meta): rv = meta["resourceVersion"] while True: to_prune = list() for gvr_str, models in mounts.items(): if len(models) == 0: to_prune.append(gvr_str) if len(to_prune) == 0: return patch = {"mounts": {gvr_str: None for gvr_str in to_prune}} _, e = util.patch_spec(g, v, r, n, ns, patch, rv=rv) if e is None: self._logger.info(f"Prune mount: {patch}") return elif e.status != 409: self._logger.warning(f"Prune mount failed due to {e}") return self._logger.info(f"Prune mount will retry due to: {e}") spec, rv, _ = util.get_spec(g, v, r, n, ns) mounts = spec.get("mount", {})
def benchmark_room_lamp(root_intent=ROOM_INTENT, skip_result=False): global measure measure = dict() measure = { "start": time.time(), # "request": None, # "forward_root": None, # "backward_root": None, "forward_leaf": None, "backward_leaf": None, } send_request(room_gvr, { "control": { "brightness": { "intent": root_intent } } }) if skip_result: return {} # wait until results are ready while True: if all(v is not None and v > 0 for v in measure.values()): break measure_spec, _, _ = get_spec(*measure_gvr) measure.update(measure_spec["obs"]) now = time.time() pp.pprint(measure) # post proc return { "ttf": now - measure["start"], "fpt": measure["forward_leaf"] - measure["start"], "bpt": now - measure["backward_leaf"], "dt": measure["backward_leaf"] - measure["forward_leaf"], }
def _sync_to_parent(group, version, plural, name, namespace, meta, spec, diff, attrs_to_trim=None, *args, **kwargs): _, _ = args, kwargs # propagation from child retries until succeed while True: parent, prv, pgn = util.get_spec(g, v, r, n, ns) # check if child exists mounts = parent.get("mount", {}) gvr_str = util.gvr(group, version, plural) nsn_str = util.spaced_name(name, namespace) if (gvr_str not in mounts or (nsn_str not in mounts[gvr_str] and name not in mounts[gvr_str])): self._logger.warning( f"unable to find the {nsn_str} or {name} in the {parent}" ) return models = mounts[gvr_str] n_ = name if name in models else nsn_str if spec is None: parent_patch = None # will convert to json null else: if models[n_].get("mode", "hide") == "hide": if attrs_to_trim is None: attrs_to_trim = set() attrs_to_trim.add("mount") # TBD rename to _gen_parent_spec parent_patch = _gen_parent_patch(spec, diff, attrs_to_trim) # add roots if parent_patch is None: # only child if len(mounts[gvr_str]) == 1: parent_patch = {"mount": {gvr_str: None}} else: parent_patch = {"mount": {gvr_str: {n_: None}}} else: parent_patch = { "mount": { gvr_str: { n_: { "spec": parent_patch, "generation": meta["generation"], } } } } # maybe rejected if parent has been updated; # continue to try until succeed resp, e = util.patch_spec(g, v, r, n, ns, parent_patch, rv=prv) if e is not None: self._logger.warning( f"Failed to sync to parent due to {e}") if e.status != 409: return # time.sleep(1) else: new_gen = resp["metadata"]["generation"] if pgn + 1 == new_gen: self._parent_skip_gen = new_gen break
def _get_spec(m): return util.get_spec(m["g"], m["v"], m["r"], m["n"], m["ns"])