Skip to content

Commit

Permalink
feat: add driver userAgent in ARM request
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Aug 9, 2021
1 parent 7fa913b commit ac18a0b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pkg/azurefile/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ var (
)

// getCloudProvider get Azure Cloud Provider
func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace string) (*azureprovider.Cloud, error) {
func getCloudProvider(kubeconfig, nodeID, secretName, secretNamespace, userAgent string) (*azureprovider.Cloud, error) {
az := &azureprovider.Cloud{
InitSecretConfig: azureprovider.InitSecretConfig{
SecretName: secretName,
SecretNamespace: secretNamespace,
CloudConfigKey: "cloud-config",
},
}
az.UserAgent = userAgent
kubeClient, err := getKubeClient(kubeconfig)
if err != nil && !os.IsNotExist(err) && err != rest.ErrNotInCluster {
return az, fmt.Errorf("failed to get KubeClient: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/azurefile/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ users:
}
os.Setenv(DefaultAzureCredentialFileEnv, fakeCredFile)
}
_, err := getCloudProvider(test.kubeconfig, "", "", "")
_, err := getCloudProvider(test.kubeconfig, "", "", "", "")
if !testutil.AssertError(err, &test.expectedErr) {
t.Errorf("desc: %s,\n input: %q, getCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/azurefile/azurefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ func (d *Driver) Run(endpoint, kubeconfig string, testBool bool) {
}
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)

d.cloud, err = getCloudProvider(kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace)
userAgent := GetUserAgent(d.Name)
klog.V(2).Infof("driver userAgent: %s", userAgent)
d.cloud, err = getCloudProvider(kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent)
if err != nil {
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/azurefile/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/Azure/go-autorest/autorest/to"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/ptypes"
"github.com/pborman/uuid"

"google.golang.org/grpc/codes"
Expand All @@ -37,6 +36,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"

timestamppb "google.golang.org/protobuf/types/known/timestamppb"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient"
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
"sigs.k8s.io/cloud-provider-azure/pkg/metrics"
Expand Down Expand Up @@ -691,9 +691,9 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
}
if exists {
klog.V(2).Infof("snapshot(%s) already exists", snapshotName)
tp, err := ptypes.TimestampProto(item.Properties.LastModified)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to covert creation timestamp: %v", err)
tp := timestamppb.New(item.Properties.LastModified)
if tp == nil {
return nil, status.Errorf(codes.Internal, "Failed to convert timestamp(%v)", item.Properties.LastModified)
}
return &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
Expand Down Expand Up @@ -724,9 +724,9 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
return nil, status.Errorf(codes.Internal, "failed to get snapshot properties from (%s): %v", snapshotShare.Snapshot(), err)
}

tp, err := ptypes.TimestampProto(properties.LastModified())
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to covert creation timestamp: %v", err)
tp := timestamppb.New(properties.LastModified())
if tp == nil {
return nil, status.Errorf(codes.Internal, "Failed to convert timestamp(%v)", properties.LastModified())
}

createResp := &csi.CreateSnapshotResponse{
Expand Down
5 changes: 5 additions & 0 deletions pkg/azurefile/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ func GetVersionYAML(driverName string) (string, error) {
}
return strings.TrimSpace(string(marshalled)), nil
}

// GetUserAgent returns user agent of the driver
func GetUserAgent(driverName string) string {
return fmt.Sprintf("%s/%s %s/%s (%s-%s) %s/%s", driverName, driverVersion, runtime.Compiler, runtime.Version(), runtime.GOARCH, runtime.GOOS, gitCommit, buildDate)
}

0 comments on commit ac18a0b

Please sign in to comment.