@@ -59,24 +59,82 @@ const (
5959
6060// NewFakeClient creates a new fake client for testing.
6161// You can choose to initialize it with a slice of runtime.Object.
62+ //
63+ // Deprecated: Please use NewClientBuilder instead.
6264func NewFakeClient (initObjs ... runtime.Object ) client.Client {
63- return NewFakeClientWithScheme ( scheme . Scheme , initObjs ... )
65+ return NewClientBuilder (). WithRuntimeObjects ( initObjs ... ). Build ( )
6466}
6567
6668// NewFakeClientWithScheme creates a new fake client with the given scheme
6769// for testing.
6870// You can choose to initialize it with a slice of runtime.Object.
71+ //
72+ // Deprecated: Please use NewClientBuilder instead.
6973func NewFakeClientWithScheme (clientScheme * runtime.Scheme , initObjs ... runtime.Object ) client.Client {
70- tracker := testing .NewObjectTracker (clientScheme , scheme .Codecs .UniversalDecoder ())
71- for _ , obj := range initObjs {
72- err := tracker .Add (obj )
73- if err != nil {
74+ return NewClientBuilder ().WithScheme (clientScheme ).WithRuntimeObjects (initObjs ... ).Build ()
75+ }
76+
77+ // NewClientBuilder returns a new builder to create a fake client.
78+ func NewClientBuilder () * ClientBuilder {
79+ return & ClientBuilder {
80+ scheme : scheme .Scheme ,
81+ }
82+ }
83+
84+ // ClientBuilder builds a fake client.
85+ type ClientBuilder struct {
86+ scheme * runtime.Scheme
87+ initObject []client.Object
88+ initLists []client.ObjectList
89+ initRuntimeObjects []runtime.Object
90+ }
91+
92+ // WithScheme sets this builder's internal scheme.
93+ // If not set, defaults to client-go's global scheme.Scheme.
94+ func (f * ClientBuilder ) WithScheme (scheme * runtime.Scheme ) * ClientBuilder {
95+ f .scheme = scheme
96+ return f
97+ }
98+
99+ // WithObjects can be optionally used to initialize this fake client with client.Object(s).
100+ func (f * ClientBuilder ) WithObjects (initObjs ... client.Object ) * ClientBuilder {
101+ f .initObject = append (f .initObject , initObjs ... )
102+ return f
103+ }
104+
105+ // WithObjects can be optionally used to initialize this fake client with client.ObjectList(s).
106+ func (f * ClientBuilder ) WithLists (initLists ... client.ObjectList ) * ClientBuilder {
107+ f .initLists = append (f .initLists , initLists ... )
108+ return f
109+ }
110+
111+ // WithObjects can be optionally used to initialize this fake client with runtime.Object(s).
112+ func (f * ClientBuilder ) WithRuntimeObjects (initRuntimeObjs ... runtime.Object ) * ClientBuilder {
113+ f .initRuntimeObjects = append (f .initRuntimeObjects , initRuntimeObjs ... )
114+ return f
115+ }
116+
117+ // Build builds and returns a new fake client.
118+ func (f * ClientBuilder ) Build () client.Client {
119+ tracker := testing .NewObjectTracker (f .scheme , scheme .Codecs .UniversalDecoder ())
120+ for _ , obj := range f .initObject {
121+ if err := tracker .Add (obj ); err != nil {
74122 panic (fmt .Errorf ("failed to add object %v to fake client: %w" , obj , err ))
75123 }
76124 }
125+ for _ , obj := range f .initLists {
126+ if err := tracker .Add (obj ); err != nil {
127+ panic (fmt .Errorf ("failed to add list %v to fake client: %w" , obj , err ))
128+ }
129+ }
130+ for _ , obj := range f .initRuntimeObjects {
131+ if err := tracker .Add (obj ); err != nil {
132+ panic (fmt .Errorf ("failed to add runtime object %v to fake client: %w" , obj , err ))
133+ }
134+ }
77135 return & fakeClient {
78- tracker : versionedTracker {ObjectTracker : tracker , scheme : clientScheme },
79- scheme : clientScheme ,
136+ tracker : versionedTracker {ObjectTracker : tracker , scheme : f . scheme },
137+ scheme : f . scheme ,
80138 }
81139}
82140
0 commit comments