-
Notifications
You must be signed in to change notification settings - Fork 273
NO-JIRA: Add WithOptions constructor and StripManagedFieldsTransform for KubeInformersForNamespaces #2379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
NO-JIRA: Add WithOptions constructor and StripManagedFieldsTransform for KubeInformersForNamespaces #2379
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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) | ||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we pls use |
||
| 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 | ||
|
|
||
| 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") | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be more robust to just call |
||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestStripManagedFieldsTransform(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| obj interface{} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls use |
||
| expectManagedNil bool | ||
| expectPassThrough bool | ||
| }{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.