@@ -5,25 +5,22 @@ package restserver
55
66import (
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
1815const (
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
4441type 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
0 commit comments