Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ type Options struct {

// DryRun instructs the client to only perform dry run requests.
DryRun *bool

// FieldOwner, if provided, sets the default field manager for all write operations
// (Create, Update, Patch, Apply) performed by this client. The field manager is used by
// the server for Server-Side Apply to track field ownership.
// For more details, see: https://kubernetes.io/docs/reference/using-api/server-side-apply/#field-management
//
// This default can be overridden for a specific call by passing a [FieldOwner] option
// to the method.
FieldOwner string
}

// CacheOptions are options for creating a cache-backed client.
Expand Down Expand Up @@ -99,6 +108,10 @@ func New(config *rest.Config, options Options) (c Client, err error) {
if err == nil && options.DryRun != nil && *options.DryRun {
c = NewDryRunClient(c)
}
if fo := options.FieldOwner; fo != "" {
c = WithFieldOwner(c, fo)
}

return c, err
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,19 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
Expect(cl.List(ctx, &corev1.NamespaceList{})).To(Succeed())
Expect(cache.Called).To(Equal(0))
})

It("should use the provided FieldOwner if provided", func(ctx SpecContext) {
cl, err := client.New(cfg, client.Options{FieldOwner: "test-owner"})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())
// no explicit FieldOwner option set on Apply method call
err = cl.Apply(ctx, corev1applyconfigurations.ConfigMap("test-cm", "default").WithData(map[string]string{"foo": "bar"}))
Expect(err).NotTo(HaveOccurred())
actual, err := clientset.CoreV1().ConfigMaps("default").Get(ctx, "test-cm", metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(actual.ManagedFields).To(HaveLen(1))
Expect(actual.ManagedFields[0].Manager).To(Equal("test-owner"))
})
})

Describe("Create", func() {
Expand Down
Loading