Skip to content

Commit

Permalink
feat: add script for components image update
Browse files Browse the repository at this point in the history
  • Loading branch information
kasia-kujawa committed Jun 18, 2024
1 parent fc53099 commit 425f3e3
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions scripts/update_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env python

import yaml
import textwrap

RED_HAT_REGISTRY = "registry.connect.redhat.com/sumologic/"
ENV_PREFIX = "RELATED_IMAGE_"
CLUSTER_SERVICE_VERSION_PATH = "bundle/manifests/operator.clusterserviceversion.yaml"
MANAGER_PATH = "config/manager/manager.yaml"

def remove_prefix(s: str, prefix: str) -> str:
if s.startswith(prefix):
return s[len(prefix):]
else:
return s

def pairwise(iterable: list) -> list:
"s -> (s0, s1), (s2, s3), (s4, s5), ..."
a = iter(iterable)
return zip(a, a)

def get_lines(file: str) -> list:
lines = []
with open("images.txt") as f:
lines = f.read().split("\n")
if RED_HAT_REGISTRY not in lines[0]:
lines = lines[1:]
return lines

def generate_image_list(image_list_file: str):
lines = get_lines(image_list_file)
related_images = []
image_envs = []
for image_with_tag, image_with_sha256 in pairwise(lines):
component_and_tag =remove_prefix(image_with_tag, RED_HAT_REGISTRY).split(":")
component = component_and_tag[0]
tag = component_and_tag[1]
env_name = "{}{}".format(ENV_PREFIX, component.upper().replace("-", "_"))
related_images.append({"name": env_name, "image": image_with_sha256 })
image_envs.append({"name": env_name, "value": image_with_sha256 })
return related_images, image_envs

def create_new_file_path(file_path: str, create_new_file: bool) -> str:
new_file_path = file_path
if create_new_file:
new_file_path = file_path.replace(".yaml", "_new.yaml")
return new_file_path

def get_helm_operator_image(related_images: str)->dict:
operator_image = None
for image in related_images:
if image["name"] == "sumologic-kubernetes-collection-helm-operator":
operator_image = image
return operator_image

def update_envs(envs: list, new_image_envs) -> list:
not_image_envs = []
for env in envs:
if ENV_PREFIX not in env["name"]:
not_image_envs.append(env)
return not_image_envs + new_image_envs

def update_cluster_service_version(file_path: str, new_related_images: list, new_image_envs: list, create_new_file=True):
with open(file_path) as cr:
cluster_service_version = yaml.safe_load(cr)
related_images = cluster_service_version["spec"]["relatedImages"]

helm_operator_image = get_helm_operator_image(related_images)
new_related_images.insert(0, helm_operator_image)
cluster_service_version["spec"]["relatedImages"] = new_related_images

# todo: search for the helm operator container
envs = cluster_service_version["spec"]["install"]["spec"]["deployments"][0]["spec"]["template"]["spec"]["containers"][1]["env"]
cluster_service_version["spec"]["install"]["spec"]["deployments"][0]["spec"]["template"]["spec"]["containers"][1]["env"] = update_envs(envs, new_image_envs)

with open(create_new_file_path(file_path, create_new_file), 'w') as cw:
yaml.dump(cluster_service_version, cw)

def update_manager(file_path: str,new_image_envs: list, create_new_file=True):
with open(file_path) as mr:
yaml_contents = yaml.safe_load_all(mr)

new_contents = []
for yaml_content in yaml_contents:
if yaml_content["kind"] != "Deployment":
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)
new_contents.append(yaml_content)

with open(create_new_file_path(file_path, create_new_file), 'w') as cw:
yaml.safe_dump_all(new_contents, cw)

if __name__ == '__main__':
image_list_file = "images.txt"
root_dir = "/operator/" #todo: change it to arg
new_file = True #todo change it to arg

related_images, image_envs = generate_image_list(image_list_file)

csv_path = root_dir + CLUSTER_SERVICE_VERSION_PATH
update_cluster_service_version(csv_path, related_images, image_envs)

m_path = root_dir + MANAGER_PATH
update_manager(m_path, image_envs)











0 comments on commit 425f3e3

Please sign in to comment.