| garciadeblas | 83775ba | 2025-07-23 18:35:24 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | # Module with custom functions to manage an App instance, invoking the corresponding KSU renderizations to appropriate target folders in a given profile. |
| 19 | |
| 20 | |
| 21 | # Import required modules |
| 22 | use ../krm * |
| 23 | # use ./replace.nu * |
| 24 | # use ./ksu.nu * |
| 25 | use ./replace.nu |
| 26 | use ./ksu.nu |
| 27 | |
| 28 | |
| 29 | # Create an instance of an App, based on an App instance model received from stdin. |
| 30 | export def create [ |
| 31 | --dry-run # If set, only prints the generated ResourceList(s) along with the target folder(s) (i.e., it does not write to any folder). |
| 32 | --print-target-folders # If set, print the target folder(s). Requires --dry-run. |
| 33 | environment: record # Record with environment variables to load. |
| 34 | ]: [ |
| 35 | record -> nothing |
| 36 | record -> table |
| 37 | ] { |
| 38 | # TODO: Format checks |
| 39 | |
| 40 | # Save the original app instance record |
| 41 | let in_instance: record = $in |
| 42 | |
| 43 | # Remove from the environment those keys that are reserved, dynamic or forbidden, since they will be overriden or may cause known issues, and add one that mimics the KSU name |
| 44 | const forbidden_keys: list<cell-path> = [ |
| 45 | $.KSU_NAME |
| 46 | $.PATTERN_NAME |
| 47 | $.BRICK_NAME |
| 48 | # Add new reserved keys here as needed: |
| 49 | # . . . |
| 50 | ] |
| 51 | let updated_environment: record = ( |
| 52 | $environment |
| 53 | | reject -i ...$forbidden_keys |
| 54 | ) |
| 55 | |
| 56 | # Load environment variables and update the record |
| 57 | let instance_rendered: record = ( |
| 58 | $in_instance |
| 59 | | replace vars $updated_environment |
| 60 | ) |
| 61 | |
| 62 | # Get the key parts |
| 63 | let app_name: string = ($instance_rendered | get $.metadata.name | str downcase) |
| 64 | let spec: record = ($instance_rendered | get spec) |
| 65 | let ksus: list<record> = ($spec | get ksus) |
| 66 | |
| 67 | # Process all App's KSUs |
| 68 | $ksus | each {|k| |
| 69 | $k |
| 70 | | ksu create --dry-run=$dry_run --print-target-folder=$print_target_folders $updated_environment |
| 71 | } |
| 72 | # Make sure it only returns a value when its's not an empty list |
| 73 | | if ($in | is-not-empty) { $in } else { $in | ignore } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | # Delete an instance of an App, based on an App instance model received from stdin. |
| 78 | export def delete [ |
| 79 | --dry-run # If set, only prints the ResourceList(s) with the resources that would be removed. |
| 80 | --print-target-folders # If set, print the target folder(s) to be removed. Requires --dry-run. |
| 81 | environment: record # Record with environment variables to load. |
| 82 | ]: [ |
| 83 | record -> nothing |
| 84 | record -> table |
| 85 | ] { |
| 86 | # Save the original app instance record |
| 87 | let in_instance: record = $in |
| 88 | |
| 89 | # Remove from the environment those keys that are reserved, dynamic or forbidden, since they will be overriden or may cause known issues, and add one that mimics the KSU name |
| 90 | const forbidden_keys: list<cell-path> = [ |
| 91 | $.KSU_NAME |
| 92 | $.PATTERN_NAME |
| 93 | $.BRICK_NAME |
| 94 | # Add new reserved keys here as needed: |
| 95 | # . . . |
| 96 | ] |
| 97 | let updated_environment: record = ( |
| 98 | $environment |
| 99 | | reject -i ...$forbidden_keys |
| 100 | ) |
| 101 | |
| 102 | # Load environment variables and update the record |
| 103 | let instance_rendered: record = ( |
| 104 | $in_instance |
| 105 | | replace vars $updated_environment |
| 106 | ) |
| 107 | |
| 108 | # Get the key parts |
| 109 | let app_name: string = ($instance_rendered | get $.metadata.name | str downcase) |
| 110 | let spec: record = ($instance_rendered | get spec) |
| 111 | let ksus: list<record> = ($spec | get ksus) |
| 112 | |
| 113 | # Process all App's KSUs |
| 114 | $ksus | each {|k| |
| 115 | $k |
| 116 | | ksu delete --dry-run=$dry_run --print-target-folder=$print_target_folders $updated_environment |
| 117 | } |
| 118 | # Make sure it only returns a value when its's not an empty list |
| 119 | | if ($in | is-not-empty) { $in } else { $in | ignore } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | # Update an instance of an App, based on an App instance model received from stdin. |
| 124 | export def "update existing" [ |
| 125 | --dry-run # If set, only prints the ResourceList(s) with the resources that would be removed. |
| 126 | --print-target-folders # If set, print the target folder(s) to be updated. Requires --dry-run. |
| 127 | --diff-files # If set, returns the list of files expected to change in the target folder(s). Requires --dry-run. |
| 128 | --diffs # If set, returns the expected full diff expected to change in the target folder(s). Requires --dry-run. It can be combined with `--diff-files` |
| 129 | environment: record # Record with environment variables to load. |
| 130 | ]: [ |
| 131 | record -> nothing |
| 132 | record -> table |
| 133 | record -> string |
| 134 | ] { |
| 135 | # Save the original app instance record |
| 136 | let in_instance: record = $in |
| 137 | |
| 138 | # Remove from the environment those keys that are reserved, dynamic or forbidden, since they will be overriden or may cause known issues, and add one that mimics the KSU name |
| 139 | const forbidden_keys: list<cell-path> = [ |
| 140 | $.KSU_NAME |
| 141 | $.PATTERN_NAME |
| 142 | $.BRICK_NAME |
| 143 | # Add new reserved keys here as needed: |
| 144 | # . . . |
| 145 | ] |
| 146 | let updated_environment: record = ( |
| 147 | $environment |
| 148 | | reject -i ...$forbidden_keys |
| 149 | ) |
| 150 | |
| 151 | # Load environment variables and update the record |
| 152 | let instance_rendered: record = ( |
| 153 | $in_instance |
| 154 | | replace vars $updated_environment |
| 155 | # Overwrite the ksu section with its original values, since we do not want to replace the placeholders yet |
| 156 | | upsert $.spec.ksus ($in_instance | get $.spec.ksus) |
| 157 | ) |
| 158 | |
| 159 | # Get the key parts |
| 160 | let app_name: string = ($instance_rendered | get $.metadata.name | str downcase) |
| 161 | let spec: record = ($instance_rendered | get spec) |
| 162 | let ksus: list<record> = ($spec | get ksus) |
| 163 | |
| 164 | # Process all App's KSUs |
| 165 | $ksus | each {|k| |
| 166 | $k |
| 167 | | ( |
| 168 | ksu update |
| 169 | --print-target-folder=$print_target_folders |
| 170 | --dry-run=$dry_run |
| 171 | --diff-files=$diff_files |
| 172 | --diff=$diffs |
| 173 | $updated_environment |
| 174 | ) |
| 175 | } |
| 176 | # Make sure it only returns a value when it is not an empty list |
| 177 | | if ($in | is-not-empty) { |
| 178 | let output: any = $in |
| 179 | |
| 180 | # If the output is a list of strings, it better provides their concatenation |
| 181 | let output_type: string = ($output | describe) |
| 182 | if ($output_type == "list<string>") { |
| 183 | $output | str join "\n" |
| 184 | # Otherwise, it returns the value as it is |
| 185 | } else { |
| 186 | $output |
| 187 | } |
| 188 | } else { $in | ignore } |
| 189 | } |
| 190 | |
| 191 | export alias update = update existing |
| 192 | |
| 193 | |
| 194 | # Get the Kustomizations that would be created on an instance of an App, based on an App instance model received from stdin. |
| 195 | export def "get kustomization" [ |
| 196 | environment: record # Record with environment variables to load. |
| 197 | ]: [ |
| 198 | record -> record |
| 199 | ] { |
| 200 | create --dry-run $environment |
| 201 | | get $.items | default [] |
| 202 | | flatten |
| 203 | | where apiVersion == 'kustomize.toolkit.fluxcd.io/v1' |
| 204 | | where kind == 'Kustomization' |
| 205 | | get $.metadata |
| 206 | | select name namespace |
| 207 | | default 'flux-system' namespace |
| 208 | } |
| 209 | |
| 210 | export alias "get kustomizations" = get kustomization |
| 211 | export alias "get ks" = get kustomization |