Skip to content

Commit 367dd4f

Browse files
committed
feat: react on device actions also if not in Running
1 parent bd874af commit 367dd4f

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

internal/controller/core/device_controller.go

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ c
8686
return ctrl.Result{}, err
8787
}
8888

89+
prov, ok := r.Provider().(provider.DeviceProvider)
90+
if !ok {
91+
err := errors.New("provider does not implement DeviceProvider interface")
92+
log.Error(err, "failed to reconcile resource")
93+
return ctrl.Result{}, err
94+
}
95+
conn, err := deviceutil.GetDeviceConnection(ctx, r, obj)
96+
if err != nil {
97+
return ctrl.Result{}, fmt.Errorf("failed to obtain device connection: %w", err)
98+
}
99+
89100
orig := obj.DeepCopy()
90101

91102
if conditions.InitializeConditions(obj, v1alpha1.ReadyCondition) {
@@ -168,10 +179,6 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ c
168179
}
169180
log.Info("Device provisioning completed, running post provisioning checks")
170181
prov, _ := r.Provider().(provider.ProvisioningProvider)
171-
conn, err := deviceutil.GetDeviceConnection(ctx, r, obj)
172-
if err != nil {
173-
return ctrl.Result{}, fmt.Errorf("failed to obtain device connection: %w", err)
174-
}
175182
if ok := prov.VerifyProvisioned(ctx, conn, obj); !ok {
176183
return ctrl.Result{RequeueAfter: r.RequeueInterval}, nil
177184
}
@@ -180,7 +187,7 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ c
180187
obj.Status.Phase = v1alpha1.DevicePhaseRunning
181188

182189
case v1alpha1.DevicePhaseRunning:
183-
if err := r.reconcile(ctx, obj); err != nil {
190+
if err := r.reconcile(ctx, obj, prov, conn); err != nil {
184191
log.Error(err, "Failed to reconcile resource")
185192
return ctrl.Result{}, err
186193
}
@@ -199,7 +206,10 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ c
199206
obj.Status.Phase = v1alpha1.DevicePhaseRunning
200207
}
201208

202-
log.Info(fmt.Sprintf("End of Reconcile(), phase was is %s", obj.Status.Phase))
209+
if err := r.handleDeviceActions(ctx, obj, prov, conn); err != nil {
210+
return ctrl.Result{}, err
211+
}
212+
203213
return ctrl.Result{}, nil
204214
}
205215

@@ -244,77 +254,67 @@ func (r *DeviceReconciler) SetupWithManager(mgr ctrl.Manager) error {
244254
Complete(r)
245255
}
246256

247-
func (r *DeviceReconciler) reconcile(ctx context.Context, device *v1alpha1.Device) (reterr error) {
248-
if prov, ok := r.Provider().(provider.DeviceProvider); ok {
249-
conn, err := deviceutil.GetDeviceConnection(ctx, r, device)
250-
if err != nil {
251-
return err
257+
func (r *DeviceReconciler) reconcile(ctx context.Context, device *v1alpha1.Device, prov provider.DeviceProvider, conn *deviceutil.Connection) (reterr error) {
258+
if err := prov.Connect(ctx, conn); err != nil {
259+
conditions.Set(device, metav1.Condition{
260+
Type: v1alpha1.ReadyCondition,
261+
Status: metav1.ConditionFalse,
262+
Reason: v1alpha1.UnreachableReason,
263+
Message: fmt.Sprintf("Failed to connect to provider: %v", err),
264+
})
265+
return fmt.Errorf("failed to connect to provider: %w", err)
266+
}
267+
defer func() {
268+
if err := prov.Disconnect(ctx, conn); err != nil {
269+
reterr = kerrors.NewAggregate([]error{reterr, err})
252270
}
271+
}()
253272

254-
if err := r.handleDeviceActions(ctx, device, prov, conn); err != nil {
255-
return err
256-
}
273+
ports, err := prov.ListPorts(ctx)
274+
if err != nil {
275+
return fmt.Errorf("failed to list device ports: %w", err)
276+
}
257277

258-
if err := prov.Connect(ctx, conn); err != nil {
259-
conditions.Set(device, metav1.Condition{
260-
Type: v1alpha1.ReadyCondition,
261-
Status: metav1.ConditionFalse,
262-
Reason: v1alpha1.UnreachableReason,
263-
Message: fmt.Sprintf("Failed to connect to provider: %v", err),
264-
})
265-
return fmt.Errorf("failed to connect to provider: %w", err)
266-
}
267-
defer func() {
268-
if err := prov.Disconnect(ctx, conn); err != nil {
269-
reterr = kerrors.NewAggregate([]error{reterr, err})
270-
}
271-
}()
278+
interfaces := new(v1alpha1.InterfaceList)
279+
if err := r.List(ctx, interfaces, client.InNamespace(device.Namespace), client.MatchingLabels{v1alpha1.DeviceLabel: device.Name}); err != nil {
280+
return fmt.Errorf("failed to list interface resources for device: %w", err)
281+
}
272282

273-
ports, err := prov.ListPorts(ctx)
274-
if err != nil {
275-
return fmt.Errorf("failed to list device ports: %w", err)
276-
}
283+
m := make(map[string]string) // ID => Resource Name
284+
for _, intf := range interfaces.Items {
285+
m[intf.Spec.Name] = intf.Name
286+
}
277287

278-
interfaces := new(v1alpha1.InterfaceList)
279-
if err := r.List(ctx, interfaces, client.InNamespace(device.Namespace), client.MatchingLabels{v1alpha1.DeviceLabel: device.Name}); err != nil {
280-
return fmt.Errorf("failed to list interface resources for device: %w", err)
288+
device.Status.Ports = make([]v1alpha1.DevicePort, len(ports))
289+
n := int32(0)
290+
for i, p := range ports {
291+
var ref *v1alpha1.LocalObjectReference
292+
if name, ok := m[p.ID]; ok {
293+
ref = &v1alpha1.LocalObjectReference{Name: name}
294+
n++
281295
}
282-
283-
m := make(map[string]string) // ID => Resource Name
284-
for _, intf := range interfaces.Items {
285-
m[intf.Spec.Name] = intf.Name
296+
device.Status.Ports[i] = v1alpha1.DevicePort{
297+
Name: p.ID,
298+
Type: p.Type,
299+
SupportedSpeedsGbps: p.SupportedSpeedsGbps,
300+
Transceiver: p.Transceiver,
301+
InterfaceRef: ref,
286302
}
303+
slices.Sort(device.Status.Ports[i].SupportedSpeedsGbps)
304+
}
287305

288-
device.Status.Ports = make([]v1alpha1.DevicePort, len(ports))
289-
n := int32(0)
290-
for i, p := range ports {
291-
var ref *v1alpha1.LocalObjectReference
292-
if name, ok := m[p.ID]; ok {
293-
ref = &v1alpha1.LocalObjectReference{Name: name}
294-
n++
295-
}
296-
device.Status.Ports[i] = v1alpha1.DevicePort{
297-
Name: p.ID,
298-
Type: p.Type,
299-
SupportedSpeedsGbps: p.SupportedSpeedsGbps,
300-
Transceiver: p.Transceiver,
301-
InterfaceRef: ref,
302-
}
303-
slices.Sort(device.Status.Ports[i].SupportedSpeedsGbps)
304-
}
306+
device.Status.PostSummary = PortSummary(device.Status.Ports)
305307

306-
device.Status.PostSummary = PortSummary(device.Status.Ports)
308+
info, err := prov.GetDeviceInfo(ctx)
309+
if err != nil {
310+
return fmt.Errorf("failed to get device details: %w", err)
311+
}
307312

308-
info, err := prov.GetDeviceInfo(ctx)
309-
if err != nil {
310-
return fmt.Errorf("failed to get device details: %w", err)
311-
}
313+
device.Status.Manufacturer = info.Manufacturer
314+
device.Status.Model = info.Model
315+
device.Status.SerialNumber = info.SerialNumber
316+
device.Status.FirmwareVersion = info.FirmwareVersion
312317

313-
device.Status.Manufacturer = info.Manufacturer
314-
device.Status.Model = info.Model
315-
device.Status.SerialNumber = info.SerialNumber
316-
device.Status.FirmwareVersion = info.FirmwareVersion
317-
}
318318
conditions.Set(device, metav1.Condition{
319319
Type: v1alpha1.ReadyCondition,
320320
Status: metav1.ConditionTrue,

0 commit comments

Comments
 (0)