Skip to content

Commit

Permalink
chore: Add context param to client functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Mar 26, 2021
1 parent 0549eeb commit b57b7e3
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 45 deletions.
4 changes: 2 additions & 2 deletions pkg/kn/commands/domain/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewDomainMappingCreateCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
reference, err := refFlags.Resolve(dynamicClient, namespace)
reference, err := refFlags.Resolve(cmd.Context(), dynamicClient, namespace)
if err != nil {
return err
}
Expand All @@ -61,7 +61,7 @@ func NewDomainMappingCreateCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
err = client.CreateDomainMapping(builder.Build())
err = client.CreateDomainMapping(cmd.Context(), builder.Build())
if err != nil {
return knerrors.GetError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/domain/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewDomainMappingDeleteCommand(p *commands.KnParams) *cobra.Command {
return err
}

err = client.DeleteDomainMapping(name)
err = client.DeleteDomainMapping(cmd.Context(), name)
if err != nil {
return knerrors.GetError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/domain/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewDomainMappingDescribeCommand(p *commands.KnParams) *cobra.Command {
return err
}

domainMapping, err := client.GetDomainMapping(args[0])
domainMapping, err := client.GetDomainMapping(cmd.Context(), args[0])
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (f *RefFlags) Add(cmd *cobra.Command) {
"If referring to a Knative service in another namespace, 'ksvc:name:namespace' combination must be provided explicitly."
}

func (f RefFlags) Resolve(knclient clientdynamic.KnDynamicClient, namespace string) (*duckv1.KReference, error) {
func (f RefFlags) Resolve(ctx context.Context, knclient clientdynamic.KnDynamicClient, namespace string) (*duckv1.KReference, error) {
client := knclient.RawClient()
if f.reference == "" {
return nil, nil
Expand All @@ -84,7 +84,7 @@ func (f RefFlags) Resolve(knclient clientdynamic.KnDynamicClient, namespace stri
if refNamespace != "" {
namespace = refNamespace
}
obj, err := client.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
obj, err := client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package domain

import (
"bytes"
"context"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestResolve(t *testing.T) {
dynamicClient := dynamicfake.CreateFakeKnDynamicClient("default", myksvc, mykroute, myksvcInOther, mykrouteInOther)
for _, c := range cases {
i := &RefFlags{c.ref}
result, err := i.Resolve(dynamicClient, "default")
result, err := i.Resolve(context.Background(), dynamicClient, "default")
if c.destination != nil {
assert.DeepEqual(t, result, c.destination)
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/domain/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewDomainMappingListCommand(p *commands.KnParams) *cobra.Command {
return err
}

domainMappingList, err := client.ListDomainMappings()
domainMappingList, err := client.ListDomainMappings(cmd.Context())
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/kn/commands/domain/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewDomainMappingUpdateCommand(p *commands.KnParams) *cobra.Command {
return err
}

toUpdate, err := client.GetDomainMapping(name)
toUpdate, err := client.GetDomainMapping(cmd.Context(), name)
if err != nil {
return err
}
Expand All @@ -62,13 +62,13 @@ func NewDomainMappingUpdateCommand(p *commands.KnParams) *cobra.Command {
return err
}

reference, err := refFlags.Resolve(dynamicClient, namespace)
reference, err := refFlags.Resolve(cmd.Context(), dynamicClient, namespace)
if err != nil {
return err
}
toUpdate.Spec.Ref = *reference

err = client.UpdateDomainMapping(toUpdate)
err = client.UpdateDomainMapping(cmd.Context(), toUpdate)
if err != nil {
return knerrors.GetError(err)
}
Expand Down
30 changes: 15 additions & 15 deletions pkg/serving/v1alpha1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ type KnServingClient interface {
Namespace() string

// GetDomainMapping
GetDomainMapping(name string) (*servingv1alpha1.DomainMapping, error)
GetDomainMapping(ctx context.Context, name string) (*servingv1alpha1.DomainMapping, error)

// CreateDomainMapping
CreateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error
CreateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error

// UpdateDomainMapping
UpdateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error
UpdateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error

// DeleteDomainMapping
DeleteDomainMapping(name string) error
DeleteDomainMapping(ctx context.Context, name string) error

// ListDomainMappings
ListDomainMappings() (*servingv1alpha1.DomainMappingList, error)
ListDomainMappings(ctx context.Context) (*servingv1alpha1.DomainMappingList, error)
}

type knServingClient struct {
Expand All @@ -68,8 +68,8 @@ func (cl *knServingClient) Namespace() string {
}

// GetDomainMapping gets DomainMapping by name
func (cl *knServingClient) GetDomainMapping(name string) (*servingv1alpha1.DomainMapping, error) {
dm, err := cl.client.DomainMappings(cl.namespace).Get(context.TODO(), name, v1.GetOptions{})
func (cl *knServingClient) GetDomainMapping(ctx context.Context, name string) (*servingv1alpha1.DomainMapping, error) {
dm, err := cl.client.DomainMappings(cl.namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
return nil, knerrors.GetError(err)
}
Expand All @@ -81,35 +81,35 @@ func (cl *knServingClient) GetDomainMapping(name string) (*servingv1alpha1.Domai
}

// CreateDomainMapping creates provided DomainMapping
func (cl *knServingClient) CreateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error {
_, err := cl.client.DomainMappings(cl.namespace).Create(context.TODO(), domainMapping, v1.CreateOptions{})
func (cl *knServingClient) CreateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error {
_, err := cl.client.DomainMappings(cl.namespace).Create(ctx, domainMapping, v1.CreateOptions{})
if err != nil {
return knerrors.GetError(err)
}
return updateServingGvk(domainMapping)
}

// UpdateDomainMapping updates provided DomainMapping
func (cl *knServingClient) UpdateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error {
_, err := cl.client.DomainMappings(cl.namespace).Update(context.TODO(), domainMapping, v1.UpdateOptions{})
func (cl *knServingClient) UpdateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error {
_, err := cl.client.DomainMappings(cl.namespace).Update(ctx, domainMapping, v1.UpdateOptions{})
if err != nil {
return knerrors.GetError(err)
}
return updateServingGvk(domainMapping)
}

// DeleteDomainMapping deletes DomainMapping by name
func (cl *knServingClient) DeleteDomainMapping(name string) error {
err := cl.client.DomainMappings(cl.namespace).Delete(context.TODO(), name, v1.DeleteOptions{})
func (cl *knServingClient) DeleteDomainMapping(ctx context.Context, name string) error {
err := cl.client.DomainMappings(cl.namespace).Delete(ctx, name, v1.DeleteOptions{})
if err != nil {
return knerrors.GetError(err)
}
return nil
}

// ListDomainMappings lists all DomainMappings
func (cl *knServingClient) ListDomainMappings() (*servingv1alpha1.DomainMappingList, error) {
domainMappingList, err := cl.client.DomainMappings(cl.namespace).List(context.TODO(), v1.ListOptions{})
func (cl *knServingClient) ListDomainMappings(ctx context.Context) (*servingv1alpha1.DomainMappingList, error) {
domainMappingList, err := cl.client.DomainMappings(cl.namespace).List(ctx, v1.ListOptions{})
if err != nil {
return nil, knerrors.GetError(err)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/serving/v1alpha1/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
"context"
"testing"

"knative.dev/client/pkg/util/mock"
Expand Down Expand Up @@ -65,7 +66,7 @@ func (sr *ServingRecorder) GetDomainMapping(name interface{}, domainMapping *ser
}

// GetDomainMapping mock function
func (c *MockKnServingClient) GetDomainMapping(name string) (*servingv1alpha1.DomainMapping, error) {
func (c *MockKnServingClient) GetDomainMapping(ctx context.Context, name string) (*servingv1alpha1.DomainMapping, error) {
call := c.recorder.r.VerifyCall("GetDomainMapping", name)
return call.Result[0].(*servingv1alpha1.DomainMapping), mock.ErrorOrNil(call.Result[1])
}
Expand All @@ -76,7 +77,7 @@ func (sr *ServingRecorder) CreateDomainMapping(domainMapping interface{}, err er
}

// CreateDomainMapping mock function
func (c *MockKnServingClient) CreateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error {
func (c *MockKnServingClient) CreateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error {
call := c.recorder.r.VerifyCall("CreateDomainMapping", domainMapping)
return mock.ErrorOrNil(call.Result[0])
}
Expand All @@ -87,7 +88,7 @@ func (sr *ServingRecorder) UpdateDomainMapping(domainMapping interface{}, err er
}

// UpdateDomainMapping mock function
func (c *MockKnServingClient) UpdateDomainMapping(domainMapping *servingv1alpha1.DomainMapping) error {
func (c *MockKnServingClient) UpdateDomainMapping(ctx context.Context, domainMapping *servingv1alpha1.DomainMapping) error {
call := c.recorder.r.VerifyCall("UpdateDomainMapping", domainMapping)
return mock.ErrorOrNil(call.Result[0])
}
Expand All @@ -98,7 +99,7 @@ func (sr *ServingRecorder) DeleteDomainMapping(name string, err error) {
}

// DeleteDomainMapping mock function
func (c *MockKnServingClient) DeleteDomainMapping(name string) error {
func (c *MockKnServingClient) DeleteDomainMapping(ctx context.Context, name string) error {
call := c.recorder.r.VerifyCall("DeleteDomainMapping", name)
return mock.ErrorOrNil(call.Result[0])
}
Expand All @@ -109,7 +110,7 @@ func (sr *ServingRecorder) ListDomainMappings(domainMappingList *servingv1alpha1
}

// ListDomainMappings mock function
func (c *MockKnServingClient) ListDomainMappings() (*servingv1alpha1.DomainMappingList, error) {
func (c *MockKnServingClient) ListDomainMappings(ctx context.Context) (*servingv1alpha1.DomainMappingList, error) {
call := c.recorder.r.VerifyCall("ListDomainMappings")
return call.Result[0].(*servingv1alpha1.DomainMappingList), mock.ErrorOrNil(call.Result[1])
}
12 changes: 7 additions & 5 deletions pkg/serving/v1alpha1/client_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
"context"
"testing"

"knative.dev/serving/pkg/apis/serving/v1alpha1"
Expand All @@ -34,11 +35,12 @@ func TestMockKnClient(t *testing.T) {
recorder.ListDomainMappings(&v1alpha1.DomainMappingList{}, nil)

// Call all services
client.GetDomainMapping("hello.foo.bar")
client.CreateDomainMapping(&v1alpha1.DomainMapping{})
client.DeleteDomainMapping("hello.foo.bar")
client.UpdateDomainMapping(&v1alpha1.DomainMapping{})
client.ListDomainMappings()
ctx := context.Background()
client.GetDomainMapping(ctx, "hello.foo.bar")
client.CreateDomainMapping(ctx, &v1alpha1.DomainMapping{})
client.DeleteDomainMapping(ctx, "hello.foo.bar")
client.UpdateDomainMapping(ctx, &v1alpha1.DomainMapping{})
client.ListDomainMappings(ctx)

// Validate
recorder.Validate()
Expand Down
19 changes: 10 additions & 9 deletions pkg/serving/v1alpha1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1alpha1

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -62,15 +63,15 @@ func TestGetDomainMapping(t *testing.T) {
})

t.Run("get domain mapping by name returns object", func(t *testing.T) {
domainMapping, err := client.GetDomainMapping(domainName)
domainMapping, err := client.GetDomainMapping(context.Background(), domainName)
assert.NilError(t, err)
assert.Equal(t, domainName, domainMapping.Name, "domain mapping name should be equal")
validateGroupVersionKind(t, domainMapping)
})

t.Run("get non-existing domain mapping by name returns error", func(t *testing.T) {
nonExistingName := "does-not-exist"
service, err := client.GetDomainMapping(nonExistingName)
service, err := client.GetDomainMapping(context.Background(), nonExistingName)
assert.Assert(t, service == nil, "no domain mapping should be returned")
assert.ErrorContains(t, err, "not found")
assert.ErrorContains(t, err, nonExistingName)
Expand All @@ -94,14 +95,14 @@ func TestCreateDomainMapping(t *testing.T) {
})

t.Run("create domain mapping without error creates a new object", func(t *testing.T) {
err := client.CreateDomainMapping(domainMapping)
err := client.CreateDomainMapping(context.Background(), domainMapping)
assert.NilError(t, err)
assert.Equal(t, domainMapping.Generation, int64(2))
validateGroupVersionKind(t, domainMapping)
})

t.Run("create domain mapping with an error returns an error object", func(t *testing.T) {
err := client.CreateDomainMapping(createDomainMapping("unknown", createServiceRef(serviceName, testNamespace)))
err := client.CreateDomainMapping(context.Background(), createDomainMapping("unknown", createServiceRef(serviceName, testNamespace)))
assert.ErrorContains(t, err, "unknown")
})
}
Expand All @@ -126,13 +127,13 @@ func TestUpdateDomainMapping(t *testing.T) {
})

t.Run("update domain mapping without error", func(t *testing.T) {
err := client.UpdateDomainMapping(domainMappingUpdate)
err := client.UpdateDomainMapping(context.Background(), domainMappingUpdate)
assert.NilError(t, err)
validateGroupVersionKind(t, domainMappingUpdate)
})

t.Run("update domain mapping with error", func(t *testing.T) {
err := client.UpdateDomainMapping(createDomainMapping("unknown", createServiceRef(serviceName, testNamespace)))
err := client.UpdateDomainMapping(context.Background(), createDomainMapping("unknown", createServiceRef(serviceName, testNamespace)))
assert.ErrorContains(t, err, "unknown")
})
}
Expand All @@ -153,13 +154,13 @@ func TestDeleteDomainMapping(t *testing.T) {
})

t.Run("delete domain mapping returns no error", func(t *testing.T) {
err := client.DeleteDomainMapping(domainName)
err := client.DeleteDomainMapping(context.Background(), domainName)
assert.NilError(t, err)
})

t.Run("delete non-existing domain mapping returns error", func(t *testing.T) {
nonExistingName := "does-not-exist"
err := client.DeleteDomainMapping(nonExistingName)
err := client.DeleteDomainMapping(context.Background(), nonExistingName)
assert.ErrorContains(t, err, "not found")
assert.ErrorContains(t, err, nonExistingName)
assert.ErrorType(t, err, &errors.StatusError{})
Expand All @@ -177,7 +178,7 @@ func TestListDomainMappings(t *testing.T) {
assert.Equal(t, testNamespace, a.GetNamespace())
return true, &servingv1alpha1.DomainMappingList{Items: []servingv1alpha1.DomainMapping{*dm1, *dm2, *dm3}}, nil
})
listServices, err := client.ListDomainMappings()
listServices, err := client.ListDomainMappings(context.Background())
assert.NilError(t, err)
assert.Assert(t, len(listServices.Items) == 3)
assert.Equal(t, listServices.Items[0].Name, "dm-1")
Expand Down

0 comments on commit b57b7e3

Please sign in to comment.