Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions pkg/operator/v1helpers/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/informers"
Expand Down Expand Up @@ -33,20 +34,37 @@ type KubeInformersForNamespaces interface {
var _ KubeInformersForNamespaces = kubeInformersForNamespaces{}

func NewKubeInformersForNamespacesWithResyncPeriod(kubeClient kubernetes.Interface, resyncInterval time.Duration, namespaces ...string) KubeInformersForNamespaces {
return NewKubeInformersForNamespacesWithOptions(kubeClient, resyncInterval, namespaces)
}

func NewKubeInformersForNamespaces(kubeClient kubernetes.Interface, namespaces ...string) KubeInformersForNamespaces {
return NewKubeInformersForNamespacesWithOptions(kubeClient, 10*time.Minute, namespaces)
}

func NewKubeInformersForNamespacesWithOptions(kubeClient kubernetes.Interface, resyncInterval time.Duration, namespaces []string, opts ...informers.SharedInformerOption) KubeInformersForNamespaces {
ret := kubeInformersForNamespaces{}
for _, namespace := range namespaces {
if len(namespace) == 0 {
ret[""] = informers.NewSharedInformerFactory(kubeClient, resyncInterval)
continue
factoryOpts := make([]informers.SharedInformerOption, 0, len(opts)+1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure pre-allocating the slice is worth the hustle. This is really only called once usually on operator startup, so we may just be allocating an empty extra slot for no performance benefit really, but also having an extra pointer allocated is not something anybody cares about, I guess.

factoryOpts = append(factoryOpts, opts...)
if len(namespace) != 0 {
factoryOpts = append(factoryOpts, informers.WithNamespace(namespace))
}
ret[namespace] = informers.NewSharedInformerFactoryWithOptions(kubeClient, resyncInterval, informers.WithNamespace(namespace))
ret[namespace] = informers.NewSharedInformerFactoryWithOptions(kubeClient, resyncInterval, factoryOpts...)
}

return ret
}

func NewKubeInformersForNamespaces(kubeClient kubernetes.Interface, namespaces ...string) KubeInformersForNamespaces {
return NewKubeInformersForNamespacesWithResyncPeriod(kubeClient, 10*time.Minute, namespaces...)
// StripManagedFieldsTransform is a cache.TransformFunc that removes
// ManagedFields from objects before they are stored in the informer cache.
func StripManagedFieldsTransform(obj interface{}) (interface{}, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we pls use any instead of interface{} ?

accessor, err := meta.Accessor(obj)
if err != nil {
return obj, nil
}
if accessor.GetManagedFields() != nil {
accessor.SetManagedFields(nil)
}
return obj, nil
}

type kubeInformersForNamespaces map[string]informers.SharedInformerFactory
Expand Down
104 changes: 104 additions & 0 deletions pkg/operator/v1helpers/informers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package v1helpers

import (
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakekube "k8s.io/client-go/kubernetes/fake"
)

func TestNewKubeInformersForNamespacesWithOptions(t *testing.T) {
kubeClient := fakekube.NewSimpleClientset()

testCases := []struct {
name string
namespaces []string
}{
{
name: "single namespace",
namespaces: []string{"openshift-config"},
},
{
name: "multiple namespaces",
namespaces: []string{"openshift-config", "openshift-config-managed"},
},
{
name: "cluster-wide",
namespaces: []string{""},
},
{
name: "cluster-wide and namespaced",
namespaces: []string{"", "openshift-config"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ki := NewKubeInformersForNamespacesWithOptions(kubeClient, 10*time.Minute, tc.namespaces)

for _, ns := range tc.namespaces {
if ki.InformersFor(ns) == nil {
t.Errorf("expected informers for namespace %q", ns)
}
}
if ki.InformersFor("not-registered") != nil {
t.Error("expected no informers for unregistered namespace")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more robust to just call ki.Namespaces() and check that? This does not really check that there are no extra namespaces registered...

})
}
}

func TestStripManagedFieldsTransform(t *testing.T) {
testCases := []struct {
name string
obj interface{}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls use any if possible, thanks.

expectManagedNil bool
expectPassThrough bool
}{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that AI loves to write weird table tests, but could we just use input and expected output objects? That is way more clear to read and reason about instead of using multiple flags.

{
name: "strips managed fields from object that has them",
obj: &metav1.ObjectMeta{
Name: "test",
Namespace: "default",
ManagedFields: []metav1.ManagedFieldsEntry{
{Manager: "kubectl", Operation: metav1.ManagedFieldsOperationApply},
},
},
expectManagedNil: true,
},
{
name: "passes through object without managed fields",
obj: &metav1.ObjectMeta{
Name: "test",
Namespace: "default",
},
expectManagedNil: true,
},
{
name: "passes through non-Object types without error",
obj: "not-an-object",
expectPassThrough: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := StripManagedFieldsTransform(tc.obj)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tc.expectPassThrough {
if result != tc.obj {
t.Fatal("expected pass-through for non-Object type")
}
return
}
accessor, ok := result.(metav1.Object)
if !ok {
t.Fatal("expected result to implement metav1.Object")
}
if tc.expectManagedNil && accessor.GetManagedFields() != nil {
t.Fatal("expected ManagedFields to be nil")
}
})
}
}