|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | + |
| 3 | +""" |
| 4 | +koparse.py parses release.yaml files from `ko` |
| 5 | +
|
| 6 | +The `ko` tool (https://github.com/google/go-containerregistry/tree/master/cmd/ko) |
| 7 | +builds images and embeds the full names of the built images in the resulting |
| 8 | +yaml files. |
| 9 | +
|
| 10 | +This script does two things: |
| 11 | +
|
| 12 | +* Parses those image names out of the release.yaml, including their digests, and |
| 13 | + outputs those to stdout |
| 14 | +* Verifies the list of built images against an expected list, to be sure that all |
| 15 | + expected images were built (and no extra images were built) |
| 16 | +""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import re |
| 21 | +import string |
| 22 | +import sys |
| 23 | + |
| 24 | + |
| 25 | +DIGEST_MARKER = "@sha256" |
| 26 | + |
| 27 | + |
| 28 | +class ImagesMismatchError(Exception): |
| 29 | + def __init__(self, missing, extra): |
| 30 | + self.missing = missing |
| 31 | + self.extra = extra |
| 32 | + |
| 33 | + def __str__(self): |
| 34 | + errs = [] |
| 35 | + if self.missing: |
| 36 | + errs.append("Images %s were expected but missing." % self.missing) |
| 37 | + if self.extra: |
| 38 | + errs.append("Images %s were present but not expected." % |
| 39 | + self.extra) |
| 40 | + return string.join(errs, " ") |
| 41 | + |
| 42 | + |
| 43 | +class BadActualImageFormatError(Exception): |
| 44 | + def __init__(self, image): |
| 45 | + self.image = image |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return "Format of image %s was unexpected, did not contain %s" % (self.image, DIGEST_MARKER) |
| 49 | + |
| 50 | + |
| 51 | +def parse_release(base, path): |
| 52 | + """Extracts built images from the release.yaml at path |
| 53 | +
|
| 54 | + Args: |
| 55 | + base: The built images will be expected to start with this string, |
| 56 | + other images will be ignored |
| 57 | + path: The path to the file (release.yaml) that will contain the built images |
| 58 | + Returns: |
| 59 | + list of the images parsed from the file |
| 60 | + """ |
| 61 | + images = [] |
| 62 | + with open(path) as f: |
| 63 | + for line in f: |
| 64 | + match = re.search(base + ".*@sha256:[0-9a-f]*", line) |
| 65 | + if match: |
| 66 | + images.append(match.group(0)) |
| 67 | + return images |
| 68 | + |
| 69 | + |
| 70 | +def compare_expected_images(expected, actual): |
| 71 | + """Ensures that the list of actual images includes only the expected images |
| 72 | +
|
| 73 | + Args: |
| 74 | + expected: A list of all of the names of images that are expected to have |
| 75 | + been built, including the path to the image without the digest |
| 76 | + actual: A list of the names of the built images, including the path to the |
| 77 | + image and the digest |
| 78 | + """ |
| 79 | + for image in actual: |
| 80 | + if DIGEST_MARKER not in image: |
| 81 | + raise BadActualImageFormatError(image) |
| 82 | + |
| 83 | + actual_no_digest = [string.split(image, DIGEST_MARKER)[0] |
| 84 | + for image in actual] |
| 85 | + |
| 86 | + missing = set(expected) - set(actual_no_digest) |
| 87 | + extra = set(actual_no_digest) - set(expected) |
| 88 | + |
| 89 | + if missing or extra: |
| 90 | + raise ImagesMismatchError(list(missing), list(extra)) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + arg_parser = argparse.ArgumentParser( |
| 95 | + description="Parse expected built images from a release.yaml created by `ko`") |
| 96 | + arg_parser.add_argument("--path", type=str, required=True, |
| 97 | + help="Path to the release.yaml") |
| 98 | + arg_parser.add_argument("--base", type=str, required=True, |
| 99 | + help="String prefix which is used to find images within the release.yaml") |
| 100 | + arg_parser.add_argument("--images", type=str, required=True, nargs="+", |
| 101 | + help="List of all images expected to be built, without digests") |
| 102 | + args = arg_parser.parse_args() |
| 103 | + |
| 104 | + try: |
| 105 | + images = parse_release(args.base, args.path) |
| 106 | + compare_expected_images(args.images, images) |
| 107 | + except (IOError, BadActualImageFormatError) as e: |
| 108 | + sys.stderr.write("Error determining built images: %s\n" % e) |
| 109 | + sys.exit(1) |
| 110 | + except (ImagesMismatchError) as e: |
| 111 | + sys.stderr.write("Expected images did not match: %s\n" % e) |
| 112 | + with open(args.path) as f: |
| 113 | + sys.stderr.write(f.read()) |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + print("\n".join(images)) |
0 commit comments