Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Add Server-Side DryRun support to Client #338

Merged
merged 6 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 6 additions & 6 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,21 @@ type client struct {
}

// Create implements client.Client
func (c *client) Create(ctx context.Context, obj runtime.Object) error {
func (c *client) Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Create(ctx, obj)
return c.unstructuredClient.Create(ctx, obj, opts...)
}
return c.typedClient.Create(ctx, obj)
return c.typedClient.Create(ctx, obj, opts...)
}

// Update implements client.Client
func (c *client) Update(ctx context.Context, obj runtime.Object) error {
func (c *client) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Update(ctx, obj)
return c.unstructuredClient.Update(ctx, obj, opts...)
}
return c.typedClient.Update(ctx, obj)
return c.typedClient.Update(ctx, obj, opts...)
}

// Delete implements client.Client
Expand Down
78 changes: 78 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ var _ = Describe("Client", func() {
// TODO(seans3): implement these
// Example: ListOptions
})

Context("with the DryRun option", func() {
It("should not create a new object", func(done Done) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("creating the object (with DryRun)")
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll())
Expect(err).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(actual).To(Equal(&appsv1.Deployment{}))

close(done)
})
})
})

Context("with unstructured objects", func() {
Expand Down Expand Up @@ -367,6 +386,33 @@ var _ = Describe("Client", func() {

})

Context("with the DryRun option", func() {
It("should not create a new object from a go struct", func(done Done) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("encoding the deployment as unstructured")
u := &unstructured.Unstructured{}
scheme.Convert(dep, u, nil)
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "apps",
Kind: "Deployment",
Version: "v1",
})

By("creating the object")
err = cl.Create(context.TODO(), u, client.CreateDryRunAll())
Expect(err).NotTo(HaveOccurred())

actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(actual).To(Equal(&appsv1.Deployment{}))

close(done)
})
})
})

Describe("Update", func() {
Expand Down Expand Up @@ -1722,6 +1768,22 @@ var _ = Describe("Client", func() {
})
})

Describe("CreateOptions", func() {
It("should allow setting DryRun to 'all'", func() {
co := &client.CreateOptions{}
client.CreateDryRunAll()(co)
all := []string{metav1.DryRunAll}
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
})

It("should produce empty metav1.CreateOptions if nil", func() {
var co *client.CreateOptions
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
co = &client.CreateOptions{}
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
})
})

Describe("DeleteOptions", func() {
It("should allow setting GracePeriodSeconds", func() {
do := &client.DeleteOptions{}
Expand Down Expand Up @@ -1846,6 +1908,22 @@ var _ = Describe("Client", func() {
Expect(lo.Namespace).To(Equal("test"))
})
})

Describe("UpdateOptions", func() {
It("should allow setting DryRun to 'all'", func() {
uo := &client.UpdateOptions{}
client.UpdateDryRunAll()(uo)
all := []string{metav1.DryRunAll}
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
})

It("should produce empty metav1.UpdateOptions if nil", func() {
var co *client.UpdateOptions
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))
co = &client.UpdateOptions{}
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))
})
})
})

var _ = Describe("DelegatingReader", func() {
Expand Down
106 changes: 104 additions & 2 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ type Reader interface {
// Writer knows how to create, delete, and update Kubernetes objects.
type Writer interface {
// Create saves the object obj in the Kubernetes cluster.
Create(ctx context.Context, obj runtime.Object) error
Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error

// Delete deletes the given obj from Kubernetes cluster.
Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOptionFunc) error

// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Update(ctx context.Context, obj runtime.Object) error
Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error
}

// StatusClient knows how to create a client which can update status subresource
Expand Down Expand Up @@ -106,6 +106,57 @@ type FieldIndexer interface {
IndexField(obj runtime.Object, field string, extractValue IndexerFunc) error
}

// CreateOptions contains options for create requests. It's generally a subset
// of metav1.CreateOptions.
type CreateOptions struct {
// When present, indicates that modifications should not be
// persisted. An invalid or unrecognized dryRun directive will
// result in an error response and no further processing of the
// request. Valid values are:
// - All: all dry run stages will be processed
DryRun []string

// Raw represents raw CreateOptions, as passed to the API server.
Raw *metav1.CreateOptions
}

// AsCreateOptions returns these options as a metav1.CreateOptions.
// This may mutate the Raw field.
func (o *CreateOptions) AsCreateOptions() *metav1.CreateOptions {

if o == nil {
return &metav1.CreateOptions{}
}
if o.Raw == nil {
o.Raw = &metav1.CreateOptions{}
}

o.Raw.DryRun = o.DryRun
return o.Raw
}

// ApplyOptions executes the given CreateOptionFuncs and returns the mutated
// CreateOptions.
func (o *CreateOptions) ApplyOptions(optFuncs []CreateOptionFunc) *CreateOptions {
for _, optFunc := range optFuncs {
optFunc(o)
}
return o
}

// CreateOptionFunc is a function that mutates a CreateOptions struct. It implements
// the functional options pattern. See
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
type CreateOptionFunc func(*CreateOptions)

// CreateDryRunAll is a functional option that sets the DryRun
// field of a CreateOptions struct to metav1.DryRunAll.
func CreateDryRunAll() CreateOptionFunc {
return func(opts *CreateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
}

// DeleteOptions contains options for delete requests. It's generally a subset
// of metav1.DeleteOptions.
type DeleteOptions struct {
Expand Down Expand Up @@ -326,3 +377,54 @@ func UseListOptions(newOpts *ListOptions) ListOptionFunc {
*opts = *newOpts
}
}

// UpdateOptions contains options for create requests. It's generally a subset
// of metav1.UpdateOptions.
type UpdateOptions struct {
// When present, indicates that modifications should not be
// persisted. An invalid or unrecognized dryRun directive will
// result in an error response and no further processing of the
// request. Valid values are:
// - All: all dry run stages will be processed
DryRun []string

// Raw represents raw UpdateOptions, as passed to the API server.
Raw *metav1.UpdateOptions
}

// AsUpdateOptions returns these options as a metav1.UpdateOptions.
// This may mutate the Raw field.
func (o *UpdateOptions) AsUpdateOptions() *metav1.UpdateOptions {

if o == nil {
return &metav1.UpdateOptions{}
}
if o.Raw == nil {
o.Raw = &metav1.UpdateOptions{}
}

o.Raw.DryRun = o.DryRun
return o.Raw
}

// ApplyOptions executes the given UpdateOptionFuncs and returns the mutated
// UpdateOptions.
func (o *UpdateOptions) ApplyOptions(optFuncs []UpdateOptionFunc) *UpdateOptions {
for _, optFunc := range optFuncs {
optFunc(o)
}
return o
}

// UpdateOptionFunc is a function that mutates a UpdateOptions struct. It implements
// the functional options pattern. See
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
type UpdateOptionFunc func(*UpdateOptions)

// UpdateDryRunAll is a functional option that sets the DryRun
// field of a UpdateOptions struct to metav1.DryRunAll.
func UpdateDryRunAll() UpdateOptionFunc {
return func(opts *UpdateOptions) {
opts.DryRun = []string{metav1.DryRunAll}
}
}
12 changes: 10 additions & 2 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,39 @@ type typedClient struct {
}

// Create implements client.Client
func (c *typedClient) Create(ctx context.Context, obj runtime.Object) error {
func (c *typedClient) Create(ctx context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
o, err := c.cache.getObjMeta(obj)
if err != nil {
return err
}

createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)
return o.Post().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Body(obj).
VersionedParams(createOpts.AsCreateOptions(), c.paramCodec).
Context(ctx).
Do().
Into(obj)
}

// Update implements client.Client
func (c *typedClient) Update(ctx context.Context, obj runtime.Object) error {
func (c *typedClient) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
o, err := c.cache.getObjMeta(obj)
if err != nil {
return err
}

updateOpts := &UpdateOptions{}
updateOpts.ApplyOptions(opts)
return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Body(obj).
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
Context(ctx).
Do().
Into(obj)
Expand Down
12 changes: 8 additions & 4 deletions pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ type unstructuredClient struct {
}

// Create implements client.Client
func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object) error {
func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object, opts ...CreateOptionFunc) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
createOpts := CreateOptions{}
createOpts.ApplyOptions(opts)
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
if err != nil {
return err
}
i, err := r.Create(u, metav1.CreateOptions{})
i, err := r.Create(u, *createOpts.AsCreateOptions())
if err != nil {
return err
}
Expand All @@ -55,16 +57,18 @@ func (uc *unstructuredClient) Create(_ context.Context, obj runtime.Object) erro
}

// Update implements client.Client
func (uc *unstructuredClient) Update(_ context.Context, obj runtime.Object) error {
func (uc *unstructuredClient) Update(_ context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
updateOpts := UpdateOptions{}
updateOpts.ApplyOptions(opts)
r, err := uc.getResourceInterface(u.GroupVersionKind(), u.GetNamespace())
if err != nil {
return err
}
i, err := r.Update(u, metav1.UpdateOptions{})
i, err := r.Update(u, *updateOpts.AsUpdateOptions())
if err != nil {
return err
}
Expand Down