summaryrefslogtreecommitdiff
path: root/cloud/pkg/devicecontroller/controller/downstream.go
diff options
context:
space:
mode:
Diffstat (limited to 'cloud/pkg/devicecontroller/controller/downstream.go')
-rw-r--r--cloud/pkg/devicecontroller/controller/downstream.go35
1 files changed, 33 insertions, 2 deletions
diff --git a/cloud/pkg/devicecontroller/controller/downstream.go b/cloud/pkg/devicecontroller/controller/downstream.go
index 808238e15..43793fddd 100644
--- a/cloud/pkg/devicecontroller/controller/downstream.go
+++ b/cloud/pkg/devicecontroller/controller/downstream.go
@@ -18,6 +18,7 @@ package controller
import (
"reflect"
+ "sync"
"time"
"github.com/google/uuid"
@@ -145,7 +146,9 @@ func (dc *DownstreamController) deviceAdded(device *v1beta1.Device) {
klog.Errorf("Failed to send device addition message %v due to error %v", msg, err)
}
- dc.sendDeviceModelMsg(device, model.InsertOperation)
+ if !isExistModel(&dc.deviceManager.Device, device) {
+ dc.sendDeviceModelMsg(device, model.InsertOperation)
+ }
dc.sendDeviceMsg(device, model.InsertOperation)
}
}
@@ -166,6 +169,32 @@ func createDevice(device *v1beta1.Device) types.Device {
return edgeDevice
}
+// isExistModel check if the target node already has the model.
+func isExistModel(deviceMap *sync.Map, device *v1beta1.Device) bool {
+ var res bool
+ targetNode := device.Spec.NodeName
+ modelName := device.Spec.DeviceModelRef.Name
+ // To find another device in deviceMap that uses the same deviceModel with exclude current device
+ deviceMap.Range(func(k, v interface{}) bool {
+ if k == device.Name {
+ return true
+ }
+ deviceItem, ok := v.(*v1beta1.Device)
+ if !ok {
+ return true
+ }
+ if deviceItem.Spec.NodeName == "" {
+ return true
+ }
+ if deviceItem.Spec.NodeName == targetNode && deviceItem.Spec.DeviceModelRef.Name == modelName {
+ res = true
+ return false
+ }
+ return true
+ })
+ return res
+}
+
// deviceUpdated updates the map, check if device is actually updated.
// If NodeName is updated, call add device for newNode, deleteDevice for old Node.
// If Spec is updated, send update message to edge
@@ -229,7 +258,9 @@ func (dc *DownstreamController) deviceDeleted(device *v1beta1.Device) {
if err != nil {
klog.Errorf("Failed to send device addition message %v due to error %v", msg, err)
}
- dc.sendDeviceModelMsg(device, model.DeleteOperation)
+ if !isExistModel(&dc.deviceManager.Device, device) {
+ dc.sendDeviceModelMsg(device, model.DeleteOperation)
+ }
dc.sendDeviceMsg(device, model.DeleteOperation)
}
}