-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathprovider.go
519 lines (445 loc) · 15.1 KB
/
provider.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
Copyright 2019 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package packet
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"github.com/packethost/packngo"
"github.com/kubermatic/machine-controller/pkg/apis/cluster/common"
"github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
cloudprovidererrors "github.com/kubermatic/machine-controller/pkg/cloudprovider/errors"
"github.com/kubermatic/machine-controller/pkg/cloudprovider/instance"
packettypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/packet/types"
cloudprovidertypes "github.com/kubermatic/machine-controller/pkg/cloudprovider/types"
"github.com/kubermatic/machine-controller/pkg/providerconfig"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
)
const (
machineUIDTag = "kubermatic-machine-controller:machine-uid"
defaultBillingCycle = "hourly"
)
// New returns a Packet provider
func New(configVarResolver *providerconfig.ConfigVarResolver) cloudprovidertypes.Provider {
return &provider{configVarResolver: configVarResolver}
}
type Config struct {
APIKey string
ProjectID string
BillingCycle string
InstanceType string
Facilities []string
Tags []string
}
// because we have both Config and RawConfig, we need to have func for each
// ideally, these would be merged into one
func (c *Config) populateDefaults() {
if c.BillingCycle == "" {
c.BillingCycle = defaultBillingCycle
}
}
func populateDefaults(c *packettypes.RawConfig) {
if c.BillingCycle.Value == "" {
c.BillingCycle.Value = defaultBillingCycle
}
}
type provider struct {
configVarResolver *providerconfig.ConfigVarResolver
}
func (p *provider) getConfig(s v1alpha1.ProviderSpec) (*Config, *packettypes.RawConfig, *providerconfigtypes.Config, error) {
if s.Value == nil {
return nil, nil, nil, fmt.Errorf("machine.spec.providerconfig.value is nil")
}
pconfig := providerconfigtypes.Config{}
err := json.Unmarshal(s.Value.Raw, &pconfig)
if err != nil {
return nil, nil, nil, err
}
rawConfig := packettypes.RawConfig{}
if err = json.Unmarshal(pconfig.CloudProviderSpec.Raw, &rawConfig); err != nil {
return nil, nil, nil, err
}
if pconfig.OperatingSystemSpec.Raw == nil {
return nil, nil, nil, errors.New("operatingSystemSpec in the MachineDeployment cannot be empty")
}
c := Config{}
c.APIKey, err = p.configVarResolver.GetConfigVarStringValueOrEnv(rawConfig.APIKey, "PACKET_API_KEY")
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get the value of \"apiKey\" field, error = %v", err)
}
c.ProjectID, err = p.configVarResolver.GetConfigVarStringValueOrEnv(rawConfig.ProjectID, "PACKET_PROJECT_ID")
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get the value of \"projectID\" field, error = %v", err)
}
c.InstanceType, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.InstanceType)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get the value of \"instanceType\" field, error = %v", err)
}
c.BillingCycle, err = p.configVarResolver.GetConfigVarStringValue(rawConfig.BillingCycle)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get the value of \"billingCycle\" field, error = %v", err)
}
for i, tag := range rawConfig.Tags {
tagValue, err := p.configVarResolver.GetConfigVarStringValue(tag)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to read the value for the Tag at index %d of the \"tags\" field, error = %v", i, err)
}
c.Tags = append(c.Tags, tagValue)
}
for i, facility := range rawConfig.Facilities {
facilityValue, err := p.configVarResolver.GetConfigVarStringValue(facility)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to read the value for the Tag at index %d of the \"facilities\" field, error = %v", i, err)
}
c.Facilities = append(c.Facilities, facilityValue)
}
// ensure we have defaults
c.populateDefaults()
return &c, &rawConfig, &pconfig, err
}
func (p *provider) getPacketDevice(machine *v1alpha1.Machine) (*packngo.Device, *packngo.Client, error) {
c, _, _, err := p.getConfig(machine.Spec.ProviderSpec)
if err != nil {
return nil, nil, cloudprovidererrors.TerminalError{
Reason: common.InvalidConfigurationMachineError,
Message: fmt.Sprintf("Failed to parse MachineSpec, due to %v", err),
}
}
client := getClient(c.APIKey)
device, err := getDeviceByTag(client, c.ProjectID, generateTag(string(machine.UID)))
if err != nil {
return nil, nil, err
}
return device, client, nil
}
func (p *provider) Validate(spec v1alpha1.MachineSpec) error {
c, _, pc, err := p.getConfig(spec.ProviderSpec)
if err != nil {
return fmt.Errorf("failed to parse config: %v", err)
}
if c.APIKey == "" {
return errors.New("apiKey is missing")
}
if c.InstanceType == "" {
return errors.New("instanceType is missing")
}
if c.ProjectID == "" {
return errors.New("projectID is missing")
}
_, err = getNameForOS(pc.OperatingSystem)
if err != nil {
return fmt.Errorf("invalid/not supported operating system specified %q: %v", pc.OperatingSystem, err)
}
client := getClient(c.APIKey)
if len(c.Facilities) == 0 || c.Facilities[0] == "" {
return fmt.Errorf("must have at least one non-blank facility")
}
// get all valid facilities
facilities, _, err := client.Facilities.List(nil)
if err != nil {
return fmt.Errorf("failed to list facilities: %v", err)
}
// ensure our requested facilities are in those facilities
if missingFacilities := itemsNotInList(facilityProp(facilities, "Code"), c.Facilities); len(missingFacilities) > 0 {
return fmt.Errorf("unknown facilities: %s", strings.Join(missingFacilities, ","))
}
// get all valid plans a.k.a. instance types
plans, _, err := client.Plans.List(nil)
if err != nil {
return fmt.Errorf("failed to list instance types / plans: %v", err)
}
// ensure our requested plan is in those plans
validPlanNames := planProp(plans, "Name")
if missingPlans := itemsNotInList(validPlanNames, []string{c.InstanceType}); len(missingPlans) > 0 {
return fmt.Errorf("unknown instance type / plan: %s, acceptable plans: %s", strings.Join(missingPlans, ","), strings.Join(validPlanNames, ","))
}
return nil
}
func (p *provider) Create(machine *v1alpha1.Machine, _ *cloudprovidertypes.ProviderData, userdata string) (instance.Instance, error) {
c, _, pc, err := p.getConfig(machine.Spec.ProviderSpec)
if err != nil {
return nil, cloudprovidererrors.TerminalError{
Reason: common.InvalidConfigurationMachineError,
Message: fmt.Sprintf("Failed to parse MachineSpec, due to %v", err),
}
}
client := getClient(c.APIKey)
imageName, err := getNameForOS(pc.OperatingSystem)
if err != nil {
return nil, cloudprovidererrors.TerminalError{
Reason: common.InvalidConfigurationMachineError,
Message: fmt.Sprintf("Invalid operating system specified %q, details = %v", pc.OperatingSystem, err),
}
}
serverCreateOpts := &packngo.DeviceCreateRequest{
Hostname: machine.Spec.Name,
UserData: userdata,
ProjectID: c.ProjectID,
Facility: c.Facilities,
BillingCycle: c.BillingCycle,
Plan: c.InstanceType,
OS: imageName,
Tags: []string{
generateTag(string(machine.UID)),
},
}
device, res, err := client.Devices.Create(serverCreateOpts)
if err != nil {
return nil, packetErrorToTerminalError(err, res, "failed to create server")
}
return &packetDevice{device: device}, nil
}
func (p *provider) Cleanup(machine *v1alpha1.Machine, data *cloudprovidertypes.ProviderData) (bool, error) {
instance, err := p.Get(machine, data)
if err != nil {
if err == cloudprovidererrors.ErrInstanceNotFound {
return true, nil
}
return false, err
}
c, _, _, err := p.getConfig(machine.Spec.ProviderSpec)
if err != nil {
return false, cloudprovidererrors.TerminalError{
Reason: common.InvalidConfigurationMachineError,
Message: fmt.Sprintf("Failed to parse MachineSpec, due to %v", err),
}
}
client := getClient(c.APIKey)
res, err := client.Devices.Delete(instance.(*packetDevice).device.ID)
if err != nil {
return false, packetErrorToTerminalError(err, res, "failed to delete the server")
}
return false, nil
}
func (p *provider) AddDefaults(spec v1alpha1.MachineSpec) (v1alpha1.MachineSpec, error) {
_, rawConfig, _, err := p.getConfig(spec.ProviderSpec)
if err != nil {
return spec, err
}
populateDefaults(rawConfig)
spec.ProviderSpec.Value, err = setProviderSpec(*rawConfig, spec.ProviderSpec)
if err != nil {
return spec, err
}
return spec, nil
}
func (p *provider) Get(machine *v1alpha1.Machine, _ *cloudprovidertypes.ProviderData) (instance.Instance, error) {
device, _, err := p.getPacketDevice(machine)
if err != nil {
return nil, err
}
if device != nil {
return &packetDevice{device: device}, nil
}
return nil, cloudprovidererrors.ErrInstanceNotFound
}
func (p *provider) MigrateUID(machine *v1alpha1.Machine, newID types.UID) error {
device, client, err := p.getPacketDevice(machine)
if err != nil {
return err
}
if device == nil {
klog.Infof("No instance exists for machine %s", machine.Name)
return nil
}
// go through existing labels, make sure that no other UID label exists
tags := make([]string, 0)
for _, t := range device.Tags {
// filter out old UID tag(s)
if _, err := getTagUID(t); err != nil {
tags = append(tags, t)
}
}
// create a new UID label
tags = append(tags, generateTag(string(newID)))
klog.Infof("Setting UID label for machine %s", machine.Name)
dur := &packngo.DeviceUpdateRequest{
Tags: &tags,
}
_, response, err := client.Devices.Update(device.ID, dur)
if err != nil {
return packetErrorToTerminalError(err, response, "failed to update UID label")
}
klog.Infof("Successfully set UID label for machine %s", machine.Name)
return nil
}
func (p *provider) GetCloudConfig(spec v1alpha1.MachineSpec) (config string, name string, err error) {
return "", "", nil
}
func (p *provider) MachineMetricsLabels(machine *v1alpha1.Machine) (map[string]string, error) {
labels := make(map[string]string)
c, _, _, err := p.getConfig(machine.Spec.ProviderSpec)
if err == nil {
labels["size"] = c.InstanceType
labels["facilities"] = strings.Join(c.Facilities, ",")
}
return labels, err
}
func (p *provider) SetMetricsForMachines(machines v1alpha1.MachineList) error {
return nil
}
type packetDevice struct {
device *packngo.Device
}
func (s *packetDevice) Name() string {
return s.device.Hostname
}
func (s *packetDevice) ID() string {
return s.device.ID
}
func (s *packetDevice) Addresses() map[string]v1.NodeAddressType {
// returns addresses in CIDR format
addresses := map[string]v1.NodeAddressType{}
for _, ip := range s.device.Network {
if ip.Public {
addresses[ip.Address] = v1.NodeExternalIP
continue
}
addresses[ip.Address] = v1.NodeInternalIP
}
return addresses
}
func (s *packetDevice) Status() instance.Status {
switch s.device.State {
case "provisioning":
return instance.StatusCreating
case "active":
return instance.StatusRunning
default:
return instance.StatusUnknown
}
}
/******
CONVENIENCE INTERNAL FUNCTIONS
******/
func setProviderSpec(rawConfig packettypes.RawConfig, s v1alpha1.ProviderSpec) (*runtime.RawExtension, error) {
if s.Value == nil {
return nil, fmt.Errorf("machine.spec.providerconfig.value is nil")
}
pconfig := providerconfigtypes.Config{}
err := json.Unmarshal(s.Value.Raw, &pconfig)
if err != nil {
return nil, err
}
rawCloudProviderSpec, err := json.Marshal(rawConfig)
if err != nil {
return nil, err
}
pconfig.CloudProviderSpec = runtime.RawExtension{Raw: rawCloudProviderSpec}
rawPconfig, err := json.Marshal(pconfig)
if err != nil {
return nil, err
}
return &runtime.RawExtension{Raw: rawPconfig}, nil
}
func getDeviceByTag(client *packngo.Client, projectID, tag string) (*packngo.Device, error) {
devices, response, err := client.Devices.List(projectID, nil)
if err != nil {
return nil, packetErrorToTerminalError(err, response, "failed to list devices")
}
for _, device := range devices {
if itemInList(device.Tags, tag) {
return &device, nil
}
}
return nil, nil
}
// given a defined Kubermatic constant for an operating system, return the canonical slug for Packet
func getNameForOS(os providerconfigtypes.OperatingSystem) (string, error) {
switch os {
case providerconfigtypes.OperatingSystemUbuntu:
return "ubuntu_20_04", nil
case providerconfigtypes.OperatingSystemCentOS:
return "centos_7", nil
case providerconfigtypes.OperatingSystemFlatcar:
return "flatcar_stable", nil
}
return "", providerconfigtypes.ErrOSNotSupported
}
func getClient(apiKey string) *packngo.Client {
return packngo.NewClientWithAuth("kubermatic", apiKey, nil)
}
func generateTag(ID string) string {
return fmt.Sprintf("%s:%s", machineUIDTag, ID)
}
func getTagUID(tag string) (string, error) {
parts := strings.Split(tag, ":")
if len(parts) < 2 || parts[0] != machineUIDTag {
return "", fmt.Errorf("not a machine UID tag")
}
return parts[1], nil
}
// packetErrorToTerminalError judges if the given error
// can be qualified as a "terminal" error, for more info see v1alpha1.MachineStatus
//
// if the given error doesn't qualify the error passed as an argument will be returned
func packetErrorToTerminalError(err error, response *packngo.Response, msg string) error {
prepareAndReturnError := func() error {
return fmt.Errorf("%s, due to %s", msg, err)
}
if err != nil {
if response != nil && response.Response != nil && response.Response.StatusCode == 403 {
// authorization primitives come from MachineSpec
// thus we are setting InvalidConfigurationMachineError
return cloudprovidererrors.TerminalError{
Reason: common.InvalidConfigurationMachineError,
Message: "A request has been rejected due to invalid credentials which were taken from the MachineSpec",
}
}
return prepareAndReturnError()
}
return err
}
func itemInList(list []string, item string) bool {
for _, elm := range list {
if elm == item {
return true
}
}
return false
}
func itemsNotInList(list, items []string) []string {
listMap := make(map[string]bool)
missing := make([]string, 0)
for _, item := range list {
listMap[item] = true
}
for _, item := range items {
if _, ok := listMap[item]; !ok {
missing = append(missing, item)
}
}
return missing
}
func facilityProp(vs []packngo.Facility, field string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
val := reflect.ValueOf(v)
vsm[i] = val.FieldByName(field).String()
}
return vsm
}
func planProp(vs []packngo.Plan, field string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
val := reflect.ValueOf(v)
vsm[i] = val.FieldByName(field).String()
}
return vsm
}