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

Commit c4a5efa

Browse files
author
JeremyCD
committed
DEV-55114 Argus shouldn't overwrite system.categories values
1 parent ab0f882 commit c4a5efa

File tree

8 files changed

+17
-32
lines changed

8 files changed

+17
-32
lines changed

pkg/device/builder/builder.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,13 @@ func setProperty(name, value string) types.DeviceOption {
101101
}
102102

103103
func getUpdatedSystemCategories(oldValue, newValue string) string {
104-
newValues := strings.Split(newValue, ",")
105-
var category string
106-
for _, nv := range newValues {
107-
if !strings.Contains(nv, "=") {
108-
category = nv
109-
break
104+
// we do not use strings.contain, because it may match as substring of some prop
105+
oldValues := strings.Split(strings.TrimSpace(oldValue), ",")
106+
for _, ov := range oldValues {
107+
if ov == newValue {
108+
return oldValue
110109
}
111110
}
112-
if !strings.Contains(oldValue, category) {
113-
oldValue = oldValue + "," + category
114-
}
111+
oldValue = oldValue + "," + newValue
115112
return oldValue
116113
}

pkg/device/builder/builder_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestBuilder_SetProperty(t *testing.T) {
2727
}
2828

2929
sysPropValue1 := "k1=v1,k2=v2"
30-
sysPropValue2 := constants.PodCategory + ",k2=v21,k3=v3"
30+
sysPropValue2 := constants.PodCategory
3131

3232
setProp = setProperty(constants.K8sSystemCategoriesPropertyKey, sysPropValue1)
3333
setProp(device)
@@ -42,6 +42,11 @@ func TestBuilder_SetProperty(t *testing.T) {
4242
if sysPropValue1+","+constants.PodCategory != getDevicePropValueByName(device, constants.K8sSystemCategoriesPropertyKey) {
4343
t.Errorf("failed to set prop %s:%s to the device", constants.K8sSystemCategoriesPropertyKey, sysPropValue2)
4444
}
45+
setProp = setProperty(constants.K8sSystemCategoriesPropertyKey, sysPropValue2)
46+
setProp(device)
47+
if sysPropValue1+","+constants.PodCategory != getDevicePropValueByName(device, constants.K8sSystemCategoriesPropertyKey) {
48+
t.Errorf("failed to set prop %s:%s to the device", constants.K8sSystemCategoriesPropertyKey, sysPropValue2)
49+
}
4550
}
4651

4752
func getDevicePropValueByName(d *models.Device, name string) string {

pkg/etcd/etcd.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/coreos/etcd/client"
1111
"github.com/logicmonitor/k8s-argus/pkg/constants"
1212
"github.com/logicmonitor/k8s-argus/pkg/types"
13-
"github.com/logicmonitor/k8s-argus/pkg/utilities"
1413
log "github.com/sirupsen/logrus"
1514
)
1615

@@ -86,11 +85,10 @@ func (c *Controller) addDevice(member *Member) {
8685

8786
// nolint: unparam
8887
func (c *Controller) args(member *Member, category string) []types.DeviceOption {
89-
categories := utilities.BuildSystemCategoriesFromLabels(category, nil)
9088
return []types.DeviceOption{
9189
c.Name(member.URL.Hostname()),
9290
c.DisplayName(fmtMemberDisplayName(member)),
93-
c.SystemCategories(categories),
91+
c.SystemCategories(category),
9492
c.Auto("clientport", member.URL.Port()),
9593
}
9694
}

pkg/utilities/utilities.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ import (
44
"regexp"
55
)
66

7-
// BuildSystemCategoriesFromLabels formats a system.categories string.
8-
func BuildSystemCategoriesFromLabels(categories string, labels map[string]string) string {
9-
for k, v := range labels {
10-
categories += "," + k + "=" + v
11-
}
12-
return categories
13-
}
14-
157
// GetLabelByPrefix takes a list of labels returns the first label matching the specified prefix
168
func GetLabelByPrefix(prefix string, labels map[string]string) (string, string) {
179
for k, v := range labels {

pkg/watch/deployment/deployment.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/logicmonitor/k8s-argus/pkg/constants"
1010
"github.com/logicmonitor/k8s-argus/pkg/permission"
1111
"github.com/logicmonitor/k8s-argus/pkg/types"
12-
"github.com/logicmonitor/k8s-argus/pkg/utilities"
1312
log "github.com/sirupsen/logrus"
1413
"k8s.io/api/apps/v1beta2"
1514
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -116,12 +115,11 @@ func (w *Watcher) move(deployment *v1beta2.Deployment) {
116115
}
117116

118117
func (w *Watcher) args(deployment *v1beta2.Deployment, category string) []types.DeviceOption {
119-
categories := utilities.BuildSystemCategoriesFromLabels(category, deployment.Labels)
120118
return []types.DeviceOption{
121119
w.Name(deployment.Name),
122120
w.ResourceLabels(deployment.Labels),
123121
w.DisplayName(fmtDeploymentDisplayName(deployment)),
124-
w.SystemCategories(categories),
122+
w.SystemCategories(category),
125123
w.Auto("name", deployment.Name),
126124
w.Auto("namespace", deployment.Namespace),
127125
w.Auto("selflink", deployment.SelfLink),

pkg/watch/node/node.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,11 @@ func (w *Watcher) move(node *v1.Node) {
147147
}
148148

149149
func (w *Watcher) args(node *v1.Node, category string) []types.DeviceOption {
150-
categories := utilities.BuildSystemCategoriesFromLabels(category, node.Labels)
151150
return []types.DeviceOption{
152151
w.Name(getInternalAddress(node.Status.Addresses).Address),
153152
w.ResourceLabels(node.Labels),
154153
w.DisplayName(node.Name),
155-
w.SystemCategories(categories),
154+
w.SystemCategories(category),
156155
w.Auto("name", node.Name),
157156
w.Auto("selflink", node.SelfLink),
158157
w.Auto("uid", string(node.UID)),

pkg/watch/pod/pod.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/logicmonitor/k8s-argus/pkg/constants"
99
"github.com/logicmonitor/k8s-argus/pkg/types"
10-
"github.com/logicmonitor/k8s-argus/pkg/utilities"
1110
log "github.com/sirupsen/logrus"
1211
v1 "k8s.io/api/core/v1"
1312
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -144,12 +143,11 @@ func (w *Watcher) move(pod *v1.Pod) {
144143
}
145144

146145
func (w *Watcher) args(pod *v1.Pod, category string) []types.DeviceOption {
147-
categories := utilities.BuildSystemCategoriesFromLabels(category, pod.Labels)
148146
options := []types.DeviceOption{
149147
w.Name(getPodDNSName(pod)),
150148
w.ResourceLabels(pod.Labels),
151149
w.DisplayName(pod.Name),
152-
w.SystemCategories(categories),
150+
w.SystemCategories(category),
153151
w.Auto("name", pod.Name),
154152
w.Auto("namespace", pod.Namespace),
155153
w.Auto("nodename", pod.Spec.NodeName),

pkg/watch/service/service.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/logicmonitor/k8s-argus/pkg/constants"
1010
"github.com/logicmonitor/k8s-argus/pkg/types"
11-
"github.com/logicmonitor/k8s-argus/pkg/utilities"
1211
log "github.com/sirupsen/logrus"
1312
v1 "k8s.io/api/core/v1"
1413
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -132,12 +131,11 @@ func (w *Watcher) move(service *v1.Service) {
132131
}
133132

134133
func (w *Watcher) args(service *v1.Service, category string) []types.DeviceOption {
135-
categories := utilities.BuildSystemCategoriesFromLabels(category, service.Labels)
136134
return []types.DeviceOption{
137135
w.Name(service.Spec.ClusterIP),
138136
w.ResourceLabels(service.Labels),
139137
w.DisplayName(fmtServiceDisplayName(service)),
140-
w.SystemCategories(categories),
138+
w.SystemCategories(category),
141139
w.Auto("name", service.Name),
142140
w.Auto("namespace", service.Namespace),
143141
w.Auto("selflink", service.SelfLink),

0 commit comments

Comments
 (0)