Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.14 as build
WORKDIR $GOPATH/src/github.com/logicmonitor/k8s-argus
ARG VERSION
COPY ./ ./
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /argus -ldflags "-X \"github.com/logicmonitor/k8s-argus/pkg/constants.Version=${VERSION}\""

FROM alpine:3.6
LABEL maintainer="LogicMonitor <[email protected]>"
RUN apk --update add ca-certificates \
&& rm -rf /var/cache/apk/* \
&& rm -rf /var/lib/apk/*
WORKDIR /app
COPY --from=build /argus /bin

ENTRYPOINT ["argus"]
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ all:
docker build --build-arg VERSION=$(VERSION) -t $(NAMESPACE)/$(REPOSITORY):v2latest .
docker tag $(NAMESPACE)/$(REPOSITORY):v2latest $(NAMESPACE)/$(REPOSITORY):$(VERSION)

dev:
docker build --build-arg VERSION=$(VERSION) -t $(NAMESPACE)/$(REPOSITORY):v2latest -f Dockerfile.dev .
docker tag $(NAMESPACE)/$(REPOSITORY):v2latest $(NAMESPACE)/$(REPOSITORY):$(VERSION)

.PHONY: docs
docs:
cd docs/source && hugo
33 changes: 33 additions & 0 deletions pkg/devicegroup/devicegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ func Find(parentID int32, name string, client *client.LMSdkGo) (*models.DeviceGr
return deviceGroup, nil
}

// FindDeviceGroupsByName searches for a device group by name.
func FindDeviceGroupsByName(name string, client *client.LMSdkGo) ([]*models.DeviceGroup, error) {
params := lm.NewGetDeviceGroupListParams()
fields := "name,id,parentId,subGroups"
params.SetFields(&fields)
filter := fmt.Sprintf("name:\"%s\"", name)
params.SetFilter(&filter)
restResponse, err := client.LM.GetDeviceGroupList(params)
if err != nil {
return nil, err
}

var deviceGroups []*models.DeviceGroup
if restResponse != nil && restResponse.Payload != nil {
deviceGroups = restResponse.Payload.Items
}

return deviceGroups, nil
}

// Exists returns true if the specified device group exists in the account
func Exists(parentID int32, name string, client *client.LMSdkGo) bool {
deviceGroup, err := Find(parentID, name, client)
Expand Down Expand Up @@ -185,6 +205,19 @@ func DeleteSubGroup(deviceGroup *models.DeviceGroup, name string, client *client
return nil
}

// DeleteGroup deletes a device group with the specified deviceGroupID.
func DeleteGroup(deviceGroup *models.DeviceGroup, client *client.LMSdkGo) error {
params := lm.NewDeleteDeviceGroupByIDParams()
params.ID = deviceGroup.ID
deleteChildren := true
params.SetDeleteChildren(&deleteChildren)
deleteHard := true
params.SetDeleteHard(&deleteHard)
log.Infof("Deleting deviceGroup:\"%s\" ID:\"%d\" ParentID:\"%d\"", *deviceGroup.Name, deviceGroup.ID, deviceGroup.ParentID)
_, err := client.LM.DeleteDeviceGroupByID(params)
return err
}

func create(name, appliesTo string, disableAlerting bool, parentID int32, client *client.LMSdkGo) (*models.DeviceGroup, error) {
params := lm.NewAddDeviceGroupParams()
params.SetBody(&models.DeviceGroup{
Expand Down
34 changes: 20 additions & 14 deletions pkg/watch/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,28 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
namespace := obj.(*v1.Namespace)
log.Debugf("Handle deleting namespace event: %s", namespace.Name)

for name, parentID := range w.DeviceGroups {
deviceGroup, err := devicegroup.Find(parentID, name, w.LMClient)
if err != nil {
log.Warnf("Failed to find namespace %s: %v", name, err)
return
}
// We should only be returned a device group if it is namespaced.
if deviceGroup == nil {
continue
}
err = devicegroup.DeleteSubGroup(deviceGroup, namespace.Name, w.LMClient)
if err != nil {
log.Errorf("Failed to delete namespace %q: %v", namespace.Name, err)
return
deviceGroups, err := devicegroup.FindDeviceGroupsByName(namespace.Name, w.LMClient)
if err != nil {
log.Errorf("Failed to get device group for namespace:\"%s\" with error: %v", namespace.Name, err)
return
}

reversedDeviceGroups := getReversedDeviceGroups(w.DeviceGroups)
for _, d := range deviceGroups {
if _, ok := reversedDeviceGroups[d.ParentID]; ok {
err = devicegroup.DeleteGroup(d, w.LMClient)
if err != nil {
log.Errorf("Failed to delete device group of namespace:\"%s\" having ID:\"%d\" with error: %v", namespace.Name, d.ID, err)
}
}
}
}
}

func getReversedDeviceGroups(deviceGroups map[string]int32) map[int32]string {
reversedDeviceGroups := make(map[int32]string)
for key, value := range deviceGroups {
reversedDeviceGroups[value] = key
}
return reversedDeviceGroups
}