Skip to content

Commit 04c6a08

Browse files
[release-0.22] 🐛Panic when trying to build more than one instance of fake.ClientBuilder (#3315)
* panic when trying to build more than one instance of fake.ClientBuilder Signed-off-by: Troy Connor <troy0820@users.noreply.github.com> * pr feedback Signed-off-by: Troy Connor <troy0820@users.noreply.github.com> --------- Signed-off-by: Troy Connor <troy0820@users.noreply.github.com> Co-authored-by: Troy Connor <troy0820@users.noreply.github.com>
1 parent 6422ed0 commit 04c6a08

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
@@ -3159,4 +3159,13 @@ var _ = Describe("Fake client builder", func() {
31593159
Expect(err).NotTo(HaveOccurred())
31603160
Expect(called).To(BeTrue())
31613161
})
3162+
3163+
It("should panic when calling build more than once", func() {
3164+
cb := NewClientBuilder()
3165+
anotherCb := cb
3166+
cb.Build()
3167+
Expect(func() {
3168+
anotherCb.Build()
3169+
}).To(Panic())
3170+
})
31623171
})

0 commit comments

Comments
 (0)