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.
1516type CNSClient struct {
1617 connectionURL string
18+ httpc http.Client
1719}
1820
1921const (
@@ -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)
162165func (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
0 commit comments