Skip to content

Commit

Permalink
Delete velero generate client from client factory.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <jxun@vmware.com>
  • Loading branch information
Xun Jiang committed Nov 2, 2023
1 parent 8e600d9 commit 1c6ca1e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 195 deletions.
17 changes: 0 additions & 17 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
clientset "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
)

//go:generate mockery --name Factory
Expand All @@ -42,9 +41,6 @@ import (
type Factory interface {
// BindFlags binds common flags (--kubeconfig, --namespace) to the passed-in FlagSet.
BindFlags(flags *pflag.FlagSet)
// Client returns a VeleroClient. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
Client() (clientset.Interface, error)
// KubeClient returns a Kubernetes client. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
KubeClient() (kubernetes.Interface, error)
Expand Down Expand Up @@ -115,19 +111,6 @@ func (f *factory) ClientConfig() (*rest.Config, error) {
return Config(f.kubeconfig, f.kubecontext, f.baseName, f.clientQPS, f.clientBurst)
}

func (f *factory) Client() (clientset.Interface, error) {
clientConfig, err := f.ClientConfig()
if err != nil {
return nil, err
}

veleroClient, err := clientset.NewForConfig(clientConfig)
if err != nil {
return nil, errors.WithStack(err)
}
return veleroClient, nil
}

func (f *factory) KubeClient() (kubernetes.Interface, error) {
clientConfig, err := f.ClientConfig()
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/client/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ func TestFactory(t *testing.T) {
assert.Equal(t, test.burst, clientConfig.Burst)
strings.Contains(clientConfig.UserAgent, test.baseName)

client, _ := f.Client()
_, e := client.Discovery().ServerGroups()
assert.Contains(t, e.Error(), fmt.Sprintf("Get \"%s/api?timeout=", test.expectedHost))
assert.NotNil(t, client)

kubeClient, _ := f.KubeClient()
group := kubeClient.NodeV1().RESTClient().APIVersion().Group
assert.NotNil(t, kubeClient)
Expand Down
39 changes: 6 additions & 33 deletions pkg/client/mocks/Factory.go

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

47 changes: 14 additions & 33 deletions pkg/discovery/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
clientgotesting "k8s.io/client-go/testing"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
discoverymocks "github.com/vmware-tanzu/velero/pkg/discovery/mocks"
"github.com/vmware-tanzu/velero/pkg/features"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
"github.com/vmware-tanzu/velero/pkg/util/logging"
Expand Down Expand Up @@ -548,39 +547,31 @@ func TestHelper_refreshServerPreferredResources(t *testing.T) {
}

tests := []struct {
name string
isGetResError bool
name string
expectedErr error
}{
{
name: "success get preferred resources",
name: "success get preferred resources",
expectedErr: nil,
},
{
name: "failed to get preferred resources",
isGetResError: true,
name: "failed to get preferred resources",
expectedErr: errors.New("Failed to discover preferred resources"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fakeClient := discoverymocks.NewServerResourcesInterface(t)

if tc.isGetResError {
fakeClient.On("ServerPreferredResources").Return(nil, errors.New("Failed to discover preferred resources"))
} else {
fakeClient.On("ServerPreferredResources").Return(apiList, nil)
}
fakeClient := velerotest.NewFakeServerResourcesInterface(apiList, []*metav1.APIGroup{}, map[schema.GroupVersion]error{}, tc.expectedErr)

resources, err := refreshServerPreferredResources(fakeClient, logrus.New())

if tc.isGetResError {
if tc.expectedErr != nil {
assert.NotNil(t, err)
assert.Nil(t, resources)
} else {
assert.Nil(t, err)
assert.NotNil(t, resources)
}

fakeClient.AssertExpectations(t)
})
}
}
Expand Down Expand Up @@ -612,41 +603,31 @@ func TestHelper_refreshServerGroupsAndResources(t *testing.T) {
},
}
tests := []struct {
name string
isGetResError bool
name string
expectedErr error
}{
{
name: "success get service groups and resouorces",
},
{
name: "failed to service groups and resouorces",
isGetResError: true,
name: "failed to service groups and resouorces",
expectedErr: errors.New("Failed to discover service groups and resouorces"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
fakeClient := discoverymocks.NewServerResourcesInterface(t)

if tc.isGetResError {
fakeClient.On("ServerGroupsAndResources").Return(nil, nil, errors.New("Failed to discover service groups and resouorces"))
} else {
fakeClient.On("ServerGroupsAndResources").Return(apiGroup, apiList, nil)
}
fakeClient := velerotest.NewFakeServerResourcesInterface(apiList, apiGroup, map[schema.GroupVersion]error{}, tc.expectedErr)

serverGroups, serverResources, err := refreshServerGroupsAndResources(fakeClient, logrus.New())

if tc.isGetResError {
if tc.expectedErr != nil {
assert.NotNil(t, err)
assert.Nil(t, serverGroups)
assert.Nil(t, serverResources)
} else {
assert.Nil(t, err)
assert.NotNil(t, serverGroups)
assert.NotNil(t, serverResources)
}

fakeClient.AssertExpectations(t)
})
}
}
Expand Down
90 changes: 5 additions & 85 deletions pkg/discovery/mocks/Helper.go

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

5 changes: 0 additions & 5 deletions pkg/test/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ import (
discoveryfake "k8s.io/client-go/discovery/fake"
dynamicfake "k8s.io/client-go/dynamic/fake"
kubefake "k8s.io/client-go/kubernetes/fake"

"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
)

// APIServer contains in-memory fakes for all of the relevant
// Kubernetes API server clients.
type APIServer struct {
VeleroClient *fake.Clientset
KubeClient *kubefake.Clientset
DynamicClient *dynamicfake.FakeDynamicClient
DiscoveryClient *DiscoveryClient
Expand All @@ -43,7 +40,6 @@ func NewAPIServer(t *testing.T) *APIServer {
t.Helper()

var (
veleroClient = fake.NewSimpleClientset()
kubeClient = kubefake.NewSimpleClientset()
dynamicClient = dynamicfake.NewSimpleDynamicClientWithCustomListKinds(runtime.NewScheme(),
map[schema.GroupVersionResource]string{
Expand All @@ -65,7 +61,6 @@ func NewAPIServer(t *testing.T) *APIServer {
)

return &APIServer{
VeleroClient: veleroClient,
KubeClient: kubeClient,
DynamicClient: dynamicClient,
DiscoveryClient: discoveryClient,
Expand Down
17 changes: 0 additions & 17 deletions test/pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ import (

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
clientset "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
)

// Factory knows how to create a VeleroClient and Kubernetes client.
type Factory interface {
// BindFlags binds common flags (--kubeconfig, --namespace) to the passed-in FlagSet.
BindFlags(flags *pflag.FlagSet)
// Client returns a VeleroClient. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
Client() (clientset.Interface, error)
// KubeClient returns a Kubernetes client. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
KubeClient() (kubernetes.Interface, error)
Expand Down Expand Up @@ -114,19 +110,6 @@ func (f *factory) ClientConfig() (*rest.Config, error) {
return Config(f.kubeconfig, f.kubecontext, f.baseName, f.clientQPS, f.clientBurst)
}

func (f *factory) Client() (clientset.Interface, error) {
clientConfig, err := f.ClientConfig()
if err != nil {
return nil, err
}

veleroClient, err := clientset.NewForConfig(clientConfig)
if err != nil {
return nil, errors.WithStack(err)
}
return veleroClient, nil
}

func (f *factory) KubeClient() (kubernetes.Interface, error) {
clientConfig, err := f.ClientConfig()
if err != nil {
Expand Down

0 comments on commit 1c6ca1e

Please sign in to comment.