| 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 helper functions to create JSON patches for Kubernetes resources as per RFC6902 (patchJson6902). |
| 19 | |
| 20 | |
| 21 | # Helper function to create an operation for a JSON patch (patchJson6902) as per RFC6902 |
| 22 | # |
| 23 | # Example: |
| 24 | # $ jsonpatch create operation add /spec/template/spec/securityContext {runAsUser: 10000, fsGroup: 1337} | to yaml |
| 25 | # |
| 26 | # op: add |
| 27 | # path: /spec/template/spec/securityContext |
| 28 | # value: |
| 29 | # runAsUser: 10000 |
| 30 | # fsGroup: 1337 |
| 31 | export def "create operation" [ |
| 32 | op: string, # Operation type: "add", "remove", "replace", "move", "copy", or "test" |
| 33 | path: string, # JSON pointer path at the target key location in format "/a/b/c" |
| 34 | value?: any # Value to be added, replaced, or removed |
| 35 | from?: string, # JSON pointer path (format "/a/b/c") at the TARGET RESOURCE to take as source in "copy" or "move" operations. |
| 36 | ]: [ |
| 37 | nothing -> record |
| 38 | ] { |
| 39 | if $op in ["add", "replace"] { |
| 40 | if not ($value | is-empty) { |
| 41 | { |
| 42 | op: $op, |
| 43 | path: $path, |
| 44 | value: $value |
| 45 | } |
| 46 | } else { |
| 47 | error make { msg: "Value is required for 'add' and 'replace' operations." } |
| 48 | } |
| 49 | } else if $op in ["remove"] { |
| 50 | { |
| 51 | op: $op, |
| 52 | path: $path |
| 53 | } |
| 54 | } else if $op in ["move", "copy"] { |
| 55 | if not ($from | is-empty) { |
| 56 | { |
| 57 | op: $op, |
| 58 | from: $from, |
| 59 | path: $path |
| 60 | } |
| 61 | } else { |
| 62 | error make { msg: "Source path is required for 'move' and 'copy' operations." } |
| 63 | } |
| 64 | } else { |
| 65 | error make { msg: "Invalid operation type. Supported values are 'add', 'remove', 'replace', 'move', 'copy'. See RFC6902 for details." } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | # Helper to create a full JSON patch (patchJson6902), including the target object specification and a list of operations |
| 71 | # |
| 72 | # Example 1: Using records directly |
| 73 | # $ jsonpatch create {kind: Deployment, name: podinfo} {op: add, target: /spec/template/spec/securityContext, value: {runAsUser: 10000, fsGroup: 1337}} | to yaml |
| 74 | # |
| 75 | # target: |
| 76 | # kind: Deployment |
| 77 | # name: podinfo |
| 78 | # patch: | |
| 79 | # - op: add |
| 80 | # path: /spec/template/spec/securityContext |
| 81 | # value: |
| 82 | # runAsUser: 10000 |
| 83 | # fsGroup: 1337 |
| 84 | # |
| 85 | # Example 2: Leveraging the operation helper function |
| 86 | # $ jsonpatch create {kind: Deployment, name: podinfo} (jsonpatch create operation add /spec/template/spec/securityContext {runAsUser: 10000, fsGroup: 1337}) | to yaml |
| 87 | # |
| 88 | # target: |
| 89 | # kind: Deployment |
| 90 | # name: podinfo |
| 91 | # patch: | |
| 92 | # - op: add |
| 93 | # path: /spec/template/spec/securityContext |
| 94 | # value: |
| 95 | # runAsUser: 10000 |
| 96 | # fsGroup: 1337 |
| 97 | export def "create" [ |
| 98 | target: record, # Target resource specification as per <https://github.com/kubernetes-sigs/kustomize/blob/master/examples/patchMultipleObjects.md> |
| 99 | ...operations: record # List of patch operations as per RFC6902 |
| 100 | ]: [ |
| 101 | nothing -> record |
| 102 | ] { |
| 103 | { |
| 104 | target: $target, |
| 105 | patch: ($operations | to yaml) |
| 106 | } |
| 107 | } |