Skip to content

Commit 538e5bc

Browse files
authored
Merge pull request #3316 from gman0/dyn-restmapper
✨ Add DynamicRESTMapper
2 parents 515982e + b1eff3d commit 538e5bc

12 files changed

+1870
-6
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ crds: $(CONTROLLER_GEN) $(YAML_PATCH) ## Generate crds
201201
codegen: $(KCP_APIGEN_GEN) crds ## Generate all
202202
go mod download
203203
./hack/update-codegen-clients.sh
204+
./hack/gen-patch-defaultrestmapper.sh
204205
$(MAKE) imports
205206
.PHONY: codegen
206207

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/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+
}

pkg/reconciler/apis/apibinding/logical_cluster_lock.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ type ExpirableLock struct {
6161
// to prevent races of multiple bindings or CRDs owning the same resource.
6262
type ResourceBindingsAnnotation map[string]ExpirableLock
6363

64+
// UnmarshalResourceBindingsAnnotation unmarshals JSON-formatted string
65+
// into ResourceBindingsAnnotation map.
66+
func UnmarshalResourceBindingsAnnotation(ann string) (ResourceBindingsAnnotation, error) {
67+
rbs := make(ResourceBindingsAnnotation)
68+
if err := json.Unmarshal([]byte(ann), &rbs); err != nil {
69+
return nil, fmt.Errorf("failed to unmarshal ResourceBindings annotation: %w", err)
70+
}
71+
if rbs == nil {
72+
rbs = make(ResourceBindingsAnnotation)
73+
}
74+
75+
return rbs, nil
76+
}
77+
6478
// WithLockedResources tries to lock the resources for the given binding. It
6579
// returns those resources that got successfully locked. If a resource is already
6680
// locked by another binding, it is skipped and returned in the second return
@@ -73,12 +87,9 @@ func WithLockedResources(crds []*apiextensionsv1.CustomResourceDefinition, now t
7387
return nil, nil, nil, fmt.Errorf("%s annotation not found, migration has to happen first", ResourceBindingsAnnotationKey)
7488
}
7589

76-
rbs := make(ResourceBindingsAnnotation)
77-
if err := json.Unmarshal([]byte(v), &rbs); err != nil {
78-
return nil, nil, nil, fmt.Errorf("failed to unmarshal ResourceBindings annotation: %w", err)
79-
}
80-
if rbs == nil {
81-
rbs = make(ResourceBindingsAnnotation)
90+
rbs, err := UnmarshalResourceBindingsAnnotation(v)
91+
if err != nil {
92+
return nil, nil, nil, err
8293
}
8394

8495
crdNames := make(map[string]bool, len(crds))

0 commit comments

Comments
 (0)