-
Notifications
You must be signed in to change notification settings - Fork 1.2k
🐛 Avoid creating a new scheme object when combining the options #2237
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
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 | ||
---|---|---|---|---|
|
@@ -38,11 +38,13 @@ import ( | |||
corev1 "k8s.io/api/core/v1" | ||||
"k8s.io/apimachinery/pkg/api/meta" | ||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
"k8s.io/apimachinery/pkg/labels" | ||||
"k8s.io/apimachinery/pkg/runtime" | ||||
"k8s.io/client-go/rest" | ||||
"k8s.io/client-go/tools/leaderelection/resourcelock" | ||||
|
||||
configv1alpha1 "k8s.io/component-base/config/v1alpha1" | ||||
"sigs.k8s.io/controller-runtime/examples/crd/pkg" | ||||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||||
"sigs.k8s.io/controller-runtime/pkg/cache/informertest" | ||||
"sigs.k8s.io/controller-runtime/pkg/client" | ||||
|
@@ -1156,6 +1158,45 @@ var _ = Describe("manger.Manager", func() { | |||
) | ||||
}) | ||||
|
||||
Context("with custom object selector", func() { | ||||
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 don't understand what exactly this is supposed to test and how this relates to the problem you described in the PR body? It also passes without your codechanges btw. It might be easier to unittest 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 am trying to test the scenario mentioned above in the description. The main issue is when a scheme is combined using the BuilderWithOptions function, a new scheme object pointer is created and that pointer is configured into the cache - controller-runtime/pkg/cache/cache.go Line 300 in c3c1f05
The issue is that the original scheme object pointer is configured into the manager/controller and not the newly created one by combineScheme, so this means all the schemes added to I think just unit-testing the |
||||
It("created with BuilderWithOptions and scheme added to manager", func() { | ||||
opts := Options{ | ||||
NewCache: cache.BuilderWithOptions(cache.Options{ | ||||
ByObject: map[client.Object]cache.ByObject{ | ||||
&corev1.Pod{}: { | ||||
Label: labels.SelectorFromSet(labels.Set{ | ||||
"test-label": "true", | ||||
}), | ||||
}, | ||||
}, | ||||
}), | ||||
} | ||||
m, err := New(cfg, opts) | ||||
Expect(err).NotTo(HaveOccurred()) | ||||
|
||||
var wgRunnableStarted sync.WaitGroup | ||||
wgRunnableStarted.Add(1) | ||||
Expect(m.Add(RunnableFunc(func(context.Context) error { | ||||
defer GinkgoRecover() | ||||
wgRunnableStarted.Done() | ||||
return nil | ||||
}))).To(Succeed()) | ||||
|
||||
Expect(pkg.AddToScheme(m.GetScheme())).NotTo(HaveOccurred()) | ||||
|
||||
ctx, cancel := context.WithCancel(context.Background()) | ||||
defer cancel() | ||||
go func() { | ||||
defer GinkgoRecover() | ||||
Expect(m.Elected()).ShouldNot(BeClosed()) | ||||
Expect(m.Start(ctx)).NotTo(HaveOccurred()) | ||||
}() | ||||
|
||||
<-m.Elected() | ||||
wgRunnableStarted.Wait() | ||||
}) | ||||
}) | ||||
|
||||
Context("should start serving metrics", func() { | ||||
var listener net.Listener | ||||
var opts Options | ||||
|
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.
Won't this leak
new
scheme types into the inherited scheme? What happens here if (somehow) a conflicting scheme is required by a different cache, and that cache shares the same inherited scheme? Will we change the scheme out from under the first cache?