Skip to content

Commit

Permalink
Merge pull request #6 from mercedes-benz/move_login_into_interface
Browse files Browse the repository at this point in the history
Move login into interface
  • Loading branch information
bavarianbidi authored Sep 20, 2023
2 parents a35bf35 + 028c38f commit a909e49
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 27 deletions.
7 changes: 4 additions & 3 deletions internal/controller/enterprise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/organization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/pool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions internal/controller/repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
15 changes: 12 additions & 3 deletions pkg/client/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
23 changes: 20 additions & 3 deletions pkg/client/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pkg/client/mock/enterprise.go

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

15 changes: 15 additions & 0 deletions pkg/client/mock/instance.go

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

15 changes: 15 additions & 0 deletions pkg/client/mock/organization.go

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

15 changes: 15 additions & 0 deletions pkg/client/mock/pool.go

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

15 changes: 15 additions & 0 deletions pkg/client/mock/repository.go

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

15 changes: 12 additions & 3 deletions pkg/client/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit a909e49

Please sign in to comment.