Skip to content

Commit 8c424b2

Browse files
authored
[CNI] Specify CNSClient HTTPClient timeout (#862)
* cnsclient timeout * update request timeout
1 parent 7e834d4 commit 8c424b2

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

cni/network/invoker_cns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type IPv4ResultInfo struct {
3939

4040
func NewCNSInvoker(podName, namespace string) (*CNSIPAMInvoker, error) {
4141
cnsURL := "http://localhost:" + strconv.Itoa(cnsPort)
42-
cnsClient, err := cnsclient.InitCnsClient(cnsURL)
42+
cnsClient, err := cnsclient.InitCnsClient(cnsURL, defaultRequestTimeout)
4343

4444
return &CNSIPAMInvoker{
4545
podName: podName,

cni/network/network.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const (
3636
dockerNetworkOption = "com.docker.network.generic"
3737
opModeTransparent = "transparent"
3838
// Supported IP version. Currently support only IPv4
39-
ipVersion = "4"
40-
ipamV6 = "azure-vnet-ipamv6"
39+
ipVersion = "4"
40+
ipamV6 = "azure-vnet-ipamv6"
41+
defaultRequestTimeout = 15 * time.Second
4142
)
4243

4344
// CNI Operation Types
@@ -395,7 +396,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) error {
395396

396397
if nwCfg.MultiTenancy {
397398
// Initialize CNSClient
398-
cnsclient.InitCnsClient(nwCfg.CNSUrl)
399+
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
399400
}
400401

401402
for _, ns := range nwCfg.PodNamespaceForDualNetwork {
@@ -743,7 +744,7 @@ func (plugin *netPlugin) Get(args *cniSkel.CmdArgs) error {
743744

744745
if nwCfg.MultiTenancy {
745746
// Initialize CNSClient
746-
cnsclient.InitCnsClient(nwCfg.CNSUrl)
747+
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
747748
}
748749

749750
// Initialize values from network config.
@@ -853,7 +854,7 @@ func (plugin *netPlugin) Delete(args *cniSkel.CmdArgs) error {
853854

854855
if nwCfg.MultiTenancy {
855856
// Initialize CNSClient
856-
cnsclient.InitCnsClient(nwCfg.CNSUrl)
857+
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
857858
}
858859

859860
switch nwCfg.Ipam.Type {
@@ -1054,7 +1055,7 @@ func (plugin *netPlugin) Update(args *cniSkel.CmdArgs) error {
10541055

10551056
// now query CNS to get the target routes that should be there in the networknamespace (as a result of update)
10561057
log.Printf("Going to collect target routes for [name=%v, namespace=%v] from CNS.", k8sPodName, k8sNamespace)
1057-
if cnsClient, err = cnsclient.InitCnsClient(nwCfg.CNSUrl); err != nil {
1058+
if cnsClient, err = cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout); err != nil {
10581059
log.Printf("Initializing CNS client error in CNI Update%v", err)
10591060
log.Printf(err.Error())
10601061
return plugin.Errorf(err.Error())

cns/cnsclient/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"sort"
77
"strings"
8+
"time"
89

910
"github.com/Azure/azure-container-networking/cns"
1011
"github.com/Azure/azure-container-networking/cns/restserver"
@@ -46,7 +47,7 @@ func HandleCNSClientCommands(cmd, arg string) error {
4647
cnsIPAddress := os.Getenv(envCNSIPAddress)
4748
cnsPort := os.Getenv(envCNSPort)
4849

49-
cnsClient, err := InitCnsClient("http://" + cnsIPAddress + ":" + cnsPort)
50+
cnsClient, err := InitCnsClient("http://"+cnsIPAddress+":"+cnsPort, 5*time.Second)
5051
if err != nil {
5152
return err
5253
}

cns/cnsclient/cnsclient.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"time"
89

910
"github.com/Azure/azure-container-networking/cns"
1011
"github.com/Azure/azure-container-networking/cns/restserver"
@@ -14,6 +15,7 @@ import (
1415
// CNSClient specifies a client to connect to Ipam Plugin.
1516
type CNSClient struct {
1617
connectionURL string
18+
httpc http.Client
1719
}
1820

1921
const (
@@ -26,14 +28,17 @@ var (
2628
)
2729

2830
// InitCnsClient initializes new cns client and returns the object
29-
func InitCnsClient(url string) (*CNSClient, error) {
31+
func InitCnsClient(url string, requestTimeout time.Duration) (*CNSClient, error) {
3032
if cnsClient == nil {
3133
if url == "" {
3234
url = defaultCnsURL
3335
}
3436

3537
cnsClient = &CNSClient{
3638
connectionURL: url,
39+
httpc: http.Client{
40+
Timeout: requestTimeout,
41+
},
3742
}
3843
}
3944

@@ -60,7 +65,6 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
6065
body bytes.Buffer
6166
)
6267

63-
httpc := &http.Client{}
6468
url := cnsClient.connectionURL + cns.GetNetworkContainerByOrchestratorContext
6569
log.Printf("GetNetworkConfiguration url %v", url)
6670

@@ -74,7 +78,7 @@ func (cnsClient *CNSClient) GetNetworkConfiguration(orchestratorContext []byte)
7478
return nil, &CNSClientError{restserver.UnexpectedError, err}
7579
}
7680

77-
res, err := httpc.Post(url, contentTypeJSON, &body)
81+
res, err := cnsClient.httpc.Post(url, contentTypeJSON, &body)
7882
if err != nil {
7983
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
8084
return nil, &CNSClientError{restserver.UnexpectedError, err}
@@ -114,7 +118,6 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(networkContainerID string)
114118
body bytes.Buffer
115119
)
116120

117-
httpc := &http.Client{}
118121
url := cnsClient.connectionURL + cns.CreateHostNCApipaEndpointPath
119122
log.Printf("CreateHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
120123

@@ -127,7 +130,7 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(networkContainerID string)
127130
return "", err
128131
}
129132

130-
res, err := httpc.Post(url, contentTypeJSON, &body)
133+
res, err := cnsClient.httpc.Post(url, contentTypeJSON, &body)
131134
if err != nil {
132135
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
133136
return "", err
@@ -162,7 +165,6 @@ func (cnsClient *CNSClient) CreateHostNCApipaEndpoint(networkContainerID string)
162165
func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string) error {
163166
var body bytes.Buffer
164167

165-
httpc := &http.Client{}
166168
url := cnsClient.connectionURL + cns.DeleteHostNCApipaEndpointPath
167169
log.Printf("DeleteHostNCApipaEndpoint url: %v for NC: %s", url, networkContainerID)
168170

@@ -176,7 +178,7 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
176178
return err
177179
}
178180

179-
res, err := httpc.Post(url, contentTypeJSON, &body)
181+
res, err := cnsClient.httpc.Post(url, contentTypeJSON, &body)
180182
if err != nil {
181183
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
182184
return err
@@ -224,7 +226,6 @@ func (cnsClient *CNSClient) RequestIPAddress(orchestratorContext []byte) (*cns.I
224226

225227
var body bytes.Buffer
226228

227-
httpc := &http.Client{}
228229
url := cnsClient.connectionURL + cns.RequestIPConfig
229230

230231
payload := &cns.IPConfigRequest{
@@ -237,7 +238,7 @@ func (cnsClient *CNSClient) RequestIPAddress(orchestratorContext []byte) (*cns.I
237238
return response, err
238239
}
239240

240-
res, err = httpc.Post(url, contentTypeJSON, &body)
241+
res, err = cnsClient.httpc.Post(url, contentTypeJSON, &body)
241242
if err != nil {
242243
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
243244
return response, err
@@ -273,7 +274,6 @@ func (cnsClient *CNSClient) ReleaseIPAddress(orchestratorContext []byte) error {
273274
body bytes.Buffer
274275
)
275276

276-
httpc := &http.Client{}
277277
url := cnsClient.connectionURL + cns.ReleaseIPConfig
278278
log.Printf("ReleaseIPAddress url %v", url)
279279

@@ -287,7 +287,7 @@ func (cnsClient *CNSClient) ReleaseIPAddress(orchestratorContext []byte) error {
287287
return err
288288
}
289289

290-
res, err = httpc.Post(url, contentTypeJSON, &body)
290+
res, err = cnsClient.httpc.Post(url, contentTypeJSON, &body)
291291
if err != nil {
292292
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
293293
return err

cns/cnsclient/cnsclient_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"strconv"
1313
"testing"
14+
"time"
1415

1516
nnc "github.com/Azure/azure-container-networking/nodenetworkconfig/api/v1alpha"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -215,7 +216,7 @@ func TestCNSClientRequestAndRelease(t *testing.T) {
215216

216217
secondaryIps := make([]string, 0)
217218
secondaryIps = append(secondaryIps, desiredIpAddress)
218-
cnsClient, _ := InitCnsClient("")
219+
cnsClient, _ := InitCnsClient("", 2*time.Second)
219220

220221
addTestStateToRestServer(t, secondaryIps)
221222

@@ -289,7 +290,7 @@ func TestCNSClientPodContextApi(t *testing.T) {
289290
desiredIpAddress := "10.0.0.5"
290291

291292
secondaryIps := []string{desiredIpAddress}
292-
cnsClient, _ := InitCnsClient("")
293+
cnsClient, _ := InitCnsClient("", 2*time.Second)
293294

294295
addTestStateToRestServer(t, secondaryIps)
295296

@@ -329,7 +330,7 @@ func TestCNSClientDebugAPI(t *testing.T) {
329330
desiredIpAddress := "10.0.0.5"
330331

331332
secondaryIps := []string{desiredIpAddress}
332-
cnsClient, _ := InitCnsClient("")
333+
cnsClient, _ := InitCnsClient("", 2*time.Second)
333334

334335
addTestStateToRestServer(t, secondaryIps)
335336

0 commit comments

Comments
 (0)