Skip to content

Commit 5853152

Browse files
authored
🐛Panic when trying to build more than one instance of fake.ClientBuilder (#3314)
* panic when trying to build more than one instance of fake.ClientBuilder Signed-off-by: Troy Connor <[email protected]> * pr feedback Signed-off-by: Troy Connor <[email protected]> --------- Signed-off-by: Troy Connor <[email protected]>
1 parent 4ecd14c commit 5853152

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pkg/client/fake/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ type ClientBuilder struct {
141141
interceptorFuncs *interceptor.Funcs
142142
typeConverters []managedfields.TypeConverter
143143
returnManagedFields bool
144+
isBuilt bool
144145

145146
// indexes maps each GroupVersionKind (GVK) to the indexes registered for that GVK.
146147
// The inner map maps from index name to IndexerFunc.
@@ -267,6 +268,9 @@ func (f *ClientBuilder) WithReturnManagedFields() *ClientBuilder {
267268

268269
// Build builds and returns a new fake client.
269270
func (f *ClientBuilder) Build() client.WithWatch {
271+
if f.isBuilt {
272+
panic("Build() must not be called multiple times when creating a ClientBuilder")
273+
}
270274
if f.scheme == nil {
271275
f.scheme = scheme.Scheme
272276
}
@@ -344,6 +348,7 @@ func (f *ClientBuilder) Build() client.WithWatch {
344348
result = interceptor.NewClient(result, *f.interceptorFuncs)
345349
}
346350

351+
f.isBuilt = true
347352
return result
348353
}
349354

pkg/client/fake/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,4 +3182,13 @@ var _ = Describe("Fake client builder", func() {
31823182
Expect(err).NotTo(HaveOccurred())
31833183
Expect(called).To(BeTrue())
31843184
})
3185+
3186+
It("should panic when calling build more than once", func() {
3187+
cb := NewClientBuilder()
3188+
anotherCb := cb
3189+
cb.Build()
3190+
Expect(func() {
3191+
anotherCb.Build()
3192+
}).To(Panic())
3193+
})
31853194
})

0 commit comments

Comments
 (0)