Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cni/network/invoker_cns.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type IPv4ResultInfo struct {

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

return &CNSIPAMInvoker{
podName: podName,
Expand Down
13 changes: 7 additions & 6 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const (
dockerNetworkOption = "com.docker.network.generic"
opModeTransparent = "transparent"
// Supported IP version. Currently support only IPv4
ipVersion = "4"
ipamV6 = "azure-vnet-ipamv6"
ipVersion = "4"
ipamV6 = "azure-vnet-ipamv6"
defaultRequestTimeout = 15 * time.Second
)

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

if nwCfg.MultiTenancy {
// Initialize CNSClient
cnsclient.InitCnsClient(nwCfg.CNSUrl)
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
}

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

if nwCfg.MultiTenancy {
// Initialize CNSClient
cnsclient.InitCnsClient(nwCfg.CNSUrl)
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
}

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

if nwCfg.MultiTenancy {
// Initialize CNSClient
cnsclient.InitCnsClient(nwCfg.CNSUrl)
cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout)
}

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

// now query CNS to get the target routes that should be there in the networknamespace (as a result of update)
log.Printf("Going to collect target routes for [name=%v, namespace=%v] from CNS.", k8sPodName, k8sNamespace)
if cnsClient, err = cnsclient.InitCnsClient(nwCfg.CNSUrl); err != nil {
if cnsClient, err = cnsclient.InitCnsClient(nwCfg.CNSUrl, defaultRequestTimeout); err != nil {
log.Printf("Initializing CNS client error in CNI Update%v", err)
log.Printf(err.Error())
return plugin.Errorf(err.Error())
Expand Down
3 changes: 2 additions & 1 deletion cns/cnsclient/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"sort"
"strings"
"time"

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

cnsClient, err := InitCnsClient("http://" + cnsIPAddress + ":" + cnsPort)
cnsClient, err := InitCnsClient("http://"+cnsIPAddress+":"+cnsPort, 5*time.Second)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions cns/cnsclient/cnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/restserver"
Expand All @@ -14,6 +15,7 @@ import (
// CNSClient specifies a client to connect to Ipam Plugin.
type CNSClient struct {
connectionURL string
httpc http.Client
}

const (
Expand All @@ -26,14 +28,17 @@ var (
)

// InitCnsClient initializes new cns client and returns the object
func InitCnsClient(url string) (*CNSClient, error) {
func InitCnsClient(url string, requestTimeout time.Duration) (*CNSClient, error) {
if cnsClient == nil {
if url == "" {
url = defaultCnsURL
}

cnsClient = &CNSClient{
connectionURL: url,
httpc: http.Client{
Timeout: requestTimeout,
},
}
}

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

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

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

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

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

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

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

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

Expand All @@ -176,7 +178,7 @@ func (cnsClient *CNSClient) DeleteHostNCApipaEndpoint(networkContainerID string)
return err
}

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

var body bytes.Buffer

httpc := &http.Client{}
url := cnsClient.connectionURL + cns.RequestIPConfig

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

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

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

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

res, err = httpc.Post(url, contentTypeJSON, &body)
res, err = cnsClient.httpc.Post(url, contentTypeJSON, &body)
if err != nil {
log.Errorf("[Azure CNSClient] HTTP Post returned error %v", err.Error())
return err
Expand Down
7 changes: 4 additions & 3 deletions cns/cnsclient/cnsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"reflect"
"strconv"
"testing"
"time"

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

secondaryIps := make([]string, 0)
secondaryIps = append(secondaryIps, desiredIpAddress)
cnsClient, _ := InitCnsClient("")
cnsClient, _ := InitCnsClient("", 2*time.Second)

addTestStateToRestServer(t, secondaryIps)

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

secondaryIps := []string{desiredIpAddress}
cnsClient, _ := InitCnsClient("")
cnsClient, _ := InitCnsClient("", 2*time.Second)

addTestStateToRestServer(t, secondaryIps)

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

secondaryIps := []string{desiredIpAddress}
cnsClient, _ := InitCnsClient("")
cnsClient, _ := InitCnsClient("", 2*time.Second)

addTestStateToRestServer(t, secondaryIps)

Expand Down