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
31 changes: 20 additions & 11 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ func (f PodInfoByIPProviderFunc) PodInfoByIP() (map[string]PodInfo, error) {
return f()
}

var GlobalPodInfoScheme podInfoScheme
var GlobalPodInfoScheme = InterfaceIDPodInfoScheme

// podInfoScheme indicates which schema should be used when generating
// the map key in the Key() function on a podInfo object.
type podInfoScheme int

const (
KubernetesPodInfoScheme podInfoScheme = iota
_ podInfoScheme = iota
InterfaceIDPodInfoScheme
InfraIDPodInfoScheme
)
Expand Down Expand Up @@ -285,12 +285,8 @@ func (p *podInfo) Key() string {
switch p.Version {
case InfraIDPodInfoScheme:
return p.PodInfraContainerID
case InterfaceIDPodInfoScheme:
return p.PodInterfaceID
case KubernetesPodInfoScheme:
return p.PodName + ":" + p.PodNamespace
default:
return p.PodName + ":" + p.PodNamespace
return p.PodInterfaceID
}
}

Expand All @@ -314,6 +310,21 @@ func (p *podInfo) SecondaryInterfacesExist() bool {
return p.SecondaryInterfaceSet
}

func (p *podInfo) UnmarshalJSON(b []byte) error {
type alias podInfo
// Unmarshal into a temporary struct to avoid infinite recursion
a := &struct {
*alias
}{
alias: (*alias)(p),
}
if err := json.Unmarshal(b, a); err != nil {
return errors.Wrap(err, "failed to unmarshal podInfo")
}
p.Version = GlobalPodInfoScheme
return nil
}

// NewPodInfo returns an implementation of PodInfo that returns the passed
// configuration for their namesake functions.
func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
Expand All @@ -328,14 +339,12 @@ func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
}
}

// UnmarshalPodInfo wraps json.Unmarshal to return an implementation of
// PodInfo.
func UnmarshalPodInfo(b []byte) (PodInfo, error) {
p := &podInfo{}
if err := json.Unmarshal(b, p); err != nil {
err := json.Unmarshal(b, p)
if err != nil {
return nil, err
}
p.Version = GlobalPodInfoScheme
return p, nil
}

Expand Down
6 changes: 4 additions & 2 deletions cns/NetworkContainerContract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestUnmarshalPodInfo(t *testing.T) {
PodName: "pod",
PodNamespace: "namespace",
},
Version: GlobalPodInfoScheme,
},
},
{
Expand All @@ -33,6 +34,7 @@ func TestUnmarshalPodInfo(t *testing.T) {
PodName: "pod",
PodNamespace: "namespace",
},
Version: GlobalPodInfoScheme,
},
},
{
Expand All @@ -44,7 +46,8 @@ func TestUnmarshalPodInfo(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnmarshalPodInfo(tt.b)
got := &podInfo{}
err := json.Unmarshal(tt.b, got)
if tt.wantErr {
assert.Error(t, err)
return
Expand All @@ -58,7 +61,6 @@ func TestUnmarshalPodInfo(t *testing.T) {

func TestNewPodInfoFromIPConfigsRequest(t *testing.T) {
GlobalPodInfoScheme = InterfaceIDPodInfoScheme
defer func() { GlobalPodInfoScheme = KubernetesPodInfoScheme }()
tests := []struct {
name string
req IPConfigsRequest
Expand Down
3 changes: 2 additions & 1 deletion cns/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func (i *IPConfigurationStatus) UnmarshalJSON(b []byte) error {
}
}
if s, ok := m["PodInfo"]; ok {
pi, err := UnmarshalPodInfo(s)
pi := NewPodInfo("", "", "", "")
err := json.Unmarshal(s, pi)
if err != nil {
return errors.Wrap(err, "failed to unmarshal key PodInfo to PodInfo")
}
Expand Down
42 changes: 0 additions & 42 deletions cns/cnireconciler/version.go

This file was deleted.

76 changes: 0 additions & 76 deletions cns/cnireconciler/version_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions cns/nodesubnet/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/cnireconciler"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/cns/nodesubnet"
"github.com/Azure/azure-container-networking/cns/restserver"
podprovider "github.com/Azure/azure-container-networking/cns/stateprovider/cns"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/store"
)
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestNewCNSPodInfoProvider(t *testing.T) {
ctx, cancel := testContext(t)
defer cancel()

podInfoByIPProvider, err := cnireconciler.NewCNSPodInfoProvider(tt.store)
podInfoByIPProvider, err := podprovider.New(tt.store)
checkErr(t, err, false)

got, err := nodesubnet.ReconcileInitialCNSState(ctx, tt.reconciler, podInfoByIPProvider)
Expand Down
51 changes: 2 additions & 49 deletions cns/restserver/internalapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,12 @@ func TestPodIPsIndexedByInterface(t *testing.T) {
"fe80::7": cns.NewPodInfo("some-guid-2", "def-eth0", "reconcilePod2", "PodNS2"),
},
expectedOutput: map[string]podIPs{
"reconcilePod1:PodNS1": {
"abc-eth0": {
PodInfo: cns.NewPodInfo("some-guid-1", "abc-eth0", "reconcilePod1", "PodNS1"),
v4IP: net.IPv4(10, 0, 0, 6),
v6IP: []byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x06},
},
"reconcilePod2:PodNS2": {
"def-eth0": {
PodInfo: cns.NewPodInfo("some-guid-2", "def-eth0", "reconcilePod2", "PodNS2"),
v4IP: net.IPv4(10, 0, 0, 7),
v6IP: []byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x07},
Expand Down Expand Up @@ -677,7 +677,6 @@ func TestReconcileNCWithExistingStateFromInterfaceID(t *testing.T) {
setEnv(t)
setOrchestratorTypeInternal(cns.KubernetesCRD)
cns.GlobalPodInfoScheme = cns.InterfaceIDPodInfoScheme
defer func() { cns.GlobalPodInfoScheme = cns.KubernetesPodInfoScheme }()

secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)

Expand Down Expand Up @@ -716,52 +715,6 @@ func TestReconcileNCWithExistingStateFromInterfaceID(t *testing.T) {
validateNCStateAfterReconcile(t, req, expectedNcCount+1, expectedAssignedPods, nil)
}

func TestReconcileCNSIPAMWithKubePodInfoProvider(t *testing.T) {
restartService()
setEnv(t)
setOrchestratorTypeInternal(cns.KubernetesCRD)

secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)

startingIndex := 6
for i := 0; i < 4; i++ {
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
secIpConfig := newSecondaryIPConfig(ipaddress, -1)
ipId := uuid.New()
secondaryIPConfigs[ipId.String()] = secIpConfig
startingIndex++
}
req := generateNetworkContainerRequest(secondaryIPConfigs, uuid.New().String(), "-1")

// the following pod info constructors leave container id and interface id blank on purpose.
// this is to simulate the information we get from the kube info provider
expectedAssignedPods := make(map[string]cns.PodInfo)
expectedAssignedPods["10.0.0.6"] = cns.NewPodInfo("", "", "customerpod1", "PodNS1")

// allocate non-vnet IP for pod in host network
expectedAssignedPods["192.168.0.1"] = cns.NewPodInfo("", "", "systempod", "kube-system")

expectedNcCount := len(svc.state.ContainerStatus)
returnCode := svc.ReconcileIPAMStateForSwift([]*cns.CreateNetworkContainerRequest{req}, expectedAssignedPods, &v1alpha.NodeNetworkConfig{
Status: v1alpha.NodeNetworkConfigStatus{
Scaler: v1alpha.Scaler{
BatchSize: batchSize,
ReleaseThresholdPercent: releasePercent,
RequestThresholdPercent: requestPercent,
},
},
Spec: v1alpha.NodeNetworkConfigSpec{
RequestedIPCount: initPoolSize,
},
})
if returnCode != types.Success {
t.Errorf("Unexpected failure on reconcile with no state %d", returnCode)
}

delete(expectedAssignedPods, "192.168.0.1")
validateNCStateAfterReconcile(t, req, expectedNcCount, expectedAssignedPods, nil)
}

func setOrchestratorTypeInternal(orchestratorType string) {
fmt.Println("setOrchestratorTypeInternal")
svc.state.OrchestratorType = orchestratorType
Expand Down
Loading
Loading