Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rmweir committed Dec 8, 2022
1 parent 9a9da02 commit 7bd6319
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 18 deletions.
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Package cache provides a cache that maintains a list for a resource type which it updates as it
*/
package cache

import (
Expand Down
11 changes: 0 additions & 11 deletions pkg/cache/sharedinformerfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
"context"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -66,16 +65,6 @@ func NewSharedCachedFactory(sharedClientFactory client.SharedClientFactory, opts
return factory
}

func NewSharedCachedFactoryWithAgent(userAgent string, factory SharedCacheFactory) (SharedCacheFactory, error) {
f, ok := factory.(*sharedCacheFactory)
if !ok {
return nil, fmt.Errorf("failed to create SharedCacheFactory with agent [%s]", userAgent)
}
factoryCopy := *f
factoryCopy.sharedClientFactory = client.NewSharedClientFactoryWithAgent(userAgent, f.sharedClientFactory)
return &factoryCopy, nil
}

func applyDefaults(opts *SharedCacheFactoryOptions) *SharedCacheFactoryOptions {
var newOpts SharedCacheFactoryOptions
if opts != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ func IsNamespaced(gvr schema.GroupVersionResource, mapper meta.RESTMapper) (bool
func (c *Client) WithAgent(userAgent string) (*Client, error) {
client := *c
config := c.Config
if config.UserAgent == "" {
return nil, fmt.Errorf("cannot create new restClient with empty userAgent")
}
config.UserAgent = userAgent
restClient, err := rest.UnversionedRESTClientFor(&config)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ func TestWithAgent(t *testing.T) {

_, err = testRestClientWithAgent.Client.Transport.RoundTrip(&http.Request{})
assert.Nil(t, err)

// with invalid config
testClient = &Client{
Config: rest.Config{},
}

testClientWithAgent, err = testClient.WithAgent("testagent")
assert.NotNil(t, err)
assert.Nil(t, testClientWithAgent)
}

func (u userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
Expand Down
172 changes: 172 additions & 0 deletions pkg/client/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions pkg/client/useragent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package client

import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
"net/http"
"testing"
)

func TestNewSharedClientFactoryWithAgent(t *testing.T) {
mockSharedClientFactory := NewMockSharedClientFactory(gomock.NewController(t))

testUserAgent := "testagent"
testConfig := rest.Config{
UserAgent: "defaultuseragent",
ContentConfig: rest.ContentConfig{
NegotiatedSerializer: serializer.WithoutConversionCodecFactory{},
},
Transport: userAgentRoundTripper{t: t, expectedUserAgent: testUserAgent},
}

testRestClient, err := rest.UnversionedRESTClientFor(&testConfig)
assert.Nil(t, err)
testClient := &Client{
RESTClient: testRestClient,
Config: testConfig,
}
mockSharedClientFactory.EXPECT().ForKind(gomock.Any()).DoAndReturn(func(gvk schema.GroupVersionKind) (*Client, error) {
return testClient, nil
}).AnyTimes()
mockSharedClientFactory.EXPECT().ForResource(gomock.Any(), gomock.Any()).DoAndReturn(func(gvr schema.GroupVersionResource, namespaced bool) (*Client, error) {
return testClient, nil
}).AnyTimes()
mockSharedClientFactory.EXPECT().ForResourceKind(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(gvr schema.GroupVersionResource, kind string, namespaced bool) *Client {
return testClient
}).AnyTimes()
testSharedClientFactoryWithAgent := NewSharedClientFactoryWithAgent("testagent", mockSharedClientFactory)

// for kind
clientWithAgent, err := testSharedClientFactoryWithAgent.ForKind(schema.GroupVersionKind{})
assert.Nil(t, err)

restClient := clientWithAgent.RESTClient.(*rest.RESTClient)
restClient.Client.Transport.RoundTrip(&http.Request{})

// for resource
clientWithAgent, err = testSharedClientFactoryWithAgent.ForResource(schema.GroupVersionResource{}, false)
assert.Nil(t, err)

restClient = clientWithAgent.RESTClient.(*rest.RESTClient)
restClient.Client.Transport.RoundTrip(&http.Request{})

// for resource kind
clientWithAgent = testSharedClientFactoryWithAgent.ForResourceKind(schema.GroupVersionResource{}, "", false)
assert.Nil(t, err)

restClient = clientWithAgent.RESTClient.(*rest.RESTClient)
restClient.Client.Transport.RoundTrip(&http.Request{})

// test with errors
mockSharedClientErrFactory := NewMockSharedClientFactory(gomock.NewController(t))
mockSharedClientErrFactory.EXPECT().ForKind(gomock.Any()).DoAndReturn(func(gvk schema.GroupVersionKind) (*Client, error) {
return nil, fmt.Errorf("some error")
}).AnyTimes()
mockSharedClientErrFactory.EXPECT().ForResource(gomock.Any(), gomock.Any()).DoAndReturn(func(gvr schema.GroupVersionResource, namespaced bool) (*Client, error) {
return nil, fmt.Errorf("some error")
}).AnyTimes()
mockSharedClientErrFactory.EXPECT().ForResourceKind(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(gvr schema.GroupVersionResource, kind string, namespaced bool) *Client {
return nil
}).AnyTimes()
testSharedClientFactoryWithAgent = NewSharedClientFactoryWithAgent("", mockSharedClientErrFactory)

// for kind
clientWithAgent, err = testSharedClientFactoryWithAgent.ForKind(schema.GroupVersionKind{})
assert.NotNil(t, err)
assert.Nil(t, clientWithAgent)

// for resource
clientWithAgent, err = testSharedClientFactoryWithAgent.ForResource(schema.GroupVersionResource{}, false)
assert.NotNil(t, err)
assert.Nil(t, clientWithAgent)

// for resource kind
clientWithAgent = testSharedClientFactoryWithAgent.ForResourceKind(schema.GroupVersionResource{}, "", false)
assert.Nil(t, clientWithAgent)
}
File renamed without changes.
3 changes: 3 additions & 0 deletions pkg/controller/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (s *sharedControllerFactoryWithAgent) ForResourceKind(gvr schema.GroupVersi

func (s *sharedControllerWithAgent) Client() *client.Client {
client := s.SharedController.Client()
if client == nil {
return client
}
clientWithAgent, err := client.WithAgent(s.userAgent)
if err != nil {
log.Debugf("failed to get client with agent [%s]", s.userAgent)
Expand Down
15 changes: 11 additions & 4 deletions pkg/controller/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ func TestNewSharedControllerFactoryWithAgent(t *testing.T) {
}
testSharedController.EXPECT().Client().DoAndReturn(func() *client.Client {
return testClient
})
}).AnyTimes()

a := NewSharedControllerWithAgent("", testSharedController)
b := a.Client().RESTClient.(*rest.RESTClient)
b.Client.Transport.RoundTrip(&http.Request{})
testSharedControllerWithAgent := NewSharedControllerWithAgent(testUserAgent, testSharedController)
testClientWithAgent := testSharedControllerWithAgent.Client().RESTClient.(*rest.RESTClient)
testClientWithAgent.Client.Transport.RoundTrip(&http.Request{})

testErrSharedController := NewMockSharedController(gomock.NewController(t))
testErrSharedController.EXPECT().Client().DoAndReturn(func() *client.Client {
return nil
}).AnyTimes()
testSharedControllerWithAgent = NewSharedControllerWithAgent(testUserAgent, testErrSharedController)
assert.Nil(t, testSharedControllerWithAgent.Client())
}

func (u userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
Expand Down

0 comments on commit 7bd6319

Please sign in to comment.