Skip to content

Commit 4ec2c72

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 inside our DynamicRESTMapper. The script added in this commit does it automatically. On-behalf-of: @SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent afda2b4 commit 4ec2c72

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
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/reconciler/dynamicrestmapper/defaultrestmapper_kcp.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+
import "k8s.io/apimachinery/pkg/api/meta" // Import the source package of the original file.
72+
73+
Header_EOF
74+
75+
awk '/^package meta$/ {flag=1; next} flag' "${DEFAULTRESTMAPPER_FILEPATH}" >> "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
76+
77+
# Next, we need to replace symbols that were local to the meta
78+
# package with their package-qualified names and fix the imports
79+
# in the resulting file.
80+
81+
# This is the list of all symbols that need to be referenced inside
82+
# the k8s.io/apimachinery/pkg/api/meta package.
83+
#
84+
# Please keep it up-to-date and sorted.
85+
symbols_from_meta_pkg=(
86+
AmbiguousResourceError
87+
NoKindMatchError
88+
NoResourceMatchError
89+
ResettableRESTMapper
90+
RESTMapper
91+
RESTMapping
92+
RESTScope
93+
RESTScopeName
94+
RESTScopeNameNamespace
95+
RESTScopeNameRoot
96+
)
97+
98+
for sym in ${symbols_from_meta_pkg[@]}; do
99+
gofmt -w -r "${sym} -> meta.${sym}" "${DEFAULTRESTMAPPER_PATCH_FILEPATH}"
100+
done
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)