Skip to content

Commit 712711b

Browse files
committed
fix(controller-runtime): Adjust RESTClientForGVK function and cache.
The required fixes are the following: * The function RESTClientForGVK has introduced the `forceDisableProtoBuf` field. The code base has been adjusted to set this field as `false` where is used. * Fake client now strips the TypeMeta fields kubernetes-sigs/controller-runtime#3229. Therefore, it is required to adjust how the cache gets the `Kind` field, otherwise, the field is empty and objects are not cached. Signed-off-by: Javier Cano Cano <[email protected]>
1 parent aa87c4e commit 712711b

File tree

4 files changed

+7
-11
lines changed

4 files changed

+7
-11
lines changed

internal/common/cache.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"k8s.io/apimachinery/pkg/types"
55
"sigs.k8s.io/controller-runtime/pkg/client"
6+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
67
)
78

89
type cacheKey struct {
@@ -35,8 +36,8 @@ func (v VersionCache) Contains(obj client.Object) bool {
3536
}
3637

3738
func (v VersionCache) Add(obj client.Object) {
38-
kind := obj.GetObjectKind().GroupVersionKind().Kind
39-
if kind == "" {
39+
gvk, _ := apiutil.GVKForObject(obj, Scheme)
40+
if gvk.Kind == "" {
4041
// Do not cache objects without kind
4142
return
4243
}
@@ -52,8 +53,9 @@ func (v VersionCache) RemoveObj(obj client.Object) {
5253
}
5354

5455
func cacheKeyFromObj(obj client.Object) cacheKey {
56+
gvk, _ := apiutil.GVKForObject(obj, Scheme)
5557
return cacheKey{
56-
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
58+
Kind: gvk.Kind,
5759
Name: obj.GetName(),
5860
Namespace: obj.GetNamespace(),
5961
}

internal/common/resource_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package common
22

33
import (
44
"context"
5-
65
. "github.com/onsi/ginkgo/v2"
76
. "github.com/onsi/gomega"
8-
97
libhandler "github.com/operator-framework/operator-lib/handler"
108
v1 "k8s.io/api/core/v1"
119
"k8s.io/apimachinery/pkg/api/errors"
@@ -304,10 +302,6 @@ func createOrUpdateTestResource(request *Request) (ReconcileResult, error) {
304302

305303
func newTestResource(namespace string) *v1.Service {
306304
return &v1.Service{
307-
TypeMeta: metav1.TypeMeta{
308-
Kind: "Service",
309-
APIVersion: "v1",
310-
},
311305
ObjectMeta: metav1.ObjectMeta{
312306
Name: "testservice",
313307
Namespace: namespace,

internal/template-validator/virtinformers/informers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func restClientForObject(obj runtime.Object, restConfig *rest.Config, scheme *ru
139139
return nil, err
140140
}
141141

142-
restClient, err := apiutil.RESTClientForGVK(gvk, false, restConfig, serializer.NewCodecFactory(scheme), httpClient)
142+
restClient, err := apiutil.RESTClientForGVK(gvk, false, false, restConfig, serializer.NewCodecFactory(scheme), httpClient)
143143
if err != nil {
144144
logger.Log.Error(err, "error creating client")
145145
return nil, err

tests/tests_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func createSspListerWatcher(cfg *rest.Config) cache.ListerWatcherWithContext {
464464
httpClient, err := rest.HTTPClientFor(cfg)
465465
Expect(err).ToNot(HaveOccurred())
466466

467-
restClient, err := apiutil.RESTClientForGVK(sspGvk, false, cfg, serializer.NewCodecFactory(testScheme), httpClient)
467+
restClient, err := apiutil.RESTClientForGVK(sspGvk, false, false, cfg, serializer.NewCodecFactory(testScheme), httpClient)
468468
Expect(err).ToNot(HaveOccurred())
469469

470470
return cache.NewListWatchFromClient(restClient, "ssps", strategy.GetNamespace(), fields.Everything())

0 commit comments

Comments
 (0)