-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
azure_cloud.go
376 lines (332 loc) · 12 KB
/
azure_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
/*
Copyright 2024 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 azure
import (
"context"
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"k8s.io/klog/v2"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
)
const (
TagClusterName = "KubernetesCluster"
// Use dash (_) as a splitter. Other CSPs use slash (/), but slash is not
// allowed as a tag key in Azure.
TagNameRolePrefix = "k8s.io_role_"
TagRoleControlPlane = "control_plane"
TagRoleMaster = "master"
TagNameEtcdClusterPrefix = "k8s.io_etcd_"
)
// AzureCloud provides clients to make API calls to Azure.
type AzureCloud interface {
fi.Cloud
AddClusterTags(tags map[string]*string)
FindVNetInfo(id, resourceGroup string) (*fi.VPCInfo, error)
SubscriptionID() string
ResourceGroup() ResourceGroupsClient
VirtualNetwork() VirtualNetworksClient
Subnet() SubnetsClient
RouteTable() RouteTablesClient
NetworkSecurityGroup() NetworkSecurityGroupsClient
ApplicationSecurityGroup() ApplicationSecurityGroupsClient
VMScaleSet() VMScaleSetsClient
VMScaleSetVM() VMScaleSetVMsClient
Disk() DisksClient
RoleAssignment() RoleAssignmentsClient
NetworkInterface() NetworkInterfacesClient
LoadBalancer() LoadBalancersClient
PublicIPAddress() PublicIPAddressesClient
NatGateway() NatGatewaysClient
}
type azureCloudImplementation struct {
subscriptionID string
location string
tags map[string]string
resourceGroupsClient ResourceGroupsClient
networkSecurityGroupsClient NetworkSecurityGroupsClient
applicationSecurityGroupsClient ApplicationSecurityGroupsClient
vnetsClient VirtualNetworksClient
subnetsClient SubnetsClient
routeTablesClient RouteTablesClient
vmscaleSetsClient VMScaleSetsClient
vmscaleSetVMsClient VMScaleSetVMsClient
disksClient DisksClient
roleAssignmentsClient RoleAssignmentsClient
networkInterfacesClient NetworkInterfacesClient
loadBalancersClient LoadBalancersClient
publicIPAddressesClient PublicIPAddressesClient
natGatewaysClient NatGatewaysClient
}
var _ fi.Cloud = &azureCloudImplementation{}
// NewAzureCloud creates a new AzureCloud.
func NewAzureCloud(subscriptionID, location string, tags map[string]string) (AzureCloud, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("error creating an identity: %s", err)
}
azureCloudImpl := &azureCloudImplementation{
subscriptionID: subscriptionID,
location: location,
tags: tags,
}
if azureCloudImpl.resourceGroupsClient, err = newResourceGroupsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.vnetsClient, err = newVirtualNetworksClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.subnetsClient, err = newSubnetsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.routeTablesClient, err = newRouteTablesClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.networkSecurityGroupsClient, err = newNetworkSecurityGroupsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.applicationSecurityGroupsClient, err = newApplicationSecurityGroupsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.vmscaleSetsClient, err = newVMScaleSetsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.vmscaleSetVMsClient, err = newVMScaleSetVMsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.disksClient, err = newDisksClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.roleAssignmentsClient, err = newRoleAssignmentsClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.networkInterfacesClient, err = newNetworkInterfacesClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.loadBalancersClient, err = newLoadBalancersClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.publicIPAddressesClient, err = newPublicIPAddressesClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
if azureCloudImpl.natGatewaysClient, err = newNatGatewaysClientImpl(subscriptionID, cred); err != nil {
return nil, err
}
return azureCloudImpl, nil
}
func (c *azureCloudImplementation) Region() string {
return c.location
}
func (c *azureCloudImplementation) ProviderID() kops.CloudProviderID {
return kops.CloudProviderAzure
}
func (c *azureCloudImplementation) DNS() (dnsprovider.Interface, error) {
return nil, errors.New("DNS not implemented on azureCloud")
}
func (c *azureCloudImplementation) FindVPCInfo(id string) (*fi.VPCInfo, error) {
return nil, errors.New("FindVPCInfo not implemented on azureCloud, use FindVNETInfo instead")
}
func (c *azureCloudImplementation) FindVNetInfo(id, resourceGroup string) (*fi.VPCInfo, error) {
vnets, err := c.vnetsClient.List(context.TODO(), resourceGroup)
if err != nil {
return nil, err
}
for _, vnet := range vnets {
if *vnet.ID != id {
continue
}
if vnet.Properties == nil {
continue
}
subnets := make([]*fi.SubnetInfo, 0)
for _, subnet := range vnet.Properties.Subnets {
if subnet.Properties == nil {
continue
}
subnets = append(subnets, &fi.SubnetInfo{
ID: *subnet.ID,
CIDR: *subnet.Properties.AddressPrefix,
})
}
if vnet.Properties.AddressSpace == nil || len(vnet.Properties.AddressSpace.AddressPrefixes) == 0 {
continue
}
return &fi.VPCInfo{
CIDR: *vnet.Properties.AddressSpace.AddressPrefixes[0],
Subnets: subnets,
}, nil
}
return nil, nil
}
func (c *azureCloudImplementation) DeleteInstance(i *cloudinstances.CloudInstance) error {
return errors.New("DeleteInstance not implemented on azureCloud")
}
// DeregisterInstance drains a cloud instance and loadbalancers.
func (c *azureCloudImplementation) DeregisterInstance(i *cloudinstances.CloudInstance) error {
klog.V(8).Info("Azure DeregisterInstance not implemented")
return nil
}
func (c *azureCloudImplementation) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
return errors.New("DeleteGroup not implemented on azureCloud")
}
func (c *azureCloudImplementation) DetachInstance(i *cloudinstances.CloudInstance) error {
return errors.New("DetachInstance not implemented on azureCloud")
}
// AddClusterTags adds cluster tags to the resource.
func (c *azureCloudImplementation) AddClusterTags(tags map[string]*string) {
for k, v := range c.tags {
tags[k] = &v
}
}
func (c *azureCloudImplementation) GetApiIngressStatus(cluster *kops.Cluster) ([]fi.ApiIngressStatus, error) {
var ingresses []fi.ApiIngressStatus
rg := cluster.AzureResourceGroupName()
lbSpec := cluster.Spec.API.LoadBalancer
if lbSpec != nil {
// Get load balancers in cluster resource group
lbs, err := c.loadBalancersClient.List(context.TODO(), rg)
if err != nil {
return nil, fmt.Errorf("getting Loadbalancer for API Ingress Status: %w", err)
}
for _, lb := range lbs {
val := lb.Tags[TagClusterName]
if val == nil || *val != cluster.Name {
continue
}
if lb.Properties == nil {
continue
}
for _, i := range lb.Properties.FrontendIPConfigurations {
if i.Properties == nil {
continue
}
switch lbSpec.Type {
case kops.LoadBalancerTypeInternal:
if i.Properties.PrivateIPAddress == nil {
continue
}
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: *i.Properties.PrivateIPAddress,
})
case kops.LoadBalancerTypePublic:
if i.Properties.PublicIPAddress == nil || i.Properties.PublicIPAddress.ID == nil {
continue
}
pips, err := c.publicIPAddressesClient.List(context.TODO(), rg)
if err != nil {
return nil, fmt.Errorf("error getting PublicIPAddress for API Ingress Status: %w", err)
}
for _, pip := range pips {
if pip.ID == nil || pip.Properties == nil || pip.Properties.IPAddress == nil || *pip.ID != *i.Properties.PublicIPAddress.ID {
continue
}
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: *pip.Properties.IPAddress,
})
}
default:
return nil, fmt.Errorf("unknown load balancer type: %q", lbSpec.Type)
}
}
}
} else {
// Get scale sets in cluster resource group and find masters scale set
scaleSets, err := c.vmscaleSetsClient.List(context.TODO(), rg)
if err != nil {
return nil, fmt.Errorf("getting cluster control plane VMSS for API ingress status: %w", err)
}
var vmssName string
for _, scaleSet := range scaleSets {
val, ok := scaleSet.Tags[TagClusterName]
val2, ok2 := scaleSet.Tags[TagNameRolePrefix+TagRoleControlPlane]
val3, ok3 := scaleSet.Tags[TagNameRolePrefix+TagRoleMaster]
if ok && *val == cluster.Name && (ok2 && *val2 == "1" || ok3 && *val3 == "1") {
vmssName = *scaleSet.Name
break
}
}
if vmssName == "" {
return nil, fmt.Errorf("getting control plane VMSS name for API ingress status")
}
// Get masters scale set network interfaces and append to api ingress status
nis, err := c.NetworkInterface().ListScaleSetsNetworkInterfaces(context.TODO(), rg, vmssName)
if err != nil {
return nil, fmt.Errorf("getting control plane VMSS network interfaces for API ingress status: %w", err)
}
for _, ni := range nis {
if ni.Properties == nil || ni.Properties.Primary == nil || !*ni.Properties.Primary {
continue
}
for _, i := range ni.Properties.IPConfigurations {
if i.Properties == nil || i.Properties.PrivateIPAddress == nil {
continue
}
ingresses = append(ingresses, fi.ApiIngressStatus{
IP: *i.Properties.PrivateIPAddress,
})
}
}
if ingresses == nil {
return nil, fmt.Errorf("getting API ingress status")
}
}
return ingresses, nil
}
func (c *azureCloudImplementation) SubscriptionID() string {
return c.subscriptionID
}
func (c *azureCloudImplementation) ResourceGroup() ResourceGroupsClient {
return c.resourceGroupsClient
}
func (c *azureCloudImplementation) VirtualNetwork() VirtualNetworksClient {
return c.vnetsClient
}
func (c *azureCloudImplementation) Subnet() SubnetsClient {
return c.subnetsClient
}
func (c *azureCloudImplementation) RouteTable() RouteTablesClient {
return c.routeTablesClient
}
func (c *azureCloudImplementation) NetworkSecurityGroup() NetworkSecurityGroupsClient {
return c.networkSecurityGroupsClient
}
func (c *azureCloudImplementation) ApplicationSecurityGroup() ApplicationSecurityGroupsClient {
return c.applicationSecurityGroupsClient
}
func (c *azureCloudImplementation) VMScaleSet() VMScaleSetsClient {
return c.vmscaleSetsClient
}
func (c *azureCloudImplementation) VMScaleSetVM() VMScaleSetVMsClient {
return c.vmscaleSetVMsClient
}
func (c *azureCloudImplementation) Disk() DisksClient {
return c.disksClient
}
func (c *azureCloudImplementation) RoleAssignment() RoleAssignmentsClient {
return c.roleAssignmentsClient
}
func (c *azureCloudImplementation) NetworkInterface() NetworkInterfacesClient {
return c.networkInterfacesClient
}
func (c *azureCloudImplementation) LoadBalancer() LoadBalancersClient {
return c.loadBalancersClient
}
func (c *azureCloudImplementation) PublicIPAddress() PublicIPAddressesClient {
return c.publicIPAddressesClient
}
func (c *azureCloudImplementation) NatGateway() NatGatewaysClient {
return c.natGatewaysClient
}