Skip to content

Commit 3c5892b

Browse files
committed
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.
1 parent dc67826 commit 3c5892b

5 files changed

Lines changed: 14 additions & 21 deletions

File tree

pkg/plugin/example/source/example.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ type ExampleSource struct {
2525
// Fetch is what gets run by aether, it should return a list of instances with
2626
// the metrics attached to them (CPU, Memory, Networking, Storage)
2727
// Your business logic should be handeled here
28-
func (e *ExampleSource) Fetch(ctx context.Context) ([]*v1.Instance, error) {
28+
func (e *ExampleSource) Fetch(ctx context.Context) (map[string]*v1.Instance, error) {
2929
// business logic here
30-
return []*v1.Instance{
31-
{
30+
return map[string]*v1.Instance{
31+
"example": {
3232
ID: "123456789",
3333
Provider: v1.Custom,
3434
Service: "On-Prem",

pkg/providers/aws/source.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Sources(ctx context.Context, cfg *config.Provider) []v1.Source {
4747

4848
// Fetch returns a slice of instances, this is to adhere to the sources
4949
// interface
50-
func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) {
50+
func (s *Source) Fetch(ctx context.Context) (map[string]*v1.Instance, error) {
5151
if s.Region == "" {
5252
return nil, errors.New("no region set")
5353
}
@@ -64,12 +64,7 @@ func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) {
6464
return nil, fmt.Errorf("failed getting instance metrics: %v", err)
6565
}
6666

67-
var instances []*v1.Instance
68-
for _, instance := range s.Client.instancesMap {
69-
instances = append(instances, instance)
70-
}
71-
72-
return instances, nil
67+
return s.Client.instancesMap, nil
7368
}
7469

7570
// Stop is used to gracefully shutdown a source

pkg/providers/gcp/source.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Sources(ctx context.Context, cfg *config.Provider) []v1.Source {
4949

5050
// Fetch returns a slice of instances, this is to adhere to the sources
5151
// interface
52-
func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) {
52+
func (s *Source) Fetch(ctx context.Context) (map[string]*v1.Instance, error) {
5353
if s.Project == nil {
5454
return nil, errors.New("no project set")
5555
}
@@ -66,18 +66,15 @@ func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) {
6666
return nil, fmt.Errorf("failed getting instance metrics: %v", err)
6767
}
6868

69-
var instances []*v1.Instance
7069
for k, instance := range s.Client.instancesMap {
71-
instances = append(instances, instance)
72-
7370
// remove terminated instances as we
7471
// shouldnt use them anymore
7572
if instance.Status == v1.InstanceTerminated {
7673
delete(s.Client.instancesMap, k)
7774
}
7875
}
7976

80-
return instances, nil
77+
return s.Client.instancesMap, nil
8178
}
8279

8380
// Stop is used to gracefully shutdown a source

pkg/source/manager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ func (m *Manager) Fetch(ctx context.Context) {
105105

106106
// publishInstances is a helper that publishes each instance in a slice on the
107107
// bus under the MetricsCollectedEvent
108-
func (m *Manager) publishInstances(instances []*v1.Instance) error {
109-
for i := range instances {
108+
func (m *Manager) publishInstances(instances map[string]*v1.Instance) error {
109+
for _, i := range instances {
110110
err := m.bus.Publish(&bus.Event{
111111
Type: v1.MetricsCollectedEvent,
112-
Data: *instances[i],
112+
Data: *i,
113113
})
114114
if err != nil {
115115
return err

pkg/types/v1/source.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ type Source interface {
77
// Stop is the "teardown" that will be used for graceful shutdown
88
Stop(context.Context) error
99

10-
// Fetch is the business logic that should return a list of instances
11-
// that have metrics attached to them mainly cpu, memory, storage and network
12-
Fetch(context.Context) ([]*Instance, error)
10+
// Fetch is the business logic that should return a map of instances
11+
// with a unique instanceID key, and value of instance that have
12+
// metrics attached to them mainly cpu, memory, storage and network
13+
Fetch(context.Context) (map[string]*Instance, error)
1314
}

0 commit comments

Comments
 (0)