diff --git a/.github/workflows/dev_builds.yaml b/.github/workflows/dev_builds.yaml index 1284a60..3e5f7b5 100644 --- a/.github/workflows/dev_builds.yaml +++ b/.github/workflows/dev_builds.yaml @@ -7,7 +7,7 @@ on: jobs: build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 env: OPERATOR_IMAGE_NAME: sumologic-kubernetes-collection-helm-operator-dev BUNDLE_IMAGE_NAME: sumologic-kubernetes-collection-helm-operator-bundle-dev diff --git a/.github/workflows/pull_requests.yml b/.github/workflows/pull_requests.yml index 5b70790..131b334 100644 --- a/.github/workflows/pull_requests.yml +++ b/.github/workflows/pull_requests.yml @@ -8,7 +8,7 @@ on: jobs: check: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - name: Install shellcheck @@ -17,14 +17,24 @@ jobs: sudo cp shellcheck-v0.7.1/shellcheck /usr/local/bin && rm -rf shellcheck-v0.7.1 - name: shellcheck run: make shellcheck + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11.6' # set the same Python version as in Vagrant virtual machine - name: Install pylint run: sudo apt-get install --yes pylint - name: Run pylint run: make pylint + - name: Install black for formatting of Python scripts + run: sudo apt-get install --yes black + - name: Check black version + run: black --version + - name: Check formatting of Python scripts + run: make black-check test-bundle-status: name: Test whether bundle.yaml is up to date - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 - name: Check bundle.yaml @@ -32,7 +42,7 @@ jobs: test-helm-operator: name: Test Helm operator - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 timeout-minutes: 45 env: IMG: sumologic/sumologic-kubernetes-helm-operator:test diff --git a/.github/workflows/release_builds.yaml b/.github/workflows/release_builds.yaml index 16a3360..27ba7cb 100644 --- a/.github/workflows/release_builds.yaml +++ b/.github/workflows/release_builds.yaml @@ -12,7 +12,7 @@ on: jobs: build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 env: OPERATOR_IMAGE_NAME: sumologic-kubernetes-collection-helm-operator BUNDLE_IMAGE_NAME: sumologic-kubernetes-collection-helm-operator-bundle diff --git a/Makefile b/Makefile index 314a485..67357e1 100644 --- a/Makefile +++ b/Makefile @@ -238,3 +238,9 @@ pylint: autopep8: autopep8 --in-place scripts/*.py + +black-check: + black --check --line-length 180 scripts/*.py + +black-format: + black --line-length 180 scripts/*.py diff --git a/scripts/generate_watches.py b/scripts/generate_watches.py index 2b6a343..1d56b2d 100755 --- a/scripts/generate_watches.py +++ b/scripts/generate_watches.py @@ -24,23 +24,29 @@ def create_new_file_path(file_path: str, create_new_file: bool) -> str: Returns: str: path to file in which changes will be save """ - new_file_path = file_path + new_path = file_path if create_new_file: - new_file_path = file_path.replace(".yaml", "_new.yaml") - return new_file_path + new_path = file_path.replace(".yaml", "_new.yaml") + return new_path def parse_args(): - """ Parses command line arguments""" + """Parses command line arguments""" parser = argparse.ArgumentParser() - parser.add_argument("--operator-repo-dir", - help="path to directory with Helm Operator repository, e.g. /operator/", default="./") parser.add_argument( - "--create-new-file", help="determines whether new yaml should be created or the exiting file should be overwritten", default=True) + "--operator-repo-dir", + help="path to directory with Helm Operator repository, e.g. /operator/", + default="./", + ) + parser.add_argument( + "--create-new-file", + help="determines whether new yaml should be created or the exiting file should be overwritten", + default=True, + ) return parser.parse_args() -if __name__ == '__main__': +if __name__ == "__main__": args = parse_args() image_keys = get_image_keys() @@ -54,5 +60,6 @@ def parse_args(): if image_key not in watches[0]["overrideValues"].keys(): overrideValues[image_key] = "" - with open(create_new_file_path(watches_file_path, args.create_new_file), 'w', encoding="utf-8") as nw: + new_file_path = create_new_file_path(watches_file_path, args.create_new_file) + with open(new_file_path, "w", encoding="utf-8") as nw: yaml.safe_dump(watches, nw) diff --git a/scripts/get_image_config_keys.py b/scripts/get_image_config_keys.py index 9b48f19..7458dfa 100644 --- a/scripts/get_image_config_keys.py +++ b/scripts/get_image_config_keys.py @@ -19,8 +19,8 @@ def values_to_dictionary(url: str) -> dict: """ with urllib.request.urlopen(url) as response: values = response.read().decode(response.headers.get_content_charset()) - values = re.sub(r'(\[\]|\{\})\n(\s+# )', r'\n\2', values, flags=re.M) - values = re.sub(r'^(\s+)# ', r'\1', values, flags=re.M) + values = re.sub(r"(\[\]|\{\})\n(\s+# )", r"\n\2", values, flags=re.M) + values = re.sub(r"^(\s+)# ", r"\1", values, flags=re.M) return yaml.load(values, Loader=SafeLoader) @@ -47,7 +47,7 @@ def extract_keys(dictionary: dict) -> list: if more_keys is None: keys.append(key) else: - keys.extend(f'{key}.{mk}' for mk in more_keys) + keys.extend(f"{key}.{mk}" for mk in more_keys) return keys @@ -75,8 +75,13 @@ def extract_keys(dictionary: dict) -> list: "metrics-server.image.tag", ] -not_needed_image_keys = ["Percentage", "falco", - "pullPolicy", "pullSecrets", "imagePullSecrets"] +not_needed_image_keys = [ + "Percentage", + "falco", + "pullPolicy", + "pullSecrets", + "imagePullSecrets", +] needed_image_keys = ["image", "tag", "repository"] @@ -112,6 +117,6 @@ def get_image_keys() -> list: return image_keys -if __name__ == '__main__': +if __name__ == "__main__": image_config_keys = get_image_keys() print("\n".join(image_config_keys)) diff --git a/scripts/update_images.py b/scripts/update_images.py index 04e2260..9785f9b 100755 --- a/scripts/update_images.py +++ b/scripts/update_images.py @@ -24,6 +24,7 @@ def pairwise(iterable: list) -> list: element = iter(iterable) return zip(element, element) + def get_lines(file_path: str) -> list: """Read file line by line removing unnecessary string at the beginning, e.g. output of get_images_sha256.sh: @@ -64,8 +65,7 @@ def generate_image_lists(image_list_file: str): related_images = [] image_envs = [] for image_with_tag, image_with_sha256 in pairwise(lines): - component, _ = image_with_tag.removeprefix( - RED_HAT_REGISTRY).split(":") + component, _ = image_with_tag.removeprefix(RED_HAT_REGISTRY).split(":") component = component.upper().replace("-", "_") env_name = f"{ENV_PREFIX}{component}" related_images.append({"name": env_name, "image": image_with_sha256}) @@ -83,10 +83,10 @@ def create_new_file_path(file_path: str, create_new_file: bool) -> str: Returns: str: path to file in which changes will be save """ - new_file_path = file_path + new_path = file_path if create_new_file: - new_file_path = file_path.replace(".yaml", "_new.yaml") - return new_file_path + new_path = file_path.replace(".yaml", "_new.yaml") + return new_path def get_helm_operator_image(images: list) -> dict: @@ -125,7 +125,7 @@ def update_envs(envs: list, new_image_envs) -> list: def update_cluster_service_version(file_path: str, new_related_images: list, new_image_envs: list, create_new_file): - """ Updates components images in bundle/manifests/operator.clusterserviceversion.yaml + """Updates components images in bundle/manifests/operator.clusterserviceversion.yaml Args: file_path (str): absolute path to bundle/manifests/operator.clusterserviceversion.yaml @@ -141,8 +141,7 @@ def update_cluster_service_version(file_path: str, new_related_images: list, new new_related_images.insert(0, helm_operator_image) cluster_service_version["spec"]["relatedImages"] = new_related_images - containers = cluster_service_version["spec"]["install"]["spec"][ - "deployments"][0]["spec"]["template"]["spec"]["containers"] + containers = cluster_service_version["spec"]["install"]["spec"]["deployments"][0]["spec"]["template"]["spec"]["containers"] # pylint: disable=C0200 for i in range(len(containers)): name = containers[i]["name"] @@ -150,12 +149,13 @@ def update_cluster_service_version(file_path: str, new_related_images: list, new envs = containers[i]["env"] containers[i]["env"] = update_envs(envs, new_image_envs) - with open(create_new_file_path(file_path, create_new_file), 'w', encoding="utf-8") as cluster_service_version_file_new: + new_file_path = create_new_file_path(file_path, create_new_file) + with open(new_file_path, "w", encoding="utf-8") as cluster_service_version_file_new: yaml.dump(cluster_service_version, cluster_service_version_file_new) def update_manager(file_path: str, new_image_envs: list, create_new_file): - """ Updates components images in config/manager/manager.yaml + """Updates components images in config/manager/manager.yaml Args: file_path (str): absolute path to config/manager/manager.yaml @@ -171,35 +171,42 @@ def update_manager(file_path: str, new_image_envs: list, create_new_file): new_contents.append(yaml_content) else: envs = yaml_content["spec"]["template"]["spec"]["containers"][0]["env"] - yaml_content["spec"]["template"]["spec"]["containers"][0]["env"] = update_envs( - envs, new_image_envs) + yaml_content["spec"]["template"]["spec"]["containers"][0]["env"] = update_envs(envs, new_image_envs) new_contents.append(yaml_content) - with open(create_new_file_path(file_path, create_new_file), 'w', encoding="utf-8") as manager_file_new: + new_file_path = create_new_file_path(file_path, create_new_file) + with open(new_file_path, "w", encoding="utf-8") as manager_file_new: yaml.safe_dump_all(new_contents, manager_file_new) def parse_args(): - """ Parses command line arguments""" + """Parses command line arguments""" parser = argparse.ArgumentParser() parser.add_argument( - "--images-file", help="file with the list of container images, output of get_images_sha256.sh from sumologic-openshift-images repository", required=True) - parser.add_argument("--operator-repo-dir", - help="path to directory with Helm Operator repository, e.g. /operator/", default="./") + "--images-file", + help="file with the list of container images, output of get_images_sha256.sh from sumologic-openshift-images repository", + required=True, + ) + parser.add_argument( + "--operator-repo-dir", + help="path to directory with Helm Operator repository, e.g. /operator/", + default="./", + ) parser.add_argument( - "--create-new-file", help="determines whether new yaml should be created or the exiting file should be overwritten", default=True) + "--create-new-file", + help="determines whether new yaml should be created or the exiting file should be overwritten", + default=True, + ) return parser.parse_args() -if __name__ == '__main__': +if __name__ == "__main__": args = parse_args() related_images_list, image_envs_list = generate_image_lists(args.images_file) - csv_path = os.path.join(args.operator_repo_dir, - CLUSTER_SERVICE_VERSION_PATH) - update_cluster_service_version( - csv_path, related_images_list, image_envs_list, args.create_new_file) + csv_path = os.path.join(args.operator_repo_dir, CLUSTER_SERVICE_VERSION_PATH) + update_cluster_service_version(csv_path, related_images_list, image_envs_list, args.create_new_file) m_path = os.path.join(args.operator_repo_dir, MANAGER_PATH) update_manager(m_path, image_envs_list, args.create_new_file) diff --git a/vagrant/provision.sh b/vagrant/provision.sh index 13f6190..dbeb380 100644 --- a/vagrant/provision.sh +++ b/vagrant/provision.sh @@ -16,7 +16,7 @@ add-apt-repository \ "deb [arch=${ARCH}] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" -apt-get install --yes docker-ce docker-ce-cli containerd.io pylint python-is-python3 python3-autopep8 python3-pip +apt-get install --yes docker-ce docker-ce-cli containerd.io pylint python-is-python3 python3-autopep8 python3-pip black usermod -aG docker vagrant # Install k8s