Skip to content

Commit 2d45308

Browse files
Address comments
1 parent e4e92ca commit 2d45308

File tree

7 files changed

+99
-107
lines changed

7 files changed

+99
-107
lines changed

cns/fakes/imdsclientfake.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ func (m *MockIMDSClient) GetVMUniqueID(ctx context.Context) (string, error) {
5858
return "55b8499d-9b42-4f85-843f-24ff69f4a643", nil
5959
}
6060

61-
func (m *MockIMDSClient) GetNCVersionsFromIMDS(ctx context.Context) (map[string]string, error) {
61+
func (m *MockIMDSClient) GetNCVersions(ctx context.Context) ([]imds.NetworkInterface, error) {
6262
if ctx.Value(SimulateError) != nil {
6363
return nil, imds.ErrUnexpectedStatusCode
6464
}
6565

6666
// Return some mock NC versions for testing
67-
return map[string]string{
68-
"nc1": "1",
69-
"nc2": "2",
67+
return []imds.NetworkInterface{
68+
{
69+
InterfaceCompartmentId: "nc1",
70+
InterfaceCompartmentVersion: "1",
71+
},
72+
{
73+
InterfaceCompartmentId: "nc2",
74+
InterfaceCompartmentVersion: "2",
75+
},
7076
}, nil
7177
}

cns/imds/client.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ const (
5353
metadataHeaderValue = "true"
5454
defaultRetryAttempts = 3
5555
defaultIMDSEndpoint = "http://169.254.169.254"
56-
ncVersion = "ncVersion"
5756
)
5857

5958
var (
@@ -104,7 +103,7 @@ func (c *Client) GetVMUniqueID(ctx context.Context) (string, error) {
104103
return vmUniqueID, nil
105104
}
106105

107-
func (c *Client) GetNCVersionsFromIMDS(ctx context.Context) (map[string]string, error) {
106+
func (c *Client) GetNCVersions(ctx context.Context) ([]NetworkInterface, error) {
108107
var networkData NetworkMetadata
109108
err := retry.Do(func() error {
110109
networkMetadata, err := c.getInstanceMetadata(ctx, imdsNetworkPath)
@@ -128,20 +127,7 @@ func (c *Client) GetNCVersionsFromIMDS(ctx context.Context) (map[string]string,
128127
return nil, err
129128
}
130129

131-
ncVersions := make(map[string]string)
132-
for _, iface := range networkData.Interface {
133-
// IMDS only returns compartment fields (interfaceCompartmentId, interfaceCompartmentVersion)
134-
// We map these to NC ID and NC version concepts
135-
// Standard fields (ncId, ncVersion) are ignored even if present
136-
ncId := iface.InterfaceCompartmentId
137-
ncVersion := iface.InterfaceCompartmentVersion
138-
139-
if ncId != "" {
140-
ncVersions[ncId] = ncVersion
141-
}
142-
}
143-
144-
return ncVersions, nil
130+
return networkData.Interface, nil
145131
}
146132

147133
func (c *Client) getInstanceMetadata(ctx context.Context, imdsComputePath string) (map[string]any, error) {
@@ -178,7 +164,6 @@ func (c *Client) getInstanceMetadata(ctx context.Context, imdsComputePath string
178164

179165
// NetworkInterface represents a network interface from IMDS
180166
type NetworkInterface struct {
181-
MacAddress string `json:"macAddress"`
182167
// IMDS only returns compartment fields - these are mapped to NC ID and NC version concepts
183168
InterfaceCompartmentId string `json:"interfaceCompartmentId,omitempty"`
184169
InterfaceCompartmentVersion string `json:"interfaceCompartmentVersion,omitempty"`

cns/imds/client_test.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,16 @@ func TestInvalidVMUniqueID(t *testing.T) {
101101
require.Equal(t, "", vmUniqueID)
102102
}
103103

104-
func TestGetNCVersionsFromIMDS(t *testing.T) {
104+
func TestGetNCVersions(t *testing.T) {
105105
networkMetadata := []byte(`{
106106
"interface": [
107107
{
108-
"macAddress": "00:0D:3A:12:34:56",
109108
"interfaceCompartmentVersion": "1",
110109
"interfaceCompartmentId": "nc-12345-67890"
111110
},
112111
{
113-
"macAddress": "00:0D:3A:CD:EF:12",
114-
"interfaceCompartmentVersion": "",
115-
"interfaceCompartmentId": "nc-abcdef-123456"
112+
"interfaceCompartmentVersion": "1",
113+
"interfaceCompartmentId": ""
116114
}
117115
]
118116
}`)
@@ -138,23 +136,28 @@ func TestGetNCVersionsFromIMDS(t *testing.T) {
138136
defer mockIMDSServer.Close()
139137

140138
imdsClient := imds.NewClient(imds.Endpoint(mockIMDSServer.URL))
141-
ncVersions, err := imdsClient.GetNCVersionsFromIMDS(context.Background())
139+
interfaces, err := imdsClient.GetNCVersions(context.Background())
142140
require.NoError(t, err, "error querying testserver")
143141

144-
expectedNCVersions := map[string]string{
145-
"nc-12345-67890": "1",
146-
"nc-abcdef-123456": "", // empty version
147-
}
148-
require.Equal(t, expectedNCVersions, ncVersions)
142+
// Verify we got the expected interfaces
143+
require.Len(t, interfaces, 2, "expected 2 interfaces")
144+
145+
// Check first interface
146+
assert.Equal(t, "nc-12345-67890", interfaces[0].InterfaceCompartmentId)
147+
assert.Equal(t, "1", interfaces[0].InterfaceCompartmentVersion)
148+
149+
// Check second interface
150+
assert.Equal(t, "", interfaces[1].InterfaceCompartmentId)
151+
assert.Equal(t, "1", interfaces[1].InterfaceCompartmentVersion)
149152
}
150153

151-
func TestGetNCVersionsFromIMDSInvalidEndpoint(t *testing.T) {
154+
func TestGetNCVersionsInvalidEndpoint(t *testing.T) {
152155
imdsClient := imds.NewClient(imds.Endpoint(string([]byte{0x7f})), imds.RetryAttempts(1))
153-
_, err := imdsClient.GetNCVersionsFromIMDS(context.Background())
156+
_, err := imdsClient.GetNCVersions(context.Background())
154157
require.Error(t, err, "expected invalid path")
155158
}
156159

157-
func TestGetNCVersionsFromIMDSInvalidJSON(t *testing.T) {
160+
func TestGetNCVersionsInvalidJSON(t *testing.T) {
158161
mockIMDSServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
159162
w.WriteHeader(http.StatusOK)
160163
_, err := w.Write([]byte("not json"))
@@ -163,15 +166,14 @@ func TestGetNCVersionsFromIMDSInvalidJSON(t *testing.T) {
163166
defer mockIMDSServer.Close()
164167

165168
imdsClient := imds.NewClient(imds.Endpoint(mockIMDSServer.URL), imds.RetryAttempts(1))
166-
_, err := imdsClient.GetNCVersionsFromIMDS(context.Background())
169+
_, err := imdsClient.GetNCVersions(context.Background())
167170
require.Error(t, err, "expected json decoding error")
168171
}
169172

170-
func TestGetNCVersionsFromIMDSNoNCIDs(t *testing.T) {
173+
func TestGetNCVersionsNoNCIDs(t *testing.T) {
171174
networkMetadataNoNC := []byte(`{
172175
"interface": [
173176
{
174-
"macAddress": "00:0D:3A:12:34:56",
175177
"ipv4": {
176178
"ipAddress": [
177179
{
@@ -180,17 +182,6 @@ func TestGetNCVersionsFromIMDSNoNCIDs(t *testing.T) {
180182
}
181183
]
182184
}
183-
},
184-
{
185-
"macAddress": "00:0D:3A:78:90:AB",
186-
"ipv4": {
187-
"ipAddress": [
188-
{
189-
"privateIpAddress": "10.0.1.4",
190-
"publicIpAddress": ""
191-
}
192-
]
193-
}
194185
}
195186
]
196187
}`)
@@ -206,7 +197,10 @@ func TestGetNCVersionsFromIMDSNoNCIDs(t *testing.T) {
206197
defer mockIMDSServer.Close()
207198

208199
imdsClient := imds.NewClient(imds.Endpoint(mockIMDSServer.URL))
209-
ncVersions, err := imdsClient.GetNCVersionsFromIMDS(context.Background())
200+
interfaces, err := imdsClient.GetNCVersions(context.Background())
210201
require.NoError(t, err, "error querying testserver")
211-
require.Empty(t, ncVersions, "expected empty NC versions map when no NC IDs present")
202+
203+
// Verify we got interfaces but they don't have compartment IDs
204+
require.Len(t, interfaces, 1, "expected 1 interface")
205+
assert.Equal(t, "", interfaces[0].InterfaceCompartmentId)
212206
}

cns/restserver/internalapi.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
172172
service.Lock()
173173
defer service.Unlock()
174174
start := time.Now()
175-
176175
programmedNCCount, err := service.syncHostNCVersion(ctx, channelMode)
177176
// even if we get an error, we want to write the CNI conflist if we have any NC programmed to any version
178177
if programmedNCCount > 0 {
@@ -208,7 +207,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
208207
logger.Errorf("Received err when change nc version %s in containerstatus to int, err msg %v", service.state.ContainerStatus[idx].CreateNetworkContainerRequest.Version, err)
209208
continue
210209
}
211-
logger.Printf("NC %s: local NC version %d, DNC NC version %d", service.state.ContainerStatus[idx].ID, localNCVersion, dncNCVersion)
210+
212211
// host NC version is the NC version from NMAgent, if it's smaller than NC version from DNC, then append it to indicate it needs update.
213212
if localNCVersion < dncNCVersion {
214213
outdatedNCs[service.state.ContainerStatus[idx].ID] = struct{}{}
@@ -223,7 +222,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
223222
if len(outdatedNCs) == 0 {
224223
return len(programmedNCs), nil
225224
}
226-
logger.Printf("outdatedNCs: %v", outdatedNCs)
225+
227226
ncVersionListResp, err := service.nma.GetNCVersionList(ctx)
228227
if err != nil {
229228
return len(programmedNCs), errors.Wrap(err, "failed to get nc version list from nmagent")
@@ -232,7 +231,6 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
232231
// Get IMDS NC versions for delegated NIC scenarios
233232
imdsNCVersions, err := service.GetIMDSNCVersions(ctx)
234233
if err != nil {
235-
logger.Printf("Failed to get NC versions from IMDS: %v", err)
236234
// If any of the NMA API check calls, imds calls fails assume that nma build doesn't have the latest changes and create empty map
237235
imdsNCVersions = make(map[string]string)
238236
}
@@ -249,7 +247,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
249247
}
250248
for ncID, version := range imdsNCVersions {
251249
if _, exists := consolidatedNCs[ncID]; !exists {
252-
consolidatedNCs[ncID] = version
250+
consolidatedNCs[strings.ToLower(ncID)] = version
253251
}
254252
}
255253
hasNC.Set(float64(len(consolidatedNCs)))
@@ -298,7 +296,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
298296
// if we didn't empty out the needs update set, NMA has not programmed all the NCs we are expecting, and we
299297
// need to return an error indicating that
300298
if len(outdatedNCs) > 0 {
301-
return len(programmedNCs), errors.Errorf("unabled to update some NCs: %v, missing or bad response from NMA or ipam", outdatedNCs)
299+
return len(programmedNCs), errors.Errorf("unable to update some NCs: %v, missing or bad response from NMA or IMDS", outdatedNCs)
302300
}
303301

304302
return len(programmedNCs), nil
@@ -359,7 +357,6 @@ func (service *HTTPRestService) ReconcileIPAssignment(podInfoByIP map[string]cns
359357

360358
for _, ip := range ips {
361359
if ncReq, ok := allSecIPsIdx[ip.String()]; ok {
362-
logger.Printf("secondary ip %s is assigned to pod %+v, ncId: %s ncVersion: %s", ip, podIPs, ncReq.NetworkContainerid, ncReq.Version)
363360
desiredIPs = append(desiredIPs, ip.String())
364361
ncIDs = append(ncIDs, ncReq.NetworkContainerid)
365362
} else {
@@ -388,7 +385,7 @@ func (service *HTTPRestService) ReconcileIPAssignment(podInfoByIP map[string]cns
388385
}
389386

390387
if _, err := requestIPConfigsHelper(service, ipconfigsRequest); err != nil {
391-
logger.Errorf("requestIPConfigsHelper failed for pod key %s, podInfo %+v, ncIds %v, error: %v", podKey, podIPs, ncIDs, err)
388+
logger.Errorf("requestIPConfigsHelper failed for pod key %s, podInfo %+v, ncIDs %v, error: %v", podKey, podIPs, ncIDs, err)
392389
return types.FailedToAllocateIPConfig
393390
}
394391
}
@@ -666,54 +663,51 @@ func (service *HTTPRestService) SetVFForAccelnetNICs() error {
666663
func (service *HTTPRestService) checkNMAgentAPISupport(ctx context.Context) (swiftV2Support bool, err error) {
667664
// Use the existing NMAgent client instead of direct HTTP calls
668665
if service.nma == nil {
669-
return false, fmt.Errorf("NMAgent client is not available")
666+
return false, errors.New("NMAgent client is not available")
670667
}
671668

672669
apis, err := service.nma.SupportedAPIs(ctx)
673670
if err != nil {
674-
return false, fmt.Errorf("failed to get supported APIs from NMAgent client: %w", err)
671+
logger.Errorf("failed to get supported APIs from NMAgent client: %v", err)
672+
return false, errors.New("failed to get supported APIs from NMAgent client")
675673
}
676674

677-
logger.Printf("[checkNMAgentAPISupport] Found %d APIs from NMAgent client", len(apis))
678-
for i, api := range apis {
679-
logger.Printf("[checkNMAgentAPISupport] API %d: %s", i+1, api)
680-
681-
if strings.Contains(api, nmAgentSwiftV2API) { // change
675+
for _, api := range apis {
676+
if strings.Contains(api, nmAgentSwiftV2API) {
682677
swiftV2Support = true
683678
}
684679
}
685680

686-
logger.Printf("[checkNMAgentAPISupport] Support check - SwiftV2: %t", swiftV2Support)
687681
return swiftV2Support, nil
688682
}
689683

690684
// GetIMDSNCVersions gets NC versions from IMDS and returns them as a map
691685
func (service *HTTPRestService) GetIMDSNCVersions(ctx context.Context) (map[string]string, error) {
692-
logger.Printf("[GetIMDSNCVersions] Getting NC versions from IMDS")
693-
694686
// Check NMAgent API support for SwiftV2, if it fails return empty map assuming support might not be available in that nma build
695687
swiftV2Support, err := service.checkNMAgentAPISupport(ctx)
696-
if err != nil {
697-
logger.Printf("[GetIMDSNCVersions] Failed to check NMAgent API support, returning empty map: %v", err)
698-
return make(map[string]string), nil
699-
}
700-
701-
if !swiftV2Support {
702-
logger.Printf("[GetIMDSNCVersions] SwiftV2 API not supported, returning empty NC versions map")
688+
if err != nil || !swiftV2Support {
703689
return make(map[string]string), nil
704690
}
705691

706-
logger.Printf("[GetIMDSNCVersions] SwiftV2 support API exists (%t), proceeding to get NC versions from IMDS", swiftV2Support)
707-
708692
imdsClient := service.imdsClient
709693

710694
// Get all NC versions from IMDS
711-
ncVersions, err := imdsClient.GetNCVersionsFromIMDS(ctx)
695+
networkInterfaces, err := imdsClient.GetNCVersions(ctx)
712696
if err != nil {
713-
logger.Printf("[GetIMDSNCVersions] Failed to get NC versions from IMDS: %v", err)
714697
return make(map[string]string), nil
715698
}
716699

717-
logger.Printf("[GetIMDSNCVersions] Successfully got %d NC versions from IMDS", len(ncVersions))
700+
// Build ncVersions map from the network interfaces
701+
ncVersions := make(map[string]string)
702+
for _, iface := range networkInterfaces {
703+
// IMDS returns interfaceCompartmentId, interfaceCompartmentVersion fields, as nc id guid has different context on nma. We map these to NC ID and NC version
704+
ncID := iface.InterfaceCompartmentId
705+
ncVersion := iface.InterfaceCompartmentVersion
706+
707+
if ncID != "" {
708+
ncVersions[ncID] = ncVersion
709+
}
710+
}
711+
718712
return ncVersions, nil
719713
}

0 commit comments

Comments
 (0)