Skip to content

Commit

Permalink
Merge pull request kubeedge#5221 from wbc6080/automated-cherry-pick-o…
Browse files Browse the repository at this point in the history
…f-#5065-upstream-release-1.15

Automated cherry pick of kubeedge#5065: bugfix: device model sync
  • Loading branch information
kubeedge-bot authored Nov 28, 2023
2 parents 4ff43c0 + d1de6ff commit b9ecdcf
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions cloud/pkg/devicecontroller/controller/downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"reflect"
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit b9ecdcf

Please sign in to comment.