-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_region.go
84 lines (71 loc) · 1.72 KB
/
provider_region.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
package yeoman
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"github.com/thejerf/suture/v4"
)
// providerRegion manages a single provider region, such as gcp:us-central1, and
// all the zones within it.
type providerRegion struct {
log *slog.Logger
name string
server *Server
zones []*zone
// Multiple services creating IPs simultaneously results in conflicts.
// We need to lock access while fetching and creating IPs.
ipStore IPStore
ipStoreMu sync.Mutex
// supervisor to manage services.
supervisor *suture.Supervisor
}
func newProviderRegion(
ctx context.Context,
name string,
ipStore IPStore,
zoneStores map[string]VMStore,
s *Server,
) (*providerRegion, error) {
supervisor := suture.New("server", suture.Spec{
EventHook: func(ev suture.Event) {
s.log.Error("event hook",
slog.String("error", ev.String()))
},
})
parts := strings.Split(string(name), ":")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid cloud provider: %s", name)
}
var (
providerName = parts[0]
region = parts[2]
)
providerRegionLog := s.log.With(
slog.String("provider", providerName),
slog.String("region", region))
p := &providerRegion{
name: name,
log: providerRegionLog,
ipStore: ipStore,
supervisor: supervisor,
server: s,
}
for zoneName, vmStore := range zoneStores {
z, err := newZone(ctx, zoneName, p, vmStore)
if err != nil {
return nil, fmt.Errorf("new zone: %w", err)
}
_ = p.supervisor.Add(z)
p.zones = append(p.zones, z)
}
return p, nil
}
func (p *providerRegion) Serve(ctx context.Context) error {
p.log.Info("serving provider region")
if err := p.supervisor.Serve(ctx); err != nil {
return fmt.Errorf("serve: %w", err)
}
return nil
}