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

Commit 3da07d1

Browse files
committed
fix(empty-devicename): Added check for empty device name.
Squashed commit of the following: commit c2646f592a3f7dc24097e2a91a6152c3e4589693 Author: Shubhashri Shirode <[email protected]> Date: Fri May 7 17:56:47 2021 +0530 DEVTS-10495: fixed review comments. commit 7b8778f5255209e13318e8523dbf87170fb019dd Author: Shubhashri Shirode <[email protected]> Date: Thu May 6 14:31:20 2021 +0530 DEVTS-10495:review comments fixed. commit 371284d4e721c56bcb9fa80eda0e1b1f5df3cef7 Author: Shubhashri Shirode <[email protected]> Date: Wed May 5 19:31:22 2021 +0530 DEVTS-10495:corrected log formats. commit 4f289798400cea963c30c2e9e72c5a72ab7fa2e4 Author: Shubhashri Shirode <[email protected]> Date: Wed May 5 19:16:07 2021 +0530 DEVTS-10495:corrected typo. commit 8e0cf7e2c366f067778a7ed9dcb770b3fde43f55 Author: Shubhashri Shirode <[email protected]> Date: Wed May 5 19:15:00 2021 +0530 DEVTS-10495:calculated latency for delete event. commit 8940a30ab1218af2fc564ceab0c4f19768beb143 Author: Shubhashri Shirode <[email protected]> Date: Wed May 5 17:23:01 2021 +0530 corrected error log. commit dc5b81ce0eb3795f0c46a7dddc109fe233d04dff Author: Shubhashri Shirode <[email protected]> Date: Wed May 5 16:44:12 2021 +0530 fix(empty-resource-name):Adde ed check for empty resource name.
1 parent 44fb38a commit 3da07d1

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

pkg/device/device.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (m *Manager) checkAndUpdateExistingDevice(lctx *lmctx.LMContext, resource s
7878
return nil, err
7979
}
8080
if len(existingDevices) == 0 {
81-
return nil, fmt.Errorf("cannot find devices with names: %s , %s", *device.DisplayName, util.GetFullDisplayName(device, resource, currentCluster))
81+
return nil, fmt.Errorf("could not find devices with names: %s , %s", *device.DisplayName, util.GetFullDisplayName(device, resource, currentCluster))
8282
}
8383
for _, existingDevice := range existingDevices {
8484
clusterName := util.GetPropertyValue(existingDevice, constants.K8sClusterNamePropertyKey)
@@ -130,7 +130,7 @@ func (m *Manager) RenameAndUpdateDevice(lctx *lmctx.LMContext, resource string,
130130
entityProperty := models.EntityProperty{Name: constants.K8sSystemCategoriesPropertyKey, Value: updatedCategories, Type: "system"}
131131
err1 := m.updateDevicePropertyByName(lctx, updatedDevice.ID, &entityProperty, resource)
132132
if err1 != nil {
133-
log.Errorf("Failed to remove device %s from conflicts group. %v", *updatedDevice.DisplayName, err1)
133+
log.Errorf("failed to remove device %s from conflicts group. %v", *updatedDevice.DisplayName, err1)
134134
return err1
135135
}
136136
}
@@ -145,12 +145,12 @@ func (m *Manager) RenameAndUpdateDevice(lctx *lmctx.LMContext, resource string,
145145
}
146146
newDevice, err := m.UpdateAndReplace(lctx, resource, device, options...)
147147
if err != nil {
148-
log.Errorf("Failed to update the device %s : %v", *device.DisplayName, err)
148+
log.Errorf("failed to update the device %s : %v", *device.DisplayName, err)
149149
return err
150150
}
151151
err2 := m.moveDeviceToConflictGroup(lctx, newDevice, resource)
152152
if err2 != nil {
153-
log.Errorf("Failed to move device %s to conflicts group: %v", *newDevice.DisplayName, err2)
153+
log.Errorf("failed to move device %s to conflicts group: %v", *newDevice.DisplayName, err2)
154154
return err2
155155
}
156156

@@ -328,8 +328,9 @@ func (m *Manager) addConflictingDevice(lctx *lmctx.LMContext, device *models.Dev
328328
log.Infof("Check and Update the existing device: %s", *device.DisplayName)
329329
updatedevice, err := m.checkAndUpdateExistingDevice(lctx, resource, device)
330330
if err != nil {
331-
log.Errorf("failed to updated device: %v", err)
332-
return nil, fmt.Errorf("failed to updated device")
331+
err2 := fmt.Errorf("failed to update existing device: %w", err)
332+
log.Error(err2.Error())
333+
return nil, err2
333334
}
334335

335336
if updatedevice == nil {
@@ -345,7 +346,7 @@ func (m *Manager) addConflictingDevice(lctx *lmctx.LMContext, device *models.Dev
345346

346347
if err != nil {
347348
log.Errorf("add new device failed: %v", err)
348-
return nil, fmt.Errorf("add new device failed")
349+
return nil, err
349350
}
350351

351352
m.DC.Set(util.GetFullDisplayName(renamedDevice, resource, currentCluster))
@@ -409,12 +410,20 @@ func (m *Manager) UpdateAndReplace(lctx *lmctx.LMContext, resource string, d *mo
409410
// UpdateAndReplaceByDisplayName implements types.DeviceManager.
410411
func (m *Manager) UpdateAndReplaceByDisplayName(lctx *lmctx.LMContext, resource, name, fullName string, filter types.UpdateFilter, labels map[string]string, options ...types.DeviceOption) (*models.Device, error) {
411412
log := lmlog.Logger(lctx)
413+
device := buildDevice(lctx, m.Config(), nil, options...)
414+
415+
if *device.Name == "" {
416+
err := fmt.Errorf("empty name is invalid for resource %v", resource)
417+
log.Warn(err.Error())
418+
return nil, err
419+
}
420+
412421
if !m.DC.Exists(fullName) {
413422
log.Infof("Missing device %v; (full name = %v) adding it now", name, fullName)
414423
return m.Add(lctx, resource, labels, options...)
415424
}
416425
if filter != nil && !filter() {
417-
log.Debugf("filtered device update %s", name)
426+
log.Debugf("Ignoring updates for device %s, %s", name, resource)
418427
return nil, nil
419428
}
420429

@@ -432,12 +441,12 @@ func (m *Manager) UpdateAndReplaceByDisplayName(lctx *lmctx.LMContext, resource,
432441
options = append(options, m.DisplayName(*existingDevice.DisplayName))
433442

434443
// Update the device.
435-
device, err := m.UpdateAndReplace(lctx, resource, existingDevice, options...)
444+
updatedDevice, err := m.UpdateAndReplace(lctx, resource, existingDevice, options...)
436445
if err != nil {
437446

438447
return nil, err
439448
}
440-
m.DC.Set(util.GetFullDisplayName(device, resource, m.Config().ClusterName))
449+
m.DC.Set(util.GetFullDisplayName(updatedDevice, resource, m.Config().ClusterName))
441450
return device, nil
442451
}
443452

@@ -495,7 +504,7 @@ func (m *Manager) updateDevicePropertyByName(lctx *lmctx.LMContext, deviceID int
495504
return fmt.Errorf("failed to update device property '%v'. Error: %v", entityProperty.Name, err)
496505
}
497506
resp := restResponse.(*lm.UpdateDevicePropertyByNameOK)
498-
log.Debugf("update property response payload : %#v", resp.Payload)
507+
log.Debugf("Update property response payload : %#v", resp.Payload)
499508

500509
return nil
501510
}
@@ -593,7 +602,7 @@ func (m *Manager) DeleteByDisplayName(lctx *lmctx.LMContext, resource, name, ful
593602
return err2
594603
}
595604
m.DC.Unset(name)
596-
log.Infof("deleted device %q", name)
605+
log.Infof("Deleted device %q", name)
597606

598607
return nil
599608
}
@@ -603,7 +612,7 @@ func (m *Manager) getExisitingDeviceByGivenProperties(lctx *lmctx.LMContext, nam
603612
existingDevices, err := m.FindByDisplayNames(lctx, resource, name, fullName)
604613

605614
if err != nil {
606-
log.Errorf("Error finding devices with names: %s, %s - %v", name, fullName, err)
615+
log.Errorf("error finding devices with names: %s, %s - %v", name, fullName, err)
607616
return nil, err
608617
}
609618

pkg/utilities/utilities.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package utilities
22

33
import (
44
"regexp"
5+
"time"
56

67
"github.com/google/uuid"
8+
"github.com/sirupsen/logrus"
9+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
710
)
811

912
// GetLabelByPrefix takes a list of labels returns the first label matching the specified prefix
@@ -23,3 +26,13 @@ func GetLabelByPrefix(prefix string, labels map[string]string) (string, string)
2326
func GetShortUUID() uint32 {
2427
return uuid.New().ID()
2528
}
29+
30+
//LogDeleteEventLatency logs latency of receiving delete event to argus.
31+
func LogDeleteEventLatency(deletionTimestamp *v1.Time, name string) {
32+
if deletionTimestamp != nil {
33+
latency := time.Since(deletionTimestamp.Time)
34+
logrus.Infof("Delete event latency for %s is %v", name, latency)
35+
} else {
36+
logrus.Warnf("resource delete time was not present in event context for %s", name)
37+
}
38+
}

pkg/watch/deployment/deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
8686
lctx := lmlog.NewLMContextWith(logrus.WithFields(logrus.Fields{"device_id": resource + "-" + deployment.Name}))
8787
log := lmlog.Logger(lctx)
8888
log.Debugf("Handling delete deployment event: %s", deployment.Name)
89+
util.LogDeleteEventLatency(deployment.DeletionTimestamp, w.getDesiredDisplayName(deployment))
8990
// Delete the deployment.
9091
if w.Config().DeleteDevices {
9192
if err := w.DeleteByDisplayName(lctx, w.Resource(), w.getDesiredDisplayName(deployment),

pkg/watch/hpa/hpa.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
8888
lctx := lmlog.NewLMContextWith(log.WithFields(log.Fields{"device_id": resource + "-" + horizontalPodAutoscaler.Name}))
8989
log := lmlog.Logger(lctx)
9090
log.Debugf("Handling delete horizontalPodAutoscaler event: %s", horizontalPodAutoscaler.Name)
91+
util.LogDeleteEventLatency(horizontalPodAutoscaler.DeletionTimestamp, w.getDesiredDisplayName(horizontalPodAutoscaler))
9192

9293
// Delete the horizontalPodAutoscaler.
9394
if w.Config().DeleteDevices {

pkg/watch/node/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
113113
log := lmlog.Logger(lctx)
114114

115115
log.Debugf("Handling delete node event: %s", w.getDesiredDisplayName(node))
116-
116+
util.LogDeleteEventLatency(node.DeletionTimestamp, w.getDesiredDisplayName(node))
117117
// nolint: dupl
118118
if w.Config().DeleteDevices {
119119
if err := w.DeleteByDisplayName(lctx, w.Resource(), w.getDesiredDisplayName(node),
@@ -149,7 +149,7 @@ func (w *Watcher) add(lctx *lmctx.LMContext, node *v1.Node) {
149149

150150
func (w *Watcher) nodeUpdateFilter(old, new *v1.Node) types.UpdateFilter {
151151
return func() bool {
152-
return getInternalAddress(old.Status.Addresses) != getInternalAddress(new.Status.Addresses)
152+
return getInternalAddress(old.Status.Addresses).Address != getInternalAddress(new.Status.Addresses).Address
153153
}
154154
}
155155

pkg/watch/pod/pod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
114114
log := lmlog.Logger(lctx)
115115

116116
log.Debugf("Handling delete pod event: %s", w.getDesiredDisplayName(pod))
117+
util.LogDeleteEventLatency(pod.DeletionTimestamp, w.getDesiredDisplayName(pod))
117118

118119
// Delete the pod.
119120
// nolint: dupl

pkg/watch/service/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (w *Watcher) DeleteFunc() func(obj interface{}) {
101101
service := obj.(*v1.Service)
102102
lctx := lmlog.NewLMContextWith(logrus.WithFields(logrus.Fields{"device_id": resource + "-" + service.Name}))
103103
log := lmlog.Logger(lctx)
104+
util.LogDeleteEventLatency(service.DeletionTimestamp, w.getDesiredDisplayName(service))
104105
// Delete the service.
105106
// nolint: dupl
106107
if w.Config().DeleteDevices {

0 commit comments

Comments
 (0)