@@ -27,6 +27,9 @@ type GetFn func(ctx context.Context, key client.ObjectKey, obj client.Object, op
27
27
// ListFn is the client.Client.List function signature
28
28
type ListFn func (ctx context.Context , list client.ObjectList , opts ... client.ListOption ) error
29
29
30
+ // CreateFn is the client.Client.Create function signature
31
+ type CreateFn func (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error
32
+
30
33
// UpdateFn is the client.Client.Update function signature
31
34
type UpdateFn func (ctx context.Context , obj client.Object , opts ... client.UpdateOption ) error
32
35
@@ -48,6 +51,7 @@ type FakeClient struct {
48
51
client.Client
49
52
getFunctions map [reflect.Type ]map [string ]GetFn
50
53
listFunctions map [reflect.Type ]ListFn
54
+ createFunctions map [reflect.Type ]map [string ]CreateFn
51
55
updateFunctions map [reflect.Type ]map [string ]UpdateFn
52
56
statusUpdateFunctions map [reflect.Type ]map [string ]UpdateSubResourceFn
53
57
deleteFn map [reflect.Type ]map [string ]DeleteFn
@@ -112,6 +116,31 @@ func (f *FakeClient) List(ctx context.Context, list client.ObjectList, opts ...c
112
116
return f .Client .List (ctx , list , opts ... )
113
117
}
114
118
119
+ // WithCreateFunction registers a custom Create function for the given object.
120
+ func (f * FakeClient ) WithCreateFunction (container GenericObjectContainer , fn CreateFn ) * FakeClient {
121
+ if f .createFunctions == nil {
122
+ f .createFunctions = make (map [reflect.Type ]map [string ]CreateFn )
123
+ }
124
+ if f .createFunctions [reflect .TypeOf (container .ClientObject ())] == nil {
125
+ f .createFunctions [reflect .TypeOf (container .ClientObject ())] = make (map [string ]CreateFn )
126
+ }
127
+ f .createFunctions [reflect .TypeOf (container .ClientObject ())][container .ObjectKey ().String ()] = fn
128
+ return f
129
+ }
130
+
131
+ // Create calls the custom Create function if it was registered for the given object.
132
+ // Otherwise, it calls the original client.Client.Create function.
133
+ func (f * FakeClient ) Create (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error {
134
+ if f .createFunctions != nil {
135
+ if functions , ok := f .createFunctions [reflect .TypeOf (obj )]; ok {
136
+ if fn , ok := functions [client .ObjectKeyFromObject (obj ).String ()]; ok {
137
+ return fn (ctx , obj , opts ... )
138
+ }
139
+ }
140
+ }
141
+ return f .Client .Create (ctx , obj , opts ... )
142
+ }
143
+
115
144
// WithUpdateFunction registers a custom Update function for the given object.
116
145
func (f * FakeClient ) WithUpdateFunction (container GenericObjectContainer , fn UpdateFn ) * FakeClient {
117
146
if f .updateFunctions == nil {
0 commit comments