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
13 changes: 9 additions & 4 deletions pkg/device/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builder
import (
"github.com/logicmonitor/k8s-argus/pkg/types"
lm "github.com/logicmonitor/lm-sdk-go"
log "github.com/sirupsen/logrus"
)

// Builder implements types.DeviceBuilder
Expand Down Expand Up @@ -48,9 +49,13 @@ func (b *Builder) System(name, value string) types.DeviceOption {

func setProperty(name, value string) types.DeviceOption {
return func(device *lm.RestDevice) {
device.CustomProperties = append(device.CustomProperties, lm.NameAndValue{
Name: name,
Value: value,
})
if value != "" {
device.CustomProperties = append(device.CustomProperties, lm.NameAndValue{
Name: name,
Value: value,
})
} else {
log.Warnf("Custom property value is empty for %q, skipping", name)
}
}
}
22 changes: 16 additions & 6 deletions pkg/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package device

import (
"fmt"
"log"

"github.com/logicmonitor/k8s-argus/pkg/config"
"github.com/logicmonitor/k8s-argus/pkg/device/builder"
"github.com/logicmonitor/k8s-argus/pkg/types"

"github.com/logicmonitor/k8s-argus/pkg/utilities"
lm "github.com/logicmonitor/lm-sdk-go"
log "github.com/sirupsen/logrus"
)

// Manager implements types.DeviceManager
Expand Down Expand Up @@ -51,21 +51,27 @@ func (m *Manager) FindByDisplayName(name string) (*lm.RestDevice, error) {
// Add implements types.DeviceManager.
func (m *Manager) Add(options ...types.DeviceOption) (*lm.RestDevice, error) {
device := buildDevice(m.Config(), options...)
log.Debugf("%#v", device)

restResponse, apiResponse, err := m.LMClient.AddDevice(*device, false)
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
return nil, err
return nil, _err
}
log.Debugf("%#v", restResponse)

return &restResponse.Data, nil
}

// UpdateAndReplaceByID implements types.DeviceManager.
func (m *Manager) UpdateAndReplaceByID(id int32, options ...types.DeviceOption) (*lm.RestDevice, error) {
device := buildDevice(m.Config(), options...)
log.Debugf("%#v", device)

restResponse, apiResponse, err := m.LMClient.UpdateDevice(*device, id, "replace")
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
return nil, err
return nil, _err
}
log.Debugf("%#v", restResponse)

return &restResponse.Data, nil
}
Expand Down Expand Up @@ -94,10 +100,13 @@ func (m *Manager) UpdateAndReplaceByName(name string, options ...types.DeviceOpt
// UpdateAndReplaceFieldByID implements types.DeviceManager.
func (m *Manager) UpdateAndReplaceFieldByID(id int32, field string, options ...types.DeviceOption) (*lm.RestDevice, error) {
device := buildDevice(m.Config(), options...)
log.Debugf("%#v", device)

restResponse, apiResponse, err := m.LMClient.PatchDeviceById(*device, id, "replace", field)
if _err := utilities.CheckAllErrors(restResponse, apiResponse, err); _err != nil {
return nil, err
return nil, _err
}
log.Debugf("%#v", restResponse)

return &restResponse.Data, nil
}
Expand All @@ -110,7 +119,7 @@ func (m *Manager) UpdateAndReplaceFieldByName(name string, field string, options
}

if d == nil {
log.Printf("Could not find device %q", name)
log.Infof("Could not find device %q", name)
return nil, nil
}

Expand Down Expand Up @@ -138,7 +147,7 @@ func (m *Manager) DeleteByName(name string) error {

// TODO: Should this return an error?
if d == nil {
log.Printf("Could not find device %q", name)
log.Infof("Could not find device %q", name)
return nil
}

Expand All @@ -157,6 +166,7 @@ func find(field, name string, client *lm.DefaultApi) (*lm.RestDevice, error) {
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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/utilities/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func CheckAllErrors(restResponse interface{}, apiResponse *logicmonitor.APIRespo
}
}

if restResponseStatus != http.StatusOK {
metrics.RESTError()
return fmt.Errorf("[REST] [%d] %s", restResponseStatus, restResponseMessage)
}

if apiResponse.StatusCode != http.StatusOK {
metrics.APIError()
return fmt.Errorf("[API] [%d] %s", apiResponse.StatusCode, restResponseMessage)
}

if http.StatusOK != restResponseStatus {
metrics.RESTError()
return fmt.Errorf("[REST] [%d] %s", restResponseStatus, restResponseMessage)
}

if err != nil {
return fmt.Errorf("[ERROR] %v", err)
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/watch/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ func (w *Watcher) update(old, new *v1.Pod) {
}

func (w *Watcher) move(pod *v1.Pod) {
if _, err := w.UpdateAndReplaceFieldByName(
pod.Name,
constants.CustomPropertiesFieldName,
w.args(pod, constants.PodDeletedCategory)...,
); err != nil {
if _, err := w.UpdateAndReplaceFieldByName(pod.Name, constants.CustomPropertiesFieldName, w.args(pod, constants.PodDeletedCategory)...); err != nil {
log.Errorf("Failed to move pod %q: %v", pod.Name, err)
return
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/watch/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ func (w *Watcher) update(old, new *v1.Service) {
}

func (w *Watcher) move(service *v1.Service) {
if _, err := w.UpdateAndReplaceFieldByName(
service.Name,
constants.CustomPropertiesFieldName,
w.args(service, constants.ServiceDeletedCategory)...,
); err != nil {
if _, err := w.UpdateAndReplaceFieldByName(service.Name, constants.CustomPropertiesFieldName, w.args(service, constants.ServiceDeletedCategory)...); err != nil {
log.Errorf("Failed to move service %q: %v", service.Name, err)
return
}
Expand Down