From 45d90d361d7d791fbc7eee327a1a29a49f57f368 Mon Sep 17 00:00:00 2001 From: Dean Roehrich Date: Tue, 10 Sep 2024 15:33:50 -0500 Subject: [PATCH] Allow kustomize examples to be bumped by crd-bumper (#208) Allow the repo-local config file to specify a comma-separated list of directories of Kustomize config files that have references to the API and that must be updated to the new hub version so that ArgoCD can sync them. Signed-off-by: Dean Roehrich --- tools/crd-bumper/crd-bumper.py | 6 +++- tools/crd-bumper/pkg/controllers.py | 38 ++++++++++++++++++++++++++ tools/crd-bumper/pkg/conversion_gen.py | 8 +----- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/tools/crd-bumper/crd-bumper.py b/tools/crd-bumper/crd-bumper.py index 1deb02c3..0ff27272 100755 --- a/tools/crd-bumper/crd-bumper.py +++ b/tools/crd-bumper/crd-bumper.py @@ -335,9 +335,13 @@ def bump_controllers(cgen, makecmd, git, stage, project, args, bumper_cfg): controllers.edit_util_conversion_test() # Bump any other, non-controller, directories of code. - if bumper_cfg is not None and 'extra_go_dirs' in bumper_cfg: + if bumper_cfg is not None and "extra_go_dirs" in bumper_cfg: for extra_dir in bumper_cfg["extra_go_dirs"].split(","): controllers.update_extras(extra_dir) + # Bump any necessary references in the config/ dir. + if bumper_cfg is not None and "extra_config_dirs" in bumper_cfg: + for extra_dir in bumper_cfg["extra_config_dirs"].split(","): + controllers.update_extra_config(extra_dir) makecmd.fmt() controllers.commit_bump_controllers(git, stage) diff --git a/tools/crd-bumper/pkg/controllers.py b/tools/crd-bumper/pkg/controllers.py index 9251b075..b9b4c762 100644 --- a/tools/crd-bumper/pkg/controllers.py +++ b/tools/crd-bumper/pkg/controllers.py @@ -104,6 +104,26 @@ def update_extras(self, dirname): if fname.endswith(".go"): self._point_at_new_hub(kinds, full_path) + def update_extra_config(self, dirname): + """Walk over Kustomize config files and bump them to point at the new hub. + This is for any resource that may be exposed to ArgoCD, which would would + otherwise mark the resource as OutOfSync because the argocd queries will + show it with the new hub. + """ + + kinds = self._project.kinds(self._prev_ver) + if len(kinds) == 0: + raise ValueError(f"Nothing found at version {self._prev_ver}") + + if os.path.isdir(dirname) is False: + raise ValueError(f"{dirname} is not a directory") + + for root, _, f_names in os.walk(dirname, followlinks=False): + for fname in f_names: + full_path = os.path.join(root, fname) + if fname.endswith(".yaml"): + self._point_config_at_new_hub(kinds, full_path) + def _walk_files(self, top, kinds): """Walk the files in the given directory, and update them to point at the new hub.""" @@ -157,6 +177,24 @@ def _point_at_new_hub(self, kinds, path): fu.replace_in_file(f"{group}{self._prev_ver}.", f"{group}{self._new_ver}.") fu.store() + def _point_config_at_new_hub(self, kinds, path): + """Update the given file to point it at the new hub.""" + + fu = FileUtil(self._dryrun, path) + + if self._preferred_alias is None: + # Pick the first kind, use its group. + group = self._project.group(kinds[0], self._prev_ver) + else: + group = self._preferred_alias + + pat = f"apiVersion: {group}" + line = fu.find_in_file(pat) + if line is not None: + line2 = line.replace(self._prev_ver, self._new_ver) + fu.replace_in_file(line, line2) + fu.store() + def _update_suite_test(self, kinds, path): """Update the suite_test.go file to include the setup of the new hub.""" diff --git a/tools/crd-bumper/pkg/conversion_gen.py b/tools/crd-bumper/pkg/conversion_gen.py index 5be249d8..0153263e 100644 --- a/tools/crd-bumper/pkg/conversion_gen.py +++ b/tools/crd-bumper/pkg/conversion_gen.py @@ -102,13 +102,6 @@ def fix_kubebuilder_import_alias(self): ) fu.store() - # for root, _, f_names in os.walk(f"api/{self._new_ver}", followlinks=False): - # for fname in f_names: - # full_path = os.path.join(root, fname) - # fu = FileUtil(self._dryrun, full_path) - # fu.replace_in_file(f"{group}{self._new_ver}", f"{self._preferred_alias}{self._new_ver}") - # fu.store() - def module(self): """Return the name of this Go module.""" @@ -389,6 +382,7 @@ def mk_spoke_fuzz_test(self): fu.append(template) # Wake up! Multi-line f-string: + # pylint: disable=f-string-without-interpolation template = f""" }}