From 3ad14266db99d7a958e76c31b57dff544b5760b7 Mon Sep 17 00:00:00 2001 From: Gabi Beyer Date: Wed, 15 May 2024 10:31:41 +0200 Subject: [PATCH] Refactor: Change Fetch signature Have the source Fetch functionality return a map of instances instead of a slice. The metric collection functionality writes to a map, so using a slice was adding an extra loop in order to return the correct type. --- pkg/plugin/example/source/example.go | 6 +++--- pkg/providers/aws/source.go | 9 ++------- pkg/providers/gcp/source.go | 7 ++----- pkg/source/manager.go | 6 +++--- pkg/types/v1/source.go | 7 ++++--- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/pkg/plugin/example/source/example.go b/pkg/plugin/example/source/example.go index 1596a3c..5cb7d8a 100644 --- a/pkg/plugin/example/source/example.go +++ b/pkg/plugin/example/source/example.go @@ -25,10 +25,10 @@ type ExampleSource struct { // Fetch is what gets run by aether, it should return a list of instances with // the metrics attached to them (CPU, Memory, Networking, Storage) // Your business logic should be handeled here -func (e *ExampleSource) Fetch(ctx context.Context) ([]*v1.Instance, error) { +func (e *ExampleSource) Fetch(ctx context.Context) (map[string]*v1.Instance, error) { // business logic here - return []*v1.Instance{ - { + return map[string]*v1.Instance{ + "example": { ID: "123456789", Provider: v1.Custom, Service: "On-Prem", diff --git a/pkg/providers/aws/source.go b/pkg/providers/aws/source.go index 8921d6a..95bbcf2 100644 --- a/pkg/providers/aws/source.go +++ b/pkg/providers/aws/source.go @@ -47,7 +47,7 @@ func Sources(ctx context.Context, cfg *config.Provider) []v1.Source { // Fetch returns a slice of instances, this is to adhere to the sources // interface -func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) { +func (s *Source) Fetch(ctx context.Context) (map[string]*v1.Instance, error) { if s.Region == "" { return nil, errors.New("no region set") } @@ -64,12 +64,7 @@ func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) { return nil, fmt.Errorf("failed getting instance metrics: %v", err) } - var instances []*v1.Instance - for _, instance := range s.Client.instancesMap { - instances = append(instances, instance) - } - - return instances, nil + return s.Client.instancesMap, nil } // Stop is used to gracefully shutdown a source diff --git a/pkg/providers/gcp/source.go b/pkg/providers/gcp/source.go index b500cef..a96ac96 100644 --- a/pkg/providers/gcp/source.go +++ b/pkg/providers/gcp/source.go @@ -49,7 +49,7 @@ func Sources(ctx context.Context, cfg *config.Provider) []v1.Source { // Fetch returns a slice of instances, this is to adhere to the sources // interface -func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) { +func (s *Source) Fetch(ctx context.Context) (map[string]*v1.Instance, error) { if s.Project == nil { return nil, errors.New("no project set") } @@ -66,10 +66,7 @@ func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) { return nil, fmt.Errorf("failed getting instance metrics: %v", err) } - var instances []*v1.Instance for k, instance := range s.Client.instancesMap { - instances = append(instances, instance) - // remove terminated instances as we // shouldnt use them anymore if instance.Status == v1.InstanceTerminated { @@ -77,7 +74,7 @@ func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) { } } - return instances, nil + return s.Client.instancesMap, nil } // Stop is used to gracefully shutdown a source diff --git a/pkg/source/manager.go b/pkg/source/manager.go index 1140cca..76e24f6 100644 --- a/pkg/source/manager.go +++ b/pkg/source/manager.go @@ -105,11 +105,11 @@ func (m *Manager) Fetch(ctx context.Context) { // publishInstances is a helper that publishes each instance in a slice on the // bus under the MetricsCollectedEvent -func (m *Manager) publishInstances(instances []*v1.Instance) error { - for i := range instances { +func (m *Manager) publishInstances(instances map[string]*v1.Instance) error { + for _, i := range instances { err := m.bus.Publish(&bus.Event{ Type: v1.MetricsCollectedEvent, - Data: *instances[i], + Data: *i, }) if err != nil { return err diff --git a/pkg/types/v1/source.go b/pkg/types/v1/source.go index 6694125..d624717 100644 --- a/pkg/types/v1/source.go +++ b/pkg/types/v1/source.go @@ -7,7 +7,8 @@ type Source interface { // Stop is the "teardown" that will be used for graceful shutdown Stop(context.Context) error - // Fetch is the business logic that should return a list of instances - // that have metrics attached to them mainly cpu, memory, storage and network - Fetch(context.Context) ([]*Instance, error) + // Fetch is the business logic that should return a map of instances + // with a unique instanceID key, and value of instance that have + // metrics attached to them mainly cpu, memory, storage and network + Fetch(context.Context) (map[string]*Instance, error) }