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

Commit 5a8c798

Browse files
author
Jeremy Tang
committed
Merge pull request #12 in DEV/k8s-argus from DEV-41682-improve-the-argus-code-for-the to develop
* commit 'e56e41e8efbe88814fcb2c0c4a6efc1240f09717': DEV-41682: Improve the argus code for the CI failure in GitHub
2 parents 4079f66 + e56e41e commit 5a8c798

File tree

3 files changed

+28
-48
lines changed

3 files changed

+28
-48
lines changed

pkg/device/device.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ func buildDevice(c *config.Config, client api.CollectorSetControllerClient, opti
5151

5252
// FindByDisplayName implements types.DeviceManager.
5353
func (m *Manager) FindByDisplayName(name string) (*lm.RestDevice, error) {
54-
return find("displayName", name, m.LMClient)
54+
filter := fmt.Sprintf("displayName:%s", name)
55+
restResponse, apiResponse, err := m.LMClient.GetDeviceList("", -1, 0, filter)
56+
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
57+
return nil, _err
58+
}
59+
log.Debugf("%#v", restResponse)
60+
if restResponse.Data.Total == 1 {
61+
return &restResponse.Data.Items[0], nil
62+
}
63+
64+
return nil, nil
5565
}
5666

5767
// Add implements types.DeviceManager.
@@ -166,20 +176,6 @@ func (m *Manager) Config() *config.Config {
166176
return m.Base.Config
167177
}
168178

169-
func find(field, name string, client *lm.DefaultApi) (*lm.RestDevice, error) {
170-
filter := fmt.Sprintf("%s:%s", field, name)
171-
restResponse, apiResponse, err := client.GetDeviceList("", -1, 0, filter)
172-
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
173-
return nil, _err
174-
}
175-
log.Debugf("%#v", restResponse)
176-
if restResponse.Data.Total == 1 {
177-
return &restResponse.Data.Items[0], nil
178-
}
179-
180-
return nil, nil
181-
}
182-
183179
// GetListByGroupID implements getting all the devices belongs to the group directly
184180
func (m *Manager) GetListByGroupID(groupID int32) ([]lm.RestDevice, error) {
185181
restResponse, apiResponse, err := m.LMClient.GetImmediateDeviceListByDeviceGroupId(groupID, "id,name,displayName,customProperties", -1, 0, "")

pkg/sync/initsyncer.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ func (i *InitSyncer) InitSync() {
4646
case constants.PodDeviceGroupName:
4747
go func() {
4848
defer wg.Done()
49-
i.initSyncPods(rest.Id)
49+
i.initSyncPodsOrServices(constants.PodDeviceGroupName, rest.Id)
5050
log.Infof("Finish syncing %v", constants.PodDeviceGroupName)
5151
}()
5252
case constants.ServiceDeviceGroupName:
5353
go func() {
5454
defer wg.Done()
55-
i.initSyncServices(rest.Id)
55+
i.initSyncPodsOrServices(constants.ServiceDeviceGroupName, rest.Id)
5656
log.Infof("Finish syncing %v", constants.ServiceDeviceGroupName)
5757
}()
5858
default:
@@ -96,10 +96,10 @@ func (i *InitSyncer) intSyncNodes(parentGroupID int32) {
9696
}
9797
}
9898

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

109109
// loop every namespace
110110
for _, subGroup := range rest.SubGroups {
111-
//get pod info from k8s
112-
podsMap, err := pod.GetPodsMap(i.DeviceManager.K8sClient, subGroup.Name)
113-
if err != nil || podsMap == nil {
114-
log.Warnf("Failed to get the pods from k8s, namespace: %v, err: %v", subGroup.Name, err)
115-
continue
111+
//get pod/service info from k8s
112+
var deviceMap map[string]string
113+
if deviceType == constants.PodDeviceGroupName {
114+
deviceMap, err = pod.GetPodsMap(i.DeviceManager.K8sClient, subGroup.Name)
115+
} else if deviceType == constants.ServiceDeviceGroupName {
116+
deviceMap, err = service.GetServicesMap(i.DeviceManager.K8sClient, subGroup.Name)
117+
} else {
118+
return
116119
}
117-
118-
// get and check all the devices in the group
119-
i.syncDevices(constants.PodDeviceGroupName, podsMap, subGroup)
120-
}
121-
}
122-
123-
func (i *InitSyncer) initSyncServices(parentGroupID int32) {
124-
rest, err := devicegroup.Find(parentGroupID, constants.ServiceDeviceGroupName, i.DeviceManager.LMClient)
125-
if err != nil || rest == nil {
126-
log.Warnf("Failed to get the pod group")
127-
return
128-
}
129-
if rest.SubGroups == nil {
130-
return
131-
}
132-
133-
// loop every namesplace
134-
for _, subGroup := range rest.SubGroups {
135-
//get service info from k8s
136-
servicesMap, err := service.GetServicesMap(i.DeviceManager.K8sClient, subGroup.Name)
137-
if err != nil || servicesMap == nil {
138-
log.Warnf("Failed to get the services from k8s, namespace: %v, err: %v", subGroup.Name, err)
120+
if err != nil || deviceMap == nil {
121+
log.Warnf("Failed to get the %s from k8s, namespace: %v, err: %v", deviceType, subGroup.Name, err)
139122
continue
140123
}
141124

142125
// get and check all the devices in the group
143-
i.syncDevices(constants.ServiceDeviceGroupName, servicesMap, subGroup)
126+
i.syncDevices(deviceType, deviceMap, subGroup)
144127
}
145128
}
146129

pkg/watch/service/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package service
44

55
import (
66
"fmt"
7+
78
"github.com/logicmonitor/k8s-argus/pkg/constants"
89
"github.com/logicmonitor/k8s-argus/pkg/types"
910
"github.com/logicmonitor/k8s-argus/pkg/utilities"

0 commit comments

Comments
 (0)