|
| 1 | +# Copyright 2026 "Google LLC" |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +locals { |
| 16 | + # Strip leading 'v' if present |
| 17 | + clean_version = var.current_version != null ? trimprefix(var.current_version, "v") : "" |
| 18 | + clean_min_version = var.minimum_version != null ? trimprefix(var.minimum_version, "v") : "" |
| 19 | + |
| 20 | + # Regex to capture strictly major.minor.patch integers, and optionally a -gke.123 build suffix |
| 21 | + version_regex = "^([0-9]+)(?:\\.([0-9]+))?(?:\\.([0-9]+))?(?:-gke\\.([0-9]+))?(?:[-+].*)?$" |
| 22 | + |
| 23 | + # Try to parse. If it fails (e.g., 'sha256-12345'), it returns null. |
| 24 | + parsed_ver = try(regex(local.version_regex, local.clean_version), null) |
| 25 | + parsed_min = try(regex(local.version_regex, local.clean_min_version), null) |
| 26 | + |
| 27 | + is_valid_current = local.parsed_ver != null |
| 28 | + is_valid_min = local.parsed_min != null |
| 29 | + |
| 30 | + is_valid_semver = local.is_valid_current && local.is_valid_min |
| 31 | + |
| 32 | + # Map to integers, defaulting to 0 for missing patch versions or gke build numbers |
| 33 | + ver_major = local.is_valid_current ? tonumber(local.parsed_ver[0]) : 0 |
| 34 | + ver_minor = local.is_valid_current ? tonumber(coalesce(local.parsed_ver[1], "0")) : 0 |
| 35 | + ver_patch = local.is_valid_current ? tonumber(coalesce(local.parsed_ver[2], "0")) : 0 |
| 36 | + ver_gke = local.is_valid_current ? tonumber(coalesce(local.parsed_ver[3], "0")) : 0 |
| 37 | + |
| 38 | + min_major = local.is_valid_min ? tonumber(local.parsed_min[0]) : 0 |
| 39 | + min_minor = local.is_valid_min ? tonumber(coalesce(local.parsed_min[1], "0")) : 0 |
| 40 | + min_patch = local.is_valid_min ? tonumber(coalesce(local.parsed_min[2], "0")) : 0 |
| 41 | + min_gke = local.is_valid_min ? tonumber(coalesce(local.parsed_min[3], "0")) : 0 |
| 42 | + |
| 43 | + # Fail-open logic for custom tags |
| 44 | + is_greater_than_or_equal = local.is_valid_min ? ( |
| 45 | + !local.is_valid_current || |
| 46 | + local.ver_major > local.min_major || |
| 47 | + (local.ver_major == local.min_major && local.ver_minor > local.min_minor) || |
| 48 | + (local.ver_major == local.min_major && local.ver_minor == local.min_minor && local.ver_patch > local.min_patch) || |
| 49 | + (local.ver_major == local.min_major && local.ver_minor == local.min_minor && local.ver_patch == local.min_patch && local.ver_gke >= local.min_gke) |
| 50 | + ) : false |
| 51 | +} |
0 commit comments