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 cns/cnsclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import "github.com/Azure/azure-container-networking/cns"

// APIClient interface to update cns state
type APIClient interface {
InitCNSState(*cns.CreateNetworkContainerRequest, map[string]*cns.KubernetesPodInfo) error
ReconcileNCState(*cns.CreateNetworkContainerRequest, map[string]cns.KubernetesPodInfo) error
CreateOrUpdateNC(cns.CreateNetworkContainerRequest) error
}
13 changes: 7 additions & 6 deletions cns/cnsclient/httpapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ func (client *Client) CreateOrUpdateNC(ncRequest cns.CreateNetworkContainerReque
return nil
}

// InitCNSState initializes cns state
func (client *Client) InitCNSState(ncRequest *cns.CreateNetworkContainerRequest, podInfoByIP map[string]*cns.KubernetesPodInfo) error {
// client.RestService.Lock()
// client.RestService.ReadyToIPAM = true
// client.RestService.Unlock()
// ReconcileNCState initializes cns state
func (client *Client) ReconcileNCState(ncRequest *cns.CreateNetworkContainerRequest, podInfoByIP map[string]cns.KubernetesPodInfo) error {
returnCode := client.RestService.ReconcileNCState(ncRequest, podInfoByIP)

if returnCode != 0 {
return fmt.Errorf("Failed to Reconcile ncState: ncRequest %+v, podInfoMap: %+v, errorCode: %d", *ncRequest, podInfoByIP, returnCode)
}

// return client.RestService.AddIPConfigsToState(ipConfigs)
return nil
}
16 changes: 9 additions & 7 deletions cns/requestcontroller/kubecontroller/crdrequestcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ func (crdRC *crdRequestController) initCNS() error {
var (
pods *corev1.PodList
pod corev1.Pod
podInfo *cns.KubernetesPodInfo
podInfo cns.KubernetesPodInfo
nodeNetConfig *nnc.NodeNetworkConfig
podInfoByIP map[string]*cns.KubernetesPodInfo
podInfoByIP map[string]cns.KubernetesPodInfo
cntxt context.Context
ncRequest cns.CreateNetworkContainerRequest
err error
Expand All @@ -191,7 +191,7 @@ func (crdRC *crdRequestController) initCNS() error {

// If instance of crd is not found, pass nil to CNSClient
if client.IgnoreNotFound(err) == nil {
return crdRC.CNSClient.InitCNSState(nil, nil)
return crdRC.CNSClient.ReconcileNCState(nil, nil)
}

// If it's any other error, log it and return
Expand All @@ -201,7 +201,7 @@ func (crdRC *crdRequestController) initCNS() error {

// If there are no NCs, pass nil to CNSClient
if len(nodeNetConfig.Status.NetworkContainers) == 0 {
return crdRC.CNSClient.InitCNSState(nil, nil)
return crdRC.CNSClient.ReconcileNCState(nil, nil)
}

// Convert to CreateNetworkContainerRequest
Expand All @@ -218,11 +218,11 @@ func (crdRC *crdRequestController) initCNS() error {

// Convert pod list to map of pod ip -> kubernetes pod info
if len(pods.Items) != 0 {
podInfoByIP = make(map[string]*cns.KubernetesPodInfo)
podInfoByIP = make(map[string]cns.KubernetesPodInfo)
for _, pod = range pods.Items {
//Only add pods that aren't on the host network
if !pod.Spec.HostNetwork {
podInfo = &cns.KubernetesPodInfo{
podInfo = cns.KubernetesPodInfo{
PodName: pod.Name,
PodNamespace: pod.Namespace,
}
Expand All @@ -231,7 +231,9 @@ func (crdRC *crdRequestController) initCNS() error {
}
}

return crdRC.CNSClient.InitCNSState(&ncRequest, podInfoByIP)
// Call cnsclient init cns passing those two things
return crdRC.CNSClient.ReconcileNCState(&ncRequest, podInfoByIP)

}

// UpdateCRDSpec updates the CRD spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (mc MockKubeClient) Update(ctx context.Context, obj runtime.Object, opts ..
type MockCNSClient struct {
MockCNSUpdated bool
MockCNSInitialized bool
Pods map[string]*cns.KubernetesPodInfo
Pods map[string]cns.KubernetesPodInfo
NCRequest *cns.CreateNetworkContainerRequest
}

Expand All @@ -103,7 +103,7 @@ func (mi *MockCNSClient) CreateOrUpdateNC(ncRequest cns.CreateNetworkContainerRe
return nil
}

func (mi *MockCNSClient) InitCNSState(ncRequest *cns.CreateNetworkContainerRequest, podInfoByIP map[string]*cns.KubernetesPodInfo) error {
func (mi *MockCNSClient) ReconcileNCState(ncRequest *cns.CreateNetworkContainerRequest, podInfoByIP map[string]cns.KubernetesPodInfo) error {
mi.MockCNSInitialized = true
mi.Pods = podInfoByIP
mi.NCRequest = ncRequest
Expand Down
98 changes: 52 additions & 46 deletions cns/restserver/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,7 @@ func TestMain(m *testing.M) {
logger.InitLogger("testlogs", 0, 0, "./")

// Create the service.
config := common.ServiceConfig{}
service, err = NewHTTPRestService(&config)
if err != nil {
fmt.Printf("Failed to create CNS object %v\n", err)
os.Exit(1)
}
svc = service.(*HTTPRestService)
svc.Name = "cns-test-server"
if err != nil {
logger.Errorf("Failed to create CNS object, err:%v.\n", err)
return
}

if service != nil {
err = service.Start(&config)
if err != nil {
logger.Errorf("Failed to start CNS, err:%v.\n", err)
return
}
}

// Get the internal http mux as test hook.
mux = service.(*HTTPRestService).Listener.GetMux()
startService()

// Setup mock nmagent server
u, err := url.Parse("tcp://" + nmagentEndpoint)
Expand Down Expand Up @@ -276,34 +254,34 @@ func TestGetNetworkContainerByOrchestratorContext(t *testing.T) {
}
}

func TestGetNetworkContainerStatus(t *testing.T) {
// requires more than 30 seconds to run
fmt.Println("Test: TestCreateNetworkContainer")
// func TestGetNetworkContainerStatus(t *testing.T) {
// // requires more than 30 seconds to run
// fmt.Println("Test: TestCreateNetworkContainer")

setEnv(t)
setOrchestratorType(t, cns.Kubernetes)
// setEnv(t)
// setOrchestratorType(t, cns.Kubernetes)

err := creatOrUpdateNetworkContainerWithName(t, "ethWebApp", "11.0.0.5", cns.AzureContainerInstance)
if err != nil {
t.Errorf("creatOrUpdateWebAppContainerWithName failed Err:%+v", err)
t.Fatal(err)
}
// err := creatOrUpdateNetworkContainerWithName(t, "ethWebApp", "11.0.0.5", cns.AzureContainerInstance)
// if err != nil {
// t.Errorf("creatOrUpdateWebAppContainerWithName failed Err:%+v", err)
// t.Fatal(err)
// }

fmt.Println("Now calling getNetworkContainerStatus")
err = getNetworkContainerStatus(t, "ethWebApp")
if err != nil {
t.Errorf("getNetworkContainerStatus failed Err:%+v", err)
t.Fatal(err)
}
// fmt.Println("Now calling getNetworkContainerStatus")
// err = getNetworkContainerStatus(t, "ethWebApp")
// if err != nil {
// t.Errorf("getNetworkContainerStatus failed Err:%+v", err)
// t.Fatal(err)
// }

fmt.Println("Now calling DeleteNetworkContainer")
// fmt.Println("Now calling DeleteNetworkContainer")

err = deleteNetworkAdapterWithName(t, "ethWebApp")
if err != nil {
t.Errorf("Deleting interface failed Err:%+v", err)
t.Fatal(err)
}
}
// err = deleteNetworkAdapterWithName(t, "ethWebApp")
// if err != nil {
// t.Errorf("Deleting interface failed Err:%+v", err)
// t.Fatal(err)
// }
// }

func TestGetInterfaceForNetworkContainer(t *testing.T) {
// requires more than 30 seconds to run
Expand Down Expand Up @@ -674,6 +652,34 @@ func setEnv(t *testing.T) *httptest.ResponseRecorder {
return w
}

func startService() {
var err error
// Create the service.
config := common.ServiceConfig{}
service, err = NewHTTPRestService(&config)
if err != nil {
fmt.Printf("Failed to create CNS object %v\n", err)
os.Exit(1)
}
svc = service.(*HTTPRestService)
svc.Name = "cns-test-server"
if err != nil {
logger.Errorf("Failed to create CNS object, err:%v.\n", err)
return
}

if service != nil {
err = service.Start(&config)
if err != nil {
logger.Errorf("Failed to start CNS, err:%v.\n", err)
return
}
}

// Get the internal http mux as test hook.
mux = service.(*HTTPRestService).Listener.GetMux()
}

// IGNORE TEST AS IT IS FAILING. TODO:- Fix it https://msazure.visualstudio.com/One/_workitems/edit/7720083
// // Tests CreateNetwork functionality.

Expand Down
1 change: 1 addition & 0 deletions cns/restserver/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
InconsistentIPConfigState = 29
InvalidSecondaryIPConfig = 30
NetworkContainerPendingStatePropagation = 31
FailedToAllocateIpConfig = 32
UnexpectedError = 99
)

Expand Down
49 changes: 49 additions & 0 deletions cns/restserver/internalapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/nmagentclient"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
)

// This file contains the internal functions called by either HTTP APIs (api.go) or
Expand Down Expand Up @@ -149,6 +150,54 @@ func (service *HTTPRestService) SyncNodeStatus(dncEP, infraVnet, nodeID string,
return
}

// This API will be called by CNS RequestController on CRD update.
func (service *HTTPRestService) ReconcileNCState(ncRequest *cns.CreateNetworkContainerRequest, podInfoByIp map[string]cns.KubernetesPodInfo) int {
// check if ncRequest is null, then return as there is no CRD state yet
if ncRequest == nil {
log.Logf("CNS starting with no NC state, podInfoMap count %d", len(podInfoByIp))
return Success
}

returnCode := service.CreateOrUpdateNetworkContainerInternal(*ncRequest)

// If the NC was created successfully, then reconcile the allocated pod state
if returnCode != Success {
return returnCode
}

// now parse the secondaryIP list, if it exists in PodInfo list, then allocate that ip
for _, secIpConfig := range ncRequest.SecondaryIPConfigs {
if podInfo, exists := podInfoByIp[secIpConfig.IPSubnet.IPAddress]; exists {
log.Logf("SecondaryIP %+v is allocated to Pod. %+v, ncId: %s", secIpConfig, podInfo, ncRequest.NetworkContainerid)

desiredIPConfig := cns.IPSubnet{
IPAddress: secIpConfig.IPSubnet.IPAddress,
PrefixLength: secIpConfig.IPSubnet.PrefixLength,
}

kubernetesPodInfo := cns.KubernetesPodInfo{
PodName: podInfo.PodName,
PodNamespace: podInfo.PodNamespace,
}
jsonContext, _ := json.Marshal(kubernetesPodInfo)

ipconfigRequest := cns.GetIPConfigRequest{
DesiredIPConfig: desiredIPConfig,
OrchestratorContext: jsonContext,
}

if _, err := requestIPConfigHelper(service, ipconfigRequest); err != nil {
log.Errorf("AllocateIPConfig failed for SecondaryIP %+v, podInfo %+v, ncId %s, error: %v", secIpConfig, podInfo, ncRequest.NetworkContainerid, err)
return FailedToAllocateIpConfig
}
} else {
log.Logf("SecondaryIP %+v is not allocated. ncId: %s", secIpConfig, ncRequest.NetworkContainerid)
}
}

return 0
}

// This API will be called by CNS RequestController on CRD update.
func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req cns.CreateNetworkContainerRequest) int {
if req.NetworkContainerid == "" {
Expand Down
Loading