|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +import json |
| 8 | +import pathlib |
| 9 | +import sys |
| 10 | + |
| 11 | +import ci.util |
| 12 | + |
| 13 | + |
| 14 | +dependency_type = ci.util.check_env('DEPENDENCY_TYPE') |
| 15 | +if not dependency_type == 'component': |
| 16 | + ci.util.fail( |
| 17 | + "don't know how to upgrade dependency type: " |
| 18 | + f'{dependency_type}' |
| 19 | + ) |
| 20 | + |
| 21 | +dependency_name = ci.util.check_env('DEPENDENCY_NAME') |
| 22 | +dependency_version = ci.util.check_env('DEPENDENCY_VERSION') |
| 23 | + |
| 24 | +images_file = pathlib.Path( |
| 25 | + ci.util.check_env('REPO_DIR'), |
| 26 | + 'imagevector', |
| 27 | + 'images.yaml', |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class ImagesParser(object): |
| 32 | + ''' |
| 33 | + a naive YAML-parser crafted for the special case of processing |
| 34 | + gardener's images.yaml file; crafted that way to preserve |
| 35 | + comments/empty lines |
| 36 | + ''' |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + images_file, |
| 40 | + names, |
| 41 | + target_version, |
| 42 | + ): |
| 43 | + self.images_file = images_file |
| 44 | + self.lines = images_file.read_text().split('\n') |
| 45 | + self.names = names |
| 46 | + self.target_version = target_version |
| 47 | + self._line_idx = 0 |
| 48 | + |
| 49 | + def _line(self): |
| 50 | + return self.lines[self._line_idx] |
| 51 | + |
| 52 | + def _next_line(self): |
| 53 | + self._line_idx += 1 |
| 54 | + return self._line() |
| 55 | + |
| 56 | + def _skip_to_next_entry(self, names): |
| 57 | + while not self._line().startswith('-'): |
| 58 | + self._next_line() |
| 59 | + name = self._line().strip().split(':')[-1].strip() |
| 60 | + |
| 61 | + if name not in names: |
| 62 | + self._next_line() |
| 63 | + return self._skip_to_next_entry(names) |
| 64 | + |
| 65 | + # found one of the entries: |
| 66 | + return name |
| 67 | + |
| 68 | + def _skip_to_next_tag(self): |
| 69 | + self._next_line() |
| 70 | + while not self._line().startswith('-'): |
| 71 | + if self._line().strip().startswith('tag:'): |
| 72 | + return |
| 73 | + self._next_line() |
| 74 | + raise RuntimeError('did not find tag attribute') |
| 75 | + |
| 76 | + def set_versions(self): |
| 77 | + while self.names: |
| 78 | + try: |
| 79 | + name = self._skip_to_next_entry(self.names) |
| 80 | + except IndexError: |
| 81 | + print(str(self.names)) |
| 82 | + ci.util.fail('don\'t know how to update ' + str(self.names)) |
| 83 | + self.names.remove(name) |
| 84 | + self._skip_to_next_tag() |
| 85 | + tag_line = self._line() |
| 86 | + indent = len(tag_line) - len(tag_line.lstrip()) |
| 87 | + patched_line = ' ' * indent + 'tag: "{version}"'.format( |
| 88 | + version=self.target_version, |
| 89 | + ) |
| 90 | + self.lines[self._line_idx] = patched_line |
| 91 | + |
| 92 | + def write_updated_file(self): |
| 93 | + self.images_file.write_text( |
| 94 | + '\n'.join(self.lines) |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +# optionally load special cases from first argument given as JSON |
| 99 | +injectedSpecialCases = {} |
| 100 | +if len(sys.argv) == 2: |
| 101 | + injectedSpecialCases = json.loads(sys.argv[1]) |
| 102 | + |
| 103 | +# handle special cases |
| 104 | +name = dependency_name.split('/')[-1] |
| 105 | +if name in injectedSpecialCases: |
| 106 | + names = injectedSpecialCases[name] |
| 107 | +elif name == 'autoscaler': |
| 108 | + names = ['cluster-autoscaler'] |
| 109 | +elif name == 'vpn2': |
| 110 | + names = ['vpn-server', 'vpn-client'] |
| 111 | +elif name == 'external-dns-management': |
| 112 | + names = ['dns-controller-manager'] |
| 113 | +elif name == 'logging': |
| 114 | + names = ['fluent-bit-plugin-installer', 'vali-curator', 'telegraf', 'event-logger', 'tune2fs'] |
| 115 | +elif name == 'etcd-custom-image': |
| 116 | + names = ['etcd'] |
| 117 | +elif name == 'egress-filter-refresher': |
| 118 | + names = ['egress-filter'] |
| 119 | +elif name == 'apiserver-proxy': |
| 120 | + names = ['apiserver-proxy-sidecar'] |
| 121 | +else: |
| 122 | + names = [name] |
| 123 | + |
| 124 | + |
| 125 | +parser = ImagesParser( |
| 126 | + images_file=images_file, |
| 127 | + names=names, |
| 128 | + target_version=dependency_version, |
| 129 | +) |
| 130 | + |
| 131 | +parser.set_versions() |
| 132 | +parser.write_updated_file() |
0 commit comments