-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticip_cloudscale.go
207 lines (161 loc) · 4.52 KB
/
elasticip_cloudscale.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/sirupsen/logrus"
"github.com/cloudscale-ch/cloudscale-go-sdk/v5"
"github.com/gofrs/uuid"
)
type cloudscaleNotifyConfig struct {
Endpoint *textURL `yaml:"endpoint"`
Token string `yaml:"token"`
ServerUUID uuid.UUID `yaml:"server-uuid"`
HostnameToServerUUID map[string]uuid.UUID `yaml:"hostname-to-server-uuid"`
}
func (cfg cloudscaleNotifyConfig) findServerUUID(hostname string) (uuid.UUID, error) {
if cfg.ServerUUID != uuid.Nil {
// Directly specified in config
return cfg.ServerUUID, nil
}
if serverUUID, ok := cfg.HostnameToServerUUID[hostname]; ok && serverUUID != uuid.Nil {
// Found using hostname
return serverUUID, nil
}
md, err := findCloudscaleServerMetadata()
if err != nil {
return uuid.Nil, fmt.Errorf("Failed to retrieve Cloudscale server metadata: %s", err)
}
if md.Meta.CloudscaleUUID != nil {
return *md.Meta.CloudscaleUUID, nil
}
return uuid.Nil, fmt.Errorf("Server UUID not found with hostname %q", hostname)
}
func (cfg cloudscaleNotifyConfig) NewProvider() (elasticIPProvider, error) {
if len(cfg.Token) < 1 {
return nil, fmt.Errorf("Authentication token required")
}
httpClient := &http.Client{
Timeout: 1 * time.Minute,
}
client := cloudscale.NewClient(httpClient)
client.UserAgent = newVersionInfo().HTTPUserAgent()
client.AuthToken = cfg.Token
if cfg.Endpoint != nil {
// Make copy to prevent modifications
baseURL := url.URL(cfg.Endpoint.URL)
client.BaseURL = &baseURL
}
hostname, err := os.Hostname()
if err != nil {
return nil, fmt.Errorf("Retrieving hostname: %s", err)
}
logrus.Debugf("Hostname %q", hostname)
serverUUID, err := cfg.findServerUUID(hostname)
if err != nil {
return nil, err
}
switch serverUUID.Variant() {
case uuid.VariantRFC4122, uuid.VariantMicrosoft:
break
default:
return nil, fmt.Errorf("Invalid UUID %q", serverUUID)
}
logrus.WithField("server-uuid", serverUUID).Debug("Server UUID")
return &cloudscaleFloatingIPProvider{
serverUUID: serverUUID.String(),
httpClient: httpClient,
client: client,
}, nil
}
type cloudscaleFloatingIPProvider struct {
serverUUID string
httpClient *http.Client
client *cloudscale.Client
}
func (p *cloudscaleFloatingIPProvider) Test(ctx context.Context) error {
var errServer, errFloatingIP error
var server *cloudscale.Server
var floatingIPs []cloudscale.FloatingIP
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
server, errServer = p.client.Servers.Get(ctx, p.serverUUID)
}()
go func() {
defer wg.Done()
floatingIPs, errFloatingIP = p.client.FloatingIPs.List(ctx)
}()
wg.Wait()
fields := logrus.Fields{}
success := true
if errServer == nil {
fields["server"] = server
} else {
success = false
logrus.Errorf("Retrieving server %q: %s", p.serverUUID, errServer)
}
if errFloatingIP == nil {
fields["floating-ips"] = floatingIPs
} else {
success = false
logrus.Errorf("Listing floating IPs failed: %s", errFloatingIP)
}
logger := logrus.WithFields(fields)
if success {
logger.Info("Test successful")
return nil
}
logger.Error("Test failed")
return errors.New("Self-test failed")
}
func (p *cloudscaleFloatingIPProvider) NewElasticIPRefresher(logger *logrus.Entry,
network netAddress) (elasticIPRefresher, error) {
return &cloudscaleFloatingIPRefresher{
provider: p,
network: network,
logger: logger,
}, nil
}
type cloudscaleFloatingIPRefresher struct {
provider *cloudscaleFloatingIPProvider
client *cloudscale.Client
network netAddress
logger *logrus.Entry
}
func (r *cloudscaleFloatingIPRefresher) String() string {
return r.network.String()
}
func (r *cloudscaleFloatingIPRefresher) Logger() *logrus.Entry {
return r.logger
}
func (r *cloudscaleFloatingIPRefresher) Refresh(ctx context.Context) error {
serverUUID := r.provider.serverUUID
ip := r.network.IP.String()
client := r.provider.client
r.logger.Infof("Set next-hop of address %s to server %s", ip, serverUUID)
req := &cloudscale.FloatingIPUpdateRequest{
Server: serverUUID,
}
err := client.FloatingIPs.Update(ctx, ip, req)
if err != nil {
r.logger.Errorf("Setting next-hop of address %s to server %s failed: %s",
ip, serverUUID, err)
if apiError, ok := err.(*cloudscale.ErrorResponse); ok {
if apiError.StatusCode >= 400 && apiError.StatusCode < 500 {
// Client error
return backoff.Permanent(apiError)
}
}
return err
}
r.logger.Debug("Refresh successful")
return nil
}