/
source.go
87 lines (69 loc) · 1.91 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gcp
import (
"context"
"errors"
"fmt"
"time"
"github.com/re-cinq/aether/pkg/config"
v1 "github.com/re-cinq/aether/pkg/types/v1"
)
// Source is a configured google source that adheres to Aethers source
// interface
type Source struct {
// Google Client
*Client
// The Project that Client is setup for
Project *string
// Teardown functionality
Shutdown func()
}
// Sources instantiates a slice of instances of the Google Sources configured
// for use, one per project configured
func Sources(ctx context.Context, cfg *config.Provider) []v1.Source {
var sources []v1.Source
// we instantiate a source per project
for index := range cfg.Accounts {
account := cfg.Accounts[index]
c, shutdown, err := New(ctx, &account)
if err != nil {
return nil
}
sources = append(sources, &Source{
Project: &account.Project,
Client: c,
Shutdown: shutdown,
})
}
return sources
}
// Fetch returns a slice of instances, this is to adhere to the sources
// interface
func (s *Source) Fetch(ctx context.Context) ([]*v1.Instance, error) {
if s.Project == nil {
return nil, errors.New("no project set")
}
s.Client.Refresh(ctx, *s.Project)
interval := config.AppConfig().Interval
if interval < 5*time.Minute {
return nil, fmt.Errorf("error interval for GCP needs to be atleast 5m. It is: %+v", interval)
}
err := s.Client.GetMetricsForInstances(ctx, *s.Project, interval.String())
if err != nil {
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 {
delete(s.Client.instancesMap, k)
}
}
return instances, nil
}
// Stop is used to gracefully shutdown a source
func (s *Source) Stop(ctx context.Context) error {
s.Shutdown()
return nil
}