From 5d216121df7a11d4ece92bac28a87cfa6259fb98 Mon Sep 17 00:00:00 2001 From: AzureAhai Date: Wed, 3 Jan 2024 16:00:29 -0800 Subject: [PATCH] Stateless CNI migration code changes --- Makefile | 16 ++-------------- cns/cnireconciler/podinfoprovider.go | 11 +++++++---- cns/cnireconciler/podinfoprovider_test.go | 6 +++--- cns/service/main.go | 2 +- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index d74f74cb3da..34a5d46e135 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ ACN_VERSION ?= $(shell git describe --exclude "azure-ipam*" --exclude "dropgz AZURE_IPAM_VERSION ?= $(notdir $(shell git describe --match "azure-ipam*" --tags --always)) CNI_VERSION ?= $(ACN_VERSION) CNI_DROPGZ_VERSION ?= $(notdir $(shell git describe --match "dropgz*" --tags --always)) -CNS_VERSION ?= "v1.5.15-22-g3d1fa46d" +CNS_VERSION ?= $(ACN_VERSION) NPM_VERSION ?= $(ACN_VERSION) ZAPAI_VERSION ?= $(notdir $(shell git describe --match "zapai*" --tags --always)) @@ -46,7 +46,6 @@ ZAPAI_VERSION ?= $(notdir $(shell git describe --match "zapai*" --tags --alway AZURE_IPAM_DIR = $(REPO_ROOT)/azure-ipam CNM_DIR = $(REPO_ROOT)/cnm/plugin CNI_NET_DIR = $(REPO_ROOT)/cni/network/plugin -STATELESS_CNI_NET_DIR = $(REPO_ROOT)/cni/network/stateless CNI_IPAM_DIR = $(REPO_ROOT)/cni/ipam/plugin CNI_IPAMV6_DIR = $(REPO_ROOT)/cni/ipam/pluginv6 CNI_TELEMETRY_DIR = $(REPO_ROOT)/cni/telemetry/service @@ -59,14 +58,11 @@ AZURE_IPAM_BUILD_DIR = $(BUILD_DIR)/azure-ipam IMAGE_DIR = $(OUTPUT_DIR)/images CNM_BUILD_DIR = $(BUILD_DIR)/cnm CNI_BUILD_DIR = $(BUILD_DIR)/cni -STATELESS_CNI_BUILD_DIR = $(CNI_BUILD_DIR)/stateless ACNCLI_BUILD_DIR = $(BUILD_DIR)/acncli CNI_MULTITENANCY_BUILD_DIR = $(BUILD_DIR)/cni-multitenancy CNI_MULTITENANCY_TRANSPARENT_VLAN_BUILD_DIR = $(BUILD_DIR)/cni-multitenancy-transparent-vlan CNI_SWIFT_BUILD_DIR = $(BUILD_DIR)/cni-swift -STATELESS_CNI_SWIFT_BUILD_DIR = $(CNI_SWIFT_BUILD_DIR)/stateless CNI_OVERLAY_BUILD_DIR = $(BUILD_DIR)/cni-overlay -STATELESS_CNI_OVERLAY_BUILD_DIR = $(CNI_OVERLAY_BUILD_DIR)/stateless CNI_BAREMETAL_BUILD_DIR = $(BUILD_DIR)/cni-baremetal CNI_DUALSTACK_BUILD_DIR = $(BUILD_DIR)/cni-dualstack CNS_BUILD_DIR = $(BUILD_DIR)/cns @@ -134,7 +130,7 @@ endif # Shorthand target names for convenience. azure-cnm-plugin: cnm-binary cnm-archive -azure-cni-plugin: azure-vnet-binary azure-vnet-stateless-binary azure-vnet-ipam-binary azure-vnet-ipamv6-binary azure-vnet-telemetry-binary cni-archive +azure-cni-plugin: azure-vnet-binary azure-vnet-ipam-binary azure-vnet-ipamv6-binary azure-vnet-telemetry-binary cni-archive azure-cns: azure-cns-binary cns-archive acncli: acncli-binary acncli-archive azure-cnms: azure-cnms-binary cnms-archive @@ -184,10 +180,6 @@ cnm-binary: azure-vnet-binary: cd $(CNI_NET_DIR) && CGO_ENABLED=0 go build -v -o $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) -ldflags "-X main.version=$(CNI_VERSION)" -gcflags="-dwarflocationlists=true" -# Build the Azure CNI stateless network binary -azure-vnet-stateless-binary: - cd $(STATELESS_CNI_NET_DIR) && CGO_ENABLED=0 go build -v -o $(STATELESS_CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) -ldflags "-X main.version=$(CNI_VERSION)" -gcflags="-dwarflocationlists=true" - # Build the Azure CNI IPAM binary. azure-vnet-ipam-binary: cd $(CNI_IPAM_DIR) && CGO_ENABLED=0 go build -v -o $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) -ldflags "-X main.version=$(CNI_VERSION)" -gcflags="-dwarflocationlists=true" @@ -683,16 +675,12 @@ endif cp cni/azure-$(GOOS)-swift.conflist $(CNI_SWIFT_BUILD_DIR)/10-azure.conflist cp telemetry/azure-vnet-telemetry.config $(CNI_SWIFT_BUILD_DIR)/azure-vnet-telemetry.config cp $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-telemetry$(EXE_EXT) $(CNI_SWIFT_BUILD_DIR) - $(MKDIR) $(STATELESS_CNI_SWIFT_BUILD_DIR) - cp $(STATELESS_CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(STATELESS_CNI_SWIFT_BUILD_DIR) cd $(CNI_SWIFT_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_SWIFT_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) azure-vnet-telemetry$(EXE_EXT) 10-azure.conflist azure-vnet-telemetry.config $(MKDIR) $(CNI_OVERLAY_BUILD_DIR) cp cni/azure-$(GOOS)-swift-overlay.conflist $(CNI_OVERLAY_BUILD_DIR)/10-azure.conflist cp telemetry/azure-vnet-telemetry.config $(CNI_OVERLAY_BUILD_DIR)/azure-vnet-telemetry.config cp $(CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-ipam$(EXE_EXT) $(CNI_BUILD_DIR)/azure-vnet-telemetry$(EXE_EXT) $(CNI_OVERLAY_BUILD_DIR) - $(MKDIR) $(STATELESS_CNI_OVERLAY_BUILD_DIR) - cp $(STATELESS_CNI_BUILD_DIR)/azure-vnet$(EXE_EXT) $(STATELESS_CNI_OVERLAY_BUILD_DIR) cd $(CNI_OVERLAY_BUILD_DIR) && $(ARCHIVE_CMD) $(CNI_OVERLAY_ARCHIVE_NAME) azure-vnet$(EXE_EXT) azure-vnet-ipam$(EXE_EXT) azure-vnet-telemetry$(EXE_EXT) 10-azure.conflist azure-vnet-telemetry.config $(MKDIR) $(CNI_DUALSTACK_BUILD_DIR) diff --git a/cns/cnireconciler/podinfoprovider.go b/cns/cnireconciler/podinfoprovider.go index 11eee2133a5..8de4fe1eb09 100644 --- a/cns/cnireconciler/podinfoprovider.go +++ b/cns/cnireconciler/podinfoprovider.go @@ -113,27 +113,30 @@ func cniStateToCnsEndpointState(state *api.AzureCNIState) (map[string]*restserve endpointState := map[string]*restserver.EndpointInfo{} for _, endpoint := range state.ContainerInterfaces { endpointInfo := &restserver.EndpointInfo{PodName: endpoint.PodName, PodNamespace: endpoint.PodNamespace, HnsEndpointID: endpoint.HNSEndpointID, HostVethName: endpoint.HostIfName, IfnameToIPMap: make(map[string]*restserver.IPInfo)} + ipInfo := &restserver.IPInfo{} for _, epIP := range endpoint.IPAddresses { if epIP.IP.To4() == nil { // is an ipv6 address ipconfig := net.IPNet{IP: epIP.IP, Mask: epIP.Mask} - for _, ipconf := range endpointInfo.IfnameToIPMap[endpoint.IfName].IPv6 { + for _, ipconf := range ipInfo.IPv6 { if ipconf.IP.Equal(ipconfig.IP) { logger.Printf("Found existing ipv6 ipconfig for infra container %s", endpoint.ContainerID) return nil, nil } } - endpointInfo.IfnameToIPMap[endpoint.IfName].IPv6 = append(endpointInfo.IfnameToIPMap[endpoint.IfName].IPv6, ipconfig) + ipInfo.IPv6 = append(ipInfo.IPv6, ipconfig) + } else { ipconfig := net.IPNet{IP: epIP.IP, Mask: epIP.Mask} - for _, ipconf := range endpointInfo.IfnameToIPMap[endpoint.IfName].IPv4 { + for _, ipconf := range ipInfo.IPv4 { if ipconf.IP.Equal(ipconfig.IP) { logger.Printf("Found existing ipv4 ipconfig for infra container %s", endpoint.ContainerID) return nil, nil } } - endpointInfo.IfnameToIPMap[endpoint.IfName].IPv4 = append(endpointInfo.IfnameToIPMap[endpoint.IfName].IPv4, ipconfig) + ipInfo.IPv4 = append(ipInfo.IPv4, ipconfig) } } + endpointInfo.IfnameToIPMap[endpoint.IfName] = ipInfo endpointState[endpoint.ContainerID] = endpointInfo } return endpointState, nil diff --git a/cns/cnireconciler/podinfoprovider_test.go b/cns/cnireconciler/podinfoprovider_test.go index d4e253a7c83..8a6cb857235 100644 --- a/cns/cnireconciler/podinfoprovider_test.go +++ b/cns/cnireconciler/podinfoprovider_test.go @@ -31,7 +31,7 @@ func TestNewCNIPodInfoProvider(t *testing.T) { { name: "good", exec: newCNIStateFakeExec( - `{"ContainerInterfaces":{"3f813b02-eth0":{"PodName":"metrics-server-77c8679d7d-6ksdh","PodNamespace":"kube-system","PodEndpointID":"3f813b02-eth0","ContainerID":"3f813b029429b4e41a09ab33b6f6d365d2ed704017524c78d1d0dece33cdaf46","IPAddresses":[{"IP":"10.241.0.17","Mask":"//8AAA=="}]},"6e688597-eth0":{"PodName":"tunnelfront-5d96f9b987-65xbn","PodNamespace":"kube-system","PodEndpointID":"6e688597-eth0","ContainerID":"6e688597eafb97c83c84e402cc72b299bfb8aeb02021e4c99307a037352c0bed","IPAddresses":[{"IP":"10.241.0.13","Mask":"//8AAA=="}]}}}`, + `{"ContainerInterfaces":{"3f813b02-eth0":{"PodName":"metrics-server-77c8679d7d-6ksdh","IfName":"eth0","PodNamespace":"kube-system","PodEndpointID":"3f813b02-eth0","ContainerID":"3f813b029429b4e41a09ab33b6f6d365d2ed704017524c78d1d0dece33cdaf46","IPAddresses":[{"IP":"10.241.0.17","Mask":"//8AAA=="}]},"6e688597-eth0":{"PodName":"tunnelfront-5d96f9b987-65xbn","IfName":"eth0","PodNamespace":"kube-system","PodEndpointID":"6e688597-eth0","ContainerID":"6e688597eafb97c83c84e402cc72b299bfb8aeb02021e4c99307a037352c0bed","IPAddresses":[{"IP":"10.241.0.13","Mask":"//8AAA=="}]}}}`, ), want: map[string]cns.PodInfo{ "10.241.0.13": cns.NewPodInfo("6e688597eafb97c83c84e402cc72b299bfb8aeb02021e4c99307a037352c0bed", "6e688597-eth0", "tunnelfront-5d96f9b987-65xbn", "kube-system"), @@ -51,14 +51,14 @@ func TestNewCNIPodInfoProvider(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - got, _, err := newCNIPodInfoProvider(tt.exec) + got, endpointState, err := newCNIPodInfoProvider(tt.exec) if tt.wantErr { assert.Error(t, err) return } assert.NoError(t, err) podInfoByIP, _ := got.PodInfoByIP() - assert.Equal(t, tt.want, podInfoByIP) + assert.Equal(t, tt.want, podInfoByIP, endpointState) }) } } diff --git a/cns/service/main.go b/cns/service/main.go index 24a278934d1..1a65b25f162 100644 --- a/cns/service/main.go +++ b/cns/service/main.go @@ -1229,7 +1229,7 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn logger.Printf("[Azure CNS] No endpoint state found, skipping initializing CNS state") if cnsconfig.StatelessCNIMigration { logger.Printf("StatelessCNI Migration is enabled") - logger.Printf("Initializing from Statefull CNI") + logger.Printf("initializing from Statefull CNI") var endpointState map[string]*restserver.EndpointInfo podInfoByIPProvider, endpointState, err = cnireconciler.NewCNIPodInfoProvider() if err != nil {