Skip to content

Commit 1b8e185

Browse files
committed
Remove the dependency of azure.json in node server
Signed-off-by: ZeroMagic <[email protected]>
1 parent ebec858 commit 1b8e185

File tree

6 files changed

+54
-26
lines changed

6 files changed

+54
-26
lines changed

pkg/blob/azure.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ func IsAzureStackCloud(cloud *azureprovider.Cloud) bool {
4646
return strings.EqualFold(cloud.Config.Cloud, "AZURESTACKCLOUD")
4747
}
4848

49-
// GetCloudProvider get Azure Cloud Provider
50-
func GetCloudProvider(kubeconfig string) (*azureprovider.Cloud, error) {
49+
// getCloudProvider get Azure Cloud Provider
50+
func getCloudProvider(kubeconfig string) (*azureprovider.Cloud, error) {
5151
kubeClient, err := getKubeClient(kubeconfig)
5252
if err != nil && !os.IsNotExist(err) && err != rest.ErrNotInCluster {
5353
return nil, fmt.Errorf("failed to get KubeClient: %v", err)
5454
}
5555

5656
az := &azureprovider.Cloud{}
57+
5758
if kubeClient != nil {
5859
klog.V(2).Infof("reading cloud config from secret")
5960
az.KubeClient = kubeClient
6061
az.InitializeCloudFromSecret()
6162
}
6263

63-
if az.TenantID == "" || az.SubscriptionID == "" {
64+
if az.TenantID == "" || az.SubscriptionID == "" || az.ResourceGroup == "" {
6465
klog.V(2).Infof("could not read cloud config from secret")
6566
credFile, ok := os.LookupEnv(DefaultAzureCredentialFileEnv)
6667
if ok {
@@ -86,6 +87,7 @@ func GetCloudProvider(kubeconfig string) (*azureprovider.Cloud, error) {
8687
if kubeClient != nil {
8788
az.KubeClient = kubeClient
8889
}
90+
8991
return az, nil
9092
}
9193

pkg/blob/azure_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
azure2 "sigs.k8s.io/cloud-provider-azure/pkg/provider"
3131
)
3232

33-
// TestGetCloudProvider tests the func GetCloudProvider().
33+
// TestGetCloudProvider tests the func getCloudProvider().
3434
// To run this unit test successfully, need to ensure /etc/kubernetes/azure.json nonexistent.
3535
func TestGetCloudProvider(t *testing.T) {
36+
3637
fakeCredFile := "fake-cred-file.json"
3738
fakeKubeConfig := "fake-kube-config"
3839
emptyKubeConfig := "empty-kube-config"
@@ -138,7 +139,7 @@ users:
138139
}
139140
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
140141
}
141-
_, err := GetCloudProvider(test.kubeconfig)
142+
_, err := getCloudProvider(test.kubeconfig)
142143
if !reflect.DeepEqual(err, test.expectedErr) {
143144
t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
144145
}
@@ -226,6 +227,7 @@ func TestGetKeyVaultSecretContent(t *testing.T) {
226227
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
227228
}
228229
}
230+
229231
func createTestFile(path string) error {
230232
f, err := os.Create(path)
231233
if err != nil {

pkg/blob/blob.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
volumeIDTemplate = "%s#%s#%s"
4747
secretNameTemplate = "azure-storage-account-%s-secret"
4848
serverNameField = "server"
49+
blobStorageEndPointField = "blobstorageendpoint"
4950
tagsField = "tags"
5051
protocolField = "protocol"
5152
accountNameField = "accountname"
@@ -115,17 +116,35 @@ func (d *Driver) Run(endpoint, kubeconfig string, testBool bool) {
115116
}
116117
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
117118

118-
cloud, err := GetCloudProvider(kubeconfig)
119-
if err != nil || cloud.TenantID == "" || cloud.SubscriptionID == "" {
120-
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
121-
}
122-
d.cloud = cloud
123-
119+
// For the controller server, the NodeID is nil. For the node server, the NodeID is not nil.
124120
if d.NodeID == "" {
121+
cloud, err := getCloudProvider(kubeconfig)
122+
if err != nil || cloud.TenantID == "" || cloud.SubscriptionID == "" {
123+
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
124+
}
125+
d.cloud = cloud
125126
// Disable UseInstanceMetadata for controller to mitigate a timeout issue using IMDS
126127
// https://github.com/kubernetes-sigs/azuredisk-csi-driver/issues/168
127128
klog.Infoln("disable UseInstanceMetadata for controller")
128129
d.cloud.Config.UseInstanceMetadata = false
130+
klog.Infoln("Starting the controller server...")
131+
} else {
132+
cloud, err := getCloudProvider(kubeconfig)
133+
if err == nil {
134+
d.cloud = cloud
135+
} else {
136+
klog.V(2).Infof("there is no azure.json provided for node server")
137+
d.cloud = &azure.Cloud{}
138+
kubeClient, err := getKubeClient(kubeconfig)
139+
if err != nil {
140+
klog.Infof("failed to get KubeClient: %v", err)
141+
}
142+
if kubeClient != nil {
143+
d.cloud.KubeClient = kubeClient
144+
}
145+
}
146+
147+
klog.V(2).Infof("Starting the node server, nodeID is(%s)", d.NodeID)
129148
}
130149

131150
d.mounter = &mount.SafeFormatAndMount{

pkg/blob/nodeserver.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
135135
attrib := req.GetVolumeContext()
136136
secrets := req.GetSecrets()
137137

138-
var blobStorageEndPoint, protocol string
138+
var serverAddress, blobStorageEndPoint, protocol string
139139
for k, v := range attrib {
140140
switch strings.ToLower(k) {
141141
case serverNameField:
142-
blobStorageEndPoint = v
142+
serverAddress = v
143143
case protocolField:
144144
protocol = v
145+
case blobStorageEndPointField:
146+
blobStorageEndPoint = v
145147
}
146148
}
147149

@@ -151,15 +153,19 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
151153
}
152154

153155
if strings.TrimSpace(blobStorageEndPoint) == "" {
156+
blobStorageEndPoint = "core.windows.net"
157+
}
158+
159+
if strings.TrimSpace(serverAddress) == "" {
154160
// server address is "accountname.blob.core.windows.net" by default
155-
blobStorageEndPoint = fmt.Sprintf("%s.blob.%s", accountName, d.cloud.Environment.StorageEndpointSuffix)
161+
serverAddress = fmt.Sprintf("%s.blob.%s", accountName, blobStorageEndPoint)
156162
}
157163

158164
if protocol == nfs {
159-
klog.V(2).Infof("target %v\nprotocol %v\n\nvolumeId %v\ncontext %v\nmountflags %v\nblobStorageEndPoint %v",
160-
targetPath, protocol, volumeID, attrib, mountFlags, blobStorageEndPoint)
165+
klog.V(2).Infof("target %v\nprotocol %v\n\nvolumeId %v\ncontext %v\nmountflags %v\nserverAddress %v",
166+
targetPath, protocol, volumeID, attrib, mountFlags, serverAddress)
161167

162-
source := fmt.Sprintf("%s:/%s/%s", blobStorageEndPoint, accountName, containerName)
168+
source := fmt.Sprintf("%s:/%s/%s", serverAddress, accountName, containerName)
163169
mountOptions := util.JoinMountOptions(mountFlags, []string{"sec=sys,vers=3,nolock"})
164170
if err := wait.PollImmediate(1*time.Second, 2*time.Minute, func() (bool, error) {
165171
return true, d.mounter.MountSensitive(source, targetPath, nfs, mountOptions, []string{})
@@ -186,12 +192,12 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
186192
args = args + " " + opt
187193
}
188194

189-
klog.V(2).Infof("target %v\nprotocol %v\n\nvolumeId %v\ncontext %v\nmountflags %v\nmountOptions %v\nargs %v\nblobStorageEndPoint %v",
190-
targetPath, protocol, volumeID, attrib, mountFlags, mountOptions, args, blobStorageEndPoint)
195+
klog.V(2).Infof("target %v\nprotocol %v\n\nvolumeId %v\ncontext %v\nmountflags %v\nmountOptions %v\nargs %v\nserverAddress %v",
196+
targetPath, protocol, volumeID, attrib, mountFlags, mountOptions, args, serverAddress)
191197
cmd := exec.Command("blobfuse", strings.Split(args, " ")...)
192198

193199
cmd.Env = append(os.Environ(), "AZURE_STORAGE_ACCOUNT="+accountName)
194-
cmd.Env = append(cmd.Env, "AZURE_STORAGE_BLOB_ENDPOINT="+blobStorageEndPoint)
200+
cmd.Env = append(cmd.Env, "AZURE_STORAGE_BLOB_ENDPOINT="+serverAddress)
195201
cmd.Env = append(cmd.Env, authEnv...)
196202

197203
output, err := cmd.CombinedOutput()

pkg/blobplugin/main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ func main() {
5454
fmt.Println(info)
5555
os.Exit(0)
5656
}
57-
if *nodeID == "" {
58-
klog.Warning("nodeid is empty")
59-
}
6057

6158
exportMetrics()
6259
handle()

test/sanity/run-test.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ function cleanup {
2525

2626
trap cleanup EXIT
2727

28-
readonly endpoint="unix:///tmp/csi.sock"
28+
readonly controllerendpoint="unix:///tmp/csi-controller.sock"
29+
readonly nodeendpoint="unix:///tmp/csi-node.sock"
2930
nodeid="CSINode"
3031
if [[ "$#" -gt 0 ]] && [[ -n "$1" ]]; then
3132
nodeid="$1"
3233
fi
3334

34-
_output/blobplugin --endpoint "$endpoint" --nodeid "$nodeid" -v=5 &
35+
_output/blobplugin --endpoint "$controllerendpoint" -v=5 &
36+
_output/blobplugin --endpoint "$nodeendpoint" --nodeid "$nodeid" -v=5 &
3537

3638
echo "Begin to run sanity test..."
3739
readonly CSI_SANITY_BIN='csi-sanity'
38-
"$CSI_SANITY_BIN" --ginkgo.v --csi.endpoint=$endpoint -ginkgo.skip="should fail when requesting to create a volume with already existing name and different capacity|should be idempotent"
40+
"$CSI_SANITY_BIN" --ginkgo.v --csi.endpoint=$nodeendpoint --csi.controllerendpoint=$controllerendpoint -ginkgo.skip="should fail when requesting to create a volume with already existing name and different capacity|should be idempotent"

0 commit comments

Comments
 (0)