@@ -53,6 +53,7 @@ import (
53
53
"k8s.io/apimachinery/pkg/watch"
54
54
"k8s.io/client-go/kubernetes/scheme"
55
55
"k8s.io/client-go/testing"
56
+ "k8s.io/utils/clock"
56
57
"k8s.io/utils/ptr"
57
58
58
59
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -77,6 +78,7 @@ type fakeClient struct {
77
78
78
79
schemeWriteLock sync.Mutex
79
80
scheme * runtime.Scheme
81
+ clock clock.Clock
80
82
81
83
restMapper meta.RESTMapper
82
84
withStatusSubresource sets.Set [schema.GroupVersionKind ]
@@ -110,6 +112,7 @@ func NewClientBuilder() *ClientBuilder {
110
112
// ClientBuilder builds a fake client.
111
113
type ClientBuilder struct {
112
114
scheme * runtime.Scheme
115
+ clock clock.Clock
113
116
restMapper meta.RESTMapper
114
117
initObject []client.Object
115
118
initLists []client.ObjectList
@@ -130,6 +133,13 @@ func (f *ClientBuilder) WithScheme(scheme *runtime.Scheme) *ClientBuilder {
130
133
return f
131
134
}
132
135
136
+ // WithClock sets this builder's internal clock.
137
+ // If not set, defaults to clock.RealClock.
138
+ func (f * ClientBuilder ) WithClock (clock clock.Clock ) * ClientBuilder {
139
+ f .clock = clock
140
+ return f
141
+ }
142
+
133
143
// WithRESTMapper sets this builder's restMapper.
134
144
// The restMapper is directly set as mapper in the Client. This can be used for example
135
145
// with a meta.DefaultRESTMapper to provide a static rest mapping.
@@ -222,6 +232,9 @@ func (f *ClientBuilder) Build() client.WithWatch {
222
232
if f .restMapper == nil {
223
233
f .restMapper = meta .NewDefaultRESTMapper ([]schema.GroupVersion {})
224
234
}
235
+ if f .clock == nil {
236
+ f .clock = clock.RealClock {}
237
+ }
225
238
226
239
var tracker versionedTracker
227
240
@@ -259,6 +272,7 @@ func (f *ClientBuilder) Build() client.WithWatch {
259
272
var result client.WithWatch = & fakeClient {
260
273
tracker : tracker ,
261
274
scheme : f .scheme ,
275
+ clock : f .clock ,
262
276
restMapper : f .restMapper ,
263
277
indexes : f .indexes ,
264
278
withStatusSubresource : withStatusSubResource ,
@@ -1190,8 +1204,8 @@ func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object,
1190
1204
return apierrors .NewNotFound (schema.GroupResource {}, "" )
1191
1205
}
1192
1206
1193
- tokenRequest .Status .Token = "fake-token"
1194
- tokenRequest .Status .ExpirationTimestamp = metav1 .Date ( 6041 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC )
1207
+ tokenRequest .Status .Token = fmt . Sprintf ( "fake-token-%d" , ( ptr . Deref ( tokenRequest . Spec . ExpirationSeconds , 3600 )))
1208
+ tokenRequest .Status .ExpirationTimestamp = metav1.Time { Time : sw . client . clock . Now (). Add ( time . Duration ( ptr . Deref ( tokenRequest . Spec . ExpirationSeconds , 3600 )) * time .Second )}
1195
1209
1196
1210
return sw .client .Get (ctx , client .ObjectKeyFromObject (obj ), obj )
1197
1211
default :
0 commit comments