Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit ee292e5

Browse files
committed
Merge pull request #49 in DEV/k8s-argus from bugfix/DEV-59446-deleted-namespaces-are-still-present-on-the-portal to develop
* commit 'd0fb53894f1475979f48653c6438318faa3085b0': DEV-59446 delete namespace code refactored DEV-59446 delete namespace code refactored DEV-59446 fixed deleted namespaces present on portal issue
1 parent 0a19c59 commit ee292e5

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

pkg/devicegroup/devicegroup.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ func Find(parentID int32, name string, client *client.LMSdkGo) (*models.DeviceGr
131131
return deviceGroup, nil
132132
}
133133

134+
// FindDeviceGroupsByName searches for a device group by name.
135+
func FindDeviceGroupsByName(name string, client *client.LMSdkGo) ([]*models.DeviceGroup, error) {
136+
params := lm.NewGetDeviceGroupListParams()
137+
fields := "name,id,parentId,subGroups"
138+
params.SetFields(&fields)
139+
filter := fmt.Sprintf("name:\"%s\"", name)
140+
params.SetFilter(&filter)
141+
restResponse, err := client.LM.GetDeviceGroupList(params)
142+
if err != nil {
143+
return nil, err
144+
}
145+
146+
var deviceGroups []*models.DeviceGroup
147+
if restResponse != nil && restResponse.Payload != nil {
148+
deviceGroups = restResponse.Payload.Items
149+
}
150+
151+
return deviceGroups, nil
152+
}
153+
134154
// Exists returns true if the specified device group exists in the account
135155
func Exists(parentID int32, name string, client *client.LMSdkGo) bool {
136156
deviceGroup, err := Find(parentID, name, client)
@@ -185,6 +205,19 @@ func DeleteSubGroup(deviceGroup *models.DeviceGroup, name string, client *client
185205
return nil
186206
}
187207

208+
// DeleteGroup deletes a device group with the specified deviceGroupID.
209+
func DeleteGroup(deviceGroup *models.DeviceGroup, client *client.LMSdkGo) error {
210+
params := lm.NewDeleteDeviceGroupByIDParams()
211+
params.ID = deviceGroup.ID
212+
deleteChildren := true
213+
params.SetDeleteChildren(&deleteChildren)
214+
deleteHard := true
215+
params.SetDeleteHard(&deleteHard)
216+
log.Infof("Deleting deviceGroup:\"%s\" ID:\"%d\" ParentID:\"%d\"", *deviceGroup.Name, deviceGroup.ID, deviceGroup.ParentID)
217+
_, err := client.LM.DeleteDeviceGroupByID(params)
218+
return err
219+
}
220+
188221
func create(name, appliesTo string, disableAlerting bool, parentID int32, client *client.LMSdkGo) (*models.DeviceGroup, error) {
189222
params := lm.NewAddDeviceGroupParams()
190223
params.SetBody(&models.DeviceGroup{

pkg/watch/namespace/namespace.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,28 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
9595
namespace := obj.(*v1.Namespace)
9696
log.Debugf("Handle deleting namespace event: %s", namespace.Name)
9797

98-
for name, parentID := range w.DeviceGroups {
99-
deviceGroup, err := devicegroup.Find(parentID, name, w.LMClient)
100-
if err != nil {
101-
log.Warnf("Failed to find namespace %s: %v", name, err)
102-
return
103-
}
104-
// We should only be returned a device group if it is namespaced.
105-
if deviceGroup == nil {
106-
continue
107-
}
108-
err = devicegroup.DeleteSubGroup(deviceGroup, namespace.Name, w.LMClient)
109-
if err != nil {
110-
log.Errorf("Failed to delete namespace %q: %v", namespace.Name, err)
111-
return
98+
deviceGroups, err := devicegroup.FindDeviceGroupsByName(namespace.Name, w.LMClient)
99+
if err != nil {
100+
log.Errorf("Failed to get device group for namespace:\"%s\" with error: %v", namespace.Name, err)
101+
return
102+
}
103+
104+
reversedDeviceGroups := getReversedDeviceGroups(w.DeviceGroups)
105+
for _, d := range deviceGroups {
106+
if _, ok := reversedDeviceGroups[d.ParentID]; ok {
107+
err = devicegroup.DeleteGroup(d, w.LMClient)
108+
if err != nil {
109+
log.Errorf("Failed to delete device group of namespace:\"%s\" having ID:\"%d\" with error: %v", namespace.Name, d.ID, err)
110+
}
112111
}
113112
}
113+
}
114+
}
114115

116+
func getReversedDeviceGroups(deviceGroups map[string]int32) map[int32]string {
117+
reversedDeviceGroups := make(map[int32]string)
118+
for key, value := range deviceGroups {
119+
reversedDeviceGroups[value] = key
115120
}
121+
return reversedDeviceGroups
116122
}

0 commit comments

Comments
 (0)