Skip to content

Commit

Permalink
chore: add python formatter
Browse files Browse the repository at this point in the history
chore: update ubuntu to 22.04 in github action workflows to have compatible black versions
  • Loading branch information
kasia-kujawa committed Jun 20, 2024
1 parent b6429c2 commit 0f42304
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev_builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,22 +17,32 @@ 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
run: make test-bundle-status

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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 16 additions & 9 deletions scripts/generate_watches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
17 changes: 11 additions & 6 deletions scripts/get_image_config_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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

Expand Down Expand Up @@ -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"]


Expand Down Expand Up @@ -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))
53 changes: 30 additions & 23 deletions scripts/update_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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})
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -141,21 +141,21 @@ 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"]
if name == "operator":
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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion vagrant/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f42304

Please sign in to comment.