-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticip_exoscale.go
232 lines (191 loc) · 5.42 KB
/
elasticip_exoscale.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"context"
"fmt"
"time"
egoscale "github.com/exoscale/egoscale/v2"
"github.com/exoscale/exoip"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
)
const (
defaultExoscaleEndpoint = "https://api.exoscale.ch/compute"
)
func findExoscaleZone() (string, error) {
var zone string
fn := func() error {
mserver, err := exoip.FindMetadataServer()
if err != nil {
return err
}
logrus.Debugf("Metadata server %q", mserver)
zone, err = exoip.FetchMetadata(mserver, "/latest/availability-zone")
return err
}
if err := metadataRetry(fn); err != nil {
return "", err
}
return zone, nil
}
func findExoscaleInstanceID() (string, error) {
var instanceID string
fn := func() error {
mserver, err := exoip.FindMetadataServer()
if err != nil {
return err
}
logrus.Debugf("Metadata server %q", mserver)
instanceID, err = exoip.FetchMetadata(mserver, "/latest/instance-id")
return err
}
if err := metadataRetry(fn); err != nil {
return "", err
}
return instanceID, nil
}
type exoscaleNotifyConfig struct {
Endpoint *textURL `yaml:"endpoint"`
Zone string `yaml:"zone"`
Key string `yaml:"key"`
Secret string `yaml:"secret"`
InstanceID string `yaml:"instance-id"`
}
func (c exoscaleNotifyConfig) NewProvider() (elasticIPProvider, error) {
var err error
if len(c.Key) < 1 {
return nil, fmt.Errorf("Authentication key required")
}
if len(c.Secret) < 1 {
return nil, fmt.Errorf("Authentication secret required")
}
zone := c.Zone
if zone == "" {
if zone, err = findExoscaleZone(); err != nil {
return nil, fmt.Errorf("Exoscale zone lookup: %s", err)
}
}
logrus.WithField("zone", zone).Debug("Exoscale zone")
instanceID := c.InstanceID
if c.InstanceID == "" {
if instanceID, err = findExoscaleInstanceID(); err != nil {
return nil, fmt.Errorf("Instance ID lookup: %s", err)
}
}
logrus.WithField("instance-id", instanceID).Debug("Instance ID")
timeoutOpt := egoscale.ClientOptWithTimeout(1 * time.Minute)
client, err := egoscale.NewClient(c.Key, c.Secret, timeoutOpt)
if err != nil {
return nil, err
}
vm, err := client.GetInstance(context.Background(), zone, instanceID)
if err != nil {
return nil, err
}
return &exoscaleElasticIPProvider{
client: client,
zone: zone,
instance: vm,
}, nil
}
type exoscaleElasticIPProvider struct {
client *egoscale.Client
zone string
instance *egoscale.Instance
}
func (p *exoscaleElasticIPProvider) Test(ctx context.Context) error {
// Check that we can list EIPs and instances
eips, err := p.client.ListElasticIPs(ctx, p.zone)
if err != nil {
return err
}
elasticIPs := []string{}
for _, eip := range eips {
elasticIPs = append(elasticIPs, eip.IPAddress.String())
}
logrus.WithField("eips", elasticIPs).Debug("Got elastic IPs")
vms, err := p.client.ListInstances(ctx, p.zone)
if err != nil {
return err
}
instances := []string{}
for _, vm := range vms {
instances = append(instances, *vm.ID)
}
logrus.WithField("instances", instances).Debug("Got instances")
return nil
}
func (p *exoscaleElasticIPProvider) NewElasticIPRefresher(logger *logrus.Entry,
network netAddress) (elasticIPRefresher, error) {
eips, err := p.client.ListElasticIPs(context.Background(), p.zone)
if err != nil {
return nil, fmt.Errorf("Elastic IP lookup: %s", err)
}
for _, eip := range eips {
logrus.WithField("eip", eip.IPAddress).Debug("Checking EIP")
if network.Contains(*eip.IPAddress) {
return &exoscaleElasticIPRefresher{
logger: logger,
network: network,
client: p.client,
eip: eip,
instance: p.instance,
zone: p.zone,
}, nil
}
}
return nil, fmt.Errorf("Unable to find elastic IP for %s", network)
}
type exoscaleElasticIPRefresher struct {
network netAddress
logger *logrus.Entry
client *egoscale.Client
eip *egoscale.ElasticIP
instance *egoscale.Instance
zone string
}
func (r *exoscaleElasticIPRefresher) String() string {
return r.network.String()
}
func (r *exoscaleElasticIPRefresher) Logger() *logrus.Entry {
return r.logger
}
func (r *exoscaleElasticIPRefresher) Refresh(ctx context.Context) error {
err := r.client.AttachInstanceToElasticIP(ctx, r.zone, r.instance, r.eip)
if err != nil {
return fmt.Errorf("while attaching the IP to this instance: %s", err)
}
logrus.Infof("Ensured that %s is attached to instance %s", r.eip.IPAddress, *r.instance.ID)
vms, err := r.client.ListInstances(ctx, r.zone)
if err != nil {
return fmt.Errorf("Unable to list instances: %s", err)
}
// Detach from other instances
var detacherrs error
for _, vm := range vms {
if *vm.ID == *r.instance.ID {
continue
}
// NOTE(sg): the response from `ListInstances()` doesn't
// contain the attached EIPs. Because of that we need to fetch
// the instance details with `GetInstance()` in order to be
// able to detach EIPs from other instances.
vmdetails, err := r.client.GetInstance(ctx, r.zone, *vm.ID)
if err != nil {
detacherrs = multierr.Append(detacherrs, err)
continue
}
if vmdetails.ElasticIPIDs != nil {
logrus.Debugf("Checking if we need to detach EIPs from %s", *vm.ID)
for _, eip := range *vmdetails.ElasticIPIDs {
if eip == *r.eip.ID {
logrus.Infof("Detaching EIP %s from %s", r.eip.IPAddress, *vm.ID)
err := r.client.DetachInstanceFromElasticIP(ctx, r.zone, vm, r.eip)
if err != nil {
detacherrs = multierr.Append(detacherrs, err)
}
}
}
}
}
return detacherrs
}