forked from Azure/AgentBaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.go
131 lines (114 loc) · 4.04 KB
/
cloud.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
package e2e_test
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-armbalancer"
)
type azureClient struct {
coreClient *azcore.Client
vmssClient *armcompute.VirtualMachineScaleSetsClient
vmssVMClient *armcompute.VirtualMachineScaleSetVMsClient
vnetClient *armnetwork.VirtualNetworksClient
resourceClient *armresources.Client
resourceGroupClient *armresources.ResourceGroupsClient
aksClient *armcontainerservice.ManagedClustersClient
}
func newAzureClient(subscription string) (*azureClient, error) {
httpClient := &http.Client{
// use a bunch of connections for load balancing
// ensure all timeouts are defined and reasonable
// ensure TLS1.2+ and HTTP2
Transport: armbalancer.New(armbalancer.Options{
PoolSize: 100,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
},
}),
}
logger := runtime.NewLogPolicy(&policy.LogOptions{
IncludeBody: true,
})
opts := &arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Transport: httpClient,
PerCallPolicies: []policy.Policy{
logger,
},
},
}
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, fmt.Errorf("failed to create credential: %w", err)
}
plOpts := runtime.PipelineOptions{}
clOpts := &azcore.ClientOptions{
Transport: httpClient,
PerCallPolicies: []policy.Policy{
runtime.NewBearerTokenPolicy(credential, []string{defaultAzureTokenScope}, nil),
logger,
},
}
// purely for telemetry, entirely unused today
coreClient, err := azcore.NewClient("agentbakere2e.e2e_test", "v0.0.0", plOpts, clOpts)
if err != nil {
return nil, fmt.Errorf("failed to create core client: %w", err)
}
aksClient, err := armcontainerservice.NewManagedClustersClient(subscription, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create aks client: %w", err)
}
vmssClient, err := armcompute.NewVirtualMachineScaleSetsClient(subscription, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create vmss client: %w", err)
}
vmssVMClient, err := armcompute.NewVirtualMachineScaleSetVMsClient(subscription, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create vmss vm client: %w", err)
}
resourceClient, err := armresources.NewClient(subscription, credential, opts)
if err != nil {
return nil, fmt.Errorf("failed to create resource client: %w", err)
}
resourceGroupClient, err := armresources.NewResourceGroupsClient(subscription, credential, opts)
if err != nil {
return nil, fmt.Errorf("failed to create resource group client: %w", err)
}
vnetClient, err := armnetwork.NewVirtualNetworksClient(subscription, credential, nil)
if err != nil {
return nil, fmt.Errorf("failed to create vnet client: %w", err)
}
var cloud = &azureClient{
coreClient: coreClient,
aksClient: aksClient,
resourceClient: resourceClient,
resourceGroupClient: resourceGroupClient,
vmssClient: vmssClient,
vmssVMClient: vmssVMClient,
vnetClient: vnetClient,
}
return cloud, nil
}