Skip to content

Commit 6738454

Browse files
committed
clean up a bit
1 parent 80f25a8 commit 6738454

File tree

3 files changed

+24
-267
lines changed

3 files changed

+24
-267
lines changed

cns/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ type GetIBDeviceInfoResponse struct {
411411
Response Response `json:"response,omitempty"` // Standard CNS response (optional for success cases)
412412
DeviceID string `json:"deviceID"` // MAC address of the device
413413
PodID string `json:"podID"` // Pod that the device is assigned to
414-
Status string `json:"status"` // Device status (e.g., "assigned", "available")
414+
Status string `json:"status"` // Device status (e.g., "pendingProgramming", "error", "programmed", "pendingDeletion", "available")
415415
ErrorCode int `json:"errorCode"` // Error code if applicable
416416
Msg string `json:"msg"` // Additional message or error description
417417
}

cns/restserver/ibdevice.go

Lines changed: 15 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@ package restserver
55

66
import (
77
"fmt"
8-
"sync"
9-
"time"
108

11-
"github.com/Azure/azure-container-networking/cns/logger"
129
"github.com/Azure/azure-container-networking/cns/types"
1310
)
1411

15-
// IBDeviceState represents the state of an InfiniBand device
16-
type IBDeviceState int
12+
// IBDeviceStatus represents the programming status of an InfiniBand device
13+
type IBDeviceStatus int
1714

1815
const (
19-
IBDevicePendingProgramming IBDeviceState = iota
16+
IBDevicePendingProgramming IBDeviceStatus = iota
2017
IBDeviceError
2118
IBDeviceProgrammed
2219
IBDevicePendingDeletion
2320
IBDeviceAvailable
2421
)
2522

26-
func (s IBDeviceState) String() string {
23+
func (s IBDeviceStatus) String() string {
2724
switch s {
2825
case IBDevicePendingProgramming:
2926
return "pendingprogramming"
@@ -42,256 +39,23 @@ func (s IBDeviceState) String() string {
4239

4340
// IBDeviceInfo contains information about an InfiniBand device
4441
type IBDeviceInfo struct {
45-
DeviceID string `json:"deviceID"`
46-
MacAddress string `json:"macAddress"`
47-
State IBDeviceState `json:"state"`
48-
PodID string `json:"podID,omitempty"`
49-
AssignedAt time.Time `json:"assignedAt,omitempty"`
50-
LastStateTransition time.Time `json:"lastStateTransition"`
51-
ErrorCode int `json:"errorCode"`
52-
ErrorMessage string `json:"errorMessage,omitempty"`
53-
mu sync.RWMutex `json:"-"`
54-
}
55-
56-
// SetState updates the device state and timestamp
57-
func (d *IBDeviceInfo) SetState(newState IBDeviceState) {
58-
d.mu.Lock()
59-
defer d.mu.Unlock()
60-
d.State = newState
61-
d.LastStateTransition = time.Now()
62-
}
63-
64-
// GetState returns the current device state
65-
func (d *IBDeviceInfo) GetState() IBDeviceState {
66-
d.mu.RLock()
67-
defer d.mu.RUnlock()
68-
return d.State
69-
}
70-
71-
// Assign assigns the device to a pod
72-
func (d *IBDeviceInfo) Assign(podID string) {
73-
d.mu.Lock()
74-
defer d.mu.Unlock()
75-
d.PodID = podID
76-
d.State = IBDeviceProgrammed
77-
d.AssignedAt = time.Now()
78-
d.LastStateTransition = time.Now()
79-
d.ErrorCode = 0
80-
d.ErrorMessage = ""
81-
}
82-
83-
// Release releases the device from a pod
84-
func (d *IBDeviceInfo) Release() {
85-
d.mu.Lock()
86-
defer d.mu.Unlock()
87-
d.PodID = ""
88-
d.State = IBDeviceAvailable
89-
d.AssignedAt = time.Time{}
90-
d.LastStateTransition = time.Now()
91-
d.ErrorCode = 0
92-
d.ErrorMessage = ""
93-
}
94-
95-
// SetError sets an error state for the device
96-
func (d *IBDeviceInfo) SetError(code int, message string) {
97-
d.mu.Lock()
98-
defer d.mu.Unlock()
99-
d.ErrorCode = code
100-
d.ErrorMessage = message
101-
d.LastStateTransition = time.Now()
102-
}
103-
104-
// IBDeviceManager manages the state of InfiniBand devices
105-
type IBDeviceManager struct {
106-
devices map[string]*IBDeviceInfo // deviceID -> device info
107-
podDevices map[string][]string // podID -> list of deviceIDs
108-
devicesByMac map[string]string // macAddress -> deviceID
109-
mu sync.RWMutex
110-
}
111-
112-
// NewIBDeviceManager creates a new IB device manager
113-
func NewIBDeviceManager() *IBDeviceManager {
114-
return &IBDeviceManager{
115-
devices: make(map[string]*IBDeviceInfo),
116-
podDevices: make(map[string][]string),
117-
devicesByMac: make(map[string]string),
118-
}
119-
}
120-
121-
// RegisterDevice registers a new IB device
122-
func (m *IBDeviceManager) RegisterDevice(deviceID, macAddress string) error {
123-
m.mu.Lock()
124-
defer m.mu.Unlock()
125-
126-
// Check if device already exists
127-
if _, exists := m.devices[deviceID]; exists {
128-
logger.Printf("[IBDeviceManager] Device %s already registered", deviceID)
129-
return nil
130-
}
131-
132-
// Check if MAC address is already registered
133-
if existingDeviceID, exists := m.devicesByMac[macAddress]; exists {
134-
logger.Printf("[IBDeviceManager] MAC address %s already registered to device %s", macAddress, existingDeviceID)
135-
return nil
136-
}
137-
138-
device := &IBDeviceInfo{
139-
DeviceID: deviceID,
140-
MacAddress: macAddress,
141-
State: IBDeviceAvailable,
142-
LastStateTransition: time.Now(),
143-
ErrorCode: 0,
144-
}
145-
146-
m.devices[deviceID] = device
147-
m.devicesByMac[macAddress] = deviceID
148-
149-
logger.Printf("[IBDeviceManager] Registered device %s with MAC %s", deviceID, macAddress)
150-
return nil
42+
MacAddress string `json:"macAddress"`
43+
Status IBDeviceStatus `json:"status"`
44+
PodID string `json:"podID,omitempty"`
45+
ErrorCode int `json:"errorCode"`
46+
ErrorMessage string `json:"errorMessage,omitempty"`
15147
}
15248

15349
// GetDevice returns device information by device ID
154-
func (m *IBDeviceManager) GetDevice(deviceID string) (*IBDeviceInfo, bool) {
155-
m.mu.RLock()
156-
defer m.mu.RUnlock()
157-
158-
device, exists := m.devices[deviceID]
159-
if !exists {
160-
return nil, false
161-
}
162-
163-
// Return a copy to avoid race conditions
164-
deviceCopy := *device
165-
return &deviceCopy, true
166-
}
167-
168-
// GetDeviceByMac returns device information by MAC address
169-
func (m *IBDeviceManager) GetDeviceByMac(macAddress string) (*IBDeviceInfo, bool) {
170-
m.mu.RLock()
171-
defer m.mu.RUnlock()
172-
173-
deviceID, exists := m.devicesByMac[macAddress]
174-
if !exists {
175-
return nil, false
176-
}
177-
178-
device, exists := m.devices[deviceID]
179-
if !exists {
180-
return nil, false
181-
}
182-
183-
// Return a copy to avoid race conditions
184-
deviceCopy := *device
185-
return &deviceCopy, true
50+
func GetDevice(deviceID string) (*IBDeviceInfo, bool) {
51+
// here we would return the device status from either the cache or mtpnc
52+
return nil, false
18653
}
18754

18855
// AssignDevicesToPod assigns multiple devices to a pod
189-
func (m *IBDeviceManager) AssignDevicesToPod(podID string, deviceIDs []string) (types.ResponseCode, string) {
190-
m.mu.Lock()
191-
defer m.mu.Unlock()
192-
193-
// Validate all devices first
194-
for _, deviceID := range deviceIDs {
195-
device, exists := m.devices[deviceID]
196-
if !exists {
197-
return types.NotFound, "Device " + deviceID + " not found"
198-
}
199-
200-
if device.GetState() != IBDeviceAvailable {
201-
if device.GetState() == IBDeviceProgrammed {
202-
return types.InvalidRequest, "Device " + deviceID + " is already assigned to pod " + device.PodID
203-
}
204-
return types.InvalidRequest, "Device " + deviceID + " is not available (state: " + device.GetState().String() + ")"
205-
}
206-
}
207-
208-
// Check if pod already has devices assigned
209-
if existingDevices, exists := m.podDevices[podID]; exists && len(existingDevices) > 0 {
210-
return types.InvalidRequest, "Pod " + podID + " already has devices assigned"
211-
}
212-
213-
// Assign all devices
214-
assignedDevices := make([]string, 0, len(deviceIDs))
215-
for _, deviceID := range deviceIDs {
216-
device := m.devices[deviceID]
217-
device.Assign(podID)
218-
assignedDevices = append(assignedDevices, deviceID)
219-
logger.Printf("[IBDeviceManager] Assigned device %s to pod %s", deviceID, podID)
220-
}
221-
222-
// Update pod device mapping
223-
m.podDevices[podID] = assignedDevices
224-
225-
return types.Success, "Successfully assigned " + fmt.Sprintf("%d", len(deviceIDs)) + " devices to pod " + podID
226-
}
227-
228-
// ReleaseDevicesFromPod releases all devices assigned to a pod
229-
func (m *IBDeviceManager) ReleaseDevicesFromPod(podID string) (types.ResponseCode, string) {
230-
m.mu.Lock()
231-
defer m.mu.Unlock()
232-
233-
deviceIDs, exists := m.podDevices[podID]
234-
if !exists || len(deviceIDs) == 0 {
235-
return types.NotFound, "No devices assigned to pod " + podID
236-
}
237-
238-
// Release all devices
239-
for _, deviceID := range deviceIDs {
240-
if device, exists := m.devices[deviceID]; exists {
241-
device.Release()
242-
logger.Printf("[IBDeviceManager] Released device %s from pod %s", deviceID, podID)
243-
}
244-
}
245-
246-
// Remove pod device mapping
247-
delete(m.podDevices, podID)
248-
249-
return types.Success, "Successfully released devices from pod " + podID
250-
}
251-
252-
// GetPodDevices returns the list of devices assigned to a pod
253-
func (m *IBDeviceManager) GetPodDevices(podID string) []string {
254-
m.mu.RLock()
255-
defer m.mu.RUnlock()
256-
257-
if devices, exists := m.podDevices[podID]; exists {
258-
// Return a copy
259-
result := make([]string, len(devices))
260-
copy(result, devices)
261-
return result
262-
}
263-
264-
return nil
265-
}
266-
267-
// GetAvailableDevices returns a list of available devices
268-
func (m *IBDeviceManager) GetAvailableDevices() []string {
269-
m.mu.RLock()
270-
defer m.mu.RUnlock()
271-
272-
var available []string
273-
for deviceID, device := range m.devices {
274-
if device.GetState() == IBDeviceAvailable {
275-
available = append(available, deviceID)
276-
}
277-
}
278-
279-
return available
280-
}
281-
282-
// GetAssignedDevices returns a list of assigned devices
283-
func (m *IBDeviceManager) GetAssignedDevices() []string {
284-
m.mu.RLock()
285-
defer m.mu.RUnlock()
286-
287-
var assigned []string
288-
for deviceID, device := range m.devices {
289-
if device.GetState() == IBDeviceProgrammed {
290-
assigned = append(assigned, deviceID)
291-
}
292-
}
293-
294-
return assigned
56+
func AssignDevicesToPod(podID string, deviceIDs []string) (types.ResponseCode, string) {
57+
// here is where we would make the MTPNC and write the devices to the spec
58+
return types.Success, fmt.Sprintf("Assigned devices %v to pod %s", deviceIDs, podID)
29559
}
29660

29761
// IsValidMacAddress validates MAC address format

docs/cns-ibdevice-api-contracts.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PUT /ibdevices/pod/{podname-podnamespace}
2929
```json
3030
{
3131
"response": {
32-
"cnsCode": 0,
32+
"errorCode": 0,
3333
"message": "Successfully assigned 2 devices to pod my-pod-my-namespace"
3434
}
3535
}
@@ -39,7 +39,7 @@ PUT /ibdevices/pod/{podname-podnamespace}
3939
```json
4040
{
4141
"response": {
42-
"cnsCode": 23,
42+
"errorCode": 23,
4343
"message": "Device 60:45:bd:a4:b5:7a is already assigned to another pod"
4444
}
4545
}
@@ -93,7 +93,7 @@ type AssignIBDevicesToPodRequest struct {
9393

9494
// Assign IB Devices Response
9595
type AssignIBDevicesToPodResponse struct {
96-
Response Response `json:"response"` // Contains cnsCode and message
96+
Response Response `json:"response"` // Contains errorCode and message
9797
}
9898

9999
// Get Device Info Request (MAC from URL path)
@@ -131,7 +131,7 @@ message AssignIBDevicesToPodRequest {
131131
}
132132
133133
message AssignIBDevicesToPodResponse {
134-
int32 cnsCode = 1; // 0 for success
134+
int32 returnCode = 1; // 0 for success
135135
string message = 2; // Response message
136136
}
137137
@@ -179,21 +179,14 @@ resp, err := client.AssignIBDevicesToPod(ctx, &pb.AssignIBDevicesToPodRequest{
179179
| Code | Name | Description |
180180
|------|-------------------|---------------------------------------|
181181
| 0 | Success | Operation completed successfully |
182-
| 2 | InvalidParameter | Invalid request parameters |
183-
| 14 | NotFound | Device not found |
184-
| 23 | InvalidRequest | Invalid request (device already assigned) |
182+
| 1 | InvalidRequest | Invalid request parameters |
183+
| 2 | NotFound | Device not found |
185184

186185
---
187186

188187
## Device Status Values
189-
190-
| Status | Description |
191-
|-------------------|--------------------------------------------|
192-
| `"available"` | Device available for assignment |
193-
| `"assigned"` | Device currently assigned to a pod |
194-
| `"pendingrelease"`| Device pending release from a pod |
195-
196-
---
188+
189+
See [ibdevice.go](../cns/restserver/ibdevice.go)
197190

198191
## Path Constants
199192

0 commit comments

Comments
 (0)