Skip to content

Commit 793abc9

Browse files
authored
refactor podinfoproviders
Signed-off-by: Evan Baker <[email protected]>
1 parent b00a1ab commit 793abc9

File tree

12 files changed

+128
-113
lines changed

12 files changed

+128
-113
lines changed

cns/NetworkContainerContract.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ var GlobalPodInfoScheme podInfoScheme
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

cns/NetworkContainerContract_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func TestUnmarshalPodInfo(t *testing.T) {
5858

5959
func TestNewPodInfoFromIPConfigsRequest(t *testing.T) {
6060
GlobalPodInfoScheme = InterfaceIDPodInfoScheme
61-
defer func() { GlobalPodInfoScheme = KubernetesPodInfoScheme }()
6261
tests := []struct {
6362
name string
6463
req IPConfigsRequest

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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

cns/restserver/nodesubnet_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"net"
66
"testing"
77

8-
"github.com/Azure/azure-container-networking/cns/cnireconciler"
98
"github.com/Azure/azure-container-networking/cns/logger"
9+
"github.com/Azure/azure-container-networking/cns/stateprovider/cns"
1010
"github.com/Azure/azure-container-networking/cns/restserver"
1111
"github.com/Azure/azure-container-networking/cns/types"
1212
"github.com/Azure/azure-container-networking/store"
@@ -71,7 +71,7 @@ func (m *MockCNIConflistGenerator) Close() error {
7171
// TestNodeSubnet tests initialization of NodeSubnet with endpoint info, and verfies that
7272
// the conflist is generated after fetching secondary IPs
7373
func TestNodeSubnet(t *testing.T) {
74-
podInfoByIPProvider, err := cnireconciler.NewCNSPodInfoProvider(getMockStore())
74+
podInfoByIPProvider, err := cns.New(getMockStore())
7575
if err != nil {
7676
t.Fatalf("NewCNSPodInfoProvider returned an error: %v", err)
7777
}

cns/service/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
cnsclient "github.com/Azure/azure-container-networking/cns/client"
2424
cnscli "github.com/Azure/azure-container-networking/cns/cmd/cli"
2525
"github.com/Azure/azure-container-networking/cns/cniconflist"
26-
"github.com/Azure/azure-container-networking/cns/cnireconciler"
2726
"github.com/Azure/azure-container-networking/cns/common"
2827
"github.com/Azure/azure-container-networking/cns/configuration"
2928
"github.com/Azure/azure-container-networking/cns/deviceplugin"
@@ -47,6 +46,8 @@ import (
4746
"github.com/Azure/azure-container-networking/cns/multitenantcontroller/multitenantoperator"
4847
"github.com/Azure/azure-container-networking/cns/restserver"
4948
restserverv2 "github.com/Azure/azure-container-networking/cns/restserver/v2"
49+
cnipodprovider "github.com/Azure/azure-container-networking/cns/stateprovider/cni"
50+
cnspodprovider "github.com/Azure/azure-container-networking/cns/stateprovider/cns"
5051
cnstypes "github.com/Azure/azure-container-networking/cns/types"
5152
"github.com/Azure/azure-container-networking/cns/wireserver"
5253
acn "github.com/Azure/azure-container-networking/common"
@@ -833,7 +834,7 @@ func main() {
833834

834835
// Check the CNI statefile mount, and if the file is empty
835836
// stub an empty JSON object
836-
if err := cnireconciler.WriteObjectToCNIStatefile(); err != nil {
837+
if err := cnipodprovider.WriteObjectToCNIStatefile(); err != nil {
837838
logger.Errorf("Failed to write empty object to CNI state: %v", err)
838839
return
839840
}
@@ -1654,7 +1655,7 @@ func getPodInfoByIPProvider(
16541655
switch {
16551656
case cnsconfig.ManageEndpointState:
16561657
logger.Printf("Initializing from self managed endpoint store")
1657-
podInfoByIPProvider, err = cnireconciler.NewCNSPodInfoProvider(httpRestServiceImplementation.EndpointStateStore) // get reference to endpoint state store from rest server
1658+
podInfoByIPProvider, err = cnspodprovider.New(httpRestServiceImplementation.EndpointStateStore) // get reference to endpoint state store from rest server
16581659
if err != nil {
16591660
if errors.Is(err, store.ErrKeyNotFound) {
16601661
logger.Printf("[Azure CNS] No endpoint state found, skipping initializing CNS state")
@@ -1664,7 +1665,7 @@ func getPodInfoByIPProvider(
16641665
}
16651666
default:
16661667
logger.Printf("Initializing from CNI")
1667-
podInfoByIPProvider, err = cnireconciler.NewCNIPodInfoProvider()
1668+
podInfoByIPProvider, err = cnipodprovider.New()
16681669
if err != nil {
16691670
return podInfoByIPProvider, errors.Wrap(err, "failed to create CNI PodInfoProvider")
16701671
}
@@ -1713,7 +1714,7 @@ func createOrUpdateNodeInfoCRD(ctx context.Context, restConfig *rest.Config, nod
17131714
// PopulateCNSEndpointState initilizes CNS Endpoint State by Migrating the CNI state.
17141715
func PopulateCNSEndpointState(endpointStateStore store.KeyValueStore) error {
17151716
logger.Printf("State Migration is enabled")
1716-
endpointState, err := cnireconciler.MigrateCNISate()
1717+
endpointState, err := cnspodprovider.MigrateCNISate()
17171718
if err != nil {
17181719
return errors.Wrap(err, "failed to create CNS Endpoint state from CNI")
17191720
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cni
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Azure/azure-container-networking/cni/api"
7+
"github.com/Azure/azure-container-networking/cni/client"
8+
"github.com/Azure/azure-container-networking/cns"
9+
"github.com/pkg/errors"
10+
"k8s.io/utils/exec"
11+
)
12+
13+
// New returns an implementation of cns.PodInfoByIPProvider
14+
// that execs out to the CNI and uses the response to build the PodInfo map.
15+
func New() (cns.PodInfoByIPProvider, error) {
16+
return podInfoProvider(exec.New())
17+
}
18+
19+
func podInfoProvider(exec exec.Interface) (cns.PodInfoByIPProvider, error) {
20+
cli := client.New(exec)
21+
state, err := cli.GetEndpointState()
22+
if err != nil {
23+
return nil, fmt.Errorf("failed to invoke CNI client.GetEndpointState(): %w", err)
24+
}
25+
return cns.PodInfoByIPProviderFunc(func() (map[string]cns.PodInfo, error) {
26+
return cniStateToPodInfoByIP(state)
27+
}), nil
28+
}
29+
30+
// cniStateToPodInfoByIP converts an AzureCNIState dumped from a CNI exec
31+
// into a PodInfo map, using the endpoint IPs as keys in the map.
32+
// for pods with multiple IPs (such as in dualstack cases), this means multiple keys in the map
33+
// will point to the same pod information.
34+
func cniStateToPodInfoByIP(state *api.AzureCNIState) (map[string]cns.PodInfo, error) {
35+
podInfoByIP := map[string]cns.PodInfo{}
36+
for _, endpoint := range state.ContainerInterfaces {
37+
for _, epIP := range endpoint.IPAddresses {
38+
podInfo := cns.NewPodInfo(endpoint.ContainerID, endpoint.PodEndpointId, endpoint.PodName, endpoint.PodNamespace)
39+
40+
ipKey := epIP.IP.String()
41+
if prevPodInfo, ok := podInfoByIP[ipKey]; ok {
42+
return nil, errors.Wrapf(cns.ErrDuplicateIP, "duplicate ip %s found for different pods: pod: %+v, pod: %+v", ipKey, podInfo, prevPodInfo)
43+
}
44+
45+
podInfoByIP[ipKey] = podInfo
46+
}
47+
}
48+
return podInfoByIP, nil
49+
}

cns/cnireconciler/podinfoprovider_test.go renamed to cns/stateprovider/cni/podinfoprovider_test.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
package cnireconciler
1+
package cni
22

33
import (
4-
"net"
54
"testing"
65

76
"github.com/Azure/azure-container-networking/cns"
8-
"github.com/Azure/azure-container-networking/cns/restserver"
9-
"github.com/Azure/azure-container-networking/store"
107
testutils "github.com/Azure/azure-container-networking/test/utils"
118
"github.com/stretchr/testify/assert"
129
"k8s.io/utils/exec"
@@ -76,54 +73,7 @@ func TestNewCNIPodInfoProvider(t *testing.T) {
7673
for _, tt := range tests {
7774
tt := tt
7875
t.Run(tt.name, func(t *testing.T) {
79-
got, err := newCNIPodInfoProvider(tt.exec)
80-
if tt.wantErr {
81-
assert.Error(t, err)
82-
return
83-
}
84-
assert.NoError(t, err)
85-
podInfoByIP, _ := got.PodInfoByIP()
86-
assert.Equal(t, tt.want, podInfoByIP)
87-
})
88-
}
89-
}
90-
91-
func TestNewCNSPodInfoProvider(t *testing.T) {
92-
goodStore := store.NewMockStore("")
93-
goodEndpointState := make(map[string]*restserver.EndpointInfo)
94-
endpointInfo := &restserver.EndpointInfo{PodName: "goldpinger-deploy-bbbf9fd7c-z8v4l", PodNamespace: "default", IfnameToIPMap: make(map[string]*restserver.IPInfo)}
95-
endpointInfo.IfnameToIPMap["eth0"] = &restserver.IPInfo{IPv4: []net.IPNet{{IP: net.IPv4(10, 241, 0, 65), Mask: net.IPv4Mask(255, 255, 255, 0)}}}
96-
97-
goodEndpointState["0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea"] = endpointInfo
98-
err := goodStore.Write(restserver.EndpointStoreKey, goodEndpointState)
99-
if err != nil {
100-
t.Fatalf("Error writing to store: %v", err)
101-
}
102-
tests := []struct {
103-
name string
104-
store store.KeyValueStore
105-
want map[string]cns.PodInfo
106-
wantErr bool
107-
}{
108-
{
109-
name: "good",
110-
store: goodStore,
111-
want: map[string]cns.PodInfo{"10.241.0.65": cns.NewPodInfo("0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea",
112-
"0a4917617e15d24dc495e407d8eb5c88e4406e58fa209e4eb75a2c2fb7045eea", "goldpinger-deploy-bbbf9fd7c-z8v4l", "default")},
113-
wantErr: false,
114-
},
115-
{
116-
name: "empty store",
117-
store: store.NewMockStore(""),
118-
want: map[string]cns.PodInfo{},
119-
wantErr: true,
120-
},
121-
}
122-
123-
for _, tt := range tests {
124-
tt := tt
125-
t.Run(tt.name, func(t *testing.T) {
126-
got, err := newCNSPodInfoProvider(tt.store)
76+
got, err := podInfoProvider(tt.exec)
12777
if tt.wantErr {
12878
assert.Error(t, err)
12979
return

cns/cnireconciler/statefile.go renamed to cns/stateprovider/cni/statefile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cnireconciler
1+
package cni
22

33
import (
44
"encoding/json"

cns/cnireconciler/statefile_test.go renamed to cns/stateprovider/cni/statefile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cnireconciler
1+
package cni
22

33
import (
44
"os"

0 commit comments

Comments
 (0)