|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +import sys |
| 6 | +import ruamel.yaml |
| 7 | + |
| 8 | +log = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class YAMLEmitterNoVersionDirective(ruamel.yaml.emitter.Emitter): |
| 12 | + def write_version_directive(self, version_text): |
| 13 | + pass |
| 14 | + |
| 15 | + def expect_document_start(self, first=False): |
| 16 | + if not isinstance(self.event, ruamel.yaml.events.DocumentStartEvent): |
| 17 | + return super().expect_document_start(first=first) |
| 18 | + version = self.event.version |
| 19 | + self.event.version = None |
| 20 | + ret = super().expect_document_start(first=first) |
| 21 | + self.event.version = version |
| 22 | + return ret |
| 23 | + |
| 24 | + |
| 25 | +class YAML(ruamel.yaml.YAML): |
| 26 | + def __init__(self, *args, **kwargs): |
| 27 | + super().__init__(*args, **kwargs) |
| 28 | + self.version = (1, 1) |
| 29 | + self.Emitter = YAMLEmitterNoVersionDirective |
| 30 | + self.preserve_quotes = True |
| 31 | + |
| 32 | + |
| 33 | +yaml = YAML() |
| 34 | + |
| 35 | +applications = [ |
| 36 | + { |
| 37 | + "name": "Jupyter Web App", |
| 38 | + "kustomization": ( |
| 39 | + "components/crud-web-apps/jupyter/" |
| 40 | + "manifests/base/kustomization.yaml" |
| 41 | + ), |
| 42 | + "images": [ |
| 43 | + { |
| 44 | + "name": "ghcr.io/kubeflow/notebooks/jupyter-web-app", |
| 45 | + "newName": "ghcr.io/kubeflow/notebooks/jupyter-web-app", |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "Tensorboards Web App", |
| 51 | + "kustomization": ( |
| 52 | + "components/crud-web-apps/tensorboards/" |
| 53 | + "manifests/base/kustomization.yaml" |
| 54 | + ), |
| 55 | + "images": [ |
| 56 | + { |
| 57 | + "name": "ghcr.io/kubeflow/kubeflow/tensorboards-web-app", |
| 58 | + "newName": "ghcr.io/kubeflow/kubeflow/tensorboards-web-app", |
| 59 | + }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + { |
| 63 | + "name": "Volumes Web App", |
| 64 | + "kustomization": ( |
| 65 | + "components/crud-web-apps/volumes/" |
| 66 | + "manifests/base/kustomization.yaml" |
| 67 | + ), |
| 68 | + "images": [ |
| 69 | + { |
| 70 | + "name": "ghcr.io/kubeflow/kubeflow/volumes-web-app", |
| 71 | + "newName": "ghcr.io/kubeflow/kubeflow/volumes-web-app", |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + "name": "Notebook Controller", |
| 77 | + "kustomization": ( |
| 78 | + "components/notebook-controller/" |
| 79 | + "config/base/kustomization.yaml" |
| 80 | + ), |
| 81 | + "images": [ |
| 82 | + { |
| 83 | + "name": "ghcr.io/kubeflow/kubeflow/notebook-controller", |
| 84 | + "newName": "ghcr.io/kubeflow/kubeflow/notebook-controller", |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + { |
| 89 | + "name": "PVC Viewer Controller", |
| 90 | + "kustomization": ( |
| 91 | + "components/pvcviewer-controller/" |
| 92 | + "config/base/kustomization.yaml" |
| 93 | + ), |
| 94 | + "images": [ |
| 95 | + { |
| 96 | + "name": "ghcr.io/kubeflow/kubeflow/pvcviewer-controller", |
| 97 | + "newName": "ghcr.io/kubeflow/kubeflow/pvcviewer-controller", |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + { |
| 102 | + "name": "Tensorboard Controller", |
| 103 | + "kustomization": ( |
| 104 | + "components/tensorboard-controller/" |
| 105 | + "config/base/kustomization.yaml" |
| 106 | + ), |
| 107 | + "images": [ |
| 108 | + { |
| 109 | + "name": "ghcr.io/kubeflow/kubeflow/tensorboard-controller", |
| 110 | + "newName": "ghcr.io/kubeflow/kubeflow/tensorboard-controller", |
| 111 | + }, |
| 112 | + ], |
| 113 | + }, |
| 114 | +] |
| 115 | + |
| 116 | + |
| 117 | +def update_manifests_images(applications, tag): |
| 118 | + for application in applications: |
| 119 | + log.info("Updating manifests for app %s", application["name"]) |
| 120 | + with open(application["kustomization"], "r") as file: |
| 121 | + kustomize = yaml.load(file) |
| 122 | + |
| 123 | + images = kustomize.get("images", []) |
| 124 | + for target_image in application["images"]: |
| 125 | + found = False |
| 126 | + for image in images: |
| 127 | + if image["name"] == target_image["name"]: |
| 128 | + image["newName"] = target_image["newName"] |
| 129 | + image["newTag"] = tag |
| 130 | + found = True |
| 131 | + break |
| 132 | + if not found: |
| 133 | + images.append({ |
| 134 | + "name": target_image["name"], |
| 135 | + "newName": target_image["newName"], |
| 136 | + "newTag": tag}) |
| 137 | + kustomize["images"] = images |
| 138 | + |
| 139 | + with open(application["kustomization"], "w") as file: |
| 140 | + yaml.dump(kustomize, file) |
| 141 | + |
| 142 | + |
| 143 | +def parse_args(): |
| 144 | + parser = argparse.ArgumentParser("Update image tags in manifests.") |
| 145 | + parser.add_argument("tag", type=str, help="Image tag to use.") |
| 146 | + return parser.parse_args() |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + logging.basicConfig(level=logging.INFO) |
| 151 | + args = parse_args() |
| 152 | + update_manifests_images(applications, args.tag) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + sys.exit(main()) |
0 commit comments