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

Commit a5f2633

Browse files
author
Andrew Rynhard
committed
refactor(*): design patterns, cleanup, and more
1 parent 174353f commit a5f2633

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

pkg/argus.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/logicmonitor/k8s-argus/pkg/etcd"
1010
"github.com/logicmonitor/k8s-argus/pkg/tree"
1111
"github.com/logicmonitor/k8s-argus/pkg/types"
12-
"github.com/logicmonitor/k8s-argus/pkg/watch"
1312
"github.com/logicmonitor/k8s-argus/pkg/watch/namespace"
1413
"github.com/logicmonitor/k8s-argus/pkg/watch/node"
1514
"github.com/logicmonitor/k8s-argus/pkg/watch/pod"
@@ -25,7 +24,7 @@ import (
2524
// Argus represents the Argus cli.
2625
type Argus struct {
2726
*types.Base
28-
Watchers []watch.Watcher
27+
Watchers []types.Watcher
2928
}
3029

3130
func newLMClient(id, key, company string) *lm.DefaultApi {
@@ -89,7 +88,7 @@ func NewArgus(base *types.Base) (*Argus, error) {
8988
return nil, err
9089
}
9190

92-
argus.Watchers = []watch.Watcher{
91+
argus.Watchers = []types.Watcher{
9392
namespace.Watcher{
9493
Base: base,
9594
DeviceGroups: deviceGroups,

pkg/tree/devicegroup/devicegroup.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ func Find(parentID int32, name string, client *lm.DefaultApi) (*lm.RestDeviceGro
128128
return deviceGroup, nil
129129
}
130130

131+
// DeleteSubGroup deletes a subgroup from a device group with the specified
132+
// name.
133+
func DeleteSubGroup(deviceGroup *lm.RestDeviceGroup, name string, client *lm.DefaultApi) error {
134+
for _, subGroup := range deviceGroup.SubGroups {
135+
if subGroup.Name != name {
136+
continue
137+
}
138+
restResponse, apiResponse, err := client.DeleteDeviceGroupById(subGroup.Id, true)
139+
return utilities.CheckAllErrors(restResponse, apiResponse, err)
140+
}
141+
142+
return nil
143+
}
144+
131145
func create(name, appliesTo string, disableAlerting bool, parentID int32, client *lm.DefaultApi) (*lm.RestDeviceGroup, error) {
132146
restResponse, apiResponse, err := client.AddDeviceGroup(lm.RestDeviceGroup{
133147
Name: name,

pkg/types/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/logicmonitor/k8s-argus/pkg/config"
55
lm "github.com/logicmonitor/lm-sdk-go"
66
"k8s.io/client-go/kubernetes"
7+
"k8s.io/client-go/pkg/runtime"
78
)
89

910
// Base is a struct for embedding
@@ -12,3 +13,12 @@ type Base struct {
1213
K8sClient *kubernetes.Clientset
1314
Config *config.Config
1415
}
16+
17+
// Watcher is the LogicMonitor Watcher interface.
18+
type Watcher interface {
19+
Resource() string
20+
ObjType() runtime.Object
21+
AddFunc() func(obj interface{})
22+
DeleteFunc() func(obj interface{})
23+
UpdateFunc() func(oldObj, newObj interface{})
24+
}

pkg/watch/namespace/namespace.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package namespace
22

33
import (
44
"github.com/logicmonitor/k8s-argus/pkg/tree/devicegroup"
5-
"github.com/logicmonitor/k8s-argus/pkg/utilities"
65

76
"github.com/logicmonitor/k8s-argus/pkg/constants"
87
"github.com/logicmonitor/k8s-argus/pkg/types"
@@ -84,19 +83,18 @@ func (w Watcher) DeleteFunc() func(obj interface{}) {
8483
deviceGroup, err := devicegroup.Find(parentID, name, w.LMClient)
8584
if err != nil {
8685
log.Printf("Failed to find namespace %s: %v", name, err)
87-
8886
return
8987
}
90-
for _, subGroup := range deviceGroup.SubGroups {
91-
if subGroup.Name == namespace.Name {
92-
restResponse, apiResponse, err := w.LMClient.DeleteDeviceGroupById(subGroup.Id, true)
93-
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
94-
log.Errorf("Failed to delete namespace %q: %v", subGroup.Name, _err)
95-
96-
return
97-
}
98-
}
88+
// We should only be returned a device group if it is namespaced.
89+
if deviceGroup == nil {
90+
continue
91+
}
92+
err = devicegroup.DeleteSubGroup(deviceGroup, namespace.Name, w.LMClient)
93+
if err != nil {
94+
log.Errorf("Failed to delete namespace %q: %v", namespace.Name, err)
95+
return
9996
}
10097
}
98+
10199
}
102100
}

pkg/watch/watch.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)