-
Notifications
You must be signed in to change notification settings - Fork 431
/
helpers.go
599 lines (530 loc) · 24.8 KB
/
helpers.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
/*
Copyright 2020 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 controllers
import (
"context"
"encoding/json"
"fmt"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/scope"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/groups"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/pkg/coalescing"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
)
const (
spIdentityWarning = "You are using Service Principal authentication for Cloud Provider Azure which is less secure than Managed Identity. " +
"Your Service Principal credentials will be written to a file on the disk of each VM in order to be accessible by Cloud Provider. " +
"To learn more, see https://capz.sigs.k8s.io/topics/identities-use-cases.html#azure-host-identity "
deprecatedManagerCredsWarning = "You're using deprecated functionality: " +
"Using Azure credentials from the manager environment is deprecated and will be removed in future releases. " +
"Please specify an AzureClusterIdentity for the AzureCluster instead, see: https://capz.sigs.k8s.io/topics/multitenancy.html "
)
type (
// Options are controller options extended.
Options struct {
controller.Options
Cache *coalescing.ReconcileCache
}
)
// AzureClusterToAzureMachinesMapper creates a mapping handler to transform AzureClusters into AzureMachines. The transform
// requires AzureCluster to map to the owning Cluster, then from the Cluster, collect the Machines belonging to the cluster,
// then finally projecting the infrastructure reference to the AzureMachine.
func AzureClusterToAzureMachinesMapper(ctx context.Context, c client.Client, ro runtime.Object, scheme *runtime.Scheme, log logr.Logger) (handler.MapFunc, error) {
gvk, err := apiutil.GVKForObject(ro, scheme)
if err != nil {
return nil, errors.Wrap(err, "failed to find GVK for AzureMachine")
}
return func(o client.Object) []ctrl.Request {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultMappingTimeout)
defer cancel()
azCluster, ok := o.(*infrav1.AzureCluster)
if !ok {
log.Error(errors.Errorf("expected an AzureCluster, got %T instead", o), "failed to map AzureCluster")
return nil
}
log = log.WithValues("AzureCluster", azCluster.Name, "Namespace", azCluster.Namespace)
// Don't handle deleted AzureClusters
if !azCluster.ObjectMeta.DeletionTimestamp.IsZero() {
log.V(4).Info("AzureCluster has a deletion timestamp, skipping mapping.")
return nil
}
clusterName, ok := GetOwnerClusterName(azCluster.ObjectMeta)
if !ok {
log.Info("unable to get the owner cluster")
return nil
}
machineList := &clusterv1.MachineList{}
machineList.SetGroupVersionKind(gvk)
// list all of the requested objects within the cluster namespace with the cluster name label
if err := c.List(ctx, machineList, client.InNamespace(azCluster.Namespace), client.MatchingLabels{clusterv1.ClusterLabelName: clusterName}); err != nil {
return nil
}
mapFunc := util.MachineToInfrastructureMapFunc(gvk)
var results []ctrl.Request
for _, machine := range machineList.Items {
m := machine
azureMachines := mapFunc(&m)
results = append(results, azureMachines...)
}
return results
}, nil
}
// GetOwnerClusterName returns the name of the owning Cluster by finding a clusterv1.Cluster in the ownership references.
func GetOwnerClusterName(obj metav1.ObjectMeta) (string, bool) {
for _, ref := range obj.OwnerReferences {
if ref.Kind != "Cluster" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return "", false
}
if gv.Group == clusterv1.GroupVersion.Group {
return ref.Name, true
}
}
return "", false
}
// GetObjectsToRequestsByNamespaceAndClusterName returns the slice of ctrl.Requests consisting the list items contained in the unstructured list.
func GetObjectsToRequestsByNamespaceAndClusterName(ctx context.Context, c client.Client, clusterKey client.ObjectKey, list *unstructured.UnstructuredList) []ctrl.Request {
// list all of the requested objects within the cluster namespace with the cluster name label
if err := c.List(ctx, list, client.InNamespace(clusterKey.Namespace), client.MatchingLabels{clusterv1.ClusterLabelName: clusterKey.Name}); err != nil {
return nil
}
results := make([]ctrl.Request, len(list.Items))
for i, obj := range list.Items {
results[i] = ctrl.Request{
NamespacedName: client.ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()},
}
}
return results
}
// referSameObject returns true if a and b point to the same object.
func referSameObject(a, b metav1.OwnerReference) bool {
aGV, err := schema.ParseGroupVersion(a.APIVersion)
if err != nil {
return false
}
bGV, err := schema.ParseGroupVersion(b.APIVersion)
if err != nil {
return false
}
return aGV.Group == bGV.Group && a.Kind == b.Kind && a.Name == b.Name
}
// GetCloudProviderSecret returns the required azure json secret for the provided parameters.
func GetCloudProviderSecret(d azure.ClusterScoper, namespace, name string, owner metav1.OwnerReference, identityType infrav1.VMIdentity, userIdentityID string) (*corev1.Secret, error) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: fmt.Sprintf("%s-azure-json", name),
Labels: map[string]string{
d.ClusterName(): string(infrav1.ResourceLifecycleOwned),
},
OwnerReferences: []metav1.OwnerReference{owner},
},
}
var controlPlaneConfig, workerNodeConfig *CloudProviderConfig
switch identityType {
case infrav1.VMIdentitySystemAssigned:
controlPlaneConfig, workerNodeConfig = systemAssignedIdentityCloudProviderConfig(d)
case infrav1.VMIdentityUserAssigned:
if len(userIdentityID) < 1 {
return nil, errors.New("expected a non-empty userIdentityID")
}
controlPlaneConfig, workerNodeConfig = userAssignedIdentityCloudProviderConfig(d, userIdentityID)
case infrav1.VMIdentityNone:
controlPlaneConfig, workerNodeConfig = newCloudProviderConfig(d)
}
controlPlaneData, err := json.MarshalIndent(controlPlaneConfig, "", " ")
if err != nil {
return nil, errors.Wrap(err, "failed control plane json marshal")
}
workerNodeData, err := json.MarshalIndent(workerNodeConfig, "", " ")
if err != nil {
return nil, errors.Wrap(err, "failed worker node json marshal")
}
secret.Data = map[string][]byte{
"control-plane-azure.json": controlPlaneData,
"worker-node-azure.json": workerNodeData,
// added for backwards compatibility
"azure.json": controlPlaneData,
}
return secret, nil
}
func systemAssignedIdentityCloudProviderConfig(d azure.ClusterScoper) (*CloudProviderConfig, *CloudProviderConfig) {
controlPlaneConfig, workerConfig := newCloudProviderConfig(d)
controlPlaneConfig.AadClientID = ""
controlPlaneConfig.AadClientSecret = ""
controlPlaneConfig.UseManagedIdentityExtension = true
workerConfig.AadClientID = ""
workerConfig.AadClientSecret = ""
workerConfig.UseManagedIdentityExtension = true
return controlPlaneConfig, workerConfig
}
func userAssignedIdentityCloudProviderConfig(d azure.ClusterScoper, identityID string) (*CloudProviderConfig, *CloudProviderConfig) {
controlPlaneConfig, workerConfig := newCloudProviderConfig(d)
controlPlaneConfig.AadClientID = ""
controlPlaneConfig.AadClientSecret = ""
controlPlaneConfig.UseManagedIdentityExtension = true
controlPlaneConfig.UserAssignedIdentityID = identityID
workerConfig.AadClientID = ""
workerConfig.AadClientSecret = ""
workerConfig.UseManagedIdentityExtension = true
workerConfig.UserAssignedIdentityID = identityID
return controlPlaneConfig, workerConfig
}
func newCloudProviderConfig(d azure.ClusterScoper) (controlPlaneConfig *CloudProviderConfig, workerConfig *CloudProviderConfig) {
subnet := getOneNodeSubnet(d)
return (&CloudProviderConfig{
Cloud: d.CloudEnvironment(),
AadClientID: d.ClientID(),
AadClientSecret: d.ClientSecret(),
TenantID: d.TenantID(),
SubscriptionID: d.SubscriptionID(),
ResourceGroup: d.ResourceGroup(),
SecurityGroupName: subnet.SecurityGroup.Name,
SecurityGroupResourceGroup: d.Vnet().ResourceGroup,
Location: d.Location(),
VMType: "vmss",
VnetName: d.Vnet().Name,
VnetResourceGroup: d.Vnet().ResourceGroup,
SubnetName: subnet.Name,
RouteTableName: subnet.RouteTable.Name,
LoadBalancerSku: "Standard",
MaximumLoadBalancerRuleCount: 250,
UseManagedIdentityExtension: false,
UseInstanceMetadata: true,
}).overrideFromSpec(d),
(&CloudProviderConfig{
Cloud: d.CloudEnvironment(),
AadClientID: d.ClientID(),
AadClientSecret: d.ClientSecret(),
TenantID: d.TenantID(),
SubscriptionID: d.SubscriptionID(),
ResourceGroup: d.ResourceGroup(),
SecurityGroupName: subnet.SecurityGroup.Name,
SecurityGroupResourceGroup: d.Vnet().ResourceGroup,
Location: d.Location(),
VMType: "vmss",
VnetName: d.Vnet().Name,
VnetResourceGroup: d.Vnet().ResourceGroup,
SubnetName: subnet.Name,
RouteTableName: subnet.RouteTable.Name,
LoadBalancerSku: "Standard",
MaximumLoadBalancerRuleCount: 250,
UseManagedIdentityExtension: false,
UseInstanceMetadata: true,
}).overrideFromSpec(d)
}
// getOneNodeSubnet returns one of the subnets for the node role.
func getOneNodeSubnet(d azure.ClusterScoper) infrav1.SubnetSpec {
for _, subnet := range d.Subnets() {
if subnet.Role == infrav1.SubnetNode {
return subnet
}
}
return infrav1.SubnetSpec{}
}
// overrideFromSpec overrides cloud provider config with the values provided in cluster spec.
func (cpc *CloudProviderConfig) overrideFromSpec(d azure.ClusterScoper) *CloudProviderConfig {
if d.CloudProviderConfigOverrides() == nil {
return cpc
}
for _, rateLimit := range d.CloudProviderConfigOverrides().RateLimits {
switch rateLimit.Name {
case infrav1.DefaultRateLimit:
cpc.RateLimitConfig = *toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.RouteRateLimit:
cpc.RouteRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.SubnetsRateLimit:
cpc.SubnetsRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.InterfaceRateLimit:
cpc.InterfaceRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.RouteTableRateLimit:
cpc.RouteTableRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.LoadBalancerRateLimit:
cpc.LoadBalancerRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.PublicIPAddressRateLimit:
cpc.PublicIPAddressRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.SecurityGroupRateLimit:
cpc.SecurityGroupRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.VirtualMachineRateLimit:
cpc.VirtualMachineRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.StorageAccountRateLimit:
cpc.StorageAccountRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.DiskRateLimit:
cpc.DiskRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.SnapshotRateLimit:
cpc.SnapshotRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.VirtualMachineScaleSetRateLimit:
cpc.VirtualMachineScaleSetRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.VirtualMachineSizesRateLimit:
cpc.VirtualMachineSizeRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
case infrav1.AvailabilitySetRateLimit:
cpc.AvailabilitySetRateLimit = toCloudProviderRateLimitConfig(rateLimit.Config)
}
}
cpc.BackOffConfig = toCloudProviderBackOffConfig(d.CloudProviderConfigOverrides().BackOffs)
return cpc
}
// toCloudProviderRateLimitConfig returns converts infrav1.RateLimitConfig to RateLimitConfig that is required with the cloud provider.
func toCloudProviderRateLimitConfig(source infrav1.RateLimitConfig) *RateLimitConfig {
rateLimitConfig := RateLimitConfig{}
rateLimitConfig.CloudProviderRateLimit = source.CloudProviderRateLimit
if source.CloudProviderRateLimitQPS != nil {
rateLimitConfig.CloudProviderRateLimitQPS = float32(source.CloudProviderRateLimitQPS.AsApproximateFloat64())
}
rateLimitConfig.CloudProviderRateLimitBucket = source.CloudProviderRateLimitBucket
if source.CloudProviderRateLimitQPSWrite != nil {
rateLimitConfig.CloudProviderRateLimitQPSWrite = float32(source.CloudProviderRateLimitQPSWrite.AsApproximateFloat64())
}
rateLimitConfig.CloudProviderRateLimitBucketWrite = source.CloudProviderRateLimitBucketWrite
return &rateLimitConfig
}
// CloudProviderConfig is an abbreviated version of the same struct in k/k.
type CloudProviderConfig struct {
Cloud string `json:"cloud"`
TenantID string `json:"tenantId"`
SubscriptionID string `json:"subscriptionId"`
AadClientID string `json:"aadClientId,omitempty"`
AadClientSecret string `json:"aadClientSecret,omitempty"`
ResourceGroup string `json:"resourceGroup"`
SecurityGroupName string `json:"securityGroupName"`
SecurityGroupResourceGroup string `json:"securityGroupResourceGroup"`
Location string `json:"location"`
VMType string `json:"vmType"`
VnetName string `json:"vnetName"`
VnetResourceGroup string `json:"vnetResourceGroup"`
SubnetName string `json:"subnetName"`
RouteTableName string `json:"routeTableName"`
LoadBalancerSku string `json:"loadBalancerSku"`
MaximumLoadBalancerRuleCount int `json:"maximumLoadBalancerRuleCount"`
UseManagedIdentityExtension bool `json:"useManagedIdentityExtension"`
UseInstanceMetadata bool `json:"useInstanceMetadata"`
UserAssignedIdentityID string `json:"userAssignedIdentityId,omitempty"`
CloudProviderRateLimitConfig
BackOffConfig
}
// CloudProviderRateLimitConfig represents the rate limiting configurations in azure cloud provider config.
// See: https://kubernetes-sigs.github.io/cloud-provider-azure/install/configs/#per-client-rate-limiting.
// This is a copy of the struct used in cloud-provider-azure: https://github.com/kubernetes-sigs/cloud-provider-azure/blob/d585c2031925b39c925624302f22f8856e29e352/pkg/provider/azure_ratelimit.go#L25
type CloudProviderRateLimitConfig struct {
RateLimitConfig
RouteRateLimit *RateLimitConfig `json:"routeRateLimit,omitempty"`
SubnetsRateLimit *RateLimitConfig `json:"subnetsRateLimit,omitempty"`
InterfaceRateLimit *RateLimitConfig `json:"interfaceRateLimit,omitempty"`
RouteTableRateLimit *RateLimitConfig `json:"routeTableRateLimit,omitempty"`
LoadBalancerRateLimit *RateLimitConfig `json:"loadBalancerRateLimit,omitempty"`
PublicIPAddressRateLimit *RateLimitConfig `json:"publicIPAddressRateLimit,omitempty"`
SecurityGroupRateLimit *RateLimitConfig `json:"securityGroupRateLimit,omitempty"`
VirtualMachineRateLimit *RateLimitConfig `json:"virtualMachineRateLimit,omitempty"`
StorageAccountRateLimit *RateLimitConfig `json:"storageAccountRateLimit,omitempty"`
DiskRateLimit *RateLimitConfig `json:"diskRateLimit,omitempty"`
SnapshotRateLimit *RateLimitConfig `json:"snapshotRateLimit,omitempty"`
VirtualMachineScaleSetRateLimit *RateLimitConfig `json:"virtualMachineScaleSetRateLimit,omitempty"`
VirtualMachineSizeRateLimit *RateLimitConfig `json:"virtualMachineSizesRateLimit,omitempty"`
AvailabilitySetRateLimit *RateLimitConfig `json:"availabilitySetRateLimit,omitempty"`
}
// RateLimitConfig indicates the rate limit config options.
// This is a copy of the struct used in cloud-provider-azure: https://github.com/kubernetes-sigs/cloud-provider-azure/blob/d585c2031925b39c925624302f22f8856e29e352/pkg/azureclients/azure_client_config.go#L48
type RateLimitConfig struct {
CloudProviderRateLimit bool `json:"cloudProviderRateLimit,omitempty"`
CloudProviderRateLimitQPS float32 `json:"cloudProviderRateLimitQPS,omitempty"`
CloudProviderRateLimitBucket int `json:"cloudProviderRateLimitBucket,omitempty"`
CloudProviderRateLimitQPSWrite float32 `json:"cloudProviderRateLimitQPSWrite,omitempty"`
CloudProviderRateLimitBucketWrite int `json:"cloudProviderRateLimitBucketWrite,omitempty"`
}
// BackOffConfig indicates the back-off config options.
// This is a copy of the struct used in cloud-provider-azure: https://github.com/kubernetes-sigs/cloud-provider-azure/blob/d585c2031925b39c925624302f22f8856e29e352/pkg/azureclients/azure_client_config.go#L48
type BackOffConfig struct {
CloudProviderBackoff bool `json:"cloudProviderBackoff,omitempty"`
CloudProviderBackoffRetries int `json:"cloudProviderBackoffRetries,omitempty"`
CloudProviderBackoffExponent float64 `json:"cloudProviderBackoffExponent,omitempty"`
CloudProviderBackoffDuration int `json:"cloudProviderBackoffDuration,omitempty"`
CloudProviderBackoffJitter float64 `json:"cloudProviderBackoffJitter,omitempty"`
}
// toCloudProviderBackOffConfig returns converts infrav1.BackOffConfig to BackOffConfig that is required with the cloud provider.
func toCloudProviderBackOffConfig(source infrav1.BackOffConfig) BackOffConfig {
backOffConfig := BackOffConfig{}
backOffConfig.CloudProviderBackoff = source.CloudProviderBackoff
if source.CloudProviderBackoffExponent != nil {
backOffConfig.CloudProviderBackoffExponent = source.CloudProviderBackoffExponent.AsApproximateFloat64()
}
backOffConfig.CloudProviderBackoffRetries = source.CloudProviderBackoffRetries
if source.CloudProviderBackoffJitter != nil {
backOffConfig.CloudProviderBackoffJitter = source.CloudProviderBackoffJitter.AsApproximateFloat64()
}
backOffConfig.CloudProviderBackoffDuration = source.CloudProviderBackoffDuration
return backOffConfig
}
func reconcileAzureSecret(ctx context.Context, kubeclient client.Client, owner metav1.OwnerReference, newSecret *corev1.Secret, clusterName string) error {
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.reconcileAzureSecret")
defer done()
// Fetch previous secret, if it exists
key := types.NamespacedName{
Namespace: newSecret.Namespace,
Name: newSecret.Name,
}
old := &corev1.Secret{}
err := kubeclient.Get(ctx, key, old)
if err != nil && !apierrors.IsNotFound(err) {
return errors.Wrap(err, "failed to fetch existing azure json")
}
// Create if it wasn't found
if apierrors.IsNotFound(err) {
if err := kubeclient.Create(ctx, newSecret); err != nil && !apierrors.IsAlreadyExists(err) {
return errors.Wrap(err, "failed to create cluster azure json")
}
return nil
}
tag, exists := old.Labels[clusterName]
if !exists || tag != string(infrav1.ResourceLifecycleOwned) {
log.V(2).Info("returning early from json reconcile, user provided secret already exists")
return nil
}
// Otherwise, check ownership and data freshness. Update as necessary
hasOwner := false
for _, ownerRef := range old.OwnerReferences {
if referSameObject(ownerRef, owner) {
hasOwner = true
break
}
}
hasData := equality.Semantic.DeepEqual(old.Data, newSecret.Data)
if hasData && hasOwner {
// no update required
log.V(2).Info("returning early from json reconcile, no update needed")
return nil
}
if !hasOwner {
old.OwnerReferences = append(old.OwnerReferences, owner)
}
if !hasData {
old.Data = newSecret.Data
}
log.V(2).Info("updating azure json")
if err := kubeclient.Update(ctx, old); err != nil {
return errors.Wrap(err, "failed to update cluster azure json when diff was required")
}
log.V(2).Info("done updating azure json")
return nil
}
// GetOwnerMachinePool returns the MachinePool object owning the current resource.
func GetOwnerMachinePool(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*capiv1exp.MachinePool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.GetOwnerMachinePool")
defer done()
for _, ref := range obj.OwnerReferences {
if ref.Kind != "MachinePool" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == capiv1exp.GroupVersion.Group {
return GetMachinePoolByName(ctx, c, obj.Namespace, ref.Name)
}
}
return nil, nil
}
// GetOwnerAzureMachinePool returns the AzureMachinePool object owning the current resource.
func GetOwnerAzureMachinePool(ctx context.Context, c client.Client, obj metav1.ObjectMeta) (*infrav1exp.AzureMachinePool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.GetOwnerAzureMachinePool")
defer done()
for _, ref := range obj.OwnerReferences {
if ref.Kind != "AzureMachinePool" {
continue
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == infrav1exp.GroupVersion.Group {
return GetAzureMachinePoolByName(ctx, c, obj.Namespace, ref.Name)
}
}
return nil, nil
}
// GetMachinePoolByName finds and return a MachinePool object using the specified params.
func GetMachinePoolByName(ctx context.Context, c client.Client, namespace, name string) (*capiv1exp.MachinePool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.GetMachinePoolByName")
defer done()
m := &capiv1exp.MachinePool{}
key := client.ObjectKey{Name: name, Namespace: namespace}
if err := c.Get(ctx, key, m); err != nil {
return nil, err
}
return m, nil
}
// GetAzureMachinePoolByName finds and return an AzureMachinePool object using the specified params.
func GetAzureMachinePoolByName(ctx context.Context, c client.Client, namespace, name string) (*infrav1exp.AzureMachinePool, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.GetAzureMachinePoolByName")
defer done()
m := &infrav1exp.AzureMachinePool{}
key := client.ObjectKey{Name: name, Namespace: namespace}
if err := c.Get(ctx, key, m); err != nil {
return nil, err
}
return m, nil
}
// ShouldDeleteIndividualResources returns false if the resource group is managed and the whole cluster is being deleted
// meaning that we can rely on a single resource group delete operation as opposed to deleting every individual VM resource.
func ShouldDeleteIndividualResources(ctx context.Context, clusterScope *scope.ClusterScope) bool {
ctx, _, done := tele.StartSpanWithLogger(ctx, "controllers.ShouldDeleteIndividualResources")
defer done()
if clusterScope.Cluster.DeletionTimestamp.IsZero() {
return true
}
grpSvc := groups.New(clusterScope)
managed, err := grpSvc.IsManaged(ctx)
// Since this is a best effort attempt to speed up delete, we don't fail the delete if we can't get the RG status.
// Instead, take the long way and delete all resources one by one.
return err != nil || !managed
}
// GetClusterIdentityFromRef returns the AzureClusterIdentity referenced by the AzureCluster.
func GetClusterIdentityFromRef(ctx context.Context, c client.Client, azureClusterNamespace string, ref *corev1.ObjectReference) (*infrav1.AzureClusterIdentity, error) {
identity := &infrav1.AzureClusterIdentity{}
if ref != nil {
namespace := ref.Namespace
if namespace == "" {
namespace = azureClusterNamespace
}
key := client.ObjectKey{Name: ref.Name, Namespace: namespace}
if err := c.Get(ctx, key, identity); err != nil {
return nil, err
}
return identity, nil
}
return nil, nil
}