From 5ed97d47cc20d320f597c7109bdc5e21f5300438 Mon Sep 17 00:00:00 2001 From: Vaibhav Kumbhar Date: Thu, 4 Jun 2020 13:30:39 +0530 Subject: [PATCH] Milestone 3.0.1 Changes (#136) * Milestone 3.0.1 Changes (#135) * 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 * Merge pull request #47 in DEV/k8s-argus from DEV-59276-argus-dev-prof to develop * commit 'e1c57b89b13dd22fa949f1ca7db2672f9e29874e': DEV-59276: Created dev profile dockerfile and updated makefile with dev profile --- Dockerfile.dev | 15 ++++++++++++++ Makefile | 4 ++++ pkg/devicegroup/devicegroup.go | 33 +++++++++++++++++++++++++++++++ pkg/watch/namespace/namespace.go | 34 +++++++++++++++++++------------- 4 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 Dockerfile.dev diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 000000000..b9d3d3d6d --- /dev/null +++ b/Dockerfile.dev @@ -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 " +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"] diff --git a/Makefile b/Makefile index e99f3dece..38042fb33 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pkg/devicegroup/devicegroup.go b/pkg/devicegroup/devicegroup.go index f6bb9c0eb..d95cacfea 100644 --- a/pkg/devicegroup/devicegroup.go +++ b/pkg/devicegroup/devicegroup.go @@ -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) @@ -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{ diff --git a/pkg/watch/namespace/namespace.go b/pkg/watch/namespace/namespace.go index fda324f20..a3e4dd1ca 100644 --- a/pkg/watch/namespace/namespace.go +++ b/pkg/watch/namespace/namespace.go @@ -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 }