Skip to content

Commit 2b11491

Browse files
committed
Add FieldOwner field to client.Options
1 parent cecf5d2 commit 2b11491

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pkg/client/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type Options struct {
5252

5353
// DryRun instructs the client to only perform dry run requests.
5454
DryRun *bool
55+
56+
// FieldOwner, if provided, instructs the client to be wrapped with WithFieldOwner function
57+
FieldOwner string
5558
}
5659

5760
// CacheOptions are options for creating a cache-backed client.
@@ -99,6 +102,10 @@ func New(config *rest.Config, options Options) (c Client, err error) {
99102
if err == nil && options.DryRun != nil && *options.DryRun {
100103
c = NewDryRunClient(c)
101104
}
105+
if fo := options.FieldOwner; fo != "" {
106+
c = WithFieldOwner(c, fo)
107+
}
108+
102109
return c, err
103110
}
104111

pkg/client/client_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,19 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
365365
Expect(cl.List(ctx, &corev1.NamespaceList{})).To(Succeed())
366366
Expect(cache.Called).To(Equal(0))
367367
})
368+
369+
It("should use the provided FieldOwner if provided", func(ctx SpecContext) {
370+
cl, err := client.New(cfg, client.Options{FieldOwner: "test-owner"})
371+
Expect(err).NotTo(HaveOccurred())
372+
Expect(cl).NotTo(BeNil())
373+
// no explicit FieldOwner option set on Apply method call
374+
err = cl.Apply(ctx, corev1applyconfigurations.ConfigMap("test-cm", "default").WithData(map[string]string{"foo": "bar"}))
375+
Expect(err).NotTo(HaveOccurred())
376+
actual, err := clientset.CoreV1().ConfigMaps("default").Get(ctx, "test-cm", metav1.GetOptions{})
377+
Expect(err).NotTo(HaveOccurred())
378+
Expect(actual.ManagedFields).To(HaveLen(1))
379+
Expect(actual.ManagedFields[0].Manager).To(Equal("test-owner"))
380+
})
368381
})
369382

370383
Describe("Create", func() {

0 commit comments

Comments
 (0)