|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: 'Locating provider-managed images' |
| 4 | +author: |
| 5 | + - 'Kurt Garloff' |
| 6 | +avatar: |
| 7 | + - 'kgarloff.jpg' |
| 8 | +--- |
| 9 | + |
| 10 | +## Purpose |
| 11 | + |
| 12 | +Many providers provide public images that they maintain for user convenience. |
| 13 | +Maintenance means that they regularly update it to include the latest bug- and |
| 14 | +security fixes. The exact policy is transparent from the image metadata as |
| 15 | +specified in SCS standard [scs-0102](https://docs.scs.community/standards/iaas/scs-0102). |
| 16 | +A few images have to be managed this way by the provider according to |
| 17 | +[scs-0104](https://docs.scs.community/standards/iaas/scs-0104). |
| 18 | +Previously (with [scs-0102-v1](https://docs.scs.community/standards/scs-0102-v1-image-metadata)) |
| 19 | +the image could be referenced by a standard name to always get the current |
| 20 | +image whereas a reference by UUID would result in an unchanged image (until |
| 21 | +it is removed according to the provider's policy that is transparent from |
| 22 | +the metadata). |
| 23 | + |
| 24 | +Some providers prefer to use different image names. We intend to allow this with |
| 25 | +[scs-0102-v2](https://docs.scs.community/standards/scs-0102-v2-image-metadata). |
| 26 | +This however means that identifying the most recent "Ubuntu 24.04" image on |
| 27 | +an SCS-compatible IaaS cloud becomes a bit harder in a portable way. |
| 28 | +This article describes how to do this. |
| 29 | + |
| 30 | +## The new `os_purpose` property |
| 31 | + |
| 32 | +While we suggest to rename or better to hide old images, there can still legitimately |
| 33 | +be several variants of images, e.g. minimal variants or Kubernetes node images etc. |
| 34 | +These must not be confused with the standard general purpose images. To avoid |
| 35 | +confusion, we have introduce a new `os_purpose` (recommended in v1.1 of scs-0102 |
| 36 | +and mandatory in v2) field, that can be set to `generic`, `minimal`, `k8snode`, |
| 37 | +`gpu`, `network`, or `custom` in v2. |
| 38 | +To now find the latest general purpose Ubuntu Noble Numbat 24.04 image, one can search the |
| 39 | +image catalog for `os_distro=ubuntu`, `os_version=24.04`, and `os_purpose=generic`. |
| 40 | +This is straightforward if all SCS clouds already comply to the new metadata standard |
| 41 | +and only have one matching image. |
| 42 | +It's a bit more complex in case we have to deal with a mixture of old and new ... |
| 43 | + |
| 44 | +## Identifying the right image using python (openstack-SDK) |
| 45 | + |
| 46 | +To find the Ubuntu 24.04 generic image, we would just do |
| 47 | + |
| 48 | +```python |
| 49 | + images = [x for x in conn.image.images(os_distro=distro, os_version=version, |
| 50 | + sort="name:desc,created_at:desc") |
| 51 | + if x.properties.get("os_purpose") == purpose] |
| 52 | +``` |
| 53 | + |
| 54 | +where `conn` is a connection to your OpenStack project and `distro`, `version` and |
| 55 | +`purpose` have been set to the lowercase strings you are looking for. |
| 56 | + |
| 57 | +Three notes: |
| 58 | + |
| 59 | +- We use a list comprehension to filter for `os_purpose` because `os_purpose` |
| 60 | + is not one of the hardcoded properties that the SDK knows unlike `os_distro` |
| 61 | + and `os_version`. |
| 62 | +- We can add additional filtering such as `visibility="public"` if we just want |
| 63 | + to look for public images. |
| 64 | +- We sort the list, so in case we have several matches, we want the images grouped |
| 65 | + by image name and within the same name have the latest images first. This would |
| 66 | + typically find the latest image both in the case where a provider renames old |
| 67 | + images "Ubuntu 24.04" to "Ubuntu 24.04 timestamp" or fails to rename them. |
| 68 | + (The latter would not be compliant with scs-0102.) |
| 69 | + |
| 70 | +It gets a bit harder when you want SCS clouds that comply to the old v1 standard |
| 71 | +and do not yet have the `os_purpose` field set. Above call then returns an empty |
| 72 | +list. We then would fall back to look for images that match `os_distro` and |
| 73 | +`os_version`, but have no `os_purpose` property. |
| 74 | + |
| 75 | +```python |
| 76 | + images = [x for x in conn.image.images(os_distro=distro, os_version=version, |
| 77 | + sort="name:desc,created_at:desc") |
| 78 | + if "os_purpose" not in x.properties] |
| 79 | +``` |
| 80 | + |
| 81 | +We have to expect several matches here and need some heuristic to find the |
| 82 | +right image, preferrably the one matching the old naming convention. |
| 83 | + |
| 84 | +Full code that does this is available in [find_img.py](find_img.py). |
| 85 | +Feel free to copy, I deliberately put this under MIT license. |
| 86 | + |
| 87 | +## Identifying the image with OpenStack CLI |
| 88 | + |
| 89 | +Unlike with Python, we can pass the `os_purpose` field just like the other |
| 90 | +properties. |
| 91 | + |
| 92 | +```bash |
| 93 | + openstack image list --property os_distro="$DIST" --property os_version="$VERS" --property os_purpose="$PURP" -f value -c ID -c Name --sort name:desc,created_at:desc |
| 94 | +``` |
| 95 | + |
| 96 | +where `OS_CLOUD` environment has been configured to access your cloud project and |
| 97 | +`DIST`, `VERS` and `PURP` are set to the lowercased image properties you |
| 98 | +are looking for. An additional filter `--public` parameter could be passed to only |
| 99 | +list public images. See above python comment for the sorting rationale. |
| 100 | + |
| 101 | +Dealing with old SCS clouds (not yet implementing v2 of scs-0102) is harder |
| 102 | +with shell code. The reason is that we can not pass a flag to `openstack |
| 103 | +image list` that would tell it to restrict results to records without an |
| 104 | +`os_purpose` property. So this requires looping over the images and filtering |
| 105 | +out all images with `os_purpose` (but not matching our request). |
| 106 | + |
| 107 | +Full code that does this is available in [find_img.sh](find_img.sh). |
| 108 | + |
| 109 | +## Terraform / opentofu |
| 110 | + |
| 111 | +TBW |
0 commit comments