From a007826b4079c6bd89b9547437b9d8ec1e3f0703 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Fri, 25 Oct 2024 16:16:38 +0200 Subject: [PATCH] strip down cluster and service sideloads (#306) --- go.mod | 2 +- go.sum | 4 +- internal/controller/argorollout_controller.go | 4 +- pkg/client/console.go | 3 +- pkg/client/service.go | 16 +++-- pkg/controller/service/reconciler.go | 6 +- pkg/test/mocks/Client_mock.go | 72 +++++++++++++++++-- 7 files changed, 87 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index a1e9b611..38ba67cf 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/open-policy-agent/gatekeeper/v3 v3.17.1 github.com/orcaman/concurrent-map/v2 v2.0.1 github.com/pkg/errors v0.9.1 - github.com/pluralsh/console/go/client v1.21.4 + github.com/pluralsh/console/go/client v1.22.1 github.com/pluralsh/controller-reconcile-helper v0.1.0 github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 github.com/pluralsh/polly v0.1.10 diff --git a/go.sum b/go.sum index fd6709fc..0fb29c90 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,8 @@ github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rK github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pluralsh/console/go/client v1.21.4 h1:LT0u/2b8HYTZtFUc/UBXqj08dD9y3R2FWU+zDHJxwMA= -github.com/pluralsh/console/go/client v1.21.4/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k= +github.com/pluralsh/console/go/client v1.22.1 h1:kC6QEiOkUnOGyJFugsPejzMNlM+4VgfJR1oST9Pxfkg= +github.com/pluralsh/console/go/client v1.22.1/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k= github.com/pluralsh/controller-reconcile-helper v0.1.0 h1:BV3dYZFH5rn8ZvZjtpkACSv/GmLEtRftNQj/Y4ddHEo= github.com/pluralsh/controller-reconcile-helper v0.1.0/go.mod h1:RxAbvSB4/jkvx616krCdNQXPbpGJXW3J1L3rASxeFOA= github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw= diff --git a/internal/controller/argorollout_controller.go b/internal/controller/argorollout_controller.go index a7ece956..b0a5fee3 100644 --- a/internal/controller/argorollout_controller.go +++ b/internal/controller/argorollout_controller.go @@ -64,7 +64,7 @@ func (r *ArgoRolloutReconciler) Reconcile(ctx context.Context, req ctrl.Request) if serviceID == "" { return ctrl.Result{}, fmt.Errorf("the service ID from the inventory annotation is empty") } - service, err := r.ConsoleClient.GetService(serviceID) + service, err := r.ConsoleClient.GetServiceDeploymentComponents(serviceID) if err != nil { return ctrl.Result{}, err } @@ -119,7 +119,7 @@ func (r *ArgoRolloutReconciler) rollback(rolloutIf clientset.RolloutInterface, r return nil } -func hasPausedRolloutComponent(service *console.GetServiceDeploymentForAgent_ServiceDeployment) bool { +func hasPausedRolloutComponent(service *console.GetServiceDeploymentComponents_ServiceDeployment) bool { for _, component := range service.Components { if component.Kind == rollouts.RolloutKind { if component.State != nil && *component.State == console.ComponentStatePaused { diff --git a/pkg/client/console.go b/pkg/client/console.go index 07aa7685..28555e63 100644 --- a/pkg/client/console.go +++ b/pkg/client/console.go @@ -47,8 +47,9 @@ type Client interface { UpdateClusterRestore(id string, attrs console.RestoreAttributes) (*console.ClusterRestoreFragment, error) SaveClusterBackup(attrs console.BackupAttributes) (*console.ClusterBackupFragment, error) GetClusterBackup(clusterID, namespace, name string) (*console.ClusterBackupFragment, error) - GetServices(after *string, first *int64) (*console.PagedClusterServices, error) + GetServices(after *string, first *int64) (*console.PagedClusterServiceIds, error) GetService(id string) (*console.GetServiceDeploymentForAgent_ServiceDeployment, error) + GetServiceDeploymentComponents(id string) (*console.GetServiceDeploymentComponents_ServiceDeployment, error) UpdateComponents(id, revisionID string, sha *string, components []*console.ComponentAttributes, errs []*console.ServiceErrorAttributes) error AddServiceErrors(id string, errs []*console.ServiceErrorAttributes) error ParsePipelineGateCR(pgFragment *console.PipelineGateFragment, operatorNamespace string) (*v1alpha1.PipelineGate, error) diff --git a/pkg/client/service.go b/pkg/client/service.go index 64d3a935..b030ed58 100644 --- a/pkg/client/service.go +++ b/pkg/client/service.go @@ -6,14 +6,13 @@ import ( console "github.com/pluralsh/console/go/client" ) -func (c *client) GetServices(after *string, first *int64) (*console.PagedClusterServices, error) { - - resp, err := c.consoleClient.PagedClusterServices(c.ctx, after, first, nil, nil) +func (c *client) GetServices(after *string, first *int64) (*console.PagedClusterServiceIds, error) { + resp, err := c.consoleClient.PagedClusterServiceIds(c.ctx, after, first, nil, nil) if err != nil { return nil, err } if resp.GetPagedClusterServices() == nil { - return nil, fmt.Errorf("the response from PagedClusterServices is nil") + return nil, fmt.Errorf("the response from PagedClusterServiceIds is nil") } return resp, nil } @@ -27,6 +26,15 @@ func (c *client) GetService(id string) (*console.GetServiceDeploymentForAgent_Se return resp.ServiceDeployment, nil } +func (c *client) GetServiceDeploymentComponents(id string) (*console.GetServiceDeploymentComponents_ServiceDeployment, error) { + resp, err := c.consoleClient.GetServiceDeploymentComponents(c.ctx, id) + if err != nil { + return nil, err + } + + return resp.ServiceDeployment, nil +} + func (c *client) UpdateComponents(id, revisionID string, sha *string, components []*console.ComponentAttributes, errs []*console.ServiceErrorAttributes) error { _, err := c.consoleClient.UpdateServiceComponents(c.ctx, id, components, revisionID, sha, errs) return err diff --git a/pkg/controller/service/reconciler.go b/pkg/controller/service/reconciler.go index f59c002b..eee641c0 100644 --- a/pkg/controller/service/reconciler.go +++ b/pkg/controller/service/reconciler.go @@ -242,10 +242,10 @@ func (s *ServiceReconciler) ShutdownQueue() { s.svcQueue.ShutDown() } -func (s *ServiceReconciler) ListServices(ctx context.Context) *algorithms.Pager[*console.ServiceDeploymentEdgeFragment] { +func (s *ServiceReconciler) ListServices(ctx context.Context) *algorithms.Pager[*console.ServiceDeploymentIDEdgeFragment] { logger := log.FromContext(ctx) logger.Info("create service pager") - fetch := func(page *string, size int64) ([]*console.ServiceDeploymentEdgeFragment, *algorithms.PageInfo, error) { + fetch := func(page *string, size int64) ([]*console.ServiceDeploymentIDEdgeFragment, *algorithms.PageInfo, error) { resp, err := s.consoleClient.GetServices(page, &size) if err != nil { logger.Error(err, "failed to fetch service list from deployments service") @@ -258,7 +258,7 @@ func (s *ServiceReconciler) ListServices(ctx context.Context) *algorithms.Pager[ } return resp.PagedClusterServices.Edges, pageInfo, nil } - return algorithms.NewPager[*console.ServiceDeploymentEdgeFragment](common2.DefaultPageSize, fetch) + return algorithms.NewPager[*console.ServiceDeploymentIDEdgeFragment](common2.DefaultPageSize, fetch) } func (s *ServiceReconciler) Poll(ctx context.Context) error { diff --git a/pkg/test/mocks/Client_mock.go b/pkg/test/mocks/Client_mock.go index 3c55a177..637948a2 100644 --- a/pkg/test/mocks/Client_mock.go +++ b/pkg/test/mocks/Client_mock.go @@ -779,24 +779,82 @@ func (_c *ClientMock_GetService_Call) RunAndReturn(run func(string) (*client.Get return _c } +// GetServiceDeploymentComponents provides a mock function with given fields: id +func (_m *ClientMock) GetServiceDeploymentComponents(id string) (*client.GetServiceDeploymentComponents_ServiceDeployment, error) { + ret := _m.Called(id) + + if len(ret) == 0 { + panic("no return value specified for GetServiceDeploymentComponents") + } + + var r0 *client.GetServiceDeploymentComponents_ServiceDeployment + var r1 error + if rf, ok := ret.Get(0).(func(string) (*client.GetServiceDeploymentComponents_ServiceDeployment, error)); ok { + return rf(id) + } + if rf, ok := ret.Get(0).(func(string) *client.GetServiceDeploymentComponents_ServiceDeployment); ok { + r0 = rf(id) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*client.GetServiceDeploymentComponents_ServiceDeployment) + } + } + + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// ClientMock_GetServiceDeploymentComponents_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetServiceDeploymentComponents' +type ClientMock_GetServiceDeploymentComponents_Call struct { + *mock.Call +} + +// GetServiceDeploymentComponents is a helper method to define mock.On call +// - id string +func (_e *ClientMock_Expecter) GetServiceDeploymentComponents(id interface{}) *ClientMock_GetServiceDeploymentComponents_Call { + return &ClientMock_GetServiceDeploymentComponents_Call{Call: _e.mock.On("GetServiceDeploymentComponents", id)} +} + +func (_c *ClientMock_GetServiceDeploymentComponents_Call) Run(run func(id string)) *ClientMock_GetServiceDeploymentComponents_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *ClientMock_GetServiceDeploymentComponents_Call) Return(_a0 *client.GetServiceDeploymentComponents_ServiceDeployment, _a1 error) *ClientMock_GetServiceDeploymentComponents_Call { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *ClientMock_GetServiceDeploymentComponents_Call) RunAndReturn(run func(string) (*client.GetServiceDeploymentComponents_ServiceDeployment, error)) *ClientMock_GetServiceDeploymentComponents_Call { + _c.Call.Return(run) + return _c +} + // GetServices provides a mock function with given fields: after, first -func (_m *ClientMock) GetServices(after *string, first *int64) (*client.PagedClusterServices, error) { +func (_m *ClientMock) GetServices(after *string, first *int64) (*client.PagedClusterServiceIds, error) { ret := _m.Called(after, first) if len(ret) == 0 { panic("no return value specified for GetServices") } - var r0 *client.PagedClusterServices + var r0 *client.PagedClusterServiceIds var r1 error - if rf, ok := ret.Get(0).(func(*string, *int64) (*client.PagedClusterServices, error)); ok { + if rf, ok := ret.Get(0).(func(*string, *int64) (*client.PagedClusterServiceIds, error)); ok { return rf(after, first) } - if rf, ok := ret.Get(0).(func(*string, *int64) *client.PagedClusterServices); ok { + if rf, ok := ret.Get(0).(func(*string, *int64) *client.PagedClusterServiceIds); ok { r0 = rf(after, first) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*client.PagedClusterServices) + r0 = ret.Get(0).(*client.PagedClusterServiceIds) } } @@ -828,12 +886,12 @@ func (_c *ClientMock_GetServices_Call) Run(run func(after *string, first *int64) return _c } -func (_c *ClientMock_GetServices_Call) Return(_a0 *client.PagedClusterServices, _a1 error) *ClientMock_GetServices_Call { +func (_c *ClientMock_GetServices_Call) Return(_a0 *client.PagedClusterServiceIds, _a1 error) *ClientMock_GetServices_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *ClientMock_GetServices_Call) RunAndReturn(run func(*string, *int64) (*client.PagedClusterServices, error)) *ClientMock_GetServices_Call { +func (_c *ClientMock_GetServices_Call) RunAndReturn(run func(*string, *int64) (*client.PagedClusterServiceIds, error)) *ClientMock_GetServices_Call { _c.Call.Return(run) return _c }