Skip to content

Commit 49fa562

Browse files
authored
Merge pull request kcp-dev#42 from xrstf/kcp-1.28
✨ Rebase kcp modifications onto v0.16.2
2 parents 7db81c7 + 769ed77 commit 49fa562

File tree

22 files changed

+444
-48
lines changed

22 files changed

+444
-48
lines changed

.prow.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
presubmits:
2+
- name: pull-controller-runtime-everything
3+
always_run: true
4+
decorate: true
5+
clone_uri: "ssh://[email protected]/kcp-dev/controller-runtime.git"
6+
labels:
7+
preset-goproxy: "true"
8+
spec:
9+
containers:
10+
- image: ghcr.io/kcp-dev/infra/build:1.20.9-1
11+
command:
12+
- make
13+
- test

DOWNSTREAM_OWNERS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
approvers:
2+
- davidfestal
3+
- ncdc
4+
- stevekuznetsov
5+
- sttts
6+
reviewers:
7+
- fabianvf
8+
- varshaprasad96

DOWNSTREAM_OWNERS_ALIASES

Whitespace-only changes.

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ $(GO_APIDIFF): $(TOOLS_DIR)/go.mod # Build go-apidiff from tools folder.
7373
$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
7474
cd $(TOOLS_DIR) && go build -tags=tools -o bin/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen
7575

76-
$(GOLANGCI_LINT): .github/workflows/golangci-lint.yml # Download golanci-lint using hack script into tools folder.
77-
hack/ensure-golangci-lint.sh \
78-
-b $(TOOLS_BIN_DIR) \
79-
$(shell cat .github/workflows/golangci-lint.yml | grep "version: v" | sed 's/.*version: //')
76+
$(GOLANGCI_LINT): .github/workflows/golangci-lint.yml # Download golangci-lint using hack script into tools folder.
77+
GOBIN=$(abspath $(TOOLS_BIN_DIR)) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(shell cat .github/workflows/golangci-lint.yml | grep "version: v" | sed 's/.*version: //')
78+
79+
.PHONY: tools
80+
tools: $(GO_APIDIFF) $(CONTROLLER_GEN) $(GOLANGCI_LINT)
8081

8182
## --------------------------------------
8283
## Linting

examples/scratch-env/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
936936
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
937937
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
938938
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
939+
github.com/kcp-dev/apimachinery/v2 v2.0.0-alpha.0.0.20230926071920-57d168bcbe34 h1:tom0JX5OmAeOOmkGv8LaYHDtA1xAKDiQL5U0vhYYgdM=
940+
github.com/kcp-dev/apimachinery/v2 v2.0.0-alpha.0.0.20230926071920-57d168bcbe34/go.mod h1:cWoaYGHl1nlzdEM2xvMzIASkEZJZLSf5nhe17M7wDhw=
941+
github.com/kcp-dev/logicalcluster/v3 v3.0.4 h1:q7KngML/QM7sWl8aVzmfZF0TPMnBwYNxsPKfwUvvBvU=
942+
github.com/kcp-dev/logicalcluster/v3 v3.0.4/go.mod h1:EWBUBxdr49fUB1cLMO4nOdBWmYifLbP1LfoL20KkXYY=
939943
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
940944
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
941945
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ require (
1010
github.com/go-logr/zapr v1.2.4
1111
github.com/google/go-cmp v0.5.9
1212
github.com/google/gofuzz v1.2.0
13+
github.com/kcp-dev/apimachinery/v2 v2.0.0-alpha.0.0.20230926071920-57d168bcbe34
14+
github.com/kcp-dev/logicalcluster/v3 v3.0.4
1315
github.com/onsi/ginkgo/v2 v2.11.0
1416
github.com/onsi/gomega v1.27.10
1517
github.com/prometheus/client_golang v1.16.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
214214
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
215215
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
216216
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
217+
github.com/kcp-dev/apimachinery/v2 v2.0.0-alpha.0.0.20230926071920-57d168bcbe34 h1:tom0JX5OmAeOOmkGv8LaYHDtA1xAKDiQL5U0vhYYgdM=
218+
github.com/kcp-dev/apimachinery/v2 v2.0.0-alpha.0.0.20230926071920-57d168bcbe34/go.mod h1:cWoaYGHl1nlzdEM2xvMzIASkEZJZLSf5nhe17M7wDhw=
219+
github.com/kcp-dev/logicalcluster/v3 v3.0.4 h1:q7KngML/QM7sWl8aVzmfZF0TPMnBwYNxsPKfwUvvBvU=
220+
github.com/kcp-dev/logicalcluster/v3 v3.0.4/go.mod h1:EWBUBxdr49fUB1cLMO4nOdBWmYifLbP1LfoL20KkXYY=
217221
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
218222
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
219223
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

pkg/cache/cache.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/http"
2323
"time"
2424

25+
"github.com/kcp-dev/apimachinery/v2/third_party/informers"
2526
corev1 "k8s.io/api/core/v1"
2627
"k8s.io/apimachinery/pkg/api/meta"
2728
"k8s.io/apimachinery/pkg/fields"
@@ -159,6 +160,14 @@ type Options struct {
159160
// instead of `reconcile.Result{}`.
160161
SyncPeriod *time.Duration
161162

163+
// NewInformerFunc is a function that is used to create SharedIndexInformers.
164+
// Defaults to cache.NewSharedIndexInformer from client-go
165+
NewInformerFunc client.NewInformerFunc
166+
167+
// Indexers is the indexers that the informers will be configured to use.
168+
// Will always have the standard NamespaceIndex.
169+
Indexers toolscache.Indexers
170+
162171
// ReaderFailOnMissingInformer configures the cache to return a ErrResourceNotCached error when a user
163172
// requests, using Get() and List(), a resource the cache does not already have an informer for.
164173
//
@@ -201,9 +210,6 @@ type Options struct {
201210
// ByObject restricts the cache's ListWatch to the desired fields per GVK at the specified object.
202211
// object, this will fall through to Default* settings.
203212
ByObject map[client.Object]ByObject
204-
205-
// newInformer allows overriding of NewSharedIndexInformer for testing.
206-
newInformer *func(toolscache.ListerWatcher, runtime.Object, time.Duration, toolscache.Indexers) toolscache.SharedIndexInformer
207213
}
208214

209215
// ByObject offers more fine-grained control over the cache's ListWatch by object.
@@ -354,7 +360,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
354360
},
355361
Transform: config.Transform,
356362
UnsafeDisableDeepCopy: pointer.BoolDeref(config.UnsafeDisableDeepCopy, false),
357-
NewInformer: opts.newInformer,
363+
NewInformer: opts.NewInformerFunc,
358364
}),
359365
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
360366
}
@@ -438,6 +444,10 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
438444
if opts.SyncPeriod == nil {
439445
opts.SyncPeriod = &defaultSyncPeriod
440446
}
447+
448+
if opts.NewInformerFunc == nil {
449+
opts.NewInformerFunc = informers.NewSharedIndexInformer
450+
}
441451
return opts, nil
442452
}
443453

pkg/cache/cache_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"time"
2828

29+
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
2930
. "github.com/onsi/ginkgo/v2"
3031
. "github.com/onsi/gomega"
3132
corev1 "k8s.io/api/core/v1"
@@ -545,13 +546,13 @@ func NonBlockingGetTest(createCacheFunc func(config *rest.Config, opts cache.Opt
545546

546547
By("creating the informer cache")
547548
v := reflect.ValueOf(&opts).Elem()
548-
newInformerField := v.FieldByName("newInformer")
549-
newFakeInformer := func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcache.SharedIndexInformer {
549+
newInformerField := v.FieldByName("NewInformerFunc")
550+
newFakeInformer := func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcpcache.ScopeableSharedIndexInformer {
550551
return &controllertest.FakeInformer{Synced: false}
551552
}
552553
reflect.NewAt(newInformerField.Type(), newInformerField.Addr().UnsafePointer()).
553554
Elem().
554-
Set(reflect.ValueOf(&newFakeInformer))
555+
Set(reflect.ValueOf(newFakeInformer))
555556
informerCache, err = createCacheFunc(cfg, opts)
556557
Expect(err).NotTo(HaveOccurred())
557558
By("running the cache and waiting for it to sync")

pkg/cache/informer_cache.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/cache/internal"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
34+
35+
"github.com/kcp-dev/logicalcluster/v3"
3436
)
3537

3638
var (
@@ -222,6 +224,13 @@ func indexByField(informer Informer, field string, extractValue client.IndexerFu
222224
}
223225
ns := meta.GetNamespace()
224226

227+
keyFunc := internal.KeyToNamespacedKey
228+
if clusterName := logicalcluster.From(obj); !clusterName.Empty() {
229+
keyFunc = func(ns, val string) string {
230+
return internal.KeyToClusteredKey(clusterName.String(), ns, val)
231+
}
232+
}
233+
225234
rawVals := extractValue(obj)
226235
var vals []string
227236
if ns == "" {
@@ -231,14 +240,15 @@ func indexByField(informer Informer, field string, extractValue client.IndexerFu
231240
// if we need to add non-namespaced versions too, double the length
232241
vals = make([]string, len(rawVals)*2)
233242
}
243+
234244
for i, rawVal := range rawVals {
235245
// save a namespaced variant, so that we can ask
236246
// "what are all the object matching a given index *in a given namespace*"
237-
vals[i] = internal.KeyToNamespacedKey(ns, rawVal)
247+
vals[i] = keyFunc(ns, rawVal)
238248
if ns != "" {
239249
// if we have a namespace, also inject a special index key for listing
240250
// regardless of the object namespace
241-
vals[i+len(rawVals)] = internal.KeyToNamespacedKey("", rawVal)
251+
vals[i+len(rawVals)] = keyFunc("", rawVal)
242252
}
243253
}
244254

0 commit comments

Comments
 (0)