-
Notifications
You must be signed in to change notification settings - Fork 135
/
azure.go
169 lines (144 loc) · 5.03 KB
/
azure.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
/*
Copyright 2017 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 azurefile
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
)
var (
DefaultAzureCredentialFileEnv = "AZURE_CREDENTIAL_FILE"
DefaultCredFilePathLinux = "/etc/kubernetes/azure.json"
DefaultCredFilePathWindows = "C:\\k\\azure.json"
storageService = "Microsoft.Storage"
)
// GetCloudProvider get Azure Cloud Provider
func GetCloudProvider(kubeconfig string) (*azure.Cloud, error) {
kubeClient, err := getKubeClient(kubeconfig)
if err != nil {
klog.Warningf("get kubeconfig(%s) failed with error: %v", kubeconfig, err)
if !os.IsNotExist(err) && err != rest.ErrNotInCluster {
return nil, fmt.Errorf("failed to get KubeClient: %v", err)
}
}
az := &azure.Cloud{}
if kubeClient != nil {
klog.V(2).Infof("reading cloud config from secret")
az.KubeClient = kubeClient
az.InitializeCloudFromSecret()
}
if az.TenantID == "" || az.SubscriptionID == "" {
klog.V(2).Infof("could not read cloud config from secret")
credFile, ok := os.LookupEnv(DefaultAzureCredentialFileEnv)
if ok && strings.TrimSpace(credFile) != "" {
klog.V(2).Infof("%s env var set as %v", DefaultAzureCredentialFileEnv, credFile)
} else {
if runtime.GOOS == "windows" {
credFile = DefaultCredFilePathWindows
} else {
credFile = DefaultCredFilePathLinux
}
klog.V(2).Infof("use default %s env var: %v", DefaultAzureCredentialFileEnv, credFile)
}
f, err := os.Open(credFile)
if err != nil {
klog.Errorf("Failed to load config from file: %s", credFile)
return nil, fmt.Errorf("Failed to load config from file: %s, cloud not get azure cloud provider", credFile)
}
defer f.Close()
klog.V(2).Infof("read cloud config from file: %s successfully", credFile)
if az, err = azure.NewCloudWithoutFeatureGates(f); err != nil {
return az, err
}
}
if kubeClient != nil {
az.KubeClient = kubeClient
}
return az, nil
}
func getKubeClient(kubeconfig string) (*kubernetes.Clientset, error) {
var (
config *rest.Config
err error
)
if kubeconfig != "" {
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err != nil {
return nil, err
}
} else {
if config, err = rest.InClusterConfig(); err != nil {
return nil, err
}
}
return kubernetes.NewForConfig(config)
}
func updateSubnetServiceEndpoints(ctx context.Context, az *azure.Cloud, subnetLockMap *lockMap) error {
if az == nil {
return fmt.Errorf("the cloud parameter is nil")
}
if subnetLockMap == nil {
return fmt.Errorf("the subnet lockMap parameter is nil")
}
resourceGroup := az.ResourceGroup
if len(az.VnetResourceGroup) > 0 {
resourceGroup = az.VnetResourceGroup
}
location := az.Location
vnetName := az.VnetName
subnetName := az.SubnetName
subnet, err := az.SubnetsClient.Get(ctx, resourceGroup, vnetName, subnetName, "")
if err != nil {
return fmt.Errorf("failed to get the subnet %s under vnet %s: %v", subnetName, vnetName, err)
}
endpointLocaions := []string{location}
storageServiceEndpoint := network.ServiceEndpointPropertiesFormat{
Service: &storageService,
Locations: &endpointLocaions,
}
storageServiceExists := false
if subnet.SubnetPropertiesFormat == nil {
subnet.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{}
}
if subnet.SubnetPropertiesFormat.ServiceEndpoints == nil {
subnet.SubnetPropertiesFormat.ServiceEndpoints = &[]network.ServiceEndpointPropertiesFormat{}
}
serviceEndpoints := *subnet.SubnetPropertiesFormat.ServiceEndpoints
for _, v := range serviceEndpoints {
if v.Service != nil && *v.Service == storageService {
storageServiceExists = true
klog.V(4).Infof("serviceEndpoint(%s) is already in subnet(%s)", storageService, subnetName)
break
}
}
if !storageServiceExists {
serviceEndpoints = append(serviceEndpoints, storageServiceEndpoint)
subnet.SubnetPropertiesFormat.ServiceEndpoints = &serviceEndpoints
lockKey := resourceGroup + vnetName + subnetName
subnetLockMap.LockEntry(lockKey)
defer subnetLockMap.UnlockEntry(lockKey)
err = az.SubnetsClient.CreateOrUpdate(context.Background(), resourceGroup, vnetName, subnetName, subnet)
if err != nil {
return fmt.Errorf("failed to update the subnet %s under vnet %s: %v", subnetName, vnetName, err)
}
klog.V(4).Infof("serviceEndpoint(%s) is appended in subnet(%s)", storageService, subnetName)
}
return nil
}