From 028c38f954caf7cf23bc5720d91a31d3c60b1292 Mon Sep 17 00:00:00 2001 From: Mario Constanti Date: Mon, 18 Sep 2023 09:08:54 +0200 Subject: [PATCH] chore: garm login to interface for possible future test cases, the login request towards garm is now also made in the corresponding interface. This will keep the code more straigt and also makes possible test-cases easier to implement. Signed-off-by: Mario Constanti --- internal/controller/enterprise_controller.go | 7 +++--- .../controller/organization_controller.go | 7 +++--- internal/controller/pool_controller.go | 8 ++++--- internal/controller/repository_controller.go | 7 +++--- pkg/client/enterprise.go | 15 +++++++++--- pkg/client/instance.go | 23 ++++++++++++++++--- pkg/client/mock/enterprise.go | 15 ++++++++++++ pkg/client/mock/instance.go | 15 ++++++++++++ pkg/client/mock/organization.go | 15 ++++++++++++ pkg/client/mock/pool.go | 15 ++++++++++++ pkg/client/mock/repository.go | 15 ++++++++++++ pkg/client/organization.go | 15 +++++++++--- pkg/client/pool.go | 15 +++++++++--- pkg/client/repository.go | 15 +++++++++--- 14 files changed, 160 insertions(+), 27 deletions(-) diff --git a/internal/controller/enterprise_controller.go b/internal/controller/enterprise_controller.go index 616ecc4c..a4a16f59 100644 --- a/internal/controller/enterprise_controller.go +++ b/internal/controller/enterprise_controller.go @@ -62,7 +62,8 @@ func (r *EnterpriseReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, nil } - scope, err := garmClient.NewEnterpriseClient(garmClient.GarmScopeParams{ + enterpriseClient := garmClient.NewEnterpriseClient() + err = enterpriseClient.Login(garmClient.GarmScopeParams{ BaseURL: r.BaseURL, Username: r.Username, Password: r.Password, @@ -74,10 +75,10 @@ func (r *EnterpriseReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Handle deleted enterprises if !enterprise.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, scope, enterprise) + return r.reconcileDelete(ctx, enterpriseClient, enterprise) } - return r.reconcileNormal(ctx, scope, enterprise) + return r.reconcileNormal(ctx, enterpriseClient, enterprise) } func (r *EnterpriseReconciler) reconcileNormal(ctx context.Context, client garmClient.EnterpriseClient, enterprise *garmoperatorv1alpha1.Enterprise) (ctrl.Result, error) { diff --git a/internal/controller/organization_controller.go b/internal/controller/organization_controller.go index a7490478..2c28eeee 100644 --- a/internal/controller/organization_controller.go +++ b/internal/controller/organization_controller.go @@ -61,7 +61,8 @@ func (r *OrganizationReconciler) Reconcile(ctx context.Context, req ctrl.Request return ctrl.Result{}, nil } - scope, err := garmClient.NewOrganizationClient(garmClient.GarmScopeParams{ + organizationClient := garmClient.NewOrganizationClient() + err = organizationClient.Login(garmClient.GarmScopeParams{ BaseURL: r.BaseURL, Username: r.Username, Password: r.Password, @@ -73,10 +74,10 @@ func (r *OrganizationReconciler) Reconcile(ctx context.Context, req ctrl.Request // Handle deleted organizations if !organization.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, scope, organization) + return r.reconcileDelete(ctx, organizationClient, organization) } - return r.reconcileNormal(ctx, scope, organization) + return r.reconcileNormal(ctx, organizationClient, organization) } func (r *OrganizationReconciler) reconcileNormal(ctx context.Context, client garmClient.OrganizationClient, organization *garmoperatorv1alpha1.Organization) (ctrl.Result, error) { diff --git a/internal/controller/pool_controller.go b/internal/controller/pool_controller.go index 1b1495fc..82c8066e 100644 --- a/internal/controller/pool_controller.go +++ b/internal/controller/pool_controller.go @@ -70,7 +70,8 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return ctrl.Result{}, nil } - poolClient, err := garmClient.NewPoolClient(garmClient.GarmScopeParams{ + poolClient := garmClient.NewPoolClient() + err := poolClient.Login(garmClient.GarmScopeParams{ BaseURL: r.BaseURL, Username: r.Username, Password: r.Password, @@ -79,7 +80,8 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. return ctrl.Result{}, err } - runnerInstanceClient, err := garmClient.NewInstanceClient(garmClient.GarmScopeParams{ + instanceClient := garmClient.NewInstanceClient() + err = instanceClient.Login(garmClient.GarmScopeParams{ BaseURL: r.BaseURL, Username: r.Username, Password: r.Password, @@ -90,7 +92,7 @@ func (r *PoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl. // handle deletion if !pool.ObjectMeta.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, poolClient, pool, runnerInstanceClient) + return r.reconcileDelete(ctx, poolClient, pool, instanceClient) } return r.reconcileNormal(ctx, poolClient, pool) diff --git a/internal/controller/repository_controller.go b/internal/controller/repository_controller.go index d61bfa70..9cfb1fe5 100644 --- a/internal/controller/repository_controller.go +++ b/internal/controller/repository_controller.go @@ -61,7 +61,8 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, nil } - scope, err := garmClient.NewRepositoryClient(garmClient.GarmScopeParams{ + repositoryClient := garmClient.NewRepositoryClient() + err = repositoryClient.Login(garmClient.GarmScopeParams{ BaseURL: r.BaseURL, Username: r.Username, Password: r.Password, @@ -73,10 +74,10 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) // Handle deleted repositories if !repository.DeletionTimestamp.IsZero() { - return r.reconcileDelete(ctx, scope, repository) + return r.reconcileDelete(ctx, repositoryClient, repository) } - return r.reconcileNormal(ctx, scope, repository) + return r.reconcileNormal(ctx, repositoryClient, repository) } func (r *RepositoryReconciler) reconcileNormal(ctx context.Context, client garmClient.RepositoryClient, repository *garmoperatorv1alpha1.Repository) (ctrl.Result, error) { diff --git a/pkg/client/enterprise.go b/pkg/client/enterprise.go index fce2d58a..ee52f483 100644 --- a/pkg/client/enterprise.go +++ b/pkg/client/enterprise.go @@ -11,6 +11,7 @@ import ( ) type EnterpriseClient interface { + Login(garmParams GarmScopeParams) error ListEnterprises(param *enterprises.ListEnterprisesParams) (*enterprises.ListEnterprisesOK, error) CreateEnterprise(param *enterprises.CreateEnterpriseParams) (*enterprises.CreateEnterpriseOK, error) GetEnterprise(param *enterprises.GetEnterpriseParams) (*enterprises.GetEnterpriseOK, error) @@ -23,13 +24,21 @@ type enterpriseClient struct { token runtime.ClientAuthInfoWriter } -func NewEnterpriseClient(garmParams GarmScopeParams) (EnterpriseClient, error) { +func NewEnterpriseClient() EnterpriseClient { + return &enterpriseClient{} +} + +func (s *enterpriseClient) Login(garmParams GarmScopeParams) error { + metrics.TotalGarmCalls.WithLabelValues("Login").Inc() garmClient, token, err := newGarmClient(garmParams) if err != nil { - return nil, err + metrics.GarmCallErrors.WithLabelValues("Login").Inc() + return err } + s.client = garmClient + s.token = token - return &enterpriseClient{garmClient, token}, nil + return nil } func (s *enterpriseClient) ListEnterprises(param *enterprises.ListEnterprisesParams) (*enterprises.ListEnterprisesOK, error) { diff --git a/pkg/client/instance.go b/pkg/client/instance.go index 6d607ea3..38535952 100644 --- a/pkg/client/instance.go +++ b/pkg/client/instance.go @@ -6,9 +6,12 @@ import ( "github.com/cloudbase/garm/client" "github.com/cloudbase/garm/client/instances" "github.com/go-openapi/runtime" + + "github.com/mercedes-benz/garm-operator/pkg/metrics" ) type InstanceClient interface { + Login(garmParams GarmScopeParams) error GetInstance(params *instances.GetInstanceParams) (*instances.GetInstanceOK, error) ListInstances(params *instances.ListInstancesParams) (*instances.ListInstancesOK, error) ListPoolInstances(params *instances.ListPoolInstancesParams) (*instances.ListPoolInstancesOK, error) @@ -19,34 +22,48 @@ type instanceClient struct { token runtime.ClientAuthInfoWriter } -func NewInstanceClient(garmParams GarmScopeParams) (InstanceClient, error) { +func NewInstanceClient() InstanceClient { + return &instanceClient{} +} + +func (i *instanceClient) Login(garmParams GarmScopeParams) error { + metrics.TotalGarmCalls.WithLabelValues("Login").Inc() garmClient, token, err := newGarmClient(garmParams) if err != nil { - return nil, err + metrics.GarmCallErrors.WithLabelValues("Login").Inc() + return err } + i.client = garmClient + i.token = token - return &instanceClient{garmClient, token}, nil + return nil } func (i *instanceClient) GetInstance(params *instances.GetInstanceParams) (*instances.GetInstanceOK, error) { + metrics.TotalGarmCalls.WithLabelValues("instances.Get").Inc() instance, err := i.client.Instances.GetInstance(params, i.token) if err != nil { + metrics.GarmCallErrors.WithLabelValues("instances.Get").Inc() return nil, err } return instance, nil } func (i *instanceClient) ListInstances(params *instances.ListInstancesParams) (*instances.ListInstancesOK, error) { + metrics.TotalGarmCalls.WithLabelValues("instances.List").Inc() instances, err := i.client.Instances.ListInstances(params, i.token) if err != nil { + metrics.GarmCallErrors.WithLabelValues("instances.List").Inc() return nil, err } return instances, nil } func (i *instanceClient) ListPoolInstances(params *instances.ListPoolInstancesParams) (*instances.ListPoolInstancesOK, error) { + metrics.TotalGarmCalls.WithLabelValues("instances.ListPool").Inc() instances, err := i.client.Instances.ListPoolInstances(params, i.token) if err != nil { + metrics.GarmCallErrors.WithLabelValues("instances.ListPool").Inc() return nil, err } return instances, nil diff --git a/pkg/client/mock/enterprise.go b/pkg/client/mock/enterprise.go index c0cebbcb..c9a1c477 100644 --- a/pkg/client/mock/enterprise.go +++ b/pkg/client/mock/enterprise.go @@ -9,6 +9,7 @@ import ( reflect "reflect" enterprises "github.com/cloudbase/garm/client/enterprises" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockEnterpriseClientMockRecorder) ListEnterprises(param interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListEnterprises", reflect.TypeOf((*MockEnterpriseClient)(nil).ListEnterprises), param) } +// Login mocks base method. +func (m *MockEnterpriseClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockEnterpriseClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockEnterpriseClient)(nil).Login), garmParams) +} + // UpdateEnterprise mocks base method. func (m *MockEnterpriseClient) UpdateEnterprise(param *enterprises.UpdateEnterpriseParams) (*enterprises.UpdateEnterpriseOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/mock/instance.go b/pkg/client/mock/instance.go index a8069e56..615d1866 100644 --- a/pkg/client/mock/instance.go +++ b/pkg/client/mock/instance.go @@ -9,6 +9,7 @@ import ( reflect "reflect" instances "github.com/cloudbase/garm/client/instances" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -79,3 +80,17 @@ func (mr *MockInstanceClientMockRecorder) ListPoolInstances(params interface{}) mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListPoolInstances", reflect.TypeOf((*MockInstanceClient)(nil).ListPoolInstances), params) } + +// Login mocks base method. +func (m *MockInstanceClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockInstanceClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockInstanceClient)(nil).Login), garmParams) +} diff --git a/pkg/client/mock/organization.go b/pkg/client/mock/organization.go index 1a2c1be7..8997d4e8 100644 --- a/pkg/client/mock/organization.go +++ b/pkg/client/mock/organization.go @@ -9,6 +9,7 @@ import ( reflect "reflect" organizations "github.com/cloudbase/garm/client/organizations" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockOrganizationClientMockRecorder) ListOrganizations(param interface{ return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListOrganizations", reflect.TypeOf((*MockOrganizationClient)(nil).ListOrganizations), param) } +// Login mocks base method. +func (m *MockOrganizationClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockOrganizationClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockOrganizationClient)(nil).Login), garmParams) +} + // UpdateOrganization mocks base method. func (m *MockOrganizationClient) UpdateOrganization(param *organizations.UpdateOrgParams) (*organizations.UpdateOrgOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/mock/pool.go b/pkg/client/mock/pool.go index b3e2b0f9..33b142d7 100644 --- a/pkg/client/mock/pool.go +++ b/pkg/client/mock/pool.go @@ -12,6 +12,7 @@ import ( organizations "github.com/cloudbase/garm/client/organizations" pools "github.com/cloudbase/garm/client/pools" repositories "github.com/cloudbase/garm/client/repositories" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -156,6 +157,20 @@ func (mr *MockPoolClientMockRecorder) ListAllPools(param interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListAllPools", reflect.TypeOf((*MockPoolClient)(nil).ListAllPools), param) } +// Login mocks base method. +func (m *MockPoolClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockPoolClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockPoolClient)(nil).Login), garmParams) +} + // UpdateEnterprisePool mocks base method. func (m *MockPoolClient) UpdateEnterprisePool(param *enterprises.UpdateEnterprisePoolParams) (*enterprises.UpdateEnterprisePoolOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/mock/repository.go b/pkg/client/mock/repository.go index e5a435df..dc5225ef 100644 --- a/pkg/client/mock/repository.go +++ b/pkg/client/mock/repository.go @@ -9,6 +9,7 @@ import ( reflect "reflect" repositories "github.com/cloudbase/garm/client/repositories" + client "github.com/mercedes-benz/garm-operator/pkg/client" gomock "go.uber.org/mock/gomock" ) @@ -94,6 +95,20 @@ func (mr *MockRepositoryClientMockRecorder) ListRepositories(param interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositories", reflect.TypeOf((*MockRepositoryClient)(nil).ListRepositories), param) } +// Login mocks base method. +func (m *MockRepositoryClient) Login(garmParams client.GarmScopeParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Login", garmParams) + ret0, _ := ret[0].(error) + return ret0 +} + +// Login indicates an expected call of Login. +func (mr *MockRepositoryClientMockRecorder) Login(garmParams interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Login", reflect.TypeOf((*MockRepositoryClient)(nil).Login), garmParams) +} + // UpdateRepository mocks base method. func (m *MockRepositoryClient) UpdateRepository(param *repositories.UpdateRepoParams) (*repositories.UpdateRepoOK, error) { m.ctrl.T.Helper() diff --git a/pkg/client/organization.go b/pkg/client/organization.go index 485c2df3..cb29ef69 100644 --- a/pkg/client/organization.go +++ b/pkg/client/organization.go @@ -11,6 +11,7 @@ import ( ) type OrganizationClient interface { + Login(garmParams GarmScopeParams) error ListOrganizations(param *organizations.ListOrgsParams) (*organizations.ListOrgsOK, error) CreateOrganization(param *organizations.CreateOrgParams) (*organizations.CreateOrgOK, error) GetOrganization(param *organizations.GetOrgParams) (*organizations.GetOrgOK, error) @@ -23,13 +24,21 @@ type organizationClient struct { token runtime.ClientAuthInfoWriter } -func NewOrganizationClient(garmParams GarmScopeParams) (OrganizationClient, error) { +func NewOrganizationClient() OrganizationClient { + return &organizationClient{} +} + +func (s *organizationClient) Login(garmParams GarmScopeParams) error { + metrics.TotalGarmCalls.WithLabelValues("Login").Inc() garmClient, token, err := newGarmClient(garmParams) if err != nil { - return nil, err + metrics.GarmCallErrors.WithLabelValues("Login").Inc() + return err } + s.client = garmClient + s.token = token - return &organizationClient{garmClient, token}, nil + return nil } func (s *organizationClient) ListOrganizations(param *organizations.ListOrgsParams) (*organizations.ListOrgsOK, error) { diff --git a/pkg/client/pool.go b/pkg/client/pool.go index 40fd098b..207c222f 100644 --- a/pkg/client/pool.go +++ b/pkg/client/pool.go @@ -14,6 +14,7 @@ import ( ) type PoolClient interface { + Login(garmParams GarmScopeParams) error ListAllPools(param *pools.ListPoolsParams) (*pools.ListPoolsOK, error) CreateRepoPool(param *repositories.CreateRepoPoolParams) (*repositories.CreateRepoPoolOK, error) CreateOrgPool(param *organizations.CreateOrgPoolParams) (*organizations.CreateOrgPoolOK, error) @@ -31,13 +32,21 @@ type poolClient struct { token runtime.ClientAuthInfoWriter } -func NewPoolClient(garmParams GarmScopeParams) (PoolClient, error) { +func NewPoolClient() PoolClient { + return &poolClient{} +} + +func (p *poolClient) Login(garmParams GarmScopeParams) error { + metrics.TotalGarmCalls.WithLabelValues("Login").Inc() garmClient, token, err := newGarmClient(garmParams) if err != nil { - return nil, err + metrics.GarmCallErrors.WithLabelValues("Login").Inc() + return err } + p.client = garmClient + p.token = token - return &poolClient{garmClient, token}, nil + return nil } func (p *poolClient) ListAllPools(param *pools.ListPoolsParams) (*pools.ListPoolsOK, error) { diff --git a/pkg/client/repository.go b/pkg/client/repository.go index a3badbca..477c8270 100644 --- a/pkg/client/repository.go +++ b/pkg/client/repository.go @@ -11,6 +11,7 @@ import ( ) type RepositoryClient interface { + Login(garmParams GarmScopeParams) error ListRepositories(param *repositories.ListReposParams) (*repositories.ListReposOK, error) CreateRepository(param *repositories.CreateRepoParams) (*repositories.CreateRepoOK, error) GetRepository(param *repositories.GetRepoParams) (*repositories.GetRepoOK, error) @@ -23,13 +24,21 @@ type repositoryClient struct { token runtime.ClientAuthInfoWriter } -func NewRepositoryClient(garmParams GarmScopeParams) (RepositoryClient, error) { +func NewRepositoryClient() RepositoryClient { + return &repositoryClient{} +} + +func (s *repositoryClient) Login(garmParams GarmScopeParams) error { + metrics.TotalGarmCalls.WithLabelValues("Login").Inc() garmClient, token, err := newGarmClient(garmParams) if err != nil { - return nil, err + metrics.GarmCallErrors.WithLabelValues("Login").Inc() + return err } + s.client = garmClient + s.token = token - return &repositoryClient{garmClient, token}, nil + return nil } func (s *repositoryClient) ListRepositories(param *repositories.ListReposParams) (*repositories.ListReposOK, error) {