Skip to content
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
26 changes: 11 additions & 15 deletions pkg/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ func buildDevice(c *config.Config, client api.CollectorSetControllerClient, opti

// FindByDisplayName implements types.DeviceManager.
func (m *Manager) FindByDisplayName(name string) (*lm.RestDevice, error) {
return find("displayName", name, m.LMClient)
filter := fmt.Sprintf("displayName:%s", name)
restResponse, apiResponse, err := m.LMClient.GetDeviceList("", -1, 0, filter)
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
return nil, _err
}
log.Debugf("%#v", restResponse)
if restResponse.Data.Total == 1 {
return &restResponse.Data.Items[0], nil
}

return nil, nil
}

// Add implements types.DeviceManager.
Expand Down Expand Up @@ -166,20 +176,6 @@ func (m *Manager) Config() *config.Config {
return m.Base.Config
}

func find(field, name string, client *lm.DefaultApi) (*lm.RestDevice, error) {
filter := fmt.Sprintf("%s:%s", field, name)
restResponse, apiResponse, err := client.GetDeviceList("", -1, 0, filter)
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
return nil, _err
}
log.Debugf("%#v", restResponse)
if restResponse.Data.Total == 1 {
return &restResponse.Data.Items[0], nil
}

return nil, nil
}

// GetListByGroupID implements getting all the devices belongs to the group directly
func (m *Manager) GetListByGroupID(groupID int32) ([]lm.RestDevice, error) {
restResponse, apiResponse, err := m.LMClient.GetImmediateDeviceListByDeviceGroupId(groupID, "id,name,displayName,customProperties", -1, 0, "")
Expand Down
49 changes: 16 additions & 33 deletions pkg/sync/initsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func (i *InitSyncer) InitSync() {
case constants.PodDeviceGroupName:
go func() {
defer wg.Done()
i.initSyncPods(rest.Id)
i.initSyncPodsOrServices(constants.PodDeviceGroupName, rest.Id)
log.Infof("Finish syncing %v", constants.PodDeviceGroupName)
}()
case constants.ServiceDeviceGroupName:
go func() {
defer wg.Done()
i.initSyncServices(rest.Id)
i.initSyncPodsOrServices(constants.ServiceDeviceGroupName, rest.Id)
log.Infof("Finish syncing %v", constants.ServiceDeviceGroupName)
}()
default:
Expand Down Expand Up @@ -96,10 +96,10 @@ func (i *InitSyncer) intSyncNodes(parentGroupID int32) {
}
}

func (i *InitSyncer) initSyncPods(parentGroupID int32) {
rest, err := devicegroup.Find(parentGroupID, constants.PodDeviceGroupName, i.DeviceManager.LMClient)
func (i *InitSyncer) initSyncPodsOrServices(deviceType string, parentGroupID int32) {
rest, err := devicegroup.Find(parentGroupID, deviceType, i.DeviceManager.LMClient)
if err != nil || rest == nil {
log.Warnf("Failed to get the pod group")
log.Warnf("Failed to get the %s group", deviceType)
return
}
if rest.SubGroups == nil {
Expand All @@ -108,39 +108,22 @@ func (i *InitSyncer) initSyncPods(parentGroupID int32) {

// loop every namespace
for _, subGroup := range rest.SubGroups {
//get pod info from k8s
podsMap, err := pod.GetPodsMap(i.DeviceManager.K8sClient, subGroup.Name)
if err != nil || podsMap == nil {
log.Warnf("Failed to get the pods from k8s, namespace: %v, err: %v", subGroup.Name, err)
continue
//get pod/service info from k8s
var deviceMap map[string]string
if deviceType == constants.PodDeviceGroupName {
deviceMap, err = pod.GetPodsMap(i.DeviceManager.K8sClient, subGroup.Name)
} else if deviceType == constants.ServiceDeviceGroupName {
deviceMap, err = service.GetServicesMap(i.DeviceManager.K8sClient, subGroup.Name)
} else {
return
}

// get and check all the devices in the group
i.syncDevices(constants.PodDeviceGroupName, podsMap, subGroup)
}
}

func (i *InitSyncer) initSyncServices(parentGroupID int32) {
rest, err := devicegroup.Find(parentGroupID, constants.ServiceDeviceGroupName, i.DeviceManager.LMClient)
if err != nil || rest == nil {
log.Warnf("Failed to get the pod group")
return
}
if rest.SubGroups == nil {
return
}

// loop every namesplace
for _, subGroup := range rest.SubGroups {
//get service info from k8s
servicesMap, err := service.GetServicesMap(i.DeviceManager.K8sClient, subGroup.Name)
if err != nil || servicesMap == nil {
log.Warnf("Failed to get the services from k8s, namespace: %v, err: %v", subGroup.Name, err)
if err != nil || deviceMap == nil {
log.Warnf("Failed to get the %s from k8s, namespace: %v, err: %v", deviceType, subGroup.Name, err)
continue
}

// get and check all the devices in the group
i.syncDevices(constants.ServiceDeviceGroupName, servicesMap, subGroup)
i.syncDevices(deviceType, deviceMap, subGroup)
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/watch/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package service

import (
"fmt"

"github.com/logicmonitor/k8s-argus/pkg/constants"
"github.com/logicmonitor/k8s-argus/pkg/types"
"github.com/logicmonitor/k8s-argus/pkg/utilities"
Expand Down