From 0b58225efb39e2d2c4dbadd0213d01cdabb9a1db Mon Sep 17 00:00:00 2001 From: Aleksandr Zimin Date: Sat, 17 Feb 2024 13:42:31 +0300 Subject: [PATCH] Add hooks Signed-off-by: Aleksandr Zimin --- hooks/ensure_crds.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 hooks/ensure_crds.py diff --git a/hooks/ensure_crds.py b/hooks/ensure_crds.py new file mode 100755 index 00000000..8fe8a0cd --- /dev/null +++ b/hooks/ensure_crds.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# +# Copyright 2023 Flant JSC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import yaml +from deckhouse import hook + +# We expect structure with possible subdirectories like this: +# +# my-module/ +# crds/ +# crd1.yaml +# crd2.yaml +# subdir/ +# crd3.yaml +# hooks/ +# ensure_crds.py # this file + +## test +config = """ +configVersion: v1 +onStartup: 5 +""" + + +def main(ctx: hook.Context): + for crd in iter_manifests(find_crds_root(__file__)): + ctx.kubernetes.create_or_update(crd) + + +def iter_manifests(root_path: str): + if not os.path.exists(root_path): + return + + for dirpath, dirnames, filenames in os.walk(top=root_path): + for filename in filenames: + if not filename.endswith(".yaml"): + # Wee only seek manifests + continue + if filename.startswith("doc-"): + # Skip dedicated doc yamls, common for Deckhouse internal modules + continue + + crd_path = os.path.join(dirpath, filename) + with open(crd_path, "r", encoding="utf-8") as f: + for manifest in yaml.safe_load_all(f): + if manifest is None: + continue + yield manifest + + for dirname in dirnames: + subroot = os.path.join(dirpath, dirname) + for manifest in iter_manifests(subroot): + yield manifest + + +def find_crds_root(hookpath): + hooks_root = os.path.dirname(hookpath) + module_root = os.path.dirname(hooks_root) + crds_root = os.path.join(module_root, "crds") + return crds_root + + +if __name__ == "__main__": + hook.run(main, config=config)