From d89e783217f833b065a9cd234dd285bfc31a01a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=9B=BD=E5=BF=A0?= <249032432@qq.com> Date: Thu, 22 Aug 2019 17:49:45 +0800 Subject: [PATCH] [bugfix] custom SD: when ip out of order, reflect.deepEqual can not correctly identify whether there is a change (#5856) * [bugfix] custom SD: when ip out of order, reflect.deepEqual can not correctly identify whether there is a change Signed-off-by: fuling * [format] makefile:Makefile.common:116: common-style Signed-off-by: fuling * [bugfix] custom sd: simonpasquier comment,It would be simpler to sort the targets alphabetically and keep reflect.DeepEqual. Signed-off-by: fuling * [bugfix]custom SD:fix sort Signed-off-by: fuling * [bugfix] custom SD : adapter.go need an empty line after "sort" Signed-off-by: fuling * [bugfix]custom SD:test sign-off Signed-off-by: fuling * [bugfix]custom SD: fix adaper_test.go Signed-off-by: fuling --- .../examples/custom-sd/adapter/adapter.go | 3 +- .../custom-sd/adapter/adapter_test.go | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/documentation/examples/custom-sd/adapter/adapter.go b/documentation/examples/custom-sd/adapter/adapter.go index 4fd0ea9db..c07a79f45 100644 --- a/documentation/examples/custom-sd/adapter/adapter.go +++ b/documentation/examples/custom-sd/adapter/adapter.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "reflect" + "sort" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -76,7 +77,7 @@ func generateTargetGroups(allTargetGroups map[string][]*targetgroup.Group) map[s newTargets = append(newTargets, string(target)) } } - + sort.Strings(newTargets) for name, value := range group.Labels { newLabels[string(name)] = string(value) } diff --git a/documentation/examples/custom-sd/adapter/adapter_test.go b/documentation/examples/custom-sd/adapter/adapter_test.go index 7a97ef1cf..2cdc98c61 100644 --- a/documentation/examples/custom-sd/adapter/adapter_test.go +++ b/documentation/examples/custom-sd/adapter/adapter_test.go @@ -151,6 +151,63 @@ func TestGenerateTargetGroups(t *testing.T) { }, }, }, + { + title: "Disordered Ips in Alibaba's application management system", + targetGroup: map[string][]*targetgroup.Group{ + "cart": { + { + Source: "alibaba", + Targets: []model.LabelSet{ + { + model.AddressLabel: "192.168.1.55", + }, + { + model.AddressLabel: "192.168.1.44", + }, + }, + Labels: model.LabelSet{ + model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"), + }, + }, + }, + "buy": { + { + Source: "alibaba", + Targets: []model.LabelSet{ + { + model.AddressLabel: "192.168.1.22", + }, + { + model.AddressLabel: "192.168.1.33", + }, + }, + Labels: model.LabelSet{ + model.LabelName("__meta_test_label"): model.LabelValue("label_test_1"), + }, + }, + }, + }, + expectedCustomSD: map[string]*customSD{ + "buy:alibaba:21c0d97a1e27e6fe": { + Targets: []string{ + "192.168.1.22", + "192.168.1.33", + }, + Labels: map[string]string{ + "__meta_test_label": "label_test_1", + }, + }, + "cart:alibaba:1112e97a13b159fa": { + Targets: []string{ + "192.168.1.44", + "192.168.1.55", + }, + Labels: map[string]string{ + "__meta_test_label": "label_test_1", + }, + }, + }, + }, } for _, testCase := range testCases {