Skip to content

Commit

Permalink
cherry-pick #1158
Browse files Browse the repository at this point in the history
and #1143
  • Loading branch information
cvvz committed Jan 8, 2024
1 parent 6715063 commit fcdfecf
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
k8s.io/mount-utils v0.28.4
k8s.io/utils v0.0.0-20231127182322-b307cd553661
sigs.k8s.io/cloud-provider-azure v1.27.1-0.20231213062409-f1ce7de3fdcb
sigs.k8s.io/cloud-provider-azure/pkg/azclient/configloader v0.0.0-20231208022044-b9ede3fc98e9 // indirect
sigs.k8s.io/cloud-provider-azure/pkg/azclient/configloader v0.0.0-20231208022044-b9ede3fc98e9
sigs.k8s.io/yaml v1.4.0
)

Expand Down
37 changes: 18 additions & 19 deletions pkg/blob/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/configloader"

azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
providerconfig "sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
Expand All @@ -52,20 +53,15 @@ func IsAzureStackCloud(cloud *azure.Cloud) bool {
}

// getCloudProvider get Azure Cloud Provider
func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent string, allowEmptyCloudConfig bool, kubeAPIQPS float64, kubeAPIBurst int) (*azure.Cloud, error) {
func getCloudProvider(ctx context.Context, kubeconfig, nodeID, secretName, secretNamespace, userAgent string, allowEmptyCloudConfig bool, kubeAPIQPS float64, kubeAPIBurst int) (*azure.Cloud, error) {
var (
config *azure.Config
kubeClient *clientset.Clientset
fromSecret bool
)

az := &azure.Cloud{
InitSecretConfig: azure.InitSecretConfig{
SecretName: secretName,
SecretNamespace: secretNamespace,
CloudConfigKey: "cloud-config",
},
}
az := &azure.Cloud{}

az.Environment.StorageEndpointSuffix = storage.DefaultBaseURL

kubeCfg, err := getKubeConfig(kubeconfig)
Expand All @@ -85,19 +81,26 @@ func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent
}

if kubeClient != nil {
klog.V(2).Infof("reading cloud config from secret %s/%s", az.SecretNamespace, az.SecretName)
az.KubeClient = kubeClient
config, err = az.GetConfigFromSecret()
klog.V(2).Infof("reading cloud config from secret %s/%s", secretNamespace, secretName)
config, err := configloader.Load[azure.Config](ctx, &configloader.K8sSecretLoaderConfig{
K8sSecretConfig: configloader.K8sSecretConfig{
SecretName: secretName,
SecretNamespace: secretNamespace,
CloudConfigKey: "cloud-config",
},
KubeClient: kubeClient,
}, nil)
if err == nil && config != nil {
fromSecret = true
}
if err != nil {
klog.V(2).Infof("InitializeCloudFromSecret: failed to get cloud config from secret %s/%s: %v", az.SecretNamespace, az.SecretName, err)
klog.V(2).Infof("InitializeCloudFromSecret: failed to get cloud config from secret %s/%s: %v", secretNamespace, secretName, err)
}
}

if config == nil {
klog.V(2).Infof("could not read cloud config from secret %s/%s", az.SecretNamespace, az.SecretName)
klog.V(2).Infof("could not read cloud config from secret %s/%s", secretNamespace, secretName)
credFile, ok := os.LookupEnv(DefaultAzureCredentialFileEnv)
if ok && strings.TrimSpace(credFile) != "" {
klog.V(2).Infof("%s env var set as %v", DefaultAzureCredentialFileEnv, credFile)
Expand All @@ -106,15 +109,11 @@ func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent
klog.V(2).Infof("use default %s env var: %v", DefaultAzureCredentialFileEnv, credFile)
}

credFileConfig, err := os.Open(credFile)
config, err = configloader.Load[azure.Config](ctx, nil, &configloader.FileLoaderConfig{
FilePath: credFile,
})
if err != nil {
klog.Warningf("load azure config from file(%s) failed with %v", credFile, err)
} else {
defer credFileConfig.Close()
klog.V(2).Infof("read cloud config from file: %s successfully", credFile)
if config, err = azure.ParseConfig(credFileConfig); err != nil {
klog.Warningf("parse config file(%s) failed with error: %v", credFile, err)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/blob/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ users:
}
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
}
cloud, err := getCloudProvider(test.kubeconfig, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig, 25.0, 50)
cloud, err := getCloudProvider(context.Background(), test.kubeconfig, test.nodeID, "", "", test.userAgent, test.allowEmptyCloudConfig, 25.0, 50)
if !reflect.DeepEqual(err, test.expectedErr) && test.expectedErr != nil && !strings.Contains(err.Error(), test.expectedErr.Error()) {
t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (d *Driver) Run(endpoint, kubeconfig string, testBool bool) {

userAgent := GetUserAgent(d.Name, d.customUserAgent, d.userAgentSuffix)
klog.V(2).Infof("driver userAgent: %s", userAgent)
d.cloud, err = getCloudProvider(kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.kubeAPIQPS, d.kubeAPIBurst)
d.cloud, err = getCloudProvider(context.Background(), kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.kubeAPIQPS, d.kubeAPIBurst)
if err != nil {
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/blob/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
Expand Down Expand Up @@ -905,7 +906,9 @@ func generateSASToken(accountName, accountKey, storageEndpointSuffix string, exp
sasURL, err := serviceClient.GetSASURL(
sas.AccountResourceTypes{Object: true, Service: false, Container: true},
sas.AccountPermissions{Read: true, List: true, Write: true},
sas.AccountServices{Blob: true}, time.Now(), time.Now().Add(time.Duration(expiryTime)*time.Minute))
time.Now().Add(time.Duration(expiryTime)*time.Minute),
&service.GetSASURLOptions{StartTime: to.Ptr(time.Now())},
)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/testsuites/pre_provisioned_sastoken_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func GenerateSASToken(accountName, accountKey string) string {
sasURL, err := serviceClient.GetSASURL(
sas.AccountResourceTypes{Object: true, Service: true, Container: true},
sas.AccountPermissions{Read: true, List: true, Write: true, Delete: true, Add: true, Create: true, Update: true},
sas.AccountServices{Blob: true}, time.Now(), time.Now().Add(10*time.Hour))
time.Now().Add(10*time.Hour), nil)
framework.ExpectNoError(err)
u, err := url.Parse(sasURL)
framework.ExpectNoError(err)
Expand Down

0 comments on commit fcdfecf

Please sign in to comment.