-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
cloud.go
940 lines (800 loc) · 31.3 KB
/
cloud.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/*
Copyright 2019 The Kubernetes 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 openstack
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/blang/semver/v4"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
cinder "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
az "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/dns/v2/recordsets"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/apiversions"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
v2pools "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
l3floatingip "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
sg "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
sgr "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/dnsprovider/pkg/dnsprovider/providers/openstack/designate"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
)
const (
TagNameEtcdClusterPrefix = "k8s.io/etcd/"
TagNameRolePrefix = "k8s.io/role/"
TagClusterName = "KubernetesCluster"
TagRoleControlPlane = "control-plane"
TagRoleMaster = "master"
TagKopsInstanceGroup = "KopsInstanceGroup"
TagKopsNetwork = "KopsNetwork"
TagKopsName = "KopsName"
TagKopsRole = "KopsRole"
ResourceTypePort = "ports"
ResourceTypeNetwork = "networks"
ResourceTypeSubnet = "subnets"
)
// readBackoff is the backoff strategy for openstack read retries.
var readBackoff = wait.Backoff{
Duration: time.Second,
Factor: 1.5,
Jitter: 0.1,
Steps: 10,
}
// writeBackoff is the backoff strategy for openstack write retries.
var writeBackoff = wait.Backoff{
Duration: time.Second,
Factor: 2,
Jitter: 0.1,
Steps: 5,
}
// deleteBackoff is the backoff strategy for openstack delete retries.
var deleteBackoff = wait.Backoff{
Duration: time.Second,
Factor: 5,
Jitter: 0.1,
Steps: 4,
}
type OpenstackCloud interface {
fi.Cloud
ComputeClient() *gophercloud.ServiceClient
BlockStorageClient() *gophercloud.ServiceClient
NetworkingClient() *gophercloud.ServiceClient
LoadBalancerClient() *gophercloud.ServiceClient
DNSClient() *gophercloud.ServiceClient
ImageClient() *gophercloud.ServiceClient
UseOctavia() bool
UseZones([]string)
// GetInstance will return a openstack server provided its ID
GetInstance(id string) (*servers.Server, error)
// ListInstances will return a slice of openstack servers provided list opts
ListInstances(servers.ListOptsBuilder) ([]servers.Server, error)
// CreateInstance will create an openstack server provided create opts
CreateInstance(servers.CreateOptsBuilder, string) (*servers.Server, error)
// DeleteInstanceWithID will delete instance
DeleteInstanceWithID(instanceID string) error
// SetVolumeTags will set the tags for the Cinder volume
SetVolumeTags(id string, tags map[string]string) error
// GetCloudTags will return the tags attached on cloud
GetCloudTags() map[string]string
// ListVolumes will return the Cinder volumes which match the options
ListVolumes(opt cinder.ListOptsBuilder) ([]cinder.Volume, error)
// CreateVolume will create a new Cinder Volume
CreateVolume(opt cinder.CreateOptsBuilder) (*cinder.Volume, error)
AttachVolume(serverID string, opt volumeattach.CreateOpts) (*volumeattach.VolumeAttachment, error)
// DeleteVolume will delete volume
DeleteVolume(volumeID string) error
// ListSecurityGroups will return the Neutron security groups which match the options
ListSecurityGroups(opt sg.ListOpts) ([]sg.SecGroup, error)
// CreateSecurityGroup will create a new Neutron security group
CreateSecurityGroup(opt sg.CreateOptsBuilder) (*sg.SecGroup, error)
// DeleteSecurityGroup will delete securitygroup
DeleteSecurityGroup(sgID string) error
// DeleteSecurityGroupRule will delete securitygrouprule
DeleteSecurityGroupRule(ruleID string) error
// ListSecurityGroupRules will return the Neutron security group rules which match the options
ListSecurityGroupRules(opt sgr.ListOpts) ([]sgr.SecGroupRule, error)
// CreateSecurityGroupRule will create a new Neutron security group rule
CreateSecurityGroupRule(opt sgr.CreateOptsBuilder) (*sgr.SecGroupRule, error)
// GetNetwork will return the Neutron network which match the id
GetNetwork(networkID string) (*networks.Network, error)
// FindNetworkBySubnetID will return network
FindNetworkBySubnetID(subnetID string) (*networks.Network, error)
// GetSubnet returns subnet using subnet id
GetSubnet(subnetID string) (*subnets.Subnet, error)
// ListNetworks will return the Neutron networks which match the options
ListNetworks(opt networks.ListOptsBuilder) ([]networks.Network, error)
// GetExternalNetwork will return the Neutron networks with the router:external property
GetExternalNetwork() (*networks.Network, error)
// GetExternalSubnet will return the subnet for floatingip which is used in external router
GetExternalSubnet() (*subnets.Subnet, error)
// GetLBFloatingSubnet will return the subnet for floatingip which is used in lb
GetLBFloatingSubnet() (*subnets.Subnet, error)
// CreateNetwork will create a new Neutron network
CreateNetwork(opt networks.CreateOptsBuilder) (*networks.Network, error)
// DeleteNetwork will delete neutron network
DeleteNetwork(networkID string) error
// AppendTag appends tag to resource
AppendTag(resource string, id string, tag string) error
// DeleteTag removes tag from resource
DeleteTag(resource string, id string, tag string) error
// ListRouters will return the Neutron routers which match the options
ListRouters(opt routers.ListOpts) ([]routers.Router, error)
// CreateRouter will create a new Neutron router
CreateRouter(opt routers.CreateOptsBuilder) (*routers.Router, error)
// DeleteRouter will delete neutron router
DeleteRouter(routerID string) error
// DeleteSubnet will delete neutron subnet
DeleteSubnet(subnetID string) error
// ListSubnets will return the Neutron subnets which match the options
ListSubnets(opt subnets.ListOptsBuilder) ([]subnets.Subnet, error)
// CreateSubnet will create a new Neutron subnet
CreateSubnet(opt subnets.CreateOptsBuilder) (*subnets.Subnet, error)
// GetKeypair will return the Nova keypair
GetKeypair(name string) (*keypairs.KeyPair, error)
// ListKeypairs will return the all Nova keypairs
ListKeypairs() ([]keypairs.KeyPair, error)
// DeleteKeyPair will delete a Nova keypair
DeleteKeyPair(name string) error
// CreateKeypair will create a new Nova Keypair
CreateKeypair(opt keypairs.CreateOptsBuilder) (*keypairs.KeyPair, error)
CreatePort(opt ports.CreateOptsBuilder) (*ports.Port, error)
// GetPort will return a Neutron port by ID
GetPort(id string) (*ports.Port, error)
// UpdatePort will update a Neutron port by ID and options
UpdatePort(id string, opt ports.UpdateOptsBuilder) (*ports.Port, error)
// ListPorts will return the Neutron ports which match the options
ListPorts(opt ports.ListOptsBuilder) ([]ports.Port, error)
// DeletePort will delete a neutron port
DeletePort(portID string) error
// CreateRouterInterface will create a new Neutron router interface
CreateRouterInterface(routerID string, opt routers.AddInterfaceOptsBuilder) (*routers.InterfaceInfo, error)
// DeleteRouterInterface will delete router interface from subnet
DeleteRouterInterface(routerID string, opt routers.RemoveInterfaceOptsBuilder) error
// CreateServerGroup will create a new server group.
CreateServerGroup(opt servergroups.CreateOptsBuilder) (*servergroups.ServerGroup, error)
// ListServerGroups will list available server groups
ListServerGroups(opts servergroups.ListOptsBuilder) ([]servergroups.ServerGroup, error)
// DeleteServerGroup will delete a nova server group
DeleteServerGroup(groupID string) error
// ListDNSZones will list available DNS zones
ListDNSZones(opt zones.ListOptsBuilder) ([]zones.Zone, error)
// ListDNSRecordsets will list the DNS recordsets for the given zone id
ListDNSRecordsets(zoneID string, opt recordsets.ListOptsBuilder) ([]recordsets.RecordSet, error)
DeleteDNSRecordset(zoneID string, rrsetID string) error
GetLB(loadbalancerID string) (*loadbalancers.LoadBalancer, error)
GetLBStats(loadbalancerID string) (*loadbalancers.Stats, error)
CreateLB(opt loadbalancers.CreateOptsBuilder) (*loadbalancers.LoadBalancer, error)
ListLBs(opt loadbalancers.ListOptsBuilder) ([]loadbalancers.LoadBalancer, error)
UpdateMemberInPool(poolID string, memberID string, opts v2pools.UpdateMemberOptsBuilder) (*v2pools.Member, error)
ListPoolMembers(poolID string, opts v2pools.ListMembersOpts) ([]v2pools.Member, error)
// DeleteLB will delete loadbalancer
DeleteLB(lbID string, opt loadbalancers.DeleteOpts) error
// DefaultInstanceType determines a suitable instance type for the specified instance group
DefaultInstanceType(cluster *kops.Cluster, ig *kops.InstanceGroup) (string, error)
// Returns the availability zones for the service client passed (compute, volume, network)
ListAvailabilityZones(serviceClient *gophercloud.ServiceClient) ([]az.AvailabilityZone, error)
AssociateToPool(server *servers.Server, poolID string, opts v2pools.CreateMemberOpts) (*v2pools.Member, error)
CreatePool(opts v2pools.CreateOpts) (*v2pools.Pool, error)
CreatePoolMonitor(opts monitors.CreateOpts) (*monitors.Monitor, error)
GetPool(poolID string) (*v2pools.Pool, error)
GetPoolMember(poolID string, memberID string) (*v2pools.Member, error)
ListPools(v2pools.ListOpts) ([]v2pools.Pool, error)
// ListMonitors will list HealthMonitors matching the provided options
ListMonitors(monitors.ListOpts) ([]monitors.Monitor, error)
// DeleteMonitor will delete a Pool resources Health Monitor
DeleteMonitor(monitorID string) error
// DeletePool will delete loadbalancer pool
DeletePool(poolID string) error
ListListeners(opts listeners.ListOpts) ([]listeners.Listener, error)
CreateListener(opts listeners.CreateOpts) (*listeners.Listener, error)
// DeleteListener will delete loadbalancer listener
DeleteListener(listenerID string) error
GetStorageAZFromCompute(azName string) (*az.AvailabilityZone, error)
GetL3FloatingIP(id string) (fip *l3floatingip.FloatingIP, err error)
GetImage(name string) (i *images.Image, err error)
GetFlavor(name string) (f *flavors.Flavor, err error)
ListServerFloatingIPs(id string) ([]*string, error)
ListL3FloatingIPs(opts l3floatingip.ListOpts) (fips []l3floatingip.FloatingIP, err error)
CreateL3FloatingIP(opts l3floatingip.CreateOpts) (fip *l3floatingip.FloatingIP, err error)
DeleteFloatingIP(id string) error
DeleteL3FloatingIP(id string) error
UseLoadBalancerVIPACL() (bool, error)
}
type openstackCloud struct {
cinderClient *gophercloud.ServiceClient
neutronClient *gophercloud.ServiceClient
novaClient *gophercloud.ServiceClient
dnsClient *gophercloud.ServiceClient
lbClient *gophercloud.ServiceClient
glanceClient *gophercloud.ServiceClient
extNetworkName *string
extSubnetName *string
floatingSubnet *string
tags map[string]string
region string
useOctavia bool
zones []string
floatingEnabled bool
useVIPACL *bool
}
var _ fi.Cloud = &openstackCloud{}
var openstackCloudInstances = make(map[string]OpenstackCloud)
func NewOpenstackCloud(cluster *kops.Cluster, uagent string) (OpenstackCloud, error) {
config := vfs.OpenstackConfig{}
region, err := config.GetRegion()
if err != nil {
return nil, fmt.Errorf("error finding openstack region: %v", err)
}
raw := openstackCloudInstances[region]
if raw != nil {
return raw, nil
}
authOption, err := config.GetCredential()
if err != nil {
return nil, err
}
provider, err := openstack.NewClient(authOption.IdentityEndpoint)
if err != nil {
return nil, fmt.Errorf("error building openstack provider client: %v", err)
}
ua := gophercloud.UserAgent{}
ua.Prepend(fmt.Sprintf("kops/%s", uagent))
provider.UserAgent = ua
klog.V(4).Infof("Using user-agent %s", ua.Join())
if cluster != nil && cluster.Spec.CloudProvider.Openstack != nil && cluster.Spec.CloudProvider.Openstack.InsecureSkipVerify != nil {
tlsconfig := &tls.Config{}
tlsconfig.InsecureSkipVerify = fi.ValueOf(cluster.Spec.CloudProvider.Openstack.InsecureSkipVerify)
transport := &http.Transport{TLSClientConfig: tlsconfig}
provider.HTTPClient = http.Client{
Transport: transport,
}
}
klog.V(2).Info("authenticating to keystone")
err = openstack.Authenticate(provider, authOption)
if err != nil {
return nil, fmt.Errorf("error building openstack authenticated client: %v", err)
}
if cluster != nil {
hasDNS := cluster.PublishesDNSRecords()
tags := map[string]string{
TagClusterName: cluster.Name,
}
return buildClients(provider, tags, cluster.Spec.CloudProvider.Openstack, config, region, hasDNS)
}
// used by protokube
return buildClients(provider, nil, nil, config, region, false)
}
func buildClients(provider *gophercloud.ProviderClient, tags map[string]string, spec *kops.OpenstackSpec, config vfs.OpenstackConfig, region string, hasDNS bool) (OpenstackCloud, error) {
cinderClient, err := openstack.NewBlockStorageV3(provider, gophercloud.EndpointOpts{
Type: "volumev3",
Region: region,
})
if err != nil {
return nil, fmt.Errorf("error building cinder client: %w", err)
}
neutronClient, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
Type: "network",
Region: region,
})
if err != nil {
return nil, fmt.Errorf("error building neutron client: %w", err)
}
novaClient, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Type: "compute",
Region: region,
})
if err != nil {
return nil, fmt.Errorf("error building nova client: %w", err)
}
// 2.47 is the minimum version where the compute API /server/details returns flavor names
novaClient.Microversion = "2.47"
glanceClient, err := openstack.NewImageServiceV2(provider, gophercloud.EndpointOpts{
Type: "image",
Region: region,
})
if err != nil {
return nil, fmt.Errorf("error building glance client: %w", err)
}
var dnsClient *gophercloud.ServiceClient
if hasDNS {
dnsClient, err = openstack.NewDNSV2(provider, gophercloud.EndpointOpts{
Type: "dns",
Region: region,
})
if err != nil {
return nil, fmt.Errorf("error building dns client: %w", err)
}
}
c := &openstackCloud{
cinderClient: cinderClient,
neutronClient: neutronClient,
novaClient: novaClient,
dnsClient: dnsClient,
glanceClient: glanceClient,
tags: tags,
region: region,
useOctavia: false,
}
setFloatingIPSupport(c, spec)
err = buildLoadBalancerClient(c, spec, provider, region)
if err != nil {
return nil, fmt.Errorf("failed to build load balancer client: %w", err)
}
openstackCloudInstances[region] = c
return c, nil
}
func setFloatingIPSupport(c *openstackCloud, spec *kops.OpenstackSpec) {
if spec == nil || spec.Router == nil {
c.floatingEnabled = false
klog.V(2).Infof("Floating IP support for OpenStack disabled")
return
}
c.floatingEnabled = true
c.extNetworkName = spec.Router.ExternalNetwork
if spec.Router.ExternalSubnet != nil {
c.extSubnetName = spec.Router.ExternalSubnet
}
}
func buildLoadBalancerClient(c *openstackCloud, spec *kops.OpenstackSpec, provider *gophercloud.ProviderClient, region string) error {
if spec == nil || spec.Loadbalancer == nil {
klog.V(2).Infof("Loadbalancer support for OpenStack disabled")
return nil
}
octavia := false
if spec.Router != nil {
if spec.Loadbalancer.FloatingNetworkID == nil &&
spec.Loadbalancer.FloatingNetwork != nil {
// This field is derived
lbNet, err := c.ListNetworks(networks.ListOpts{
Name: fi.ValueOf(spec.Loadbalancer.FloatingNetwork),
})
if err != nil || len(lbNet) != 1 {
return fmt.Errorf("could not establish floating network id")
}
spec.Loadbalancer.FloatingNetworkID = fi.PtrTo(lbNet[0].ID)
}
if spec.Loadbalancer.UseOctavia != nil {
octavia = fi.ValueOf(spec.Loadbalancer.UseOctavia)
}
if spec.Loadbalancer.FloatingSubnet != nil {
c.floatingSubnet = spec.Loadbalancer.FloatingSubnet
}
} else if fi.ValueOf(spec.Loadbalancer.UseOctavia) {
return fmt.Errorf("cluster configured to use octavia, but router was not configured")
}
c.useOctavia = octavia
var lbClient *gophercloud.ServiceClient
if octavia {
klog.V(2).Infof("Openstack using Octavia lbaasv2 api")
client, err := openstack.NewLoadBalancerV2(provider, gophercloud.EndpointOpts{
Region: region,
})
if err != nil {
return fmt.Errorf("error building lb client: %w", err)
}
lbClient = client
} else {
klog.V(2).Infof("Openstack using deprecated lbaasv2 api")
client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
Region: region,
})
if err != nil {
return fmt.Errorf("error building lb client: %w", err)
}
lbClient = client
}
c.lbClient = lbClient
return nil
}
// UseZones add unique zone names to openstackcloud
func (c *openstackCloud) UseZones(zones []string) {
c.zones = zones
}
func (c *openstackCloud) UseOctavia() bool {
return c.useOctavia
}
func (c *openstackCloud) ComputeClient() *gophercloud.ServiceClient {
return c.novaClient
}
func (c *openstackCloud) BlockStorageClient() *gophercloud.ServiceClient {
return c.cinderClient
}
func (c *openstackCloud) NetworkingClient() *gophercloud.ServiceClient {
return c.neutronClient
}
func (c *openstackCloud) LoadBalancerClient() *gophercloud.ServiceClient {
return c.lbClient
}
func (c *openstackCloud) DNSClient() *gophercloud.ServiceClient {
return c.dnsClient
}
func (c *openstackCloud) ImageClient() *gophercloud.ServiceClient {
return c.glanceClient
}
func (c *openstackCloud) Region() string {
return c.region
}
func (c *openstackCloud) ProviderID() kops.CloudProviderID {
return kops.CloudProviderOpenstack
}
func (c *openstackCloud) DNS() (dnsprovider.Interface, error) {
provider, err := dnsprovider.GetDnsProvider(designate.ProviderName, nil)
if err != nil {
return nil, fmt.Errorf("error building (Designate) DNS provider: %v", err)
}
return provider, nil
}
// FindVPCInfo list subnets in network
func (c *openstackCloud) FindVPCInfo(id string) (*fi.VPCInfo, error) {
return findVPCInfo(c, id, c.zones)
}
func findVPCInfo(c OpenstackCloud, id string, zones []string) (*fi.VPCInfo, error) {
vpcInfo := &fi.VPCInfo{}
// Find subnets in the network
{
if len(zones) == 0 {
return nil, fmt.Errorf("could not initialize zones")
}
klog.V(2).Infof("Calling ListSubnets for subnets in Network %q", id)
opt := subnets.ListOpts{
NetworkID: id,
}
subnets, err := c.ListSubnets(opt)
if err != nil {
return nil, fmt.Errorf("error listing subnets in network %q: %v", id, err)
}
for index, subnet := range subnets {
zone := zones[int(index)%len(zones)]
subnetInfo := &fi.SubnetInfo{
ID: subnet.ID,
CIDR: subnet.CIDR,
Zone: zone,
}
vpcInfo.Subnets = append(vpcInfo.Subnets, subnetInfo)
}
}
return vpcInfo, nil
}
// DeleteGroup in openstack will delete servergroup, instances and ports
func (c *openstackCloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
return deleteGroup(c, g)
}
// InstanceInClusterAndIG checks if instance is in current cluster and instancegroup
func InstanceInClusterAndIG(instance servers.Server, clusterName string, instanceGroupName string) bool {
value, ok := instance.Metadata[TagKopsInstanceGroup]
if !ok || value != instanceGroupName {
return false
}
cName, clusterok := instance.Metadata["k8s"]
if !clusterok || cName != clusterName {
return false
}
return true
}
func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
cluster := g.Raw.(*kops.Cluster)
allInstances, err := c.ListInstances(servers.ListOpts{
Name: fmt.Sprintf("^%s", g.InstanceGroup.Name),
})
if err != nil {
return err
}
instances := []servers.Server{}
for _, instance := range allInstances {
if !InstanceInClusterAndIG(instance, cluster.Name, g.InstanceGroup.Name) {
continue
}
instances = append(instances, instance)
}
for _, instance := range instances {
err := c.DeleteInstanceWithID(instance.ID)
if err != nil {
return fmt.Errorf("could not delete instance %q: %v", instance.ID, err)
}
}
ports, err := c.ListPorts(ports.ListOpts{})
if err != nil {
return fmt.Errorf("could not list ports %v", err)
}
for _, port := range ports {
if strings.HasPrefix(port.Name, fmt.Sprintf("port-%s", g.InstanceGroup.Name)) && fi.ArrayContains(port.Tags, fmt.Sprintf("%s=%s", TagClusterName, cluster.Name)) {
err := c.DeletePort(port.ID)
if err != nil {
return fmt.Errorf("could not delete port %q: %v", port.ID, err)
}
}
}
sgName := g.InstanceGroup.Name
if name, ok := g.InstanceGroup.Annotations[OS_ANNOTATION+SERVER_GROUP_NAME]; ok {
sgName = name
}
sgs, err := c.ListServerGroups(servergroups.ListOpts{})
if err != nil {
return fmt.Errorf("could not list server groups %v", err)
}
for _, sg := range sgs {
if fmt.Sprintf("%s-%s", cluster.Name, sgName) == sg.Name {
if len(sg.Members) == 0 {
err = c.DeleteServerGroup(sg.ID)
if err != nil {
return fmt.Errorf("could not delete server group %q: %v", sg.ID, err)
}
} else {
klog.Infof("Server group %q still has members (IDs: %s), delete not executed", sg.ID, strings.Join(sg.Members, ", "))
}
break
}
}
return nil
}
func (c *openstackCloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
return getCloudGroups(c, cluster, instancegroups, warnUnmatched, nodes)
}
func getCloudGroups(c OpenstackCloud, cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
nodeMap := cloudinstances.GetNodeMap(nodes, cluster)
groups := make(map[string]*cloudinstances.CloudInstanceGroup)
for _, ig := range instancegroups {
var err error
groups[ig.ObjectMeta.Name], err = osBuildCloudInstanceGroup(c, cluster, ig, nodeMap)
if err != nil {
return nil, fmt.Errorf("error getting cloud instance group %q: %v", ig.ObjectMeta.Name, err)
}
}
return groups, nil
}
func (c *openstackCloud) GetCloudTags() map[string]string {
return c.tags
}
func (c *openstackCloud) UseLoadBalancerVIPACL() (bool, error) {
if c.useVIPACL != nil {
return *c.useVIPACL, nil
}
use, err := useLoadBalancerVIPACL(c)
if err != nil {
return false, err
}
c.useVIPACL = &use
return use, nil
}
func useLoadBalancerVIPACL(c OpenstackCloud) (bool, error) {
if c.LoadBalancerClient() == nil {
return false, nil
}
allPages, err := apiversions.List(c.LoadBalancerClient()).AllPages()
if err != nil {
return false, err
}
versions, err := apiversions.ExtractAPIVersions(allPages)
if err != nil {
return false, err
}
if len(versions) == 0 {
return false, fmt.Errorf("loadbalancer API versions not found")
}
ver, err := semver.ParseTolerant(versions[len(versions)-1].ID)
if err != nil {
return false, err
}
// https://github.com/kubernetes/cloud-provider-openstack/blob/721615aa256bbddbd481cfb4a887c3ab180c5563/pkg/util/openstack/loadbalancer.go#L108
return ver.Compare(semver.MustParse("2.12.0")) > 0, nil
}
type Address struct {
IPType string `mapstructure:"OS-EXT-IPS:type"`
Addr string
}
func (c *openstackCloud) GetApiIngressStatus(cluster *kops.Cluster) ([]fi.ApiIngressStatus, error) {
return getApiIngressStatus(c, cluster)
}
func getApiIngressStatus(c OpenstackCloud, cluster *kops.Cluster) ([]fi.ApiIngressStatus, error) {
if cluster.Spec.CloudProvider.Openstack.Loadbalancer != nil {
return getLoadBalancerIngressStatus(c, cluster)
} else {
return getIPIngressStatus(c, cluster)
}
}
func getLoadBalancerIngressStatus(c OpenstackCloud, cluster *kops.Cluster) ([]fi.ApiIngressStatus, error) {
var ingresses []fi.ApiIngressStatus
lbName := "api." + cluster.Name
if cluster.Spec.API.PublicName != "" {
lbName = cluster.Spec.API.PublicName
}
// Note that this must match OpenstackModel lb name
klog.V(2).Infof("Querying Openstack to find Loadbalancers for API (%q)", cluster.Name)
lbList, err := c.ListLBs(loadbalancers.ListOpts{
Name: lbName,
})
if err != nil {
return ingresses, fmt.Errorf("GetApiIngressStatus: Failed to list openstack loadbalancers: %v", err)
}
for _, lb := range lbList {
// Must Find Floating IP related to this lb
fips, err := c.ListL3FloatingIPs(l3floatingip.ListOpts{
PortID: lb.VipPortID,
})
if err != nil {
return ingresses, fmt.Errorf("GetApiIngressStatus: Failed to list floating IP's: %v", err)
}
for _, fip := range fips {
if fip.PortID == lb.VipPortID {
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: fip.FloatingIP,
})
}
}
}
return ingresses, nil
}
func getIPIngressStatus(c OpenstackCloud, cluster *kops.Cluster) (ingresses []fi.ApiIngressStatus, err error) {
done, err := vfs.RetryWithBackoff(readBackoff, func() (bool, error) {
instances, err := c.ListInstances(servers.ListOpts{})
if err != nil {
return false, fmt.Errorf("GetApiIngressStatus: Failed to list control plane nodes: %v", err)
}
for _, instance := range instances {
val, ok := instance.Metadata["k8s"]
val2, ok2 := instance.Metadata["KopsRole"]
if ok && val == cluster.Name && ok2 {
role, success := kops.ParseInstanceGroupRole(val2, false)
if success && role == kops.InstanceGroupRoleControlPlane {
ifName := instance.Metadata[TagKopsNetwork]
address, err := GetServerFixedIP(&instance, ifName)
if err == nil {
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: address,
})
} else {
ips, err := c.ListServerFloatingIPs(instance.ID)
if err != nil {
return false, err
}
for _, ip := range ips {
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: fi.ValueOf(ip),
})
}
}
}
}
}
return true, nil
})
if done {
return ingresses, nil
} else {
if err == nil {
err = wait.ErrWaitTimeout
}
return ingresses, err
}
}
func isNotFound(err error) bool {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return true
}
if _, ok := err.(gophercloud.ErrResourceNotFound); ok {
return true
}
if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
if errCode.Actual == http.StatusNotFound {
return true
}
}
return false
}
func MakeCloudConfig(osc *kops.OpenstackSpec) []string {
var lines []string
// Support mapping of older keystone API
tenantName := os.Getenv("OS_TENANT_NAME")
if tenantName == "" {
tenantName = os.Getenv("OS_PROJECT_NAME")
}
tenantID := os.Getenv("OS_TENANT_ID")
if tenantID == "" {
tenantID = os.Getenv("OS_PROJECT_ID")
}
lines = append(lines,
fmt.Sprintf("auth-url=\"%s\"", os.Getenv("OS_AUTH_URL")),
fmt.Sprintf("username=\"%s\"", os.Getenv("OS_USERNAME")),
fmt.Sprintf("password=\"%s\"", os.Getenv("OS_PASSWORD")),
fmt.Sprintf("region=\"%s\"", os.Getenv("OS_REGION_NAME")),
fmt.Sprintf("tenant-id=\"%s\"", tenantID),
fmt.Sprintf("tenant-name=\"%s\"", tenantName),
fmt.Sprintf("domain-name=\"%s\"", os.Getenv("OS_DOMAIN_NAME")),
fmt.Sprintf("domain-id=\"%s\"", os.Getenv("OS_DOMAIN_ID")),
fmt.Sprintf("application-credential-id=\"%s\"", os.Getenv("OS_APPLICATION_CREDENTIAL_ID")),
fmt.Sprintf("application-credential-secret=\"%s\"", os.Getenv("OS_APPLICATION_CREDENTIAL_SECRET")),
"",
)
if lb := osc.Loadbalancer; lb != nil {
ingressHostnameSuffix := "nip.io"
if fi.ValueOf(lb.IngressHostnameSuffix) != "" {
ingressHostnameSuffix = fi.ValueOf(lb.IngressHostnameSuffix)
}
lines = append(lines,
"[LoadBalancer]",
fmt.Sprintf("floating-network-id=%s", fi.ValueOf(lb.FloatingNetworkID)),
fmt.Sprintf("lb-method=%s", fi.ValueOf(lb.Method)),
fmt.Sprintf("lb-provider=%s", fi.ValueOf(lb.Provider)),
fmt.Sprintf("use-octavia=%t", fi.ValueOf(lb.UseOctavia)),
fmt.Sprintf("manage-security-groups=%t", fi.ValueOf(lb.ManageSecGroups)),
fmt.Sprintf("enable-ingress-hostname=%t", fi.ValueOf(lb.EnableIngressHostname)),
fmt.Sprintf("ingress-hostname-suffix=%s", ingressHostnameSuffix),
"",
)
if monitor := osc.Monitor; monitor != nil {
lines = append(lines,
"create-monitor=yes",
fmt.Sprintf("monitor-delay=%s", fi.ValueOf(monitor.Delay)),
fmt.Sprintf("monitor-timeout=%s", fi.ValueOf(monitor.Timeout)),
fmt.Sprintf("monitor-max-retries=%d", fi.ValueOf(monitor.MaxRetries)),
"",
)
}
}
if bs := osc.BlockStorage; bs != nil {
// Block Storage Config
lines = append(lines,
"[BlockStorage]",
fmt.Sprintf("bs-version=%s", fi.ValueOf(bs.Version)),
fmt.Sprintf("ignore-volume-az=%t", fi.ValueOf(bs.IgnoreAZ)),
fmt.Sprintf("ignore-volume-microversion=%t", fi.ValueOf(bs.IgnoreVolumeMicroVersion)),
"")
}
if networking := osc.Network; networking != nil {
// Networking Config
// https://github.com/kubernetes/cloud-provider-openstack/blob/master/docs/openstack-cloud-controller-manager/using-openstack-cloud-controller-manager.md#networking
var networkingLines []string
if networking.IPv6SupportDisabled != nil {
networkingLines = append(networkingLines, fmt.Sprintf("ipv6-support-disabled=%t", fi.ValueOf(networking.IPv6SupportDisabled)))
}
for _, name := range networking.PublicNetworkNames {
networkingLines = append(networkingLines, fmt.Sprintf("public-network-name=%s", fi.ValueOf(name)))
}
for _, name := range networking.InternalNetworkNames {
networkingLines = append(networkingLines, fmt.Sprintf("internal-network-name=%s", fi.ValueOf(name)))
}
if networking.AddressSortOrder != nil {
networkingLines = append(networkingLines, fmt.Sprintf("address-sort-order=%s", fi.ValueOf(networking.AddressSortOrder)))
}
if len(networkingLines) > 0 {
lines = append(lines, "[Networking]")
lines = append(lines, networkingLines...)
lines = append(lines, "")
}
}
return lines
}