Skip to content

Commit 9df7534

Browse files
authored
chore: remove legacy kube-init and default to CNI state (#3383)
* chore: remove legacy kube-init and default to CNI state Signed-off-by: Evan Baker <[email protected]> * refactor podinfoproviders Signed-off-by: Evan Baker <[email protected]> * fix lints Signed-off-by: GitHub <[email protected]> --------- Signed-off-by: Evan Baker <[email protected]> Signed-off-by: GitHub <[email protected]>
1 parent f60073f commit 9df7534

16 files changed

+281
-441
lines changed

cns/NetworkContainerContract.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@ func (f PodInfoByIPProviderFunc) PodInfoByIP() (map[string]PodInfo, error) {
201201
return f()
202202
}
203203

204-
var GlobalPodInfoScheme podInfoScheme
204+
var GlobalPodInfoScheme = InterfaceIDPodInfoScheme
205205

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

210210
const (
211-
KubernetesPodInfoScheme podInfoScheme = iota
211+
_ podInfoScheme = iota
212212
InterfaceIDPodInfoScheme
213213
InfraIDPodInfoScheme
214214
)
@@ -285,12 +285,8 @@ func (p *podInfo) Key() string {
285285
switch p.Version {
286286
case InfraIDPodInfoScheme:
287287
return p.PodInfraContainerID
288-
case InterfaceIDPodInfoScheme:
289-
return p.PodInterfaceID
290-
case KubernetesPodInfoScheme:
291-
return p.PodName + ":" + p.PodNamespace
292288
default:
293-
return p.PodName + ":" + p.PodNamespace
289+
return p.PodInterfaceID
294290
}
295291
}
296292

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

313+
func (p *podInfo) UnmarshalJSON(b []byte) error {
314+
type alias podInfo
315+
// Unmarshal into a temporary struct to avoid infinite recursion
316+
a := &struct {
317+
*alias
318+
}{
319+
alias: (*alias)(p),
320+
}
321+
if err := json.Unmarshal(b, a); err != nil {
322+
return errors.Wrap(err, "failed to unmarshal podInfo")
323+
}
324+
p.Version = GlobalPodInfoScheme
325+
return nil
326+
}
327+
317328
// NewPodInfo returns an implementation of PodInfo that returns the passed
318329
// configuration for their namesake functions.
319330
func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
@@ -328,14 +339,12 @@ func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
328339
}
329340
}
330341

331-
// UnmarshalPodInfo wraps json.Unmarshal to return an implementation of
332-
// PodInfo.
333342
func UnmarshalPodInfo(b []byte) (PodInfo, error) {
334343
p := &podInfo{}
335-
if err := json.Unmarshal(b, p); err != nil {
344+
err := json.Unmarshal(b, p)
345+
if err != nil {
336346
return nil, err
337347
}
338-
p.Version = GlobalPodInfoScheme
339348
return p, nil
340349
}
341350

cns/NetworkContainerContract_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestUnmarshalPodInfo(t *testing.T) {
2323
PodName: "pod",
2424
PodNamespace: "namespace",
2525
},
26+
Version: GlobalPodInfoScheme,
2627
},
2728
},
2829
{
@@ -33,6 +34,7 @@ func TestUnmarshalPodInfo(t *testing.T) {
3334
PodName: "pod",
3435
PodNamespace: "namespace",
3536
},
37+
Version: GlobalPodInfoScheme,
3638
},
3739
},
3840
{
@@ -44,7 +46,8 @@ func TestUnmarshalPodInfo(t *testing.T) {
4446
}
4547
for _, tt := range tests {
4648
t.Run(tt.name, func(t *testing.T) {
47-
got, err := UnmarshalPodInfo(tt.b)
49+
got := &podInfo{}
50+
err := json.Unmarshal(tt.b, got)
4851
if tt.wantErr {
4952
assert.Error(t, err)
5053
return
@@ -58,7 +61,6 @@ func TestUnmarshalPodInfo(t *testing.T) {
5861

5962
func TestNewPodInfoFromIPConfigsRequest(t *testing.T) {
6063
GlobalPodInfoScheme = InterfaceIDPodInfoScheme
61-
defer func() { GlobalPodInfoScheme = KubernetesPodInfoScheme }()
6264
tests := []struct {
6365
name string
6466
req IPConfigsRequest

cns/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (i *IPConfigurationStatus) UnmarshalJSON(b []byte) error {
167167
}
168168
}
169169
if s, ok := m["PodInfo"]; ok {
170-
pi, err := UnmarshalPodInfo(s)
170+
pi := NewPodInfo("", "", "", "")
171+
err := json.Unmarshal(s, pi)
171172
if err != nil {
172173
return errors.Wrap(err, "failed to unmarshal key PodInfo to PodInfo")
173174
}

cns/cnireconciler/version.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

cns/cnireconciler/version_test.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

cns/nodesubnet/initialization_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"testing"
77

88
"github.com/Azure/azure-container-networking/cns"
9-
"github.com/Azure/azure-container-networking/cns/cnireconciler"
109
"github.com/Azure/azure-container-networking/cns/logger"
1110
"github.com/Azure/azure-container-networking/cns/nodesubnet"
1211
"github.com/Azure/azure-container-networking/cns/restserver"
12+
podprovider "github.com/Azure/azure-container-networking/cns/stateprovider/cns"
1313
"github.com/Azure/azure-container-networking/cns/types"
1414
"github.com/Azure/azure-container-networking/store"
1515
)
@@ -88,7 +88,7 @@ func TestNewCNSPodInfoProvider(t *testing.T) {
8888
ctx, cancel := testContext(t)
8989
defer cancel()
9090

91-
podInfoByIPProvider, err := cnireconciler.NewCNSPodInfoProvider(tt.store)
91+
podInfoByIPProvider, err := podprovider.New(tt.store)
9292
checkErr(t, err, false)
9393

9494
got, err := nodesubnet.ReconcileInitialCNSState(ctx, tt.reconciler, podInfoByIPProvider)

cns/restserver/internalapi_test.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,12 @@ func TestPodIPsIndexedByInterface(t *testing.T) {
602602
"fe80::7": cns.NewPodInfo("some-guid-2", "def-eth0", "reconcilePod2", "PodNS2"),
603603
},
604604
expectedOutput: map[string]podIPs{
605-
"reconcilePod1:PodNS1": {
605+
"abc-eth0": {
606606
PodInfo: cns.NewPodInfo("some-guid-1", "abc-eth0", "reconcilePod1", "PodNS1"),
607607
v4IP: net.IPv4(10, 0, 0, 6),
608608
v6IP: []byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x06},
609609
},
610-
"reconcilePod2:PodNS2": {
610+
"def-eth0": {
611611
PodInfo: cns.NewPodInfo("some-guid-2", "def-eth0", "reconcilePod2", "PodNS2"),
612612
v4IP: net.IPv4(10, 0, 0, 7),
613613
v6IP: []byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x07},
@@ -677,7 +677,6 @@ func TestReconcileNCWithExistingStateFromInterfaceID(t *testing.T) {
677677
setEnv(t)
678678
setOrchestratorTypeInternal(cns.KubernetesCRD)
679679
cns.GlobalPodInfoScheme = cns.InterfaceIDPodInfoScheme
680-
defer func() { cns.GlobalPodInfoScheme = cns.KubernetesPodInfoScheme }()
681680

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

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

719-
func TestReconcileCNSIPAMWithKubePodInfoProvider(t *testing.T) {
720-
restartService()
721-
setEnv(t)
722-
setOrchestratorTypeInternal(cns.KubernetesCRD)
723-
724-
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
725-
726-
startingIndex := 6
727-
for i := 0; i < 4; i++ {
728-
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
729-
secIpConfig := newSecondaryIPConfig(ipaddress, -1)
730-
ipId := uuid.New()
731-
secondaryIPConfigs[ipId.String()] = secIpConfig
732-
startingIndex++
733-
}
734-
req := generateNetworkContainerRequest(secondaryIPConfigs, uuid.New().String(), "-1")
735-
736-
// the following pod info constructors leave container id and interface id blank on purpose.
737-
// this is to simulate the information we get from the kube info provider
738-
expectedAssignedPods := make(map[string]cns.PodInfo)
739-
expectedAssignedPods["10.0.0.6"] = cns.NewPodInfo("", "", "customerpod1", "PodNS1")
740-
741-
// allocate non-vnet IP for pod in host network
742-
expectedAssignedPods["192.168.0.1"] = cns.NewPodInfo("", "", "systempod", "kube-system")
743-
744-
expectedNcCount := len(svc.state.ContainerStatus)
745-
returnCode := svc.ReconcileIPAMStateForSwift([]*cns.CreateNetworkContainerRequest{req}, expectedAssignedPods, &v1alpha.NodeNetworkConfig{
746-
Status: v1alpha.NodeNetworkConfigStatus{
747-
Scaler: v1alpha.Scaler{
748-
BatchSize: batchSize,
749-
ReleaseThresholdPercent: releasePercent,
750-
RequestThresholdPercent: requestPercent,
751-
},
752-
},
753-
Spec: v1alpha.NodeNetworkConfigSpec{
754-
RequestedIPCount: initPoolSize,
755-
},
756-
})
757-
if returnCode != types.Success {
758-
t.Errorf("Unexpected failure on reconcile with no state %d", returnCode)
759-
}
760-
761-
delete(expectedAssignedPods, "192.168.0.1")
762-
validateNCStateAfterReconcile(t, req, expectedNcCount, expectedAssignedPods, nil)
763-
}
764-
765718
func setOrchestratorTypeInternal(orchestratorType string) {
766719
fmt.Println("setOrchestratorTypeInternal")
767720
svc.state.OrchestratorType = orchestratorType

0 commit comments

Comments
 (0)