/
helper.go
142 lines (119 loc) · 4.24 KB
/
helper.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package infrastructure
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/gardener/gardener/extensions/pkg/controller"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
api "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
apiv1alpha1 "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure/v1alpha1"
azureclient "github.com/gardener/gardener-extension-provider-azure/pkg/azure/client"
)
// MakeCluster returns a cluster object used for testing.
func MakeCluster(pods, services string, region string, countFaultDomain, countUpdateDomain int32) *controller.Cluster {
var (
shoot = gardencorev1beta1.Shoot{
Spec: gardencorev1beta1.ShootSpec{
Networking: &gardencorev1beta1.Networking{
Pods: &pods,
Services: &services,
},
},
}
cloudProfileConfig = apiv1alpha1.CloudProfileConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: apiv1alpha1.SchemeGroupVersion.String(),
Kind: "CloudProfileConfig",
},
CountFaultDomains: []apiv1alpha1.DomainCount{
{Region: region, Count: countFaultDomain},
},
CountUpdateDomains: []apiv1alpha1.DomainCount{
{Region: region, Count: countUpdateDomain},
},
}
cloudProfileConfigJSON, _ = json.Marshal(cloudProfileConfig)
cloudProfile = gardencorev1beta1.CloudProfile{
Spec: gardencorev1beta1.CloudProfileSpec{
ProviderConfig: &runtime.RawExtension{
Raw: cloudProfileConfigJSON,
},
},
}
)
return &controller.Cluster{
Shoot: &shoot,
CloudProfile: &cloudProfile,
}
}
// IsShootResourceGroupAvailable determines if the managed resource group exists on Azure.
func IsShootResourceGroupAvailable(ctx context.Context, factory azureclient.Factory, infra *extensionsv1alpha1.Infrastructure, infraConfig *api.InfrastructureConfig) (bool, error) {
if infraConfig.ResourceGroup != nil {
return true, nil
}
groupClient, err := factory.Group()
if err != nil {
return false, err
}
resourceGroup, err := groupClient.Get(ctx, infra.Namespace)
if err != nil {
return false, err
}
if resourceGroup == nil {
return false, nil
}
return true, nil
}
// DeleteNodeSubnetIfExists will delete the nodes subnet(s) if exists.
func DeleteNodeSubnetIfExists(ctx context.Context, factory azureclient.Factory, infra *extensionsv1alpha1.Infrastructure, infraConfig *api.InfrastructureConfig) error {
if infraConfig.Networks.VNet.ResourceGroup == nil || infraConfig.Networks.VNet.Name == nil {
return nil
}
subnetClient, err := factory.Subnet()
if err != nil {
return err
}
subnets, err := subnetClient.List(ctx, *infraConfig.Networks.VNet.ResourceGroup, *infraConfig.Networks.VNet.Name)
if err != nil {
return err
}
subnetNamePrefix := fmt.Sprintf("%s-nodes", infra.Namespace)
for _, subnet := range subnets {
if !strings.HasPrefix(*subnet.Name, subnetNamePrefix) {
continue
}
if err := subnetClient.Delete(ctx, *infraConfig.Networks.VNet.ResourceGroup, *infraConfig.Networks.VNet.Name, *subnet.Name); err != nil {
return err
}
}
return nil
}
// DeleteShootResourceGroupIfExists will delete the shoot's resource group if it exists.
func DeleteShootResourceGroupIfExists(ctx context.Context, factory azureclient.Factory, infra *extensionsv1alpha1.Infrastructure, cfg *api.InfrastructureConfig, status *api.InfrastructureStatus) error {
// skip if using user resource group.
if cfg.ResourceGroup != nil {
return nil
}
groupClient, err := factory.Group()
if err != nil {
return err
}
return groupClient.Delete(ctx, ShootResourceGroupName(infra, cfg, status))
}
// ShootResourceGroupName returns the expected name of the resource group.
func ShootResourceGroupName(infra *extensionsv1alpha1.Infrastructure, cfg *api.InfrastructureConfig, status *api.InfrastructureStatus) string {
if cfg.ResourceGroup != nil {
return cfg.ResourceGroup.Name
}
if status != nil && len(status.ResourceGroup.Name) > 0 {
return status.ResourceGroup.Name
}
return infra.Namespace
}