Skip to content

Commit 3893e04

Browse files
authored
✨ Add FieldOwner field to client.Options (#3389)
* Add FieldOwner field to client.Options * Update client.Options.FieldOwner documentation
1 parent d81cc2d commit 3893e04

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pkg/client/client.go

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

5353
// DryRun instructs the client to only perform dry run requests.
5454
DryRun *bool
55+
56+
// FieldOwner, if provided, sets the default field manager for all write operations
57+
// (Create, Update, Patch, Apply) performed by this client. The field manager is used by
58+
// the server for Server-Side Apply to track field ownership.
59+
// For more details, see: https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management
60+
//
61+
// This default can be overridden for a specific call by passing a [FieldOwner] option
62+
// to the method.
63+
FieldOwner string
5564
}
5665

5766
// CacheOptions are options for creating a cache-backed client.
@@ -99,6 +108,10 @@ func New(config *rest.Config, options Options) (c Client, err error) {
99108
if err == nil && options.DryRun != nil && *options.DryRun {
100109
c = NewDryRunClient(c)
101110
}
111+
if fo := options.FieldOwner; fo != "" {
112+
c = WithFieldOwner(c, fo)
113+
}
114+
102115
return c, err
103116
}
104117

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)