Skip to content

Commit 9016ed1

Browse files
committed
Added code generator for DefaultRESTMapper
We need to fork upstream's DefaultRESTMapper to be able to update and delete its contents dynamically. The script added in this commit does it automatically. On-behalf-of: SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent 91a950e commit 9016ed1

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

hack/gen-patch-defaultrestmapper.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 The KCP Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)
22+
DEFAULTRESTMAPPER_FILEPATH="${DEFAULTRESTMAPPER_FILEPATH:-$( go list -m -json k8s.io/apimachinery | jq -r .Dir )/pkg/api/meta/restmapper.go}"
23+
DEFAULTRESTMAPPER_PATCH_FILEPATH="${DEFAULTRESTMAPPER_PATCH_FILEPATH:-${REPO_ROOT}/pkg/dynamicrestmapper/defaultrestmapper_patch.go}"
24+
25+
function usage {
26+
echo "gen-patch-defaultrestmapper copies upstream's DefaultRESTMapper,"
27+
echo "makes the changes needed by our DynamicRESTMapper, and stores"
28+
echo "the result in ${DEFAULTRESTMAPPER_PATCH_FILEPATH}."
29+
echo
30+
echo "By default, gen-patch-defaultrestmapper tries to look for"
31+
echo "restmapper.go in:"
32+
echo
33+
printf "\t%s,\n" "${DEFAULTRESTMAPPER_FILEPATH}"
34+
echo
35+
echo "as detected by 'go list -m -json k8s.io/apimachinery'."
36+
echo "If you wish to use a different path, pass it in through"
37+
echo "DEFAULTRESTMAPPER_FILEPATH environment variable."
38+
echo
39+
echo "Usage:"
40+
echo
41+
printf "\t%s\n" "${0}"
42+
exit 0
43+
}
44+
45+
# Script input validation.
46+
47+
[[ $# != 0 ]] && usage
48+
[[ -f "${DEFAULTRESTMAPPER_FILEPATH}" ]] || { echo "Error: ${DEFAULTRESTMAPPER_FILEPATH} (from DEFAULTRESTMAPPER_FILEPATH) is not a file" 1>&2 ; exit 1 ; }
49+
touch "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
50+
[[ -f "${DEFAULTRESTMAPPER_PATCH_FILEPATH}" ]] || { echo "Error: ${DEFAULTRESTMAPPER_PATCH_FILEPATH} (from DEFAULTRESTMAPPER_PATCH_FILEPATH) is not a file" 1>&2 ; exit 1 ; }
51+
52+
# File generation.
53+
54+
# First, we generate the preamble with our package name, and
55+
# paste upstream's defaultrestmapper.go contents, starting right
56+
# after its `package meta` declaration.
57+
58+
cat "${REPO_ROOT}/hack/boilerplate/boilerplate.go.txt" > "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
59+
sed -i "s/YEAR/$(date +'%Y')/" "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
60+
cat >> "${DEFAULTRESTMAPPER_PATCH_FILEPATH}" << Header_EOF
61+
62+
package dynamicrestmapper
63+
64+
// Code generated by hack/gen-patch-defaultrestmapper.sh
65+
// Original file in k8s.io/apimachinery/pkg/api/meta/restmapper.go.
66+
67+
// We need this DefaultRESTMapper fork to be able to modify its
68+
// internal mappings, to support update and deletion operations.
69+
// These are implemented separately, in defaultrestmapper_mutable.go.
70+
71+
Header_EOF
72+
73+
awk '/^package meta$/ {flag=1; next} flag' "${DEFAULTRESTMAPPER_FILEPATH}" >> "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
74+
75+
# Next, we need to replace symbols that were local to the meta
76+
# package with their package-qualified names and fix the imports
77+
# in the resulting file.
78+
79+
# This is the list of all symbols that need to be referenced inside
80+
# the k8s.io/apimachinery/pkg/api/meta package.
81+
#
82+
# Please keep it up-to-date and sorted.
83+
symbols_from_meta_pkg=(
84+
AmbiguousResourceError
85+
NoKindMatchError
86+
NoResourceMatchError
87+
ResettableRESTMapper
88+
RESTMapper
89+
RESTMapping
90+
RESTScope
91+
RESTScopeName
92+
RESTScopeNameNamespace
93+
RESTScopeNameRoot
94+
)
95+
96+
for sym in ${symbols_from_meta_pkg[@]}; do
97+
gofmt -w -r "${sym} -> meta.${sym}" "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
98+
done
99+
100+
goimports -w "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
101+
102+
# Inform the caller if there were changes. Something could have broken.
103+
104+
git diff --quiet -- "${DEFAULTRESTMAPPER_PATCH_FILEPATH}" || {
105+
echo "Warning: ${DEFAULTRESTMAPPER_PATCH_FILEPATH} was modified." 1>&2
106+
echo "Warning: Please inspect the changes before continuing. Run:" 1>&2
107+
printf "\n\tgit diff -- %s\n\n" "${DEFAULTRESTMAPPER_PATCH_FILEPATH}" 1>&2
108+
exit 0
109+
}

0 commit comments

Comments
 (0)