Skip to content

Commit 6db5d8f

Browse files
author
Riya
committed
updated logic to update state file in place instead of delete state file upon pod subnet expansion
1 parent e84df8d commit 6db5d8f

File tree

2 files changed

+19
-80
lines changed

2 files changed

+19
-80
lines changed

cns/restserver/internalapi.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net"
1212
"net/http"
1313
"net/http/httptest"
14-
"os"
1514
"reflect"
1615
"strconv"
1716
"strings"
@@ -23,16 +22,13 @@ import (
2322
"github.com/Azure/azure-container-networking/cns/types"
2423
"github.com/Azure/azure-container-networking/common"
2524
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
26-
"github.com/labstack/gommon/log"
2725
"github.com/pkg/errors"
28-
"go.uber.org/zap"
2926
)
3027

3128
const (
3229
// Known API names we care about
3330
expectedIMDSAPIVersion = "2025-07-24"
3431
PrefixOnNicNCVersion = "1"
35-
cnsStateFilePath = "/var/lib/azure-network/azure-cns.json"
3632
)
3733

3834
// This file contains the internal functions called by either HTTP APIs (api.go) or
@@ -634,24 +630,23 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
634630
if ok {
635631
existingReq := existingNCInfo.CreateNetworkContainerRequest
636632
if !reflect.DeepEqual(existingReq.IPConfiguration.IPSubnet, req.IPConfiguration.IPSubnet) {
637-
logger.Errorf("[Azure CNS] Error. PrimaryCA is not same, NCId %s, old CA %s/%d, new CA %s/%d",
638-
req.NetworkContainerid,
639-
existingReq.IPConfiguration.IPSubnet.IPAddress,
640-
existingReq.IPConfiguration.IPSubnet.PrefixLength,
641-
req.IPConfiguration.IPSubnet.IPAddress,
642-
req.IPConfiguration.IPSubnet.PrefixLength)
643-
// delete the existing azure-cns file
644-
if err := os.Remove(cnsStateFilePath); err != nil && !os.IsNotExist(err) {
645-
log.Error("Error deleting azure-cns state file", zap.Error(err))
633+
634+
logger.Debugf("Updating PrimaryCA for NCId %s", req.NetworkContainerid)
635+
636+
// Update the in-memory state
637+
service.Lock()
638+
ncInfo := service.state.ContainerStatus[req.NetworkContainerid]
639+
ncInfo.CreateNetworkContainerRequest.IPConfiguration.IPSubnet = req.IPConfiguration.IPSubnet
640+
service.state.ContainerStatus[req.NetworkContainerid] = ncInfo
641+
service.Unlock()
642+
643+
// Persist the updated state to disk
644+
if err := service.saveState(); err != nil {
645+
logger.Errorf("Failed to save updated CNS state: %v", err)
646+
return types.UnexpectedError
646647
}
647648

648-
// panic (this will force azure-cns daemonset to restart and pick up the new state with updated nnc)
649-
panic(fmt.Sprintf("PrimaryCA mismatch for NCId %s: old CA %s/%d, new CA %s/%d",
650-
req.NetworkContainerid,
651-
existingReq.IPConfiguration.IPSubnet.IPAddress,
652-
existingReq.IPConfiguration.IPSubnet.PrefixLength,
653-
req.IPConfiguration.IPSubnet.IPAddress,
654-
req.IPConfiguration.IPSubnet.PrefixLength))
649+
logger.Debugf("Successfully updated PrimaryCA and saved CNS state")
655650
}
656651
}
657652

cns/restserver/internalapi_test.go

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,12 @@ func TestReconcileNCStatePrimaryIPChangeShouldFail(t *testing.T) {
9595
},
9696
}
9797

98-
// Create dummy CNS state file
99-
stateFile := "/var/lib/azure-network/azure-cns.json"
100-
_ = os.WriteFile(stateFile, []byte("dummy"), 0o600)
101-
defer os.Remove(stateFile)
102-
103-
defer func() {
104-
if r := recover(); r == nil {
105-
t.Errorf("Expected panic on PrimaryCA mismatch, but did not panic")
106-
}
107-
if _, err := os.Stat(stateFile); !os.IsNotExist(err) {
108-
t.Errorf("Expected CNS state file to be deleted, but it still exists")
109-
}
110-
}()
111-
11298
// now try to reconcile the state where the NC primary IP has changed
11399
_ = svc.ReconcileIPAMStateForSwift(ncReqs, map[string]cns.PodInfo{}, &v1alpha.NodeNetworkConfig{})
100+
101+
// Validate that the state was updated
102+
updatedNC := svc.state.ContainerStatus[ncID]
103+
assert.Equal(t, "10.0.2.0", updatedNC.CreateNetworkContainerRequest.IPConfiguration.IPSubnet.IPAddress, "PrimaryCA should be updated in state")
114104
}
115105

116106
// TestReconcileNCStateGatewayChange tests that NC state gets updated when reconciled
@@ -1692,49 +1682,3 @@ func setupIMDSMockAPIsWithCustomIDs(svc *HTTPRestService, interfaceIDs []string)
16921682
// Return cleanup function
16931683
return func() { svc.imdsClient = originalIMDS }
16941684
}
1695-
1696-
func TestCreateOrUpdateNCInternal_PrimaryCAMismatchShouldPanicAndDeleteStateFile(t *testing.T) {
1697-
restartService()
1698-
setEnv(t)
1699-
setOrchestratorTypeInternal(cns.KubernetesCRD)
1700-
1701-
// Step 1: Create initial NC with PrimaryCA "10.0.0.5/24"
1702-
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
1703-
ipaddress := "10.0.0.6"
1704-
secIPConfig := newSecondaryIPConfig(ipaddress, -1)
1705-
ipID := uuid.New()
1706-
secondaryIPConfigs[ipID.String()] = secIPConfig
1707-
1708-
ncID := "test-nc"
1709-
ncVersion := "-1"
1710-
req := generateNetworkContainerRequest(secondaryIPConfigs, ncID, ncVersion)
1711-
req.IPConfiguration.IPSubnet.IPAddress = "10.0.0.5"
1712-
req.IPConfiguration.IPSubnet.PrefixLength = 24
1713-
returnCode := svc.CreateOrUpdateNetworkContainerInternal(req)
1714-
if returnCode != 0 {
1715-
t.Fatalf("Failed to createNetworkContainerRequest, req: %+v, err: %d", req, returnCode)
1716-
}
1717-
validateNetworkRequest(t, *req)
1718-
1719-
// Step 2: Prepare a request with a different PrimaryCA
1720-
reqMismatch := generateNetworkContainerRequest(secondaryIPConfigs, ncID, ncVersion)
1721-
reqMismatch.IPConfiguration.IPSubnet.IPAddress = "10.0.0.7" // different IP
1722-
reqMismatch.IPConfiguration.IPSubnet.PrefixLength = 24
1723-
1724-
// Step 3: Create dummy CNS state file
1725-
stateFile := "/var/lib/azure-network/azure-cns.json"
1726-
_ = os.WriteFile(stateFile, []byte("dummy"), 0o600)
1727-
defer os.Remove(stateFile)
1728-
1729-
defer func() {
1730-
if r := recover(); r == nil {
1731-
t.Errorf("Expected panic on PrimaryCA mismatch, but did not panic")
1732-
}
1733-
if _, err := os.Stat(stateFile); !os.IsNotExist(err) {
1734-
t.Errorf("Expected CNS state file to be deleted, but it still exists")
1735-
}
1736-
}()
1737-
1738-
// Step 4: Should panic and delete state file
1739-
svc.CreateOrUpdateNetworkContainerInternal(reqMismatch)
1740-
}

0 commit comments

Comments
 (0)