From 425f9acb88516b23ce6b388f1deef3151601f446 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 15 Jun 2020 20:39:09 +0200 Subject: [PATCH 1/2] Set aci user agent for MS metrics --- azure/aci.go | 5 +++++ azure/resourcegroup.go | 2 ++ 2 files changed, 7 insertions(+) diff --git a/azure/aci.go b/azure/aci.go index ebce23044..19c4f09d4 100644 --- a/azure/aci.go +++ b/azure/aci.go @@ -21,6 +21,9 @@ import ( "github.com/docker/api/context/store" ) + +const aciDockerUserAgent = "docker-cli" + func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error { containerGroupsClient, err := getContainerGroupsClient(aciContext.SubscriptionID) if err != nil { @@ -238,6 +241,7 @@ func getContainerGroupsClient(subscriptionID string) (containerinstance.Containe } containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) containerGroupsClient.Authorizer = auth + containerGroupsClient.UserAgent= aciDockerUserAgent containerGroupsClient.PollingDelay = 5 * time.Second containerGroupsClient.RetryAttempts = 30 containerGroupsClient.RetryDuration = 1 * time.Second @@ -250,6 +254,7 @@ func getContainerClient(subscriptionID string) (containerinstance.ContainerClien return containerinstance.ContainerClient{}, err } containerClient := containerinstance.NewContainerClient(subscriptionID) + containerClient.UserAgent=aciDockerUserAgent containerClient.Authorizer = auth return containerClient, nil } diff --git a/azure/resourcegroup.go b/azure/resourcegroup.go index d63770706..c757adcc8 100644 --- a/azure/resourcegroup.go +++ b/azure/resourcegroup.go @@ -94,6 +94,7 @@ func getSubscriptionsClient() (subscription.SubscriptionsClient, error) { return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error()) } subc.Authorizer = authorizer + subc.UserAgent=aciDockerUserAgent return subc, nil } @@ -101,5 +102,6 @@ func getGroupsClient(subscriptionID string) resources.GroupsClient { groupsClient := resources.NewGroupsClient(subscriptionID) authorizer, _ := login.NewAuthorizerFromLogin() groupsClient.Authorizer = authorizer + groupsClient.UserAgent=aciDockerUserAgent return groupsClient } From 885e0d0aa4dac355413ed6618ddd2df70f3f9547 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 15 Jun 2020 21:17:10 +0200 Subject: [PATCH 2/2] common authoriser & user agent setup for all clients --- azure/aci.go | 23 ++++++++++++++--------- azure/resourcegroup.go | 38 +++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/azure/aci.go b/azure/aci.go index 19c4f09d4..7195ae9db 100644 --- a/azure/aci.go +++ b/azure/aci.go @@ -21,7 +21,6 @@ import ( "github.com/docker/api/context/store" ) - const aciDockerUserAgent = "docker-cli" func createACIContainers(ctx context.Context, aciContext store.AciContext, groupDefinition containerinstance.ContainerGroup) error { @@ -235,26 +234,32 @@ func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, conta } func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) { - auth, err := login.NewAuthorizerFromLogin() + containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) + err := setupClient(&containerGroupsClient.Client) if err != nil { return containerinstance.ContainerGroupsClient{}, err } - containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) - containerGroupsClient.Authorizer = auth - containerGroupsClient.UserAgent= aciDockerUserAgent containerGroupsClient.PollingDelay = 5 * time.Second containerGroupsClient.RetryAttempts = 30 containerGroupsClient.RetryDuration = 1 * time.Second return containerGroupsClient, nil } -func getContainerClient(subscriptionID string) (containerinstance.ContainerClient, error) { +func setupClient(aciClient *autorest.Client) error { + aciClient.UserAgent = aciDockerUserAgent auth, err := login.NewAuthorizerFromLogin() + if err != nil { + return err + } + aciClient.Authorizer = auth + return nil +} + +func getContainerClient(subscriptionID string) (containerinstance.ContainerClient, error) { + containerClient := containerinstance.NewContainerClient(subscriptionID) + err := setupClient(&containerClient.Client) if err != nil { return containerinstance.ContainerClient{}, err } - containerClient := containerinstance.NewContainerClient(subscriptionID) - containerClient.UserAgent=aciDockerUserAgent - containerClient.Authorizer = auth return containerClient, nil } diff --git a/azure/resourcegroup.go b/azure/resourcegroup.go index c757adcc8..e20657b6e 100644 --- a/azure/resourcegroup.go +++ b/azure/resourcegroup.go @@ -7,7 +7,6 @@ import ( "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription" "github.com/pkg/errors" - "github.com/docker/api/azure/login" "github.com/docker/api/errdefs" ) @@ -30,13 +29,20 @@ func NewACIResourceGroupHelper() ACIResourceGroupHelper { // GetGroup get a resource group from its name func (mgt aciResourceGroupHelperImpl) GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error) { - gc := getGroupsClient(subscriptionID) + gc, err := getGroupsClient(subscriptionID) + if err != nil { + return resources.Group{}, err + } return gc.Get(ctx, groupName) } // ListGroups list resource groups func (mgt aciResourceGroupHelperImpl) ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error) { - gc := getGroupsClient(subscriptionID) + gc, err := getGroupsClient(subscriptionID) + if err != nil { + return nil, err + } + groupResponse, err := gc.List(ctx, "", nil) if err != nil { return nil, err @@ -48,13 +54,20 @@ func (mgt aciResourceGroupHelperImpl) ListGroups(ctx context.Context, subscripti // CreateOrUpdate create or update a resource group func (mgt aciResourceGroupHelperImpl) CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error) { - gc := getGroupsClient(subscriptionID) + gc, err := getGroupsClient(subscriptionID) + if err != nil { + return resources.Group{}, err + } return gc.CreateOrUpdate(ctx, resourceGroupName, parameters) } // Delete deletes a resource group func (mgt aciResourceGroupHelperImpl) Delete(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) { - gc := getGroupsClient(subscriptionID) + gc, err := getGroupsClient(subscriptionID) + if err != nil { + return err + } + future, err := gc.Delete(ctx, resourceGroupName) if err != nil { return err @@ -89,19 +102,18 @@ func (mgt aciResourceGroupHelperImpl) GetSubscriptionIDs(ctx context.Context) ([ func getSubscriptionsClient() (subscription.SubscriptionsClient, error) { subc := subscription.NewSubscriptionsClient() - authorizer, err := login.NewAuthorizerFromLogin() + err := setupClient(&subc.Client) if err != nil { return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error()) } - subc.Authorizer = authorizer - subc.UserAgent=aciDockerUserAgent return subc, nil } -func getGroupsClient(subscriptionID string) resources.GroupsClient { +func getGroupsClient(subscriptionID string) (resources.GroupsClient, error) { groupsClient := resources.NewGroupsClient(subscriptionID) - authorizer, _ := login.NewAuthorizerFromLogin() - groupsClient.Authorizer = authorizer - groupsClient.UserAgent=aciDockerUserAgent - return groupsClient + err := setupClient(&groupsClient.Client) + if err != nil { + return resources.GroupsClient{}, err + } + return groupsClient, nil }