Skip to content

Commit fdd9a5c

Browse files
committed
Use fakeClock in subresourceClient
1 parent 4cef7c9 commit fdd9a5c

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

pkg/client/fake/client.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
"k8s.io/apimachinery/pkg/watch"
5454
"k8s.io/client-go/kubernetes/scheme"
5555
"k8s.io/client-go/testing"
56+
"k8s.io/utils/clock"
5657
"k8s.io/utils/ptr"
5758

5859
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -77,6 +78,7 @@ type fakeClient struct {
7778

7879
schemeWriteLock sync.Mutex
7980
scheme *runtime.Scheme
81+
clock clock.Clock
8082

8183
restMapper meta.RESTMapper
8284
withStatusSubresource sets.Set[schema.GroupVersionKind]
@@ -110,6 +112,7 @@ func NewClientBuilder() *ClientBuilder {
110112
// ClientBuilder builds a fake client.
111113
type ClientBuilder struct {
112114
scheme *runtime.Scheme
115+
clock clock.Clock
113116
restMapper meta.RESTMapper
114117
initObject []client.Object
115118
initLists []client.ObjectList
@@ -130,6 +133,13 @@ func (f *ClientBuilder) WithScheme(scheme *runtime.Scheme) *ClientBuilder {
130133
return f
131134
}
132135

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+
133143
// WithRESTMapper sets this builder's restMapper.
134144
// The restMapper is directly set as mapper in the Client. This can be used for example
135145
// with a meta.DefaultRESTMapper to provide a static rest mapping.
@@ -222,6 +232,9 @@ func (f *ClientBuilder) Build() client.WithWatch {
222232
if f.restMapper == nil {
223233
f.restMapper = meta.NewDefaultRESTMapper([]schema.GroupVersion{})
224234
}
235+
if f.clock == nil {
236+
f.clock = clock.RealClock{}
237+
}
225238

226239
var tracker versionedTracker
227240

@@ -259,6 +272,7 @@ func (f *ClientBuilder) Build() client.WithWatch {
259272
var result client.WithWatch = &fakeClient{
260273
tracker: tracker,
261274
scheme: f.scheme,
275+
clock: f.clock,
262276
restMapper: f.restMapper,
263277
indexes: f.indexes,
264278
withStatusSubresource: withStatusSubResource,
@@ -1190,8 +1204,8 @@ func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object,
11901204
return apierrors.NewNotFound(schema.GroupResource{}, "")
11911205
}
11921206

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)}
11951209

11961210
return sw.client.Get(ctx, client.ObjectKeyFromObject(obj), obj)
11971211
default:

pkg/client/fake/client_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"k8s.io/apimachinery/pkg/types"
4646
"k8s.io/apimachinery/pkg/watch"
4747
"k8s.io/client-go/kubernetes/fake"
48+
testclock "k8s.io/utils/clock/testing"
4849
"k8s.io/utils/ptr"
4950

5051
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -1964,14 +1965,38 @@ var _ = Describe("Fake client", func() {
19641965

19651966
It("should create a ServiceAccount token through the token subresource", func() {
19661967
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1967-
cl := NewClientBuilder().WithObjects(sa).Build()
1968+
fakeNow := time.Date(2021, 10, 4, 10, 0, 0, 0, time.UTC)
1969+
fakeClock := testclock.NewFakeClock(fakeNow)
1970+
1971+
cl := NewClientBuilder().WithClock(fakeClock).WithObjects(sa).Build()
1972+
1973+
tokenRequest := &authenticationv1.TokenRequest{
1974+
Spec: authenticationv1.TokenRequestSpec{
1975+
ExpirationSeconds: ptr.To[int64](600),
1976+
},
1977+
}
1978+
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
1979+
Expect(err).NotTo(HaveOccurred())
1980+
1981+
Expect(tokenRequest.Status.Token).To(Equal("fake-token-600"))
1982+
expectedTimeStamp := metav1.NewTime(fakeNow.Add(600 * time.Second))
1983+
Expect(tokenRequest.Status.ExpirationTimestamp).To(Equal(metav1.Time{Time: expectedTimeStamp.Time}))
1984+
})
1985+
1986+
It("should create a ServiceAccount token with default expiration if expirationSeconds is nil through the token subresource", func() {
1987+
sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1988+
fakeNow := time.Date(2021, 10, 4, 10, 0, 0, 0, time.UTC)
1989+
fakeClock := testclock.NewFakeClock(fakeNow)
1990+
1991+
cl := NewClientBuilder().WithClock(fakeClock).WithObjects(sa).Build()
19681992

19691993
tokenRequest := &authenticationv1.TokenRequest{}
19701994
err := cl.SubResource("token").Create(context.Background(), sa, tokenRequest)
19711995
Expect(err).NotTo(HaveOccurred())
19721996

1973-
Expect(tokenRequest.Status.Token).NotTo(Equal(""))
1974-
Expect(tokenRequest.Status.ExpirationTimestamp).NotTo(Equal(metav1.Time{}))
1997+
Expect(tokenRequest.Status.Token).To(Equal("fake-token-3600"))
1998+
expectedTimeStamp := metav1.NewTime(fakeNow.Add(3600 * time.Second))
1999+
Expect(tokenRequest.Status.ExpirationTimestamp).To(Equal(metav1.Time{Time: expectedTimeStamp.Time}))
19752000
})
19762001

19772002
It("should return not found when creating a token for a ServiceAccount that doesn't exist", func() {

0 commit comments

Comments
 (0)