blob: dde50959262a7e10529d8db7b874453f35f6d9ea [file] [log] [blame]
garciadeblas83775ba2025-07-23 18:35:24 +02001#######################################################################################
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 utility functions to concatenate descriptions of Kubernetes resources, either embedded in ResourceLists or from plain multi-resource manifests (i.e. lists of records).
19
20
21# Join two ResourceLists, one from stdin and another from the first argument.
22# Empty records at stdin or at the parameter are valid and should be treated as empty ResourceLists.
23export def resourcelists [
24 resourcelist2: record # 2nd `ResourceList` to concatenate
25]: [
26 record -> record
27 nothing -> record
28] {
29 # Gather the input and convert to record if empty
30 let list1: record = if $in == null { {} } else { $in }
31 let list2: record = if $resourcelist2 == null { {} } else { $resourcelist2 }
32
33 # If both are empty, returns an empty ResourceList
34 if $list1 == {} and $list2 == {} {
35 {
36 apiVersion: "config.kubernetes.io/v1"
37 kind: "ResourceList"
38 items: []
39 }
40 } else if $list2 == {} {
41 # If the second ResourceList is empty, returns just the one from stdin
42 $list1
43 } else {
44 # Merge both resource lists strategically
45 {
46 apiVersion: "config.kubernetes.io/v1"
47 kind: "ResourceList"
48 items: ($list1.items? | append $list2.items?)
49 }
50 # ALTERNATIVELY: $in_list | merge deep --strategy "append" $source_list
51 ## Strategy is "append", so that item lists are appended
52 }
53}
54
55export alias resourcelist = resourcelists
56export alias rl = resourcelists
57
58
59# Join two ResourceList files
60# NOT EXPORTED
61def "resourcelists from files" [file1: path, file2: path] {
62 let list1 = (open $file1)
63 let list2 = (open $file2)
64 $list1 | merge $list2
65}
66
67alias join_lists = resourcelists from files
68
69
70# Join two manifests, one from stdin and another from the first argument
71# Empty manifests at stdin or at the parameter are valid and should be treated as empty manifests.
72export def manifests [
73 mnfst2: any # 2nd manifest to concatenate
74]: [
75 any -> list<any>
76] {
77
78 # Gather the input and convert to list
79 # let manifest1: list<any> = if $in == null { [] } else { $in }
80 # let manifest2: list<any> = if $mnfst2 == null { [] } else { $mnfst2 }
81 let manifest1: list<any> = (if $in == null { [] }
82 else if ($in | describe | str starts-with "record") { [ $in ] }
83 else if ($in | describe | str starts-with "list") or ($in | describe | str starts-with "table") { $in }
84 else { error make {msg: $"Error: Expected a record or a list of records, but received ($in | describe)."}})
85
86 let manifest2: list<any> = (if $mnfst2 == null { [] }
87 else if ($mnfst2 | describe | str starts-with "record") { [ $mnfst2 ] }
88 else if ($mnfst2 | describe | str starts-with "list") or ($mnfst2 | describe | str starts-with "table") { $mnfst2 }
89 else { error make {msg: $"Error: Expected a record or a list of records, but received ($mnfst2 | describe)."}})
90
91 # Return the concatenation
92 [
93 $manifest1
94 $manifest2
95 ] | flatten
96
97 # How to convert to YAML manifests again:
98 #
99 # let merged_manifests = ($manifest1 | manifests $manifest2)
100 # $merged_manifests
101 # | each { |obj| $obj | to yaml }
102 # | str join "---\n"
103}
104
105export alias manifest = manifests